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