analysis.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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 a code-segemnt, a set bit means it's data-segment
  42. type bitvec []byte
  43. func (bits *bitvec) set(pos uint64) {
  44. (*bits)[pos/8] |= 0x80 >> (pos % 8)
  45. }
  46. func (bits *bitvec) set8(pos uint64) {
  47. (*bits)[pos/8] |= 0xFF >> (pos % 8)
  48. (*bits)[pos/8+1] |= ^(0xFF >> (pos % 8))
  49. }
  50. // codeSegment checks if the position is in a code segment
  51. func (bits *bitvec) codeSegment(pos uint64) bool {
  52. return ((*bits)[pos/8] & (0x80 >> (pos % 8))) == 0
  53. }
  54. // jumpdests creates a bitmap of the code, where 1 represents a DATA-segment,
  55. // and 0 represents code-segment
  56. func codeBitmap(code []byte) []byte {
  57. //The map 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. }