From e4aa362e756ab9f3ffaed034a28361c555961b60 Mon Sep 17 00:00:00 2001 From: Jared Wasinger Date: Sat, 30 Nov 2024 00:05:58 +0700 Subject: [PATCH] finishing touches --- core/vm/gas_table.go | 16 +++++++++++----- core/vm/instructions.go | 2 +- core/vm/jump_table.go | 6 +++--- core/vm/opcodes.go | 4 ++-- 4 files changed, 17 insertions(+), 11 deletions(-) diff --git a/core/vm/gas_table.go b/core/vm/gas_table.go index c7f2a9f5fb..476ead6203 100644 --- a/core/vm/gas_table.go +++ b/core/vm/gas_table.go @@ -54,7 +54,6 @@ func evmmaxMemoryGasCost(pc uint64, scope *ScopeContext, newMemSize uint64, newE curEVMMAXMemSizePadded := toWordSize(curEVMMAXMemSize) * 32 newEVMMAXMemSizePadded := toWordSize(newEVMMAXMemSize) * 32 - // if newEVMMAXMemSize + newEVMMemSize > curEVMMAXMemSize + curEVMMemSize if newMemSizePadded > uint64(scope.Memory.Len()) || newEVMMAXMemSizePadded > curEVMMAXMemSizePadded { // if this is called by the invocation of SETUPX, the new evm memory is // 0, but we still need it to compute the fee @@ -548,7 +547,7 @@ func isPowerOfTwo(val *big.Int) bool { return false } -func gasSetupx(pc uint64, evm *EVM, scope *ScopeContext, memorySize uint64) (uint64, error) { +func gasSetmodx(pc uint64, evm *EVM, scope *ScopeContext, memorySize uint64) (uint64, error) { if !scope.Stack.Back(0).IsUint64() || !scope.Stack.Back(2).IsUint64() || !scope.Stack.Back(3).IsUint64() { return 0, errors.New("one or more parameters overflows 64 bits") } @@ -619,9 +618,11 @@ func gasStorex(pc uint64, evm *EVM, scope *ScopeContext, memorySize uint64) (uin } if scope.modExtState.active.IsModulusBinary() { + // TODO: seems pretty impossible that this can overflow, just adding a note + // here to remind myself to double-check this once again. return toWordSize(storeSize) * params.CopyGas, nil } else { - return count.Uint64() * uint64(params.MulmodxCost[int(scope.modExtState.active.ElemSize()/8)-1]), nil + return count.Uint64() * params.MulmodxCost[int(scope.modExtState.active.ElemSize()/8)-1], nil } } @@ -646,16 +647,21 @@ func gasLoadx(pc uint64, evm *EVM, scope *ScopeContext, memorySize uint64) (uint return 0, errors.New("out of bounds destination") } - loadSize := count.Uint64() * uint64(scope.modExtState.active.ElemSize()) + loadSize, overflow := math.SafeMul(count.Uint64(), uint64(scope.modExtState.active.ElemSize())) + if overflow { + return 0, fmt.Errorf("overflow") + } last, overflow := math.SafeAdd(dst.Uint64(), loadSize) if overflow || last > uint64(scope.Memory.Len()) { return 0, errors.New("out of bounds destination") } if scope.modExtState.active.IsModulusBinary() { + // TODO: seems pretty impossible that this can overflow, just adding a note + // here to remind myself to double-check this once again. return toWordSize(loadSize) * params.CopyGas, nil } else { - return count.Uint64() * uint64(params.MulmodxCost[int(scope.modExtState.active.ElemSize()/8)-1]), nil + return count.Uint64() * params.MulmodxCost[int(scope.modExtState.active.ElemSize()/8)-1], nil } } diff --git a/core/vm/instructions.go b/core/vm/instructions.go index 9d70cf30c2..5752d54fd8 100644 --- a/core/vm/instructions.go +++ b/core/vm/instructions.go @@ -954,7 +954,7 @@ func makeLog(size int) executionFunc { } } -func opSetupx(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) { +func opSetmodx(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) { id, modOffset, modSize, allocSize := scope.Stack.pop(), scope.Stack.pop(), scope.Stack.pop(), scope.Stack.pop() modulus := scope.Memory.GetCopy(modOffset.Uint64(), modSize.Uint64()) diff --git a/core/vm/jump_table.go b/core/vm/jump_table.go index d56efc930c..f6c10d0932 100644 --- a/core/vm/jump_table.go +++ b/core/vm/jump_table.go @@ -104,9 +104,9 @@ func newPragueEOFInstructionSet() JumpTable { func newEVMMAXInstructionSet() JumpTable { instructionSet := newCancunInstructionSet() - instructionSet[SETUPX] = &operation{ - execute: opSetupx, - dynamicGas: gasSetupx, + instructionSet[SETMODX] = &operation{ + execute: opSetmodx, + dynamicGas: gasSetmodx, minStack: minStack(4, 0), maxStack: maxStack(4, 0), } diff --git a/core/vm/opcodes.go b/core/vm/opcodes.go index 5e1a97df43..8027903f44 100644 --- a/core/vm/opcodes.go +++ b/core/vm/opcodes.go @@ -204,7 +204,7 @@ const ( // 0xc0 range - extended-width modular arithmetic ops. const ( - SETUPX OpCode = 0xc0 + iota + SETMODX OpCode = 0xc0 + iota LOADX STOREX ADDMODX @@ -418,7 +418,7 @@ var opCodeToString = [256]string{ SWAP16: "SWAP16", // 0xc0 range - extended-range modular arithmetic ops - SETUPX: "SETUPX", + SETMODX: "SETMODX", LOADX: "LOADX", STOREX: "STOREX", ADDMODX: "ADDMODX",