analysis.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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(0b1100_0000_0000_0000)
  19. set3BitsMask = uint16(0b1110_0000_0000_0000)
  20. set4BitsMask = uint16(0b1111_0000_0000_0000)
  21. set5BitsMask = uint16(0b1111_1000_0000_0000)
  22. set6BitsMask = uint16(0b1111_1100_0000_0000)
  23. set7BitsMask = uint16(0b1111_1110_0000_0000)
  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. var lookup = [8]byte{
  30. 0x80, 0x40, 0x20, 0x10, 0x8, 0x4, 0x2, 0x1,
  31. }
  32. func (bits bitvec) set1(pos uint64) {
  33. bits[pos/8] |= lookup[pos%8]
  34. }
  35. func (bits bitvec) setN(flag uint16, pos uint64) {
  36. a := flag >> (pos % 8)
  37. bits[pos/8] |= byte(a >> 8)
  38. if b := byte(a); b != 0 {
  39. // If the bit-setting affects the neighbouring byte, we can assign - no need to OR it,
  40. // since it's the first write to that byte
  41. bits[pos/8+1] = b
  42. }
  43. }
  44. func (bits bitvec) set8(pos uint64) {
  45. a := byte(0xFF >> (pos % 8))
  46. bits[pos/8] |= a
  47. bits[pos/8+1] = ^a
  48. }
  49. func (bits bitvec) set16(pos uint64) {
  50. a := byte(0xFF >> (pos % 8))
  51. bits[pos/8] |= a
  52. bits[pos/8+1] = 0xFF
  53. bits[pos/8+2] = ^a
  54. }
  55. // codeSegment checks if the position is in a code segment.
  56. func (bits *bitvec) codeSegment(pos uint64) bool {
  57. return ((*bits)[pos/8] & (0x80 >> (pos % 8))) == 0
  58. }
  59. // codeBitmap collects data locations in code.
  60. func codeBitmap(code []byte) bitvec {
  61. // The bitmap is 4 bytes longer than necessary, in case the code
  62. // ends with a PUSH32, the algorithm will push zeroes onto the
  63. // bitvector outside the bounds of the actual code.
  64. bits := make(bitvec, len(code)/8+1+4)
  65. return codeBitmapInternal(code, bits)
  66. }
  67. // codeBitmapInternal is the internal implementation of codeBitmap.
  68. // It exists for the purpose of being able to run benchmark tests
  69. // without dynamic allocations affecting the results.
  70. func codeBitmapInternal(code, bits bitvec) bitvec {
  71. for pc := uint64(0); pc < uint64(len(code)); {
  72. op := OpCode(code[pc])
  73. pc++
  74. if op < PUSH1 || op > PUSH32 {
  75. continue
  76. }
  77. numbits := op - PUSH1 + 1
  78. if numbits >= 8 {
  79. for ; numbits >= 16; numbits -= 16 {
  80. bits.set16(pc)
  81. pc += 16
  82. }
  83. for ; numbits >= 8; numbits -= 8 {
  84. bits.set8(pc)
  85. pc += 8
  86. }
  87. }
  88. switch numbits {
  89. case 1:
  90. bits.set1(pc)
  91. pc += 1
  92. case 2:
  93. bits.setN(set2BitsMask, pc)
  94. pc += 2
  95. case 3:
  96. bits.setN(set3BitsMask, pc)
  97. pc += 3
  98. case 4:
  99. bits.setN(set4BitsMask, pc)
  100. pc += 4
  101. case 5:
  102. bits.setN(set5BitsMask, pc)
  103. pc += 5
  104. case 6:
  105. bits.setN(set6BitsMask, pc)
  106. pc += 6
  107. case 7:
  108. bits.setN(set7BitsMask, pc)
  109. pc += 7
  110. }
  111. }
  112. return bits
  113. }