Skip to content

Commit b566cfd

Browse files
holimankaralabe
authored andcommitted
core/evm: avoid copying memory for input in calls (#20177)
* core/evm, contracts: avoid copying memory for input in calls + make ecrecover not modify input buffer * core/vm: optimize mstore a bit * core/vm: change Get -> GetCopy in vm memory access
1 parent 7a6d5d0 commit b566cfd

File tree

6 files changed

+84
-18
lines changed

6 files changed

+84
-18
lines changed

core/vm/contracts.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,8 +106,13 @@ func (c *ecrecover) Run(input []byte) ([]byte, error) {
106106
if !allZero(input[32:63]) || !crypto.ValidateSignatureValues(v, r, s, false) {
107107
return nil, nil
108108
}
109+
// We must make sure not to modify the 'input', so placing the 'v' along with
110+
// the signature needs to be done on a new allocation
111+
sig := make([]byte, 65)
112+
copy(sig, input[64:128])
113+
sig[64] = v
109114
// v needs to be at the end for libsecp256k1
110-
pubKey, err := crypto.Ecrecover(input[:32], append(input[64:128], v))
115+
pubKey, err := crypto.Ecrecover(input[:32], sig)
111116
// make sure the public key is a valid one
112117
if err != nil {
113118
return nil, nil

core/vm/contracts_test.go

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
package vm
1818

1919
import (
20+
"bytes"
2021
"fmt"
2122
"math/big"
2223
"reflect"
@@ -409,6 +410,11 @@ func testPrecompiled(addr string, test precompiledTest, t *testing.T) {
409410
} else if common.Bytes2Hex(res) != test.expected {
410411
t.Errorf("Expected %v, got %v", test.expected, common.Bytes2Hex(res))
411412
}
413+
// Verify that the precompile did not touch the input buffer
414+
exp := common.Hex2Bytes(test.input)
415+
if !bytes.Equal(in, exp) {
416+
t.Errorf("Precompiled %v modified input data", addr)
417+
}
412418
})
413419
}
414420

@@ -423,6 +429,11 @@ func testPrecompiledFailure(addr string, test precompiledFailureTest, t *testing
423429
if !reflect.DeepEqual(err, test.expectedError) {
424430
t.Errorf("Expected error [%v], got [%v]", test.expectedError, err)
425431
}
432+
// Verify that the precompile did not touch the input buffer
433+
exp := common.Hex2Bytes(test.input)
434+
if !bytes.Equal(in, exp) {
435+
t.Errorf("Precompiled %v modified input data", addr)
436+
}
426437
})
427438
}
428439

@@ -574,3 +585,55 @@ func TestPrecompileBlake2FMalformedInput(t *testing.T) {
574585
testPrecompiledFailure("09", test, t)
575586
}
576587
}
588+
589+
// EcRecover test vectors
590+
var ecRecoverTests = []precompiledTest{
591+
{
592+
input: "a8b53bdf3306a35a7103ab5504a0c9b492295564b6202b1942a84ef300107281" +
593+
"000000000000000000000000000000000000000000000000000000000000001b" +
594+
"3078356531653033663533636531386237373263636230303933666637316633" +
595+
"6635336635633735623734646362333161383561613862383839326234653862" +
596+
"1122334455667788991011121314151617181920212223242526272829303132",
597+
expected: "",
598+
name: "CallEcrecoverUnrecoverableKey",
599+
},
600+
{
601+
input: "18c547e4f7b0f325ad1e56f57e26c745b09a3e503d86e00e5255ff7f715d3d1c" +
602+
"000000000000000000000000000000000000000000000000000000000000001c" +
603+
"73b1693892219d736caba55bdb67216e485557ea6b6af75f37096c9aa6a5a75f" +
604+
"eeb940b1d03b21e36b0e47e79769f095fe2ab855bd91e3a38756b7d75a9c4549",
605+
expected: "000000000000000000000000a94f5374fce5edbc8e2a8697c15331677e6ebf0b",
606+
name: "ValidKey",
607+
},
608+
{
609+
input: "18c547e4f7b0f325ad1e56f57e26c745b09a3e503d86e00e5255ff7f715d3d1c" +
610+
"100000000000000000000000000000000000000000000000000000000000001c" +
611+
"73b1693892219d736caba55bdb67216e485557ea6b6af75f37096c9aa6a5a75f" +
612+
"eeb940b1d03b21e36b0e47e79769f095fe2ab855bd91e3a38756b7d75a9c4549",
613+
expected: "",
614+
name: "InvalidHighV-bits-1",
615+
},
616+
{
617+
input: "18c547e4f7b0f325ad1e56f57e26c745b09a3e503d86e00e5255ff7f715d3d1c" +
618+
"000000000000000000000000000000000000001000000000000000000000001c" +
619+
"73b1693892219d736caba55bdb67216e485557ea6b6af75f37096c9aa6a5a75f" +
620+
"eeb940b1d03b21e36b0e47e79769f095fe2ab855bd91e3a38756b7d75a9c4549",
621+
expected: "",
622+
name: "InvalidHighV-bits-2",
623+
},
624+
{
625+
input: "18c547e4f7b0f325ad1e56f57e26c745b09a3e503d86e00e5255ff7f715d3d1c" +
626+
"000000000000000000000000000000000000001000000000000000000000011c" +
627+
"73b1693892219d736caba55bdb67216e485557ea6b6af75f37096c9aa6a5a75f" +
628+
"eeb940b1d03b21e36b0e47e79769f095fe2ab855bd91e3a38756b7d75a9c4549",
629+
expected: "",
630+
name: "InvalidHighV-bits-3",
631+
},
632+
}
633+
634+
func TestPrecompiledEcrecover(t *testing.T) {
635+
for _, test := range ecRecoverTests {
636+
testPrecompiled("01", test, t)
637+
}
638+
639+
}

core/vm/instructions.go

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -384,7 +384,7 @@ func opSAR(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *
384384

385385
func opSha3(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
386386
offset, size := stack.pop(), stack.pop()
387-
data := memory.Get(offset.Int64(), size.Int64())
387+
data := memory.GetPtr(offset.Int64(), size.Int64())
388388

389389
if interpreter.hasher == nil {
390390
interpreter.hasher = sha3.NewLegacyKeccak256().(keccakState)
@@ -602,11 +602,9 @@ func opPop(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *
602602
}
603603

604604
func opMload(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
605-
offset := stack.pop()
606-
val := interpreter.intPool.get().SetBytes(memory.Get(offset.Int64(), 32))
607-
stack.push(val)
608-
609-
interpreter.intPool.put(offset)
605+
v := stack.peek()
606+
offset := v.Int64()
607+
v.SetBytes(memory.GetPtr(offset, 32))
610608
return nil, nil
611609
}
612610

@@ -691,7 +689,7 @@ func opCreate(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memor
691689
var (
692690
value = stack.pop()
693691
offset, size = stack.pop(), stack.pop()
694-
input = memory.Get(offset.Int64(), size.Int64())
692+
input = memory.GetCopy(offset.Int64(), size.Int64())
695693
gas = contract.Gas
696694
)
697695
if interpreter.evm.chainRules.IsEIP150 {
@@ -725,7 +723,7 @@ func opCreate2(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memo
725723
endowment = stack.pop()
726724
offset, size = stack.pop(), stack.pop()
727725
salt = stack.pop()
728-
input = memory.Get(offset.Int64(), size.Int64())
726+
input = memory.GetCopy(offset.Int64(), size.Int64())
729727
gas = contract.Gas
730728
)
731729

@@ -757,7 +755,7 @@ func opCall(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory
757755
toAddr := common.BigToAddress(addr)
758756
value = math.U256(value)
759757
// Get the arguments from the memory.
760-
args := memory.Get(inOffset.Int64(), inSize.Int64())
758+
args := memory.GetPtr(inOffset.Int64(), inSize.Int64())
761759

762760
if value.Sign() != 0 {
763761
gas += params.CallStipend
@@ -786,7 +784,7 @@ func opCallCode(pc *uint64, interpreter *EVMInterpreter, contract *Contract, mem
786784
toAddr := common.BigToAddress(addr)
787785
value = math.U256(value)
788786
// Get arguments from the memory.
789-
args := memory.Get(inOffset.Int64(), inSize.Int64())
787+
args := memory.GetPtr(inOffset.Int64(), inSize.Int64())
790788

791789
if value.Sign() != 0 {
792790
gas += params.CallStipend
@@ -814,7 +812,7 @@ func opDelegateCall(pc *uint64, interpreter *EVMInterpreter, contract *Contract,
814812
addr, inOffset, inSize, retOffset, retSize := stack.pop(), stack.pop(), stack.pop(), stack.pop(), stack.pop()
815813
toAddr := common.BigToAddress(addr)
816814
// Get arguments from the memory.
817-
args := memory.Get(inOffset.Int64(), inSize.Int64())
815+
args := memory.GetPtr(inOffset.Int64(), inSize.Int64())
818816

819817
ret, returnGas, err := interpreter.evm.DelegateCall(contract, toAddr, args, gas)
820818
if err != nil {
@@ -839,7 +837,7 @@ func opStaticCall(pc *uint64, interpreter *EVMInterpreter, contract *Contract, m
839837
addr, inOffset, inSize, retOffset, retSize := stack.pop(), stack.pop(), stack.pop(), stack.pop(), stack.pop()
840838
toAddr := common.BigToAddress(addr)
841839
// Get arguments from the memory.
842-
args := memory.Get(inOffset.Int64(), inSize.Int64())
840+
args := memory.GetPtr(inOffset.Int64(), inSize.Int64())
843841

844842
ret, returnGas, err := interpreter.evm.StaticCall(contract, toAddr, args, gas)
845843
if err != nil {
@@ -895,7 +893,7 @@ func makeLog(size int) executionFunc {
895893
topics[i] = common.BigToHash(stack.pop())
896894
}
897895

898-
d := memory.Get(mStart.Int64(), mSize.Int64())
896+
d := memory.GetCopy(mStart.Int64(), mSize.Int64())
899897
interpreter.evm.StateDB.AddLog(&types.Log{
900898
Address: contract.Address(),
901899
Topics: topics,

core/vm/instructions_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -509,12 +509,12 @@ func TestOpMstore(t *testing.T) {
509509
v := "abcdef00000000000000abba000000000deaf000000c0de00100000000133700"
510510
stack.pushN(new(big.Int).SetBytes(common.Hex2Bytes(v)), big.NewInt(0))
511511
opMstore(&pc, evmInterpreter, nil, mem, stack)
512-
if got := common.Bytes2Hex(mem.Get(0, 32)); got != v {
512+
if got := common.Bytes2Hex(mem.GetCopy(0, 32)); got != v {
513513
t.Fatalf("Mstore fail, got %v, expected %v", got, v)
514514
}
515515
stack.pushN(big.NewInt(0x1), big.NewInt(0))
516516
opMstore(&pc, evmInterpreter, nil, mem, stack)
517-
if common.Bytes2Hex(mem.Get(0, 32)) != "0000000000000000000000000000000000000000000000000000000000000001" {
517+
if common.Bytes2Hex(mem.GetCopy(0, 32)) != "0000000000000000000000000000000000000000000000000000000000000001" {
518518
t.Fatalf("Mstore failed to overwrite previous value")
519519
}
520520
poolOfIntPools.put(evmInterpreter.intPool)

core/vm/memory.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ func (m *Memory) Resize(size uint64) {
7070
}
7171

7272
// Get returns offset + size as a new slice
73-
func (m *Memory) Get(offset, size int64) (cpy []byte) {
73+
func (m *Memory) GetCopy(offset, size int64) (cpy []byte) {
7474
if size == 0 {
7575
return nil
7676
}

eth/tracers/tracer.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ func (mw *memoryWrapper) slice(begin, end int64) []byte {
9999
log.Warn("Tracer accessed out of bound memory", "available", mw.memory.Len(), "offset", begin, "size", end-begin)
100100
return nil
101101
}
102-
return mw.memory.Get(begin, end-begin)
102+
return mw.memory.GetCopy(begin, end-begin)
103103
}
104104

105105
// getUint returns the 32 bytes at the specified address interpreted as a uint.

0 commit comments

Comments
 (0)