analysis.go 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. // Copyright 2014 The go-ethereum Authors
  2. // This file is part of go-ethereum.
  3. //
  4. // go-ethereum is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU Lesser General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // go-ethereum is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU Lesser General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU Lesser General Public License
  15. // along with go-ethereum. If not, see <http://www.gnu.org/licenses/>.
  16. package vm
  17. import (
  18. "math/big"
  19. "github.com/ethereum/go-ethereum/common"
  20. )
  21. var bigMaxUint64 = new(big.Int).SetUint64(^uint64(0))
  22. // destinations stores one map per contract (keyed by hash of code).
  23. // The maps contain an entry for each location of a JUMPDEST
  24. // instruction.
  25. type destinations map[common.Hash]map[uint64]struct{}
  26. // has checks whether code has a JUMPDEST at dest.
  27. func (d destinations) has(codehash common.Hash, code []byte, dest *big.Int) bool {
  28. // PC cannot go beyond len(code) and certainly can't be bigger than 64bits.
  29. // Don't bother checking for JUMPDEST in that case.
  30. if dest.Cmp(bigMaxUint64) > 0 {
  31. return false
  32. }
  33. m, analysed := d[codehash]
  34. if !analysed {
  35. m = jumpdests(code)
  36. d[codehash] = m
  37. }
  38. _, ok := m[dest.Uint64()]
  39. return ok
  40. }
  41. // jumpdests creates a map that contains an entry for each
  42. // PC location that is a JUMPDEST instruction.
  43. func jumpdests(code []byte) map[uint64]struct{} {
  44. m := make(map[uint64]struct{})
  45. for pc := uint64(0); pc < uint64(len(code)); pc++ {
  46. var op OpCode = OpCode(code[pc])
  47. switch op {
  48. case PUSH1, PUSH2, PUSH3, PUSH4, PUSH5, PUSH6, PUSH7, PUSH8, PUSH9, PUSH10, PUSH11, PUSH12, PUSH13, PUSH14, PUSH15, PUSH16, PUSH17, PUSH18, PUSH19, PUSH20, PUSH21, PUSH22, PUSH23, PUSH24, PUSH25, PUSH26, PUSH27, PUSH28, PUSH29, PUSH30, PUSH31, PUSH32:
  49. a := uint64(op) - uint64(PUSH1) + 1
  50. pc += a
  51. case JUMPDEST:
  52. m[pc] = struct{}{}
  53. }
  54. }
  55. return m
  56. }