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