analysis.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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. const (
  18. set2BitsMask = uint16(0b11)
  19. set3BitsMask = uint16(0b111)
  20. set4BitsMask = uint16(0b1111)
  21. set5BitsMask = uint16(0b1_1111)
  22. set6BitsMask = uint16(0b11_1111)
  23. set7BitsMask = uint16(0b111_1111)
  24. )
  25. // bitvec is a bit vector which maps bytes in a program.
  26. // An unset bit means the byte is an opcode, a set bit means
  27. // it's data (i.e. argument of PUSHxx).
  28. type bitvec []byte
  29. func (bits bitvec) set1(pos uint64) {
  30. bits[pos/8] |= 1 << (pos % 8)
  31. }
  32. func (bits bitvec) setN(flag uint16, pos uint64) {
  33. a := flag << (pos % 8)
  34. bits[pos/8] |= byte(a)
  35. if b := byte(a >> 8); b != 0 {
  36. bits[pos/8+1] = b
  37. }
  38. }
  39. func (bits bitvec) set8(pos uint64) {
  40. a := byte(0xFF << (pos % 8))
  41. bits[pos/8] |= a
  42. bits[pos/8+1] = ^a
  43. }
  44. func (bits bitvec) set16(pos uint64) {
  45. a := byte(0xFF << (pos % 8))
  46. bits[pos/8] |= a
  47. bits[pos/8+1] = 0xFF
  48. bits[pos/8+2] = ^a
  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] >> (pos % 8)) & 1) == 0
  53. }
  54. // codeBitmap collects data locations in code.
  55. func codeBitmap(code []byte) bitvec {
  56. // The bitmap is 4 bytes longer than necessary, in case the code
  57. // ends with a PUSH32, the algorithm will push zeroes onto the
  58. // bitvector outside the bounds of the actual code.
  59. bits := make(bitvec, len(code)/8+1+4)
  60. return codeBitmapInternal(code, bits)
  61. }
  62. // codeBitmapInternal is the internal implementation of codeBitmap.
  63. // It exists for the purpose of being able to run benchmark tests
  64. // without dynamic allocations affecting the results.
  65. func codeBitmapInternal(code, bits bitvec) bitvec {
  66. for pc := uint64(0); pc < uint64(len(code)); {
  67. op := OpCode(code[pc])
  68. pc++
  69. if int8(op) < int8(PUSH1) { // If not PUSH (the int8(op) > int(PUSH32) is always false).
  70. continue
  71. }
  72. numbits := op - PUSH1 + 1
  73. if numbits >= 8 {
  74. for ; numbits >= 16; numbits -= 16 {
  75. bits.set16(pc)
  76. pc += 16
  77. }
  78. for ; numbits >= 8; numbits -= 8 {
  79. bits.set8(pc)
  80. pc += 8
  81. }
  82. }
  83. switch numbits {
  84. case 1:
  85. bits.set1(pc)
  86. pc += 1
  87. case 2:
  88. bits.setN(set2BitsMask, pc)
  89. pc += 2
  90. case 3:
  91. bits.setN(set3BitsMask, pc)
  92. pc += 3
  93. case 4:
  94. bits.setN(set4BitsMask, pc)
  95. pc += 4
  96. case 5:
  97. bits.setN(set5BitsMask, pc)
  98. pc += 5
  99. case 6:
  100. bits.setN(set6BitsMask, pc)
  101. pc += 6
  102. case 7:
  103. bits.setN(set7BitsMask, pc)
  104. pc += 7
  105. }
  106. }
  107. return bits
  108. }