From e09a836655fae65a916eb4de380acf74986d59fb Mon Sep 17 00:00:00 2001 From: Jeffrey Wilcke Date: Thu, 2 Feb 2017 00:12:58 +0100 Subject: [PATCH] core/vm, params: implemented STATIC_CALL - EIP#116 --- core/vm/evm.go | 45 +++++++++++++++++++++++++++++++++++++++++ core/vm/instructions.go | 39 +++++++++++++++++++++++++++++++++-- core/vm/interpreter.go | 11 ++++++++++ core/vm/jump_table.go | 5 +++++ core/vm/opcodes.go | 3 +++ 5 files changed, 101 insertions(+), 2 deletions(-) diff --git a/core/vm/evm.go b/core/vm/evm.go index 87d12d2cb1..e36ca33d44 100644 --- a/core/vm/evm.go +++ b/core/vm/evm.go @@ -114,6 +114,51 @@ func (evm *EVM) Cancel() { atomic.StoreInt32(&evm.abort, 1) } +func (evm *EVM) StaticCall(caller ContractRef, addr common.Address, input []byte, gas uint64) (ret []byte, leftOverGas uint64, err error) { + if evm.vmConfig.NoRecursion && evm.depth > 0 { + return nil, gas, nil + } + + // Depth check execution. Fail if we're trying to execute above the + // limit. + if evm.depth > int(params.CallCreateDepth) { + return nil, gas, ErrDepth + } + + // make sure the readonly is only set if we aren't in readonly yet + // this makes also sure that the readonly flag isn't removed for + // child calls. + if !evm.interpreter.readonly { + evm.interpreter.readonly = true + defer func() { evm.interpreter.readonly = false }() + } + + var ( + to = AccountRef(addr) + snapshot = evm.StateDB.Snapshot() + ) + if !evm.StateDB.Exist(addr) { + return nil, gas, nil + } + + // initialise a new contract and set the code that is to be used by the + // E The contract is a scoped evmironment for this execution context + // only. + contract := NewContract(caller, to, new(big.Int), gas) + contract.SetCallCode(&addr, evm.StateDB.GetCodeHash(addr), evm.StateDB.GetCode(addr)) + + ret, err = evm.interpreter.Run(contract, input) + // When an error was returned by the EVM or when setting the creation code + // above we revert to the snapshot and consume any gas remaining. Additionally + // when we're in homestead this also counts for code storage gas errors. + if err != nil { + contract.UseGas(contract.Gas) + + evm.StateDB.RevertToSnapshot(snapshot) + } + return ret, contract.Gas, err +} + // Call executes the contract associated with the addr with the given input as parameters. It also handles any // necessary value transfer required and takes the necessary steps to create accounts and reverses the state in // case of an execution error or failed value transfer. diff --git a/core/vm/instructions.go b/core/vm/instructions.go index d8737785f1..2e1959e8c0 100644 --- a/core/vm/instructions.go +++ b/core/vm/instructions.go @@ -29,8 +29,9 @@ import ( ) var ( - bigZero = new(big.Int) - errRevert = errors.New("standard throw") + bigZero = new(big.Int) + errWriteProtection = errors.New("evm write protection") + errRevert = errors.New("standard throw") ) func opAdd(pc *uint64, evm *EVM, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) { @@ -573,6 +574,40 @@ func opCreate(pc *uint64, evm *EVM, contract *Contract, memory *Memory, stack *S return nil, nil } +func opStaticCall(pc *uint64, evm *EVM, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) { + // EIP116 availability check; return an invalid opcode error on fault + if !evm.ChainConfig().IsMetropolis(evm.BlockNumber) { + return nil, fmt.Errorf("invalid opcode %x", STATIC_CALL) + } + + // pop gas + gas := stack.pop().Uint64() + // pop address + addr := stack.pop() + // pop input size and offset + inOffset, inSize := stack.pop(), stack.pop() + // pop return size and offset + retOffset, retSize := stack.pop(), stack.pop() + + address := common.BigToAddress(addr) + + // Get the arguments from the memory + args := memory.Get(inOffset.Int64(), inSize.Int64()) + + ret, returnGas, err := evm.StaticCall(contract, address, args, gas) + if err != nil { + stack.push(new(big.Int)) + } else { + stack.push(big.NewInt(1)) + + memory.Set(retOffset.Uint64(), retSize.Uint64(), ret) + } + contract.Gas += returnGas + + evm.interpreter.intPool.put(addr, inOffset, inSize, retOffset, retSize) + return nil, nil +} + func opCall(pc *uint64, evm *EVM, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) { gas := stack.pop().Uint64() // pop gas and value of the stack. diff --git a/core/vm/interpreter.go b/core/vm/interpreter.go index 00eb3ac418..b38d42ed3f 100644 --- a/core/vm/interpreter.go +++ b/core/vm/interpreter.go @@ -60,6 +60,8 @@ type Interpreter struct { cfg Config gasTable params.GasTable intPool *intPool + + readonly bool } // NewInterpreter returns a new instance of the Interpreter. @@ -128,6 +130,15 @@ func (evm *Interpreter) Run(contract *Contract, input []byte) (ret []byte, err e // get the operation from the jump table matching the opcode operation := evm.cfg.JumpTable[op] + // if the interpreter is operating in readonly mode, make sure no + // state-modifying operation is performed. + if evm.readonly && evm.env.chainConfig.IsMetropolis(evm.env.BlockNumber) { + if operation.writes || + ((op == CALL || op == CALLCODE) && stack.Back(3).BitLen() > 0) { + return nil, errWriteProtection + } + + } // if the op is invalid abort the process and return an error if !operation.valid { diff --git a/core/vm/jump_table.go b/core/vm/jump_table.go index ccb38e8dae..3e5097857d 100644 --- a/core/vm/jump_table.go +++ b/core/vm/jump_table.go @@ -47,6 +47,8 @@ type operation struct { // jumps indicates whether operation made a jump. This prevents the program // counter from further incrementing. jumps bool + // writes determines whether this a state modifying operation + writes bool // valid is used to check whether the retrieved operation is valid and known valid bool } @@ -357,6 +359,7 @@ func NewJumpTable() [256]operation { gasCost: gasSStore, validateStack: makeStackFunc(2, 0), valid: true, + writes: true, }, JUMP: { execute: opJump, @@ -829,6 +832,7 @@ func NewJumpTable() [256]operation { validateStack: makeStackFunc(3, 1), memorySize: memoryCreate, valid: true, + writes: true, }, CALL: { execute: opCall, @@ -865,6 +869,7 @@ func NewJumpTable() [256]operation { validateStack: makeStackFunc(1, 0), halts: true, valid: true, + writes: true, }, } } diff --git a/core/vm/opcodes.go b/core/vm/opcodes.go index 3c3796d2d2..d063bffb0e 100644 --- a/core/vm/opcodes.go +++ b/core/vm/opcodes.go @@ -201,6 +201,7 @@ const ( CALLCODE RETURN DELEGATECALL + STATIC_CALL REVERT = 0xfd SELFDESTRUCT = 0xff @@ -358,6 +359,7 @@ var opCodeToString = map[OpCode]string{ CALLCODE: "CALLCODE", DELEGATECALL: "DELEGATECALL", SELFDESTRUCT: "SELFDESTRUCT", + STATIC_CALL: "STATIC_CALL", PUSH: "PUSH", DUP: "DUP", @@ -407,6 +409,7 @@ var stringToOp = map[string]OpCode{ "CALLDATASIZE": CALLDATASIZE, "CALLDATACOPY": CALLDATACOPY, "DELEGATECALL": DELEGATECALL, + "STATIC_CALL": STATIC_CALL, "CODESIZE": CODESIZE, "CODECOPY": CODECOPY, "GASPRICE": GASPRICE,