Skip to content

Commit 0b978f9

Browse files
authored
Merge pull request #14981 from karalabe/metropolis-returndata
core/vm: implement RETURNDATA metropolis opcodes
2 parents 76069ee + 64d199e commit 0b978f9

9 files changed

Lines changed: 280 additions & 195 deletions

File tree

‎core/vm/evm.go‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -261,9 +261,9 @@ func (evm *EVM) StaticCall(caller ContractRef, addr common.Address, input []byte
261261
// Make sure the readonly is only set if we aren't in readonly yet
262262
// this makes also sure that the readonly flag isn't removed for
263263
// child calls.
264-
if !evm.interpreter.readonly {
265-
evm.interpreter.readonly = true
266-
defer func() { evm.interpreter.readonly = false }()
264+
if !evm.interpreter.readOnly {
265+
evm.interpreter.readOnly = true
266+
defer func() { evm.interpreter.readOnly = false }()
267267
}
268268

269269
var (

‎core/vm/gas_table.go‎

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,33 @@ func constGasFunc(gas uint64) gasFunc {
6565
}
6666
}
6767

68-
func gasCalldataCopy(gt params.GasTable, evm *EVM, contract *Contract, stack *Stack, mem *Memory, memorySize uint64) (uint64, error) {
68+
func gasCallDataCopy(gt params.GasTable, evm *EVM, contract *Contract, stack *Stack, mem *Memory, memorySize uint64) (uint64, error) {
69+
gas, err := memoryGasCost(mem, memorySize)
70+
if err != nil {
71+
return 0, err
72+
}
73+
74+
var overflow bool
75+
if gas, overflow = math.SafeAdd(gas, GasFastestStep); overflow {
76+
return 0, errGasUintOverflow
77+
}
78+
79+
words, overflow := bigUint64(stack.Back(2))
80+
if overflow {
81+
return 0, errGasUintOverflow
82+
}
83+
84+
if words, overflow = math.SafeMul(toWordSize(words), params.CopyGas); overflow {
85+
return 0, errGasUintOverflow
86+
}
87+
88+
if gas, overflow = math.SafeAdd(gas, words); overflow {
89+
return 0, errGasUintOverflow
90+
}
91+
return gas, nil
92+
}
93+
94+
func gasReturnDataCopy(gt params.GasTable, evm *EVM, contract *Contract, stack *Stack, mem *Memory, memorySize uint64) (uint64, error) {
6995
gas, err := memoryGasCost(mem, memorySize)
7096
if err != nil {
7197
return 0, err

‎core/vm/instructions.go‎

Lines changed: 52 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,9 @@ import (
2929
)
3030

3131
var (
32-
bigZero = new(big.Int)
33-
errWriteProtection = errors.New("evm: write protection")
32+
bigZero = new(big.Int)
33+
errWriteProtection = errors.New("evm: write protection")
34+
errReturnDataOutOfBounds = errors.New("evm: return data out of bounds")
3435
)
3536

3637
func opAdd(pc *uint64, evm *EVM, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
@@ -242,13 +243,15 @@ func opAnd(pc *uint64, evm *EVM, contract *Contract, memory *Memory, stack *Stac
242243
evm.interpreter.intPool.put(y)
243244
return nil, nil
244245
}
246+
245247
func opOr(pc *uint64, evm *EVM, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
246248
x, y := stack.pop(), stack.pop()
247249
stack.push(x.Or(x, y))
248250

249251
evm.interpreter.intPool.put(y)
250252
return nil, nil
251253
}
254+
252255
func opXor(pc *uint64, evm *EVM, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
253256
x, y := stack.pop(), stack.pop()
254257
stack.push(x.Xor(x, y))
@@ -268,6 +271,7 @@ func opByte(pc *uint64, evm *EVM, contract *Contract, memory *Memory, stack *Sta
268271
evm.interpreter.intPool.put(th)
269272
return nil, nil
270273
}
274+
271275
func opAddmod(pc *uint64, evm *EVM, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
272276
x, y, z := stack.pop(), stack.pop(), stack.pop()
273277
if z.Cmp(bigZero) > 0 {
@@ -281,6 +285,7 @@ func opAddmod(pc *uint64, evm *EVM, contract *Contract, memory *Memory, stack *S
281285
evm.interpreter.intPool.put(y, z)
282286
return nil, nil
283287
}
288+
284289
func opMulmod(pc *uint64, evm *EVM, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
285290
x, y, z := stack.pop(), stack.pop(), stack.pop()
286291
if z.Cmp(bigZero) > 0 {
@@ -338,25 +343,47 @@ func opCallValue(pc *uint64, evm *EVM, contract *Contract, memory *Memory, stack
338343
return nil, nil
339344
}
340345

341-
func opCalldataLoad(pc *uint64, evm *EVM, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
346+
func opCallDataLoad(pc *uint64, evm *EVM, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
342347
stack.push(new(big.Int).SetBytes(getDataBig(contract.Input, stack.pop(), big32)))
343348
return nil, nil
344349
}
345350

346-
func opCalldataSize(pc *uint64, evm *EVM, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
351+
func opCallDataSize(pc *uint64, evm *EVM, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
347352
stack.push(evm.interpreter.intPool.get().SetInt64(int64(len(contract.Input))))
348353
return nil, nil
349354
}
350355

351-
func opCalldataCopy(pc *uint64, evm *EVM, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
356+
func opCallDataCopy(pc *uint64, evm *EVM, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
357+
var (
358+
memOffset = stack.pop()
359+
dataOffset = stack.pop()
360+
length = stack.pop()
361+
)
362+
memory.Set(memOffset.Uint64(), length.Uint64(), getDataBig(contract.Input, dataOffset, length))
363+
364+
evm.interpreter.intPool.put(memOffset, dataOffset, length)
365+
return nil, nil
366+
}
367+
368+
func opReturnDataSize(pc *uint64, evm *EVM, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
369+
stack.push(evm.interpreter.intPool.get().SetUint64(uint64(len(evm.interpreter.returnData))))
370+
return nil, nil
371+
}
372+
373+
func opReturnDataCopy(pc *uint64, evm *EVM, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
352374
var (
353-
mOff = stack.pop()
354-
cOff = stack.pop()
355-
l = stack.pop()
375+
memOffset = stack.pop()
376+
dataOffset = stack.pop()
377+
length = stack.pop()
356378
)
357-
memory.Set(mOff.Uint64(), l.Uint64(), getDataBig(contract.Input, cOff, l))
379+
defer evm.interpreter.intPool.put(memOffset, dataOffset, length)
380+
381+
end := new(big.Int).Add(dataOffset, length)
382+
if end.BitLen() > 64 || uint64(len(evm.interpreter.returnData)) < end.Uint64() {
383+
return nil, errReturnDataOutOfBounds
384+
}
385+
memory.Set(memOffset.Uint64(), length.Uint64(), evm.interpreter.returnData[dataOffset.Uint64():end.Uint64()])
358386

359-
evm.interpreter.intPool.put(mOff, cOff, l)
360387
return nil, nil
361388
}
362389

@@ -378,31 +405,28 @@ func opCodeSize(pc *uint64, evm *EVM, contract *Contract, memory *Memory, stack
378405

379406
func opCodeCopy(pc *uint64, evm *EVM, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
380407
var (
381-
mOff = stack.pop()
382-
cOff = stack.pop()
383-
l = stack.pop()
408+
memOffset = stack.pop()
409+
codeOffset = stack.pop()
410+
length = stack.pop()
384411
)
385-
codeCopy := getDataBig(contract.Code, cOff, l)
412+
codeCopy := getDataBig(contract.Code, codeOffset, length)
413+
memory.Set(memOffset.Uint64(), length.Uint64(), codeCopy)
386414

387-
memory.Set(mOff.Uint64(), l.Uint64(), codeCopy)
388-
389-
evm.interpreter.intPool.put(mOff, cOff, l)
415+
evm.interpreter.intPool.put(memOffset, codeOffset, length)
390416
return nil, nil
391417
}
392418

393419
func opExtCodeCopy(pc *uint64, evm *EVM, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
394420
var (
395-
addr = common.BigToAddress(stack.pop())
396-
mOff = stack.pop()
397-
cOff = stack.pop()
398-
l = stack.pop()
421+
addr = common.BigToAddress(stack.pop())
422+
memOffset = stack.pop()
423+
codeOffset = stack.pop()
424+
length = stack.pop()
399425
)
400-
codeCopy := getDataBig(evm.StateDB.GetCode(addr), cOff, l)
401-
402-
memory.Set(mOff.Uint64(), l.Uint64(), codeCopy)
403-
404-
evm.interpreter.intPool.put(mOff, cOff, l)
426+
codeCopy := getDataBig(evm.StateDB.GetCode(addr), codeOffset, length)
427+
memory.Set(memOffset.Uint64(), length.Uint64(), codeCopy)
405428

429+
evm.interpreter.intPool.put(memOffset, codeOffset, length)
406430
return nil, nil
407431
}
408432

@@ -507,6 +531,7 @@ func opJump(pc *uint64, evm *EVM, contract *Contract, memory *Memory, stack *Sta
507531
evm.interpreter.intPool.put(pos)
508532
return nil, nil
509533
}
534+
510535
func opJumpi(pc *uint64, evm *EVM, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
511536
pos, cond := stack.pop(), stack.pop()
512537
if cond.Sign() != 0 {
@@ -522,6 +547,7 @@ func opJumpi(pc *uint64, evm *EVM, contract *Contract, memory *Memory, stack *St
522547
evm.interpreter.intPool.put(pos, cond)
523548
return nil, nil
524549
}
550+
525551
func opJumpdest(pc *uint64, evm *EVM, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
526552
return nil, nil
527553
}

‎core/vm/interpreter.go‎

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,8 @@ type Interpreter struct {
5959
gasTable params.GasTable
6060
intPool *intPool
6161

62-
readonly bool
62+
readOnly bool // Whether to throw on stateful modifications
63+
returnData []byte // Last CALL's return data for subsequent reuse
6364
}
6465

6566
// NewInterpreter returns a new instance of the Interpreter.
@@ -88,7 +89,7 @@ func NewInterpreter(evm *EVM, cfg Config) *Interpreter {
8889

8990
func (in *Interpreter) enforceRestrictions(op OpCode, operation operation, stack *Stack) error {
9091
if in.evm.chainRules.IsMetropolis {
91-
if in.readonly {
92+
if in.readOnly {
9293
// If the interpreter is operating in readonly mode, make sure no
9394
// state-modifying operation is performed. The 3rd stack item
9495
// for a call operation is the value. Transfering value from one
@@ -113,6 +114,10 @@ func (in *Interpreter) Run(snapshot int, contract *Contract, input []byte) (ret
113114
in.evm.depth++
114115
defer func() { in.evm.depth-- }()
115116

117+
// Reset the previous call's return data. It's unimportant to preserve the old buffer
118+
// as every returning call will return new data anyway.
119+
in.returnData = nil
120+
116121
// Don't bother with the execution if there's no code.
117122
if len(contract.Code) == 0 {
118123
return nil, nil
@@ -213,10 +218,10 @@ func (in *Interpreter) Run(snapshot int, contract *Contract, input []byte) (ret
213218
case !operation.jumps:
214219
pc++
215220
}
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
221+
// if the operation clears the return data (e.g. it has returning data)
222+
// set the last return to the result of the operation.
223+
if operation.returns {
224+
in.returnData = res
220225
}
221226
}
222227
return nil, nil

‎core/vm/jump_table.go‎

Lines changed: 25 additions & 5 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+
// returns determines whether the opertions sets the return data
57+
returns bool
5658
}
5759

5860
var (
@@ -72,6 +74,20 @@ func NewMetropolisInstructionSet() [256]operation {
7274
validateStack: makeStackFunc(6, 1),
7375
memorySize: memoryStaticCall,
7476
valid: true,
77+
returns: true,
78+
}
79+
instructionSet[RETURNDATASIZE] = operation{
80+
execute: opReturnDataSize,
81+
gasCost: constGasFunc(GasQuickStep),
82+
validateStack: makeStackFunc(0, 1),
83+
valid: true,
84+
}
85+
instructionSet[RETURNDATACOPY] = operation{
86+
execute: opReturnDataCopy,
87+
gasCost: gasReturnDataCopy,
88+
validateStack: makeStackFunc(3, 0),
89+
memorySize: memoryReturnDataCopy,
90+
valid: true,
7591
}
7692
return instructionSet
7793
}
@@ -86,6 +102,7 @@ func NewHomesteadInstructionSet() [256]operation {
86102
validateStack: makeStackFunc(6, 1),
87103
memorySize: memoryDelegateCall,
88104
valid: true,
105+
returns: true,
89106
}
90107
return instructionSet
91108
}
@@ -271,22 +288,22 @@ func NewFrontierInstructionSet() [256]operation {
271288
valid: true,
272289
},
273290
CALLDATALOAD: {
274-
execute: opCalldataLoad,
291+
execute: opCallDataLoad,
275292
gasCost: constGasFunc(GasFastestStep),
276293
validateStack: makeStackFunc(1, 1),
277294
valid: true,
278295
},
279296
CALLDATASIZE: {
280-
execute: opCalldataSize,
297+
execute: opCallDataSize,
281298
gasCost: constGasFunc(GasQuickStep),
282299
validateStack: makeStackFunc(0, 1),
283300
valid: true,
284301
},
285302
CALLDATACOPY: {
286-
execute: opCalldataCopy,
287-
gasCost: gasCalldataCopy,
303+
execute: opCallDataCopy,
304+
gasCost: gasCallDataCopy,
288305
validateStack: makeStackFunc(3, 0),
289-
memorySize: memoryCalldataCopy,
306+
memorySize: memoryCallDataCopy,
290307
valid: true,
291308
},
292309
CODESIZE: {
@@ -867,20 +884,23 @@ func NewFrontierInstructionSet() [256]operation {
867884
memorySize: memoryCreate,
868885
valid: true,
869886
writes: true,
887+
returns: true,
870888
},
871889
CALL: {
872890
execute: opCall,
873891
gasCost: gasCall,
874892
validateStack: makeStackFunc(7, 1),
875893
memorySize: memoryCall,
876894
valid: true,
895+
returns: true,
877896
},
878897
CALLCODE: {
879898
execute: opCallCode,
880899
gasCost: gasCallCode,
881900
validateStack: makeStackFunc(7, 1),
882901
memorySize: memoryCall,
883902
valid: true,
903+
returns: true,
884904
},
885905
RETURN: {
886906
execute: opReturn,

‎core/vm/memory.go‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ import "fmt"
2222
type Memory struct {
2323
store []byte
2424
lastGasCost uint64
25-
lastReturn []byte
2625
}
2726

2827
func NewMemory() *Memory {

‎core/vm/memory_table.go‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,11 @@ func memorySha3(stack *Stack) *big.Int {
2626
return calcMemSize(stack.Back(0), stack.Back(1))
2727
}
2828

29-
func memoryCalldataCopy(stack *Stack) *big.Int {
29+
func memoryCallDataCopy(stack *Stack) *big.Int {
30+
return calcMemSize(stack.Back(0), stack.Back(2))
31+
}
32+
33+
func memoryReturnDataCopy(stack *Stack) *big.Int {
3034
return calcMemSize(stack.Back(0), stack.Back(2))
3135
}
3236

0 commit comments

Comments
 (0)