Skip to content

Commit 76069ee

Browse files
authored
Merge pull request #14978 from karalabe/metropolis-staticcall
core/vm: implement metropolis static call opcode
2 parents 3040243 + 3df7142 commit 76069ee

7 files changed

Lines changed: 168 additions & 22 deletions

File tree

‎core/vm/evm.go‎

Lines changed: 61 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -123,19 +123,20 @@ func (evm *EVM) Cancel() {
123123
atomic.StoreInt32(&evm.abort, 1)
124124
}
125125

126-
// Call executes the contract associated with the addr with the given input as parameters. It also handles any
127-
// necessary value transfer required and takes the necessary steps to create accounts and reverses the state in
128-
// case of an execution error or failed value transfer.
126+
// Call executes the contract associated with the addr with the given input as
127+
// parameters. It also handles any necessary value transfer required and takes
128+
// the necessary steps to create accounts and reverses the state in case of an
129+
// execution error or failed value transfer.
129130
func (evm *EVM) Call(caller ContractRef, addr common.Address, input []byte, gas uint64, value *big.Int) (ret []byte, leftOverGas uint64, err error) {
130131
if evm.vmConfig.NoRecursion && evm.depth > 0 {
131132
return nil, gas, nil
132133
}
133134

134-
// Depth check execution. Fail if we're trying to execute above the
135-
// limit.
135+
// Fail if we're trying to execute above the call depth limit
136136
if evm.depth > int(params.CallCreateDepth) {
137137
return nil, gas, ErrDepth
138138
}
139+
// Fail if we're trying to transfer more than the available balance
139140
if !evm.Context.CanTransfer(evm.StateDB, caller.Address(), value) {
140141
return nil, gas, ErrInsufficientBalance
141142
}
@@ -173,21 +174,23 @@ func (evm *EVM) Call(caller ContractRef, addr common.Address, input []byte, gas
173174
return ret, contract.Gas, err
174175
}
175176

176-
// CallCode executes the contract associated with the addr with the given input as parameters. It also handles any
177-
// necessary value transfer required and takes the necessary steps to create accounts and reverses the state in
178-
// case of an execution error or failed value transfer.
177+
// CallCode executes the contract associated with the addr with the given input
178+
// as parameters. It also handles any necessary value transfer required and takes
179+
// the necessary steps to create accounts and reverses the state in case of an
180+
// execution error or failed value transfer.
179181
//
180-
// CallCode differs from Call in the sense that it executes the given address' code with the caller as context.
182+
// CallCode differs from Call in the sense that it executes the given address'
183+
// code with the caller as context.
181184
func (evm *EVM) CallCode(caller ContractRef, addr common.Address, input []byte, gas uint64, value *big.Int) (ret []byte, leftOverGas uint64, err error) {
182185
if evm.vmConfig.NoRecursion && evm.depth > 0 {
183186
return nil, gas, nil
184187
}
185188

186-
// Depth check execution. Fail if we're trying to execute above the
187-
// limit.
189+
// Fail if we're trying to execute above the call depth limit
188190
if evm.depth > int(params.CallCreateDepth) {
189191
return nil, gas, ErrDepth
190192
}
193+
// Fail if we're trying to transfer more than the available balance
191194
if !evm.CanTransfer(evm.StateDB, caller.Address(), value) {
192195
return nil, gas, ErrInsufficientBalance
193196
}
@@ -211,18 +214,16 @@ func (evm *EVM) CallCode(caller ContractRef, addr common.Address, input []byte,
211214
return ret, contract.Gas, err
212215
}
213216

214-
// DelegateCall executes the contract associated with the addr with the given input as parameters.
215-
// It reverses the state in case of an execution error.
217+
// DelegateCall executes the contract associated with the addr with the given input
218+
// as parameters. It reverses the state in case of an execution error.
216219
//
217-
// DelegateCall differs from CallCode in the sense that it executes the given address' code with the caller as context
218-
// and the caller is set to the caller of the caller.
220+
// DelegateCall differs from CallCode in the sense that it executes the given address'
221+
// code with the caller as context and the caller is set to the caller of the caller.
219222
func (evm *EVM) DelegateCall(caller ContractRef, addr common.Address, input []byte, gas uint64) (ret []byte, leftOverGas uint64, err error) {
220223
if evm.vmConfig.NoRecursion && evm.depth > 0 {
221224
return nil, gas, nil
222225
}
223-
224-
// Depth check execution. Fail if we're trying to execute above the
225-
// limit.
226+
// Fail if we're trying to execute above the call depth limit
226227
if evm.depth > int(params.CallCreateDepth) {
227228
return nil, gas, ErrDepth
228229
}
@@ -232,7 +233,7 @@ func (evm *EVM) DelegateCall(caller ContractRef, addr common.Address, input []by
232233
to = AccountRef(caller.Address())
233234
)
234235

235-
// Iinitialise a new contract and make initialise the delegate values
236+
// Initialise a new contract and make initialise the delegate values
236237
contract := NewContract(caller, to, nil, gas).AsDelegate()
237238
contract.SetCallCode(&addr, evm.StateDB.GetCodeHash(addr), evm.StateDB.GetCode(addr))
238239

@@ -245,6 +246,47 @@ func (evm *EVM) DelegateCall(caller ContractRef, addr common.Address, input []by
245246
return ret, contract.Gas, err
246247
}
247248

249+
// StaticCall executes the contract associated with the addr with the given input
250+
// as parameters while disallowing any modifications to the state during the call.
251+
// Opcodes that attempt to perform such modifications will result in exceptions
252+
// instead of performing the modifications.
253+
func (evm *EVM) StaticCall(caller ContractRef, addr common.Address, input []byte, gas uint64) (ret []byte, leftOverGas uint64, err error) {
254+
if evm.vmConfig.NoRecursion && evm.depth > 0 {
255+
return nil, gas, nil
256+
}
257+
// Fail if we're trying to execute above the call depth limit
258+
if evm.depth > int(params.CallCreateDepth) {
259+
return nil, gas, ErrDepth
260+
}
261+
// Make sure the readonly is only set if we aren't in readonly yet
262+
// this makes also sure that the readonly flag isn't removed for
263+
// child calls.
264+
if !evm.interpreter.readonly {
265+
evm.interpreter.readonly = true
266+
defer func() { evm.interpreter.readonly = false }()
267+
}
268+
269+
var (
270+
to = AccountRef(addr)
271+
snapshot = evm.StateDB.Snapshot()
272+
)
273+
// Initialise a new contract and set the code that is to be used by the
274+
// EVM. The contract is a scoped environment for this execution context
275+
// only.
276+
contract := NewContract(caller, to, new(big.Int), gas)
277+
contract.SetCallCode(&addr, evm.StateDB.GetCodeHash(addr), evm.StateDB.GetCode(addr))
278+
279+
// When an error was returned by the EVM or when setting the creation code
280+
// above we revert to the snapshot and consume any gas remaining. Additionally
281+
// when we're in Homestead this also counts for code storage gas errors.
282+
ret, err = run(evm, snapshot, contract, input)
283+
if err != nil {
284+
contract.UseGas(contract.Gas)
285+
evm.StateDB.RevertToSnapshot(snapshot)
286+
}
287+
return ret, contract.Gas, err
288+
}
289+
248290
// Create creates a new contract using code as deployment code.
249291
func (evm *EVM) Create(caller ContractRef, code []byte, gas uint64, value *big.Int) (ret []byte, contractAddr common.Address, leftOverGas uint64, err error) {
250292
if evm.vmConfig.NoRecursion && evm.depth > 0 {

‎core/vm/gas_table.go‎

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -423,6 +423,33 @@ func gasDelegateCall(gt params.GasTable, evm *EVM, contract *Contract, stack *St
423423
return gas, nil
424424
}
425425

426+
func gasStaticCall(gt params.GasTable, evm *EVM, contract *Contract, stack *Stack, mem *Memory, memorySize uint64) (uint64, error) {
427+
gas, err := memoryGasCost(mem, memorySize)
428+
if err != nil {
429+
return 0, err
430+
}
431+
var overflow bool
432+
if gas, overflow = math.SafeAdd(gas, gt.Calls); overflow {
433+
return 0, errGasUintOverflow
434+
}
435+
436+
cg, err := callGas(gt, contract.Gas, gas, stack.Back(0))
437+
if err != nil {
438+
return 0, err
439+
}
440+
// Replace the stack item with the new gas calculation. This means that
441+
// either the original item is left on the stack or the item is replaced by:
442+
// (availableGas - gas) * 63 / 64
443+
// We replace the stack item so that it's available when the opCall instruction is
444+
// called.
445+
stack.data[stack.len()-1] = new(big.Int).SetUint64(cg)
446+
447+
if gas, overflow = math.SafeAdd(gas, cg); overflow {
448+
return 0, errGasUintOverflow
449+
}
450+
return gas, nil
451+
}
452+
426453
func gasPush(gt params.GasTable, evm *EVM, contract *Contract, stack *Stack, mem *Memory, memorySize uint64) (uint64, error) {
427454
return GasFastestStep, nil
428455
}

‎core/vm/instructions.go‎

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
package vm
1818

1919
import (
20+
"errors"
2021
"fmt"
2122
"math/big"
2223

@@ -28,7 +29,8 @@ import (
2829
)
2930

3031
var (
31-
bigZero = new(big.Int)
32+
bigZero = new(big.Int)
33+
errWriteProtection = errors.New("evm: write protection")
3234
)
3335

3436
func opAdd(pc *uint64, evm *EVM, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
@@ -656,6 +658,35 @@ func opDelegateCall(pc *uint64, evm *EVM, contract *Contract, memory *Memory, st
656658
return ret, nil
657659
}
658660

661+
func opStaticCall(pc *uint64, evm *EVM, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
662+
// pop gas
663+
gas := stack.pop().Uint64()
664+
// pop address
665+
addr := stack.pop()
666+
// pop input size and offset
667+
inOffset, inSize := stack.pop(), stack.pop()
668+
// pop return size and offset
669+
retOffset, retSize := stack.pop(), stack.pop()
670+
671+
address := common.BigToAddress(addr)
672+
673+
// Get the arguments from the memory
674+
args := memory.Get(inOffset.Int64(), inSize.Int64())
675+
676+
ret, returnGas, err := evm.StaticCall(contract, address, args, gas)
677+
if err != nil {
678+
stack.push(new(big.Int))
679+
} else {
680+
stack.push(big.NewInt(1))
681+
682+
memory.Set(retOffset.Uint64(), retSize.Uint64(), ret)
683+
}
684+
contract.Gas += returnGas
685+
686+
evm.interpreter.intPool.put(addr, inOffset, inSize, retOffset, retSize)
687+
return ret, nil
688+
}
689+
659690
func opReturn(pc *uint64, evm *EVM, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
660691
offset, size := stack.pop(), stack.pop()
661692
ret := memory.GetPtr(offset.Int64(), size.Int64())

‎core/vm/interpreter.go‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,8 @@ func NewInterpreter(evm *EVM, cfg Config) *Interpreter {
6969
// we'll set the default jump table.
7070
if !cfg.JumpTable[STOP].valid {
7171
switch {
72+
case evm.ChainConfig().IsMetropolis(evm.BlockNumber):
73+
cfg.JumpTable = metropolisInstructionSet
7274
case evm.ChainConfig().IsHomestead(evm.BlockNumber):
7375
cfg.JumpTable = homesteadInstructionSet
7476
default:
@@ -85,6 +87,18 @@ func NewInterpreter(evm *EVM, cfg Config) *Interpreter {
8587
}
8688

8789
func (in *Interpreter) enforceRestrictions(op OpCode, operation operation, stack *Stack) error {
90+
if in.evm.chainRules.IsMetropolis {
91+
if in.readonly {
92+
// If the interpreter is operating in readonly mode, make sure no
93+
// state-modifying operation is performed. The 3rd stack item
94+
// for a call operation is the value. Transfering value from one
95+
// account to the others means the state is modified and should also
96+
// return with an error.
97+
if operation.writes || (op == CALL && stack.Back(2).BitLen() > 0) {
98+
return errWriteProtection
99+
}
100+
}
101+
}
88102
return nil
89103
}
90104

@@ -95,6 +109,7 @@ func (in *Interpreter) enforceRestrictions(op OpCode, operation operation, stack
95109
// considered a revert-and-consume-all-gas operation. No error specific checks
96110
// should be handled to reduce complexity and errors further down the in.
97111
func (in *Interpreter) Run(snapshot int, contract *Contract, input []byte) (ret []byte, err error) {
112+
// Increment the call depth which is restricted to 1024
98113
in.evm.depth++
99114
defer func() { in.evm.depth-- }()
100115

‎core/vm/jump_table.go‎

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,10 +56,26 @@ type operation struct {
5656
}
5757

5858
var (
59-
frontierInstructionSet = NewFrontierInstructionSet()
60-
homesteadInstructionSet = NewHomesteadInstructionSet()
59+
frontierInstructionSet = NewFrontierInstructionSet()
60+
homesteadInstructionSet = NewHomesteadInstructionSet()
61+
metropolisInstructionSet = NewMetropolisInstructionSet()
6162
)
6263

64+
// NewMetropolisInstructionSet returns the frontier, homestead and
65+
// metropolis instructions.
66+
func NewMetropolisInstructionSet() [256]operation {
67+
// instructions that can be executed during the homestead phase.
68+
instructionSet := NewHomesteadInstructionSet()
69+
instructionSet[STATICCALL] = operation{
70+
execute: opStaticCall,
71+
gasCost: gasStaticCall,
72+
validateStack: makeStackFunc(6, 1),
73+
memorySize: memoryStaticCall,
74+
valid: true,
75+
}
76+
return instructionSet
77+
}
78+
6379
// NewHomesteadInstructionSet returns the frontier and homestead
6480
// instructions that can be executed during the homestead phase.
6581
func NewHomesteadInstructionSet() [256]operation {
@@ -810,34 +826,39 @@ func NewFrontierInstructionSet() [256]operation {
810826
validateStack: makeStackFunc(2, 0),
811827
memorySize: memoryLog,
812828
valid: true,
829+
writes: true,
813830
},
814831
LOG1: {
815832
execute: makeLog(1),
816833
gasCost: makeGasLog(1),
817834
validateStack: makeStackFunc(3, 0),
818835
memorySize: memoryLog,
819836
valid: true,
837+
writes: true,
820838
},
821839
LOG2: {
822840
execute: makeLog(2),
823841
gasCost: makeGasLog(2),
824842
validateStack: makeStackFunc(4, 0),
825843
memorySize: memoryLog,
826844
valid: true,
845+
writes: true,
827846
},
828847
LOG3: {
829848
execute: makeLog(3),
830849
gasCost: makeGasLog(3),
831850
validateStack: makeStackFunc(5, 0),
832851
memorySize: memoryLog,
833852
valid: true,
853+
writes: true,
834854
},
835855
LOG4: {
836856
execute: makeLog(4),
837857
gasCost: makeGasLog(4),
838858
validateStack: makeStackFunc(6, 0),
839859
memorySize: memoryLog,
840860
valid: true,
861+
writes: true,
841862
},
842863
CREATE: {
843864
execute: opCreate,

‎core/vm/memory_table.go‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,13 @@ func memoryDelegateCall(stack *Stack) *big.Int {
7474
return math.BigMax(x, y)
7575
}
7676

77+
func memoryStaticCall(stack *Stack) *big.Int {
78+
x := calcMemSize(stack.Back(4), stack.Back(5))
79+
y := calcMemSize(stack.Back(2), stack.Back(3))
80+
81+
return math.BigMax(x, y)
82+
}
83+
7784
func memoryReturn(stack *Stack) *big.Int {
7885
return calcMemSize(stack.Back(0), stack.Back(1))
7986
}

‎core/vm/opcodes.go‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,7 @@ const (
201201
CALLCODE
202202
RETURN
203203
DELEGATECALL
204+
STATICCALL = 0xfa
204205

205206
SELFDESTRUCT = 0xff
206207
)
@@ -355,6 +356,7 @@ var opCodeToString = map[OpCode]string{
355356
RETURN: "RETURN",
356357
CALLCODE: "CALLCODE",
357358
DELEGATECALL: "DELEGATECALL",
359+
STATICCALL: "STATICCALL",
358360
SELFDESTRUCT: "SELFDESTRUCT",
359361

360362
PUSH: "PUSH",
@@ -405,6 +407,7 @@ var stringToOp = map[string]OpCode{
405407
"CALLDATASIZE": CALLDATASIZE,
406408
"CALLDATACOPY": CALLDATACOPY,
407409
"DELEGATECALL": DELEGATECALL,
410+
"STATICCALL": STATICCALL,
408411
"CODESIZE": CODESIZE,
409412
"CODECOPY": CODECOPY,
410413
"GASPRICE": GASPRICE,

0 commit comments

Comments
 (0)