Skip to content

Commit 3d123bc

Browse files
obscurenkaralabe
authored andcommitted
core/vm: implement metropolis static call opcode
1 parent 9facf64 commit 3d123bc

7 files changed

Lines changed: 153 additions & 3 deletions

File tree

‎core/vm/evm.go‎

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,51 @@ func (evm *EVM) DelegateCall(caller ContractRef, addr common.Address, input []by
245245
return ret, contract.Gas, err
246246
}
247247

248+
func (evm *EVM) StaticCall(caller ContractRef, addr common.Address, input []byte, gas uint64) (ret []byte, leftOverGas uint64, err error) {
249+
if evm.vmConfig.NoRecursion && evm.depth > 0 {
250+
return nil, gas, nil
251+
}
252+
253+
// Depth check execution. Fail if we're trying to execute above the
254+
// limit.
255+
if evm.depth > int(params.CallCreateDepth) {
256+
return nil, gas, ErrDepth
257+
}
258+
259+
// make sure the readonly is only set if we aren't in readonly yet
260+
// this makes also sure that the readonly flag isn't removed for
261+
// child calls.
262+
if !evm.interpreter.readonly {
263+
evm.interpreter.readonly = true
264+
defer func() { evm.interpreter.readonly = false }()
265+
}
266+
267+
var (
268+
to = AccountRef(addr)
269+
snapshot = evm.StateDB.Snapshot()
270+
)
271+
if !evm.StateDB.Exist(addr) {
272+
return nil, gas, nil
273+
}
274+
275+
// initialise a new contract and set the code that is to be used by the
276+
// EVM. The contract is a scoped evmironment for this execution context
277+
// only.
278+
contract := NewContract(caller, to, new(big.Int), gas)
279+
contract.SetCallCode(&addr, evm.StateDB.GetCodeHash(addr), evm.StateDB.GetCode(addr))
280+
281+
ret, err = evm.interpreter.Run(snapshot, contract, input)
282+
// When an error was returned by the EVM or when setting the creation code
283+
// above we revert to the snapshot and consume any gas remaining. Additionally
284+
// when we're in homestead this also counts for code storage gas errors.
285+
if err != nil {
286+
contract.UseGas(contract.Gas)
287+
288+
evm.StateDB.RevertToSnapshot(snapshot)
289+
}
290+
return ret, contract.Gas, err
291+
}
292+
248293
// Create creates a new contract using code as deployment code.
249294
func (evm *EVM) Create(caller ContractRef, code []byte, gas uint64, value *big.Int) (ret []byte, contractAddr common.Address, leftOverGas uint64, err error) {
250295
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: 16 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,19 @@ 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 4th 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 ||
98+
((op == CALL || op == CALLCODE) && stack.Back(3).BitLen() > 0) {
99+
return errWriteProtection
100+
}
101+
}
102+
}
88103
return nil
89104
}
90105

@@ -95,6 +110,7 @@ func (in *Interpreter) enforceRestrictions(op OpCode, operation operation, stack
95110
// considered a revert-and-consume-all-gas operation. No error specific checks
96111
// should be handled to reduce complexity and errors further down the in.
97112
func (in *Interpreter) Run(snapshot int, contract *Contract, input []byte) (ret []byte, err error) {
113+
// Increment the call depth which is restricted to 1024
98114
in.evm.depth++
99115
defer func() { in.evm.depth-- }()
100116

‎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)