Skip to content

Commit 4e0fea4

Browse files
committed
core/vm: polish RETURNDATA, add missing returns to CALL*
1 parent 9bd6068 commit 4e0fea4

7 files changed

Lines changed: 75 additions & 72 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: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ 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) {
6969
gas, err := memoryGasCost(mem, memorySize)
7070
if err != nil {
7171
return 0, err

‎core/vm/instructions.go‎

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

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

3737
func opAdd(pc *uint64, evm *EVM, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
@@ -243,13 +243,15 @@ func opAnd(pc *uint64, evm *EVM, contract *Contract, memory *Memory, stack *Stac
243243
evm.interpreter.intPool.put(y)
244244
return nil, nil
245245
}
246+
246247
func opOr(pc *uint64, evm *EVM, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
247248
x, y := stack.pop(), stack.pop()
248249
stack.push(x.Or(x, y))
249250

250251
evm.interpreter.intPool.put(y)
251252
return nil, nil
252253
}
254+
253255
func opXor(pc *uint64, evm *EVM, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
254256
x, y := stack.pop(), stack.pop()
255257
stack.push(x.Xor(x, y))
@@ -269,6 +271,7 @@ func opByte(pc *uint64, evm *EVM, contract *Contract, memory *Memory, stack *Sta
269271
evm.interpreter.intPool.put(th)
270272
return nil, nil
271273
}
274+
272275
func opAddmod(pc *uint64, evm *EVM, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
273276
x, y, z := stack.pop(), stack.pop(), stack.pop()
274277
if z.Cmp(bigZero) > 0 {
@@ -282,6 +285,7 @@ func opAddmod(pc *uint64, evm *EVM, contract *Contract, memory *Memory, stack *S
282285
evm.interpreter.intPool.put(y, z)
283286
return nil, nil
284287
}
288+
285289
func opMulmod(pc *uint64, evm *EVM, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
286290
x, y, z := stack.pop(), stack.pop(), stack.pop()
287291
if z.Cmp(bigZero) > 0 {
@@ -339,25 +343,25 @@ func opCallValue(pc *uint64, evm *EVM, contract *Contract, memory *Memory, stack
339343
return nil, nil
340344
}
341345

342-
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) {
343347
stack.push(new(big.Int).SetBytes(getDataBig(contract.Input, stack.pop(), big32)))
344348
return nil, nil
345349
}
346350

347-
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) {
348352
stack.push(evm.interpreter.intPool.get().SetInt64(int64(len(contract.Input))))
349353
return nil, nil
350354
}
351355

352-
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) {
353357
var (
354-
mOff = stack.pop()
355-
cOff = stack.pop()
356-
l = stack.pop()
358+
memOffset = stack.pop()
359+
dataOffset = stack.pop()
360+
length = stack.pop()
357361
)
358-
memory.Set(mOff.Uint64(), l.Uint64(), getDataBig(contract.Input, cOff, l))
362+
memory.Set(memOffset.Uint64(), length.Uint64(), getDataBig(contract.Input, dataOffset, length))
359363

360-
evm.interpreter.intPool.put(mOff, cOff, l)
364+
evm.interpreter.intPool.put(memOffset, dataOffset, length)
361365
return nil, nil
362366
}
363367

@@ -368,17 +372,17 @@ func opReturnDataSize(pc *uint64, evm *EVM, contract *Contract, memory *Memory,
368372

369373
func opReturnDataCopy(pc *uint64, evm *EVM, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
370374
var (
371-
mOff = stack.pop()
372-
cOff = stack.pop()
373-
l = stack.pop()
375+
memOffset = stack.pop()
376+
dataOffset = stack.pop()
377+
length = stack.pop()
374378
)
375-
defer evm.interpreter.intPool.put(mOff, cOff, l)
379+
defer evm.interpreter.intPool.put(memOffset, dataOffset, length)
376380

377-
cEnd := new(big.Int).Add(cOff, l)
378-
if cEnd.BitLen() > 64 || uint64(len(evm.interpreter.returnData)) < cEnd.Uint64() {
379-
return nil, errReadOutOfBound
381+
end := new(big.Int).Add(dataOffset, length)
382+
if end.BitLen() > 64 || uint64(len(evm.interpreter.returnData)) < end.Uint64() {
383+
return nil, errReturnDataOutOfBounds
380384
}
381-
memory.Set(mOff.Uint64(), l.Uint64(), evm.interpreter.returnData[cOff.Uint64():cEnd.Uint64()])
385+
memory.Set(memOffset.Uint64(), length.Uint64(), evm.interpreter.returnData[dataOffset.Uint64():end.Uint64()])
382386

383387
return nil, nil
384388
}
@@ -401,31 +405,28 @@ func opCodeSize(pc *uint64, evm *EVM, contract *Contract, memory *Memory, stack
401405

402406
func opCodeCopy(pc *uint64, evm *EVM, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
403407
var (
404-
mOff = stack.pop()
405-
cOff = stack.pop()
406-
l = stack.pop()
408+
memOffset = stack.pop()
409+
codeOffset = stack.pop()
410+
length = stack.pop()
407411
)
408-
codeCopy := getDataBig(contract.Code, cOff, l)
409-
410-
memory.Set(mOff.Uint64(), l.Uint64(), codeCopy)
412+
codeCopy := getDataBig(contract.Code, codeOffset, length)
413+
memory.Set(memOffset.Uint64(), length.Uint64(), codeCopy)
411414

412-
evm.interpreter.intPool.put(mOff, cOff, l)
415+
evm.interpreter.intPool.put(memOffset, codeOffset, length)
413416
return nil, nil
414417
}
415418

416419
func opExtCodeCopy(pc *uint64, evm *EVM, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
417420
var (
418-
addr = common.BigToAddress(stack.pop())
419-
mOff = stack.pop()
420-
cOff = stack.pop()
421-
l = stack.pop()
421+
addr = common.BigToAddress(stack.pop())
422+
memOffset = stack.pop()
423+
codeOffset = stack.pop()
424+
length = stack.pop()
422425
)
423-
codeCopy := getDataBig(evm.StateDB.GetCode(addr), cOff, l)
424-
425-
memory.Set(mOff.Uint64(), l.Uint64(), codeCopy)
426-
427-
evm.interpreter.intPool.put(mOff, cOff, l)
426+
codeCopy := getDataBig(evm.StateDB.GetCode(addr), codeOffset, length)
427+
memory.Set(memOffset.Uint64(), length.Uint64(), codeCopy)
428428

429+
evm.interpreter.intPool.put(memOffset, codeOffset, length)
429430
return nil, nil
430431
}
431432

@@ -530,6 +531,7 @@ func opJump(pc *uint64, evm *EVM, contract *Contract, memory *Memory, stack *Sta
530531
evm.interpreter.intPool.put(pos)
531532
return nil, nil
532533
}
534+
533535
func opJumpi(pc *uint64, evm *EVM, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
534536
pos, cond := stack.pop(), stack.pop()
535537
if cond.Sign() != 0 {
@@ -545,6 +547,7 @@ func opJumpi(pc *uint64, evm *EVM, contract *Contract, memory *Memory, stack *St
545547
evm.interpreter.intPool.put(pos, cond)
546548
return nil, nil
547549
}
550+
548551
func opJumpdest(pc *uint64, evm *EVM, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
549552
return nil, nil
550553
}

‎core/vm/interpreter.go‎

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

62-
readonly bool
63-
// returnData contains the last call's return data
64-
returnData []byte
62+
readOnly bool // Whether to throw on stateful modifications
63+
returnData []byte // Last CALL's return data for subsequent reuse
6564
}
6665

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

9190
func (in *Interpreter) enforceRestrictions(op OpCode, operation operation, stack *Stack) error {
9291
if in.evm.chainRules.IsMetropolis {
93-
if in.readonly {
92+
if in.readOnly {
9493
// If the interpreter is operating in readonly mode, make sure no
9594
// state-modifying operation is performed. The 3rd stack item
9695
// for a call operation is the value. Transfering value from one
@@ -221,7 +220,7 @@ func (in *Interpreter) Run(snapshot int, contract *Contract, input []byte) (ret
221220
}
222221
// if the operation clears the return data (e.g. it has returning data)
223222
// set the last return to the result of the operation.
224-
if operation.clearsReturndata {
223+
if operation.returns {
225224
in.returnData = res
226225
}
227226
}

‎core/vm/jump_table.go‎

Lines changed: 28 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +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
56+
// returns determines whether the opertions sets the return data
57+
returns bool
5858
}
5959

6060
var (
@@ -74,6 +74,7 @@ func NewMetropolisInstructionSet() [256]operation {
7474
validateStack: makeStackFunc(6, 1),
7575
memorySize: memoryStaticCall,
7676
valid: true,
77+
returns: true,
7778
}
7879
instructionSet[RETURNDATASIZE] = operation{
7980
execute: opReturnDataSize,
@@ -101,6 +102,7 @@ func NewHomesteadInstructionSet() [256]operation {
101102
validateStack: makeStackFunc(6, 1),
102103
memorySize: memoryDelegateCall,
103104
valid: true,
105+
returns: true,
104106
}
105107
return instructionSet
106108
}
@@ -286,22 +288,22 @@ func NewFrontierInstructionSet() [256]operation {
286288
valid: true,
287289
},
288290
CALLDATALOAD: {
289-
execute: opCalldataLoad,
291+
execute: opCallDataLoad,
290292
gasCost: constGasFunc(GasFastestStep),
291293
validateStack: makeStackFunc(1, 1),
292294
valid: true,
293295
},
294296
CALLDATASIZE: {
295-
execute: opCalldataSize,
297+
execute: opCallDataSize,
296298
gasCost: constGasFunc(GasQuickStep),
297299
validateStack: makeStackFunc(0, 1),
298300
valid: true,
299301
},
300302
CALLDATACOPY: {
301-
execute: opCalldataCopy,
302-
gasCost: gasCalldataCopy,
303+
execute: opCallDataCopy,
304+
gasCost: gasCallDataCopy,
303305
validateStack: makeStackFunc(3, 0),
304-
memorySize: memoryCalldataCopy,
306+
memorySize: memoryCallDataCopy,
305307
valid: true,
306308
},
307309
CODESIZE: {
@@ -876,29 +878,29 @@ func NewFrontierInstructionSet() [256]operation {
876878
writes: true,
877879
},
878880
CREATE: {
879-
execute: opCreate,
880-
gasCost: gasCreate,
881-
validateStack: makeStackFunc(3, 1),
882-
memorySize: memoryCreate,
883-
valid: true,
884-
writes: true,
885-
clearsReturndata: true,
881+
execute: opCreate,
882+
gasCost: gasCreate,
883+
validateStack: makeStackFunc(3, 1),
884+
memorySize: memoryCreate,
885+
valid: true,
886+
writes: true,
887+
returns: true,
886888
},
887889
CALL: {
888-
execute: opCall,
889-
gasCost: gasCall,
890-
validateStack: makeStackFunc(7, 1),
891-
memorySize: memoryCall,
892-
valid: true,
893-
clearsReturndata: true,
890+
execute: opCall,
891+
gasCost: gasCall,
892+
validateStack: makeStackFunc(7, 1),
893+
memorySize: memoryCall,
894+
valid: true,
895+
returns: true,
894896
},
895897
CALLCODE: {
896-
execute: opCallCode,
897-
gasCost: gasCallCode,
898-
validateStack: makeStackFunc(7, 1),
899-
memorySize: memoryCall,
900-
valid: true,
901-
clearsReturndata: true,
898+
execute: opCallCode,
899+
gasCost: gasCallCode,
900+
validateStack: makeStackFunc(7, 1),
901+
memorySize: memoryCall,
902+
valid: true,
903+
returns: true,
902904
},
903905
RETURN: {
904906
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: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ 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 {
3030
return calcMemSize(stack.Back(0), stack.Back(2))
3131
}
3232

0 commit comments

Comments
 (0)