analysis.go 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. // Copyright 2014 The go-ethereum Authors
  2. // This file is part of the go-ethereum library.
  3. //
  4. // The go-ethereum library 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. // The go-ethereum library 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 the go-ethereum library. 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. // destinations stores one map per contract (keyed by hash of code).
  22. // The maps contain an entry for each location of a JUMPDEST
  23. // instruction.
  24. type destinations map[common.Hash]bitvec
  25. // has checks whether code has a JUMPDEST at dest.
  26. func (d destinations) has(codehash common.Hash, code []byte, dest *big.Int) bool {
  27. // PC cannot go beyond len(code) and certainly can't be bigger than 63bits.
  28. // Don't bother checking for JUMPDEST in that case.
  29. udest := dest.Uint64()
  30. if dest.BitLen() >= 63 || udest >= uint64(len(code)) {
  31. return false
  32. }
  33. m, analysed := d[codehash]
  34. if !analysed {
  35. m = codeBitmap(code)
  36. d[codehash] = m
  37. }
  38. return OpCode(code[udest]) == JUMPDEST && m.codeSegment(udest)
  39. }
  40. // bitvec is a bit vector which maps bytes in a program.
  41. // An unset bit means the byte is an opcode, a set bit means
  42. // it's data (i.e. argument of PUSHxx).
  43. type bitvec []byte
  44. func (bits *bitvec) set(pos uint64) {
  45. (*bits)[pos/8] |= 0x80 >> (pos % 8)
  46. }
  47. func (bits *bitvec) set8(pos uint64) {
  48. (*bits)[pos/8] |= 0xFF >> (pos % 8)
  49. (*bits)[pos/8+1] |= ^(0xFF >> (pos % 8))
  50. }
  51. // codeSegment checks if the position is in a code segment.
  52. func (bits *bitvec) codeSegment(pos uint64) bool {
  53. return ((*bits)[pos/8] & (0x80 >> (pos % 8))) == 0
  54. }
  55. // codeBitmap collects data locations in code.
  56. func codeBitmap(code []byte) bitvec {
  57. // The bitmap is 4 bytes longer than necessary, in case the code
  58. // ends with a PUSH32, the algorithm will push zeroes onto the
  59. // bitvector outside the bounds of the actual code.
  60. bits := make(bitvec, len(code)/8+1+4)
  61. for pc := uint64(0); pc < uint64(len(code)); {
  62. op := OpCode(code[pc])
  63. if op >= PUSH1 && op <= PUSH32 {
  64. numbits := op - PUSH1 + 1
  65. pc++
  66. for ; numbits >= 8; numbits -= 8 {
  67. bits.set8(pc) // 8
  68. pc += 8
  69. }
  70. for ; numbits > 0; numbits-- {
  71. bits.set(pc)
  72. pc++
  73. }
  74. } else {
  75. pc++
  76. }
  77. }
  78. return bits
  79. }