github.com/MetalBlockchain/subnet-evm@v0.4.9/core/vm/analysis.go (about) 1 // (c) 2019-2020, Ava Labs, Inc. 2 // 3 // This file is a derived work, based on the go-ethereum library whose original 4 // notices appear below. 5 // 6 // It is distributed under a license compatible with the licensing terms of the 7 // original code from which it is derived. 8 // 9 // Much love to the original authors for their work. 10 // ********** 11 // Copyright 2014 The go-ethereum Authors 12 // This file is part of the go-ethereum library. 13 // 14 // The go-ethereum library is free software: you can redistribute it and/or modify 15 // it under the terms of the GNU Lesser General Public License as published by 16 // the Free Software Foundation, either version 3 of the License, or 17 // (at your option) any later version. 18 // 19 // The go-ethereum library is distributed in the hope that it will be useful, 20 // but WITHOUT ANY WARRANTY; without even the implied warranty of 21 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 22 // GNU Lesser General Public License for more details. 23 // 24 // You should have received a copy of the GNU Lesser General Public License 25 // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>. 26 27 package vm 28 29 const ( 30 set2BitsMask = uint16(0b11) 31 set3BitsMask = uint16(0b111) 32 set4BitsMask = uint16(0b1111) 33 set5BitsMask = uint16(0b1_1111) 34 set6BitsMask = uint16(0b11_1111) 35 set7BitsMask = uint16(0b111_1111) 36 ) 37 38 // bitvec is a bit vector which maps bytes in a program. 39 // An unset bit means the byte is an opcode, a set bit means 40 // it's data (i.e. argument of PUSHxx). 41 type bitvec []byte 42 43 func (bits bitvec) set1(pos uint64) { 44 bits[pos/8] |= 1 << (pos % 8) 45 } 46 47 func (bits bitvec) setN(flag uint16, pos uint64) { 48 a := flag << (pos % 8) 49 bits[pos/8] |= byte(a) 50 if b := byte(a >> 8); b != 0 { 51 bits[pos/8+1] = b 52 } 53 } 54 55 func (bits bitvec) set8(pos uint64) { 56 a := byte(0xFF << (pos % 8)) 57 bits[pos/8] |= a 58 bits[pos/8+1] = ^a 59 } 60 61 func (bits bitvec) set16(pos uint64) { 62 a := byte(0xFF << (pos % 8)) 63 bits[pos/8] |= a 64 bits[pos/8+1] = 0xFF 65 bits[pos/8+2] = ^a 66 } 67 68 // codeSegment checks if the position is in a code segment. 69 func (bits *bitvec) codeSegment(pos uint64) bool { 70 return (((*bits)[pos/8] >> (pos % 8)) & 1) == 0 71 } 72 73 // codeBitmap collects data locations in code. 74 func codeBitmap(code []byte) bitvec { 75 // The bitmap is 4 bytes longer than necessary, in case the code 76 // ends with a PUSH32, the algorithm will push zeroes onto the 77 // bitvector outside the bounds of the actual code. 78 bits := make(bitvec, len(code)/8+1+4) 79 return codeBitmapInternal(code, bits) 80 } 81 82 // codeBitmapInternal is the internal implementation of codeBitmap. 83 // It exists for the purpose of being able to run benchmark tests 84 // without dynamic allocations affecting the results. 85 func codeBitmapInternal(code, bits bitvec) bitvec { 86 for pc := uint64(0); pc < uint64(len(code)); { 87 op := OpCode(code[pc]) 88 pc++ 89 if int8(op) < int8(PUSH1) { // If not PUSH (the int8(op) > int(PUSH32) is always false). 90 continue 91 } 92 numbits := op - PUSH1 + 1 93 if numbits >= 8 { 94 for ; numbits >= 16; numbits -= 16 { 95 bits.set16(pc) 96 pc += 16 97 } 98 for ; numbits >= 8; numbits -= 8 { 99 bits.set8(pc) 100 pc += 8 101 } 102 } 103 switch numbits { 104 case 1: 105 bits.set1(pc) 106 pc += 1 107 case 2: 108 bits.setN(set2BitsMask, pc) 109 pc += 2 110 case 3: 111 bits.setN(set3BitsMask, pc) 112 pc += 3 113 case 4: 114 bits.setN(set4BitsMask, pc) 115 pc += 4 116 case 5: 117 bits.setN(set5BitsMask, pc) 118 pc += 5 119 case 6: 120 bits.setN(set6BitsMask, pc) 121 pc += 6 122 case 7: 123 bits.setN(set7BitsMask, pc) 124 pc += 7 125 } 126 } 127 return bits 128 }