mirror of https://github.com/ethereum/go-ethereum
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
209 lines
6.6 KiB
209 lines
6.6 KiB
9 years ago
|
// Copyright 2014 The go-ethereum Authors
|
||
9 years ago
|
// This file is part of the go-ethereum library.
|
||
9 years ago
|
//
|
||
9 years ago
|
// The go-ethereum library is free software: you can redistribute it and/or modify
|
||
9 years ago
|
// it under the terms of the GNU Lesser General Public License as published by
|
||
|
// the Free Software Foundation, either version 3 of the License, or
|
||
|
// (at your option) any later version.
|
||
|
//
|
||
9 years ago
|
// The go-ethereum library is distributed in the hope that it will be useful,
|
||
9 years ago
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||
9 years ago
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||
9 years ago
|
// GNU Lesser General Public License for more details.
|
||
|
//
|
||
|
// You should have received a copy of the GNU Lesser General Public License
|
||
9 years ago
|
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
|
||
9 years ago
|
|
||
10 years ago
|
package vm
|
||
10 years ago
|
|
||
10 years ago
|
import (
|
||
|
"fmt"
|
||
|
"math/big"
|
||
8 years ago
|
"sync/atomic"
|
||
9 years ago
|
"time"
|
||
10 years ago
|
|
||
10 years ago
|
"github.com/ethereum/go-ethereum/common"
|
||
8 years ago
|
"github.com/ethereum/go-ethereum/common/math"
|
||
10 years ago
|
"github.com/ethereum/go-ethereum/crypto"
|
||
9 years ago
|
"github.com/ethereum/go-ethereum/logger"
|
||
|
"github.com/ethereum/go-ethereum/logger/glog"
|
||
10 years ago
|
"github.com/ethereum/go-ethereum/params"
|
||
10 years ago
|
)
|
||
10 years ago
|
|
||
8 years ago
|
// Config are the configuration options for the Interpreter
|
||
9 years ago
|
type Config struct {
|
||
8 years ago
|
// Debug enabled debugging Interpreter options
|
||
8 years ago
|
Debug bool
|
||
|
// EnableJit enabled the JIT VM
|
||
9 years ago
|
EnableJit bool
|
||
8 years ago
|
// ForceJit forces the JIT VM
|
||
|
ForceJit bool
|
||
|
// Tracer is the op code logger
|
||
|
Tracer Tracer
|
||
8 years ago
|
// NoRecursion disabled Interpreter call, callcode,
|
||
8 years ago
|
// delegate call and create.
|
||
|
NoRecursion bool
|
||
8 years ago
|
// Disable gas metering
|
||
|
DisableGasMetering bool
|
||
8 years ago
|
// Enable recording of SHA3/keccak preimages
|
||
|
EnablePreimageRecording bool
|
||
8 years ago
|
// JumpTable contains the EVM instruction table. This
|
||
|
// may me left uninitialised and will be set the default
|
||
|
// table.
|
||
|
JumpTable [256]operation
|
||
9 years ago
|
}
|
||
|
|
||
8 years ago
|
// Interpreter is used to run Ethereum based contracts and will utilise the
|
||
9 years ago
|
// passed environment to query external sources for state information.
|
||
8 years ago
|
// The Interpreter will run the byte code VM or JIT VM based on the passed
|
||
9 years ago
|
// configuration.
|
||
8 years ago
|
type Interpreter struct {
|
||
|
env *EVM
|
||
|
cfg Config
|
||
|
gasTable params.GasTable
|
||
8 years ago
|
intPool *intPool
|
||
10 years ago
|
}
|
||
|
|
||
8 years ago
|
// NewInterpreter returns a new instance of the Interpreter.
|
||
|
func NewInterpreter(env *EVM, cfg Config) *Interpreter {
|
||
|
// We use the STOP instruction whether to see
|
||
|
// the jump table was initialised. If it was not
|
||
|
// we'll set the default jump table.
|
||
|
if !cfg.JumpTable[STOP].valid {
|
||
|
cfg.JumpTable = defaultJumpTable
|
||
|
}
|
||
|
|
||
|
return &Interpreter{
|
||
|
env: env,
|
||
|
cfg: cfg,
|
||
|
gasTable: env.ChainConfig().GasTable(env.BlockNumber),
|
||
8 years ago
|
intPool: newIntPool(),
|
||
9 years ago
|
}
|
||
10 years ago
|
}
|
||
|
|
||
10 years ago
|
// Run loops and evaluates the contract's code with the given input data
|
||
8 years ago
|
func (evm *Interpreter) Run(contract *Contract, input []byte) (ret []byte, err error) {
|
||
|
evm.env.depth++
|
||
|
defer func() { evm.env.depth-- }()
|
||
10 years ago
|
|
||
9 years ago
|
if contract.CodeAddr != nil {
|
||
8 years ago
|
if p := PrecompiledContracts[*contract.CodeAddr]; p != nil {
|
||
|
return RunPrecompiledContract(p, input, contract)
|
||
9 years ago
|
}
|
||
|
}
|
||
|
|
||
9 years ago
|
// Don't bother with the execution if there's no code.
|
||
|
if len(contract.Code) == 0 {
|
||
9 years ago
|
return nil, nil
|
||
9 years ago
|
}
|
||
|
|
||
8 years ago
|
codehash := contract.CodeHash // codehash is used when doing jump dest caching
|
||
|
if codehash == (common.Hash{}) {
|
||
|
codehash = crypto.Keccak256Hash(contract.Code)
|
||
|
}
|
||
9 years ago
|
|
||
10 years ago
|
var (
|
||
8 years ago
|
op OpCode // current opcode
|
||
|
mem = NewMemory() // bound memory
|
||
|
stack = newstack() // local stack
|
||
10 years ago
|
// For optimisation reason we're using uint64 as the program counter.
|
||
9 years ago
|
// It's theoretically possible to go above 2^64. The YP defines the PC to be uint256. Practically much less so feasible.
|
||
8 years ago
|
pc = uint64(0) // program counter
|
||
8 years ago
|
cost uint64
|
||
10 years ago
|
)
|
||
9 years ago
|
contract.Input = input
|
||
10 years ago
|
|
||
10 years ago
|
// User defer pattern to check for an error and, based on the error being nil or not, use all gas and return.
|
||
10 years ago
|
defer func() {
|
||
9 years ago
|
if err != nil && evm.cfg.Debug {
|
||
8 years ago
|
// XXX For debugging
|
||
|
//fmt.Printf("%04d: %8v cost = %-8d stack = %-8d ERR = %v\n", pc, op, cost, stack.len(), err)
|
||
|
// TODO update the tracer
|
||
|
g, c := new(big.Int).SetUint64(contract.Gas), new(big.Int).SetUint64(cost)
|
||
|
evm.cfg.Tracer.CaptureState(evm.env, pc, op, g, c, mem, stack, contract, evm.env.depth, err)
|
||
10 years ago
|
}
|
||
|
}()
|
||
10 years ago
|
|
||
9 years ago
|
if glog.V(logger.Debug) {
|
||
8 years ago
|
glog.Infof("evm running: %x\n", codehash[:4])
|
||
9 years ago
|
tstart := time.Now()
|
||
|
defer func() {
|
||
8 years ago
|
glog.Infof("evm done: %x. time: %v\n", codehash[:4], time.Since(tstart))
|
||
9 years ago
|
}()
|
||
10 years ago
|
}
|
||
|
|
||
8 years ago
|
// The Interpreter main run loop (contextual). This loop runs until either an
|
||
8 years ago
|
// explicit STOP, RETURN or SUICIDE is executed, an error occurred during
|
||
8 years ago
|
// the execution of one of the operations or until the evm.done is set by
|
||
|
// the parent context.Context.
|
||
|
for atomic.LoadInt32(&evm.env.abort) == 0 {
|
||
10 years ago
|
// Get the memory location of pc
|
||
9 years ago
|
op = contract.GetOp(pc)
|
||
10 years ago
|
|
||
8 years ago
|
// get the operation from the jump table matching the opcode
|
||
|
operation := evm.cfg.JumpTable[op]
|
||
9 years ago
|
|
||
8 years ago
|
// if the op is invalid abort the process and return an error
|
||
|
if !operation.valid {
|
||
|
return nil, fmt.Errorf("invalid opcode %x", op)
|
||
9 years ago
|
}
|
||
|
|
||
8 years ago
|
// validate the stack and make sure there enough stack items available
|
||
|
// to perform the operation
|
||
|
if err := operation.validateStack(stack); err != nil {
|
||
|
return nil, err
|
||
10 years ago
|
}
|
||
|
|
||
8 years ago
|
var memorySize uint64
|
||
8 years ago
|
// calculate the new memory size and expand the memory to fit
|
||
|
// the operation
|
||
|
if operation.memorySize != nil {
|
||
8 years ago
|
memSize, overflow := bigUint64(operation.memorySize(stack))
|
||
|
if overflow {
|
||
|
return nil, errGasUintOverflow
|
||
|
}
|
||
8 years ago
|
// memory is expanded in words of 32 bytes. Gas
|
||
|
// is also calculated in words.
|
||
8 years ago
|
if memorySize, overflow = math.SafeMul(toWordSize(memSize), 32); overflow {
|
||
|
return nil, errGasUintOverflow
|
||
|
}
|
||
8 years ago
|
}
|
||
|
|
||
8 years ago
|
if !evm.cfg.DisableGasMetering {
|
||
|
// consume the gas and return an error if not enough gas is available.
|
||
|
// cost is explicitly set so that the capture state defer method cas get the proper cost
|
||
8 years ago
|
cost, err = operation.gasCost(evm.gasTable, evm.env, contract, stack, mem, memorySize)
|
||
|
if err != nil || !contract.UseGas(cost) {
|
||
8 years ago
|
return nil, ErrOutOfGas
|
||
|
}
|
||
10 years ago
|
}
|
||
8 years ago
|
if memorySize > 0 {
|
||
|
mem.Resize(memorySize)
|
||
10 years ago
|
}
|
||
10 years ago
|
|
||
8 years ago
|
if evm.cfg.Debug {
|
||
8 years ago
|
g, c := new(big.Int).SetUint64(contract.Gas), new(big.Int).SetUint64(cost)
|
||
|
evm.cfg.Tracer.CaptureState(evm.env, pc, op, g, c, mem, stack, contract, evm.env.depth, err)
|
||
10 years ago
|
}
|
||
8 years ago
|
// XXX For debugging
|
||
|
//fmt.Printf("%04d: %8v cost = %-8d stack = %-8d\n", pc, op, cost, stack.len())
|
||
8 years ago
|
|
||
8 years ago
|
// execute the operation
|
||
|
res, err := operation.execute(&pc, evm.env, contract, mem, stack)
|
||
8 years ago
|
// verifyPool is a build flag. Pool verification makes sure the integrity
|
||
|
// of the integer pool by comparing values to a default value.
|
||
|
if verifyPool {
|
||
|
verifyIntegerPool(evm.intPool)
|
||
|
}
|
||
8 years ago
|
switch {
|
||
|
case err != nil:
|
||
|
return nil, err
|
||
|
case operation.halts:
|
||
|
return res, nil
|
||
|
case !operation.jumps:
|
||
|
pc++
|
||
10 years ago
|
}
|
||
10 years ago
|
}
|
||
8 years ago
|
return nil, nil
|
||
10 years ago
|
}
|