Skip to content

Commit 4a3aed3

Browse files
authored
core/vm: use uint64 in memory for indices everywhere (#30252)
Consistently use `uint64` for indices in `Memory` and drop lots of type conversions from `uint64` to `int64`. --------- Co-authored-by: lmittmann <[email protected]>
1 parent 978041f commit 4a3aed3

File tree

2 files changed

+18
-25
lines changed

2 files changed

+18
-25
lines changed

core/vm/instructions.go

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,7 @@ func opSAR(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte
232232

233233
func opKeccak256(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) {
234234
offset, size := scope.Stack.pop(), scope.Stack.peek()
235-
data := scope.Memory.GetPtr(int64(offset.Uint64()), int64(size.Uint64()))
235+
data := scope.Memory.GetPtr(offset.Uint64(), size.Uint64())
236236

237237
if interpreter.hasher == nil {
238238
interpreter.hasher = crypto.NewKeccakState()
@@ -502,7 +502,7 @@ func opPop(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte
502502

503503
func opMload(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) {
504504
v := scope.Stack.peek()
505-
offset := int64(v.Uint64())
505+
offset := v.Uint64()
506506
v.SetBytes(scope.Memory.GetPtr(offset, 32))
507507
return nil, nil
508508
}
@@ -670,7 +670,7 @@ func opCreate(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]b
670670
var (
671671
value = scope.Stack.pop()
672672
offset, size = scope.Stack.pop(), scope.Stack.pop()
673-
input = scope.Memory.GetCopy(int64(offset.Uint64()), int64(size.Uint64()))
673+
input = scope.Memory.GetCopy(offset.Uint64(), size.Uint64())
674674
gas = scope.Contract.Gas
675675
)
676676
if interpreter.evm.chainRules.IsEIP150 {
@@ -714,7 +714,7 @@ func opCreate2(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]
714714
endowment = scope.Stack.pop()
715715
offset, size = scope.Stack.pop(), scope.Stack.pop()
716716
salt = scope.Stack.pop()
717-
input = scope.Memory.GetCopy(int64(offset.Uint64()), int64(size.Uint64()))
717+
input = scope.Memory.GetCopy(offset.Uint64(), size.Uint64())
718718
gas = scope.Contract.Gas
719719
)
720720

@@ -752,7 +752,7 @@ func opCall(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byt
752752
addr, value, inOffset, inSize, retOffset, retSize := stack.pop(), stack.pop(), stack.pop(), stack.pop(), stack.pop(), stack.pop()
753753
toAddr := common.Address(addr.Bytes20())
754754
// Get the arguments from the memory.
755-
args := scope.Memory.GetPtr(int64(inOffset.Uint64()), int64(inSize.Uint64()))
755+
args := scope.Memory.GetPtr(inOffset.Uint64(), inSize.Uint64())
756756

757757
if interpreter.readOnly && !value.IsZero() {
758758
return nil, ErrWriteProtection
@@ -788,7 +788,7 @@ func opCallCode(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([
788788
addr, value, inOffset, inSize, retOffset, retSize := stack.pop(), stack.pop(), stack.pop(), stack.pop(), stack.pop(), stack.pop()
789789
toAddr := common.Address(addr.Bytes20())
790790
// Get arguments from the memory.
791-
args := scope.Memory.GetPtr(int64(inOffset.Uint64()), int64(inSize.Uint64()))
791+
args := scope.Memory.GetPtr(inOffset.Uint64(), inSize.Uint64())
792792

793793
if !value.IsZero() {
794794
gas += params.CallStipend
@@ -821,7 +821,7 @@ func opDelegateCall(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext
821821
addr, inOffset, inSize, retOffset, retSize := stack.pop(), stack.pop(), stack.pop(), stack.pop(), stack.pop()
822822
toAddr := common.Address(addr.Bytes20())
823823
// Get arguments from the memory.
824-
args := scope.Memory.GetPtr(int64(inOffset.Uint64()), int64(inSize.Uint64()))
824+
args := scope.Memory.GetPtr(inOffset.Uint64(), inSize.Uint64())
825825

826826
ret, returnGas, err := interpreter.evm.DelegateCall(scope.Contract, toAddr, args, gas)
827827
if err != nil {
@@ -850,7 +850,7 @@ func opStaticCall(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext)
850850
addr, inOffset, inSize, retOffset, retSize := stack.pop(), stack.pop(), stack.pop(), stack.pop(), stack.pop()
851851
toAddr := common.Address(addr.Bytes20())
852852
// Get arguments from the memory.
853-
args := scope.Memory.GetPtr(int64(inOffset.Uint64()), int64(inSize.Uint64()))
853+
args := scope.Memory.GetPtr(inOffset.Uint64(), inSize.Uint64())
854854

855855
ret, returnGas, err := interpreter.evm.StaticCall(scope.Contract, toAddr, args, gas)
856856
if err != nil {
@@ -871,14 +871,14 @@ func opStaticCall(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext)
871871

872872
func opReturn(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) {
873873
offset, size := scope.Stack.pop(), scope.Stack.pop()
874-
ret := scope.Memory.GetPtr(int64(offset.Uint64()), int64(size.Uint64()))
874+
ret := scope.Memory.GetPtr(offset.Uint64(), size.Uint64())
875875

876876
return ret, errStopToken
877877
}
878878

879879
func opRevert(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) {
880880
offset, size := scope.Stack.pop(), scope.Stack.pop()
881-
ret := scope.Memory.GetPtr(int64(offset.Uint64()), int64(size.Uint64()))
881+
ret := scope.Memory.GetPtr(offset.Uint64(), size.Uint64())
882882

883883
interpreter.returnData = ret
884884
return ret, ErrExecutionReverted
@@ -947,7 +947,7 @@ func makeLog(size int) executionFunc {
947947
topics[i] = addr.Bytes32()
948948
}
949949

950-
d := scope.Memory.GetCopy(int64(mStart.Uint64()), int64(mSize.Uint64()))
950+
d := scope.Memory.GetCopy(mStart.Uint64(), mSize.Uint64())
951951
interpreter.evm.StateDB.AddLog(&types.Log{
952952
Address: scope.Contract.Address(),
953953
Topics: topics,

core/vm/memory.go

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -66,32 +66,25 @@ func (m *Memory) Resize(size uint64) {
6666
}
6767

6868
// GetCopy returns offset + size as a new slice
69-
func (m *Memory) GetCopy(offset, size int64) (cpy []byte) {
69+
func (m *Memory) GetCopy(offset, size uint64) (cpy []byte) {
7070
if size == 0 {
7171
return nil
7272
}
7373

74-
if len(m.store) > int(offset) {
75-
cpy = make([]byte, size)
76-
copy(cpy, m.store[offset:offset+size])
77-
78-
return
79-
}
80-
74+
// memory is always resized before being accessed, no need to check bounds
75+
cpy = make([]byte, size)
76+
copy(cpy, m.store[offset:offset+size])
8177
return
8278
}
8379

8480
// GetPtr returns the offset + size
85-
func (m *Memory) GetPtr(offset, size int64) []byte {
81+
func (m *Memory) GetPtr(offset, size uint64) []byte {
8682
if size == 0 {
8783
return nil
8884
}
8985

90-
if len(m.store) > int(offset) {
91-
return m.store[offset : offset+size]
92-
}
93-
94-
return nil
86+
// memory is always resized before being accessed, no need to check bounds
87+
return m.store[offset : offset+size]
9588
}
9689

9790
// Len returns the length of the backing slice

0 commit comments

Comments
 (0)