github.com/avence12/go-ethereum@v1.5.10-0.20170320123548-1dfd65f6d047/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  import (
    20  	"math/big"
    21  
    22  	"github.com/ethereum/go-ethereum/common"
    23  )
    24  
    25  var bigMaxUint64 = new(big.Int).SetUint64(^uint64(0))
    26  
    27  // destinations stores one map per contract (keyed by hash of code).
    28  // The maps contain an entry for each location of a JUMPDEST
    29  // instruction.
    30  type destinations map[common.Hash]map[uint64]struct{}
    31  
    32  // has checks whether code has a JUMPDEST at dest.
    33  func (d destinations) has(codehash common.Hash, code []byte, dest *big.Int) bool {
    34  	// PC cannot go beyond len(code) and certainly can't be bigger than 64bits.
    35  	// Don't bother checking for JUMPDEST in that case.
    36  	if dest.Cmp(bigMaxUint64) > 0 {
    37  		return false
    38  	}
    39  	m, analysed := d[codehash]
    40  	if !analysed {
    41  		m = jumpdests(code)
    42  		d[codehash] = m
    43  	}
    44  	_, ok := m[dest.Uint64()]
    45  	return ok
    46  }
    47  
    48  // jumpdests creates a map that contains an entry for each
    49  // PC location that is a JUMPDEST instruction.
    50  func jumpdests(code []byte) map[uint64]struct{} {
    51  	m := make(map[uint64]struct{})
    52  	for pc := uint64(0); pc < uint64(len(code)); pc++ {
    53  		var op OpCode = OpCode(code[pc])
    54  		switch op {
    55  		case PUSH1, PUSH2, PUSH3, PUSH4, PUSH5, PUSH6, PUSH7, PUSH8, PUSH9, PUSH10, PUSH11, PUSH12, PUSH13, PUSH14, PUSH15, PUSH16, PUSH17, PUSH18, PUSH19, PUSH20, PUSH21, PUSH22, PUSH23, PUSH24, PUSH25, PUSH26, PUSH27, PUSH28, PUSH29, PUSH30, PUSH31, PUSH32:
    56  			a := uint64(op) - uint64(PUSH1) + 1
    57  			pc += a
    58  		case JUMPDEST:
    59  			m[pc] = struct{}{}
    60  		}
    61  	}
    62  	return m
    63  }