Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

core/vm: use uint64 in Memory for indices everywhere #30252

Merged
merged 2 commits into from
Aug 8, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 11 additions & 11 deletions core/vm/instructions.go
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,7 @@ func opSAR(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte

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

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

func opMload(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) {
v := scope.Stack.peek()
offset := int64(v.Uint64())
offset := v.Uint64()
v.SetBytes(scope.Memory.GetPtr(offset, 32))
return nil, nil
}
Expand Down Expand Up @@ -590,7 +590,7 @@ func opCreate(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]b
var (
value = scope.Stack.pop()
offset, size = scope.Stack.pop(), scope.Stack.pop()
input = scope.Memory.GetCopy(int64(offset.Uint64()), int64(size.Uint64()))
input = scope.Memory.GetCopy(offset.Uint64(), size.Uint64())
gas = scope.Contract.Gas
)
if interpreter.evm.chainRules.IsEIP150 {
Expand Down Expand Up @@ -634,7 +634,7 @@ func opCreate2(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]
endowment = scope.Stack.pop()
offset, size = scope.Stack.pop(), scope.Stack.pop()
salt = scope.Stack.pop()
input = scope.Memory.GetCopy(int64(offset.Uint64()), int64(size.Uint64()))
input = scope.Memory.GetCopy(offset.Uint64(), size.Uint64())
gas = scope.Contract.Gas
)

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

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

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

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

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

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

return ret, errStopToken
}

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

interpreter.returnData = ret
return ret, ErrExecutionReverted
Expand Down Expand Up @@ -867,7 +867,7 @@ func makeLog(size int) executionFunc {
topics[i] = addr.Bytes32()
}

d := scope.Memory.GetCopy(int64(mStart.Uint64()), int64(mSize.Uint64()))
d := scope.Memory.GetCopy(mStart.Uint64(), mSize.Uint64())
interpreter.evm.StateDB.AddLog(&types.Log{
Address: scope.Contract.Address(),
Topics: topics,
Expand Down
10 changes: 4 additions & 6 deletions core/vm/memory.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,28 +66,26 @@ func (m *Memory) Resize(size uint64) {
}

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

if len(m.store) > int(offset) {
if uint64(len(m.store)) > offset {
cpy = make([]byte, size)
copy(cpy, m.store[offset:offset+size])

return
}

return
}

// GetPtr returns the offset + size
func (m *Memory) GetPtr(offset, size int64) []byte {
func (m *Memory) GetPtr(offset, size uint64) []byte {
if size == 0 {
return nil
}

if len(m.store) > int(offset) {
if uint64(len(m.store)) > offset {
return m.store[offset : offset+size]
}

Expand Down