Skip to content

Commit 9bd6068

Browse files
obscurenkaralabe
authored andcommitted
core/vm: implement RETURNDATA metropolis opcodes
1 parent 76069ee commit 9bd6068

6 files changed

Lines changed: 253 additions & 171 deletions

File tree

‎core/vm/gas_table.go‎

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,32 @@ func gasCalldataCopy(gt params.GasTable, evm *EVM, contract *Contract, stack *St
9191
return gas, nil
9292
}
9393

94+
func gasReturnDataCopy(gt params.GasTable, evm *EVM, contract *Contract, stack *Stack, mem *Memory, memorySize uint64) (uint64, error) {
95+
gas, err := memoryGasCost(mem, memorySize)
96+
if err != nil {
97+
return 0, err
98+
}
99+
100+
var overflow bool
101+
if gas, overflow = math.SafeAdd(gas, GasFastestStep); overflow {
102+
return 0, errGasUintOverflow
103+
}
104+
105+
words, overflow := bigUint64(stack.Back(2))
106+
if overflow {
107+
return 0, errGasUintOverflow
108+
}
109+
110+
if words, overflow = math.SafeMul(toWordSize(words), params.CopyGas); overflow {
111+
return 0, errGasUintOverflow
112+
}
113+
114+
if gas, overflow = math.SafeAdd(gas, words); overflow {
115+
return 0, errGasUintOverflow
116+
}
117+
return gas, nil
118+
}
119+
94120
func gasSStore(gt params.GasTable, evm *EVM, contract *Contract, stack *Stack, mem *Memory, memorySize uint64) (uint64, error) {
95121
var (
96122
y, x = stack.Back(1), stack.Back(0)

‎core/vm/instructions.go‎

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ import (
3131
var (
3232
bigZero = new(big.Int)
3333
errWriteProtection = errors.New("evm: write protection")
34+
errReadOutOfBound = errors.New("evm: read out of bound")
3435
)
3536

3637
func opAdd(pc *uint64, evm *EVM, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
@@ -360,6 +361,28 @@ func opCalldataCopy(pc *uint64, evm *EVM, contract *Contract, memory *Memory, st
360361
return nil, nil
361362
}
362363

364+
func opReturnDataSize(pc *uint64, evm *EVM, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
365+
stack.push(evm.interpreter.intPool.get().SetUint64(uint64(len(evm.interpreter.returnData))))
366+
return nil, nil
367+
}
368+
369+
func opReturnDataCopy(pc *uint64, evm *EVM, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
370+
var (
371+
mOff = stack.pop()
372+
cOff = stack.pop()
373+
l = stack.pop()
374+
)
375+
defer evm.interpreter.intPool.put(mOff, cOff, l)
376+
377+
cEnd := new(big.Int).Add(cOff, l)
378+
if cEnd.BitLen() > 64 || uint64(len(evm.interpreter.returnData)) < cEnd.Uint64() {
379+
return nil, errReadOutOfBound
380+
}
381+
memory.Set(mOff.Uint64(), l.Uint64(), evm.interpreter.returnData[cOff.Uint64():cEnd.Uint64()])
382+
383+
return nil, nil
384+
}
385+
363386
func opExtCodeSize(pc *uint64, evm *EVM, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
364387
a := stack.pop()
365388

‎core/vm/interpreter.go‎

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,8 @@ type Interpreter struct {
6060
intPool *intPool
6161

6262
readonly bool
63+
// returnData contains the last call's return data
64+
returnData []byte
6365
}
6466

6567
// NewInterpreter returns a new instance of the Interpreter.
@@ -113,6 +115,10 @@ func (in *Interpreter) Run(snapshot int, contract *Contract, input []byte) (ret
113115
in.evm.depth++
114116
defer func() { in.evm.depth-- }()
115117

118+
// Reset the previous call's return data. It's unimportant to preserve the old buffer
119+
// as every returning call will return new data anyway.
120+
in.returnData = nil
121+
116122
// Don't bother with the execution if there's no code.
117123
if len(contract.Code) == 0 {
118124
return nil, nil
@@ -213,10 +219,10 @@ func (in *Interpreter) Run(snapshot int, contract *Contract, input []byte) (ret
213219
case !operation.jumps:
214220
pc++
215221
}
216-
// if the operation returned a value make sure that is also set
217-
// the last return data.
218-
if res != nil {
219-
mem.lastReturn = ret
222+
// if the operation clears the return data (e.g. it has returning data)
223+
// set the last return to the result of the operation.
224+
if operation.clearsReturndata {
225+
in.returnData = res
220226
}
221227
}
222228
return nil, nil

‎core/vm/jump_table.go‎

Lines changed: 34 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,8 @@ type operation struct {
5353
valid bool
5454
// reverts determined whether the operation reverts state
5555
reverts bool
56+
// clearsReturndata determines whether the opertions clears the return data
57+
clearsReturndata bool
5658
}
5759

5860
var (
@@ -73,6 +75,19 @@ func NewMetropolisInstructionSet() [256]operation {
7375
memorySize: memoryStaticCall,
7476
valid: true,
7577
}
78+
instructionSet[RETURNDATASIZE] = operation{
79+
execute: opReturnDataSize,
80+
gasCost: constGasFunc(GasQuickStep),
81+
validateStack: makeStackFunc(0, 1),
82+
valid: true,
83+
}
84+
instructionSet[RETURNDATACOPY] = operation{
85+
execute: opReturnDataCopy,
86+
gasCost: gasReturnDataCopy,
87+
validateStack: makeStackFunc(3, 0),
88+
memorySize: memoryReturnDataCopy,
89+
valid: true,
90+
}
7691
return instructionSet
7792
}
7893

@@ -861,26 +876,29 @@ func NewFrontierInstructionSet() [256]operation {
861876
writes: true,
862877
},
863878
CREATE: {
864-
execute: opCreate,
865-
gasCost: gasCreate,
866-
validateStack: makeStackFunc(3, 1),
867-
memorySize: memoryCreate,
868-
valid: true,
869-
writes: true,
879+
execute: opCreate,
880+
gasCost: gasCreate,
881+
validateStack: makeStackFunc(3, 1),
882+
memorySize: memoryCreate,
883+
valid: true,
884+
writes: true,
885+
clearsReturndata: true,
870886
},
871887
CALL: {
872-
execute: opCall,
873-
gasCost: gasCall,
874-
validateStack: makeStackFunc(7, 1),
875-
memorySize: memoryCall,
876-
valid: true,
888+
execute: opCall,
889+
gasCost: gasCall,
890+
validateStack: makeStackFunc(7, 1),
891+
memorySize: memoryCall,
892+
valid: true,
893+
clearsReturndata: true,
877894
},
878895
CALLCODE: {
879-
execute: opCallCode,
880-
gasCost: gasCallCode,
881-
validateStack: makeStackFunc(7, 1),
882-
memorySize: memoryCall,
883-
valid: true,
896+
execute: opCallCode,
897+
gasCost: gasCallCode,
898+
validateStack: makeStackFunc(7, 1),
899+
memorySize: memoryCall,
900+
valid: true,
901+
clearsReturndata: true,
884902
},
885903
RETURN: {
886904
execute: opReturn,

‎core/vm/memory_table.go‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,10 @@ func memoryCalldataCopy(stack *Stack) *big.Int {
3030
return calcMemSize(stack.Back(0), stack.Back(2))
3131
}
3232

33+
func memoryReturnDataCopy(stack *Stack) *big.Int {
34+
return calcMemSize(stack.Back(0), stack.Back(2))
35+
}
36+
3337
func memoryCodeCopy(stack *Stack) *big.Int {
3438
return calcMemSize(stack.Back(0), stack.Back(2))
3539
}

0 commit comments

Comments
 (0)