diff --git a/core/vm/gas_table.go b/core/vm/gas_table.go index de47e07ac0..046311f9cc 100644 --- a/core/vm/gas_table.go +++ b/core/vm/gas_table.go @@ -71,7 +71,7 @@ func memoryCopierGas(stackpos int) gasFunc { return GasCosts{}, err } // And gas for copying data, charged per word at param.CopyGas - words, overflow := stack.Back(stackpos).Uint64WithOverflow() + words, overflow := stack.back(stackpos).Uint64WithOverflow() if overflow { return GasCosts{}, ErrGasUintOverflow } @@ -100,7 +100,7 @@ func gasSStore(evm *EVM, contract *Contract, stack *Stack, mem *Memory, memorySi return GasCosts{}, ErrWriteProtection } var ( - y, x = stack.Back(1), stack.Back(0) + y, x = stack.back(1), stack.back(0) current, original = evm.StateDB.GetStateAndCommittedState(contract.Address(), x.Bytes32()) ) // The legacy gas metering only takes into consideration the current state @@ -192,7 +192,7 @@ func gasSStoreEIP2200(evm *EVM, contract *Contract, stack *Stack, mem *Memory, m } // Gas sentry honoured, do the actual gas calculation based on the stored value var ( - y, x = stack.Back(1), stack.Back(0) + y, x = stack.back(1), stack.back(0) current, original = evm.StateDB.GetStateAndCommittedState(contract.Address(), x.Bytes32()) ) value := common.Hash(y.Bytes32()) @@ -228,7 +228,7 @@ func gasSStoreEIP2200(evm *EVM, contract *Contract, stack *Stack, mem *Memory, m func makeGasLog(n uint64) gasFunc { return func(evm *EVM, contract *Contract, stack *Stack, mem *Memory, memorySize uint64) (GasCosts, error) { - requestedSize, overflow := stack.Back(1).Uint64WithOverflow() + requestedSize, overflow := stack.back(1).Uint64WithOverflow() if overflow { return GasCosts{}, ErrGasUintOverflow } @@ -261,7 +261,7 @@ func gasKeccak256(evm *EVM, contract *Contract, stack *Stack, mem *Memory, memor if err != nil { return GasCosts{}, err } - wordGas, overflow := stack.Back(1).Uint64WithOverflow() + wordGas, overflow := stack.back(1).Uint64WithOverflow() if overflow { return GasCosts{}, ErrGasUintOverflow } @@ -299,7 +299,7 @@ func gasCreate2(evm *EVM, contract *Contract, stack *Stack, mem *Memory, memoryS if err != nil { return GasCosts{}, err } - wordGas, overflow := stack.Back(2).Uint64WithOverflow() + wordGas, overflow := stack.back(2).Uint64WithOverflow() if overflow { return GasCosts{}, ErrGasUintOverflow } @@ -317,7 +317,7 @@ func gasCreateEip3860(evm *EVM, contract *Contract, stack *Stack, mem *Memory, m if err != nil { return GasCosts{}, err } - size, overflow := stack.Back(2).Uint64WithOverflow() + size, overflow := stack.back(2).Uint64WithOverflow() if overflow { return GasCosts{}, ErrGasUintOverflow } @@ -336,7 +336,7 @@ func gasCreate2Eip3860(evm *EVM, contract *Contract, stack *Stack, mem *Memory, if err != nil { return GasCosts{}, err } - size, overflow := stack.Back(2).Uint64WithOverflow() + size, overflow := stack.back(2).Uint64WithOverflow() if overflow { return GasCosts{}, ErrGasUintOverflow } @@ -352,7 +352,7 @@ func gasCreate2Eip3860(evm *EVM, contract *Contract, stack *Stack, mem *Memory, } func gasExpFrontier(evm *EVM, contract *Contract, stack *Stack, mem *Memory, memorySize uint64) (GasCosts, error) { - expByteLen := uint64((stack.Back(1).BitLen() + 7) / 8) + expByteLen := uint64((stack.back(1).BitLen() + 7) / 8) var ( gas = expByteLen * params.ExpByteFrontier // no overflow check required. Max is 256 * ExpByte gas @@ -365,7 +365,7 @@ func gasExpFrontier(evm *EVM, contract *Contract, stack *Stack, mem *Memory, mem } func gasExpEIP158(evm *EVM, contract *Contract, stack *Stack, mem *Memory, memorySize uint64) (GasCosts, error) { - expByteLen := uint64((stack.Back(1).BitLen() + 7) / 8) + expByteLen := uint64((stack.back(1).BitLen() + 7) / 8) var ( gas = expByteLen * params.ExpByteEIP158 // no overflow check required. Max is 256 * ExpByte gas @@ -390,7 +390,7 @@ func makeCallVariantGasCost(intrinsicFunc gasFunc) gasFunc { if err != nil { return GasCosts{}, err } - evm.callGasTemp, err = callGas(evm.chainRules.IsEIP150, contract.Gas.RegularGas, intrinsic.RegularGas, stack.Back(0)) + evm.callGasTemp, err = callGas(evm.chainRules.IsEIP150, contract.Gas.RegularGas, intrinsic.RegularGas, stack.back(0)) if err != nil { return GasCosts{}, err } @@ -405,8 +405,8 @@ func makeCallVariantGasCost(intrinsicFunc gasFunc) gasFunc { func gasCallIntrinsic(evm *EVM, contract *Contract, stack *Stack, mem *Memory, memorySize uint64) (GasCosts, error) { var ( gas uint64 - transfersValue = !stack.Back(2).IsZero() - address = common.Address(stack.Back(1).Bytes20()) + transfersValue = !stack.back(2).IsZero() + address = common.Address(stack.back(1).Bytes20()) ) if evm.readOnly && transfersValue { return GasCosts{}, ErrWriteProtection @@ -453,7 +453,7 @@ func gasCallCodeIntrinsic(evm *EVM, contract *Contract, stack *Stack, mem *Memor gas uint64 overflow bool ) - if stack.Back(2).Sign() != 0 && !evm.chainRules.IsEIP4762 { + if stack.back(2).Sign() != 0 && !evm.chainRules.IsEIP4762 { gas += params.CallValueTransferGas } if gas, overflow = math.SafeAdd(gas, memoryGas); overflow { @@ -487,7 +487,7 @@ func gasSelfdestruct(evm *EVM, contract *Contract, stack *Stack, mem *Memory, me // EIP150 homestead gas reprice fork: if evm.chainRules.IsEIP150 { gas = params.SelfdestructGasEIP150 - var address = common.Address(stack.Back(0).Bytes20()) + var address = common.Address(stack.back(0).Bytes20()) if evm.chainRules.IsEIP158 { // if empty and transfers value diff --git a/core/vm/instructions.go b/core/vm/instructions.go index 9a84726349..4b05092cc7 100644 --- a/core/vm/instructions.go +++ b/core/vm/instructions.go @@ -1003,7 +1003,7 @@ func opDupN(pc *uint64, evm *EVM, scope *ScopeContext) ([]byte, error) { } //The n‘th stack item is duplicated at the top of the stack. - scope.Stack.push(scope.Stack.Back(n - 1)) + scope.Stack.push(scope.Stack.back(n - 1)) *pc += 1 return nil, nil } @@ -1032,7 +1032,7 @@ func opSwapN(pc *uint64, evm *EVM, scope *ScopeContext) ([]byte, error) { // The (n+1)’th stack item is swapped with the top of the stack. top := scope.Stack.peek() - nth := scope.Stack.Back(n) + nth := scope.Stack.back(n) *top, *nth = *nth, *top *pc += 1 return nil, nil @@ -1064,8 +1064,8 @@ func opExchange(pc *uint64, evm *EVM, scope *ScopeContext) ([]byte, error) { } // The (n+1)’th stack item is swapped with the (m+1)’th stack item. - nth := scope.Stack.Back(n) - mth := scope.Stack.Back(m) + nth := scope.Stack.back(n) + mth := scope.Stack.back(m) *nth, *mth = *mth, *nth *pc += 1 return nil, nil diff --git a/core/vm/memory_table.go b/core/vm/memory_table.go index 63ad967850..8f30cbeee6 100644 --- a/core/vm/memory_table.go +++ b/core/vm/memory_table.go @@ -17,59 +17,59 @@ package vm func memoryKeccak256(stack *Stack) (uint64, bool) { - return calcMemSize64(stack.Back(0), stack.Back(1)) + return calcMemSize64(stack.back(0), stack.back(1)) } func memoryCallDataCopy(stack *Stack) (uint64, bool) { - return calcMemSize64(stack.Back(0), stack.Back(2)) + return calcMemSize64(stack.back(0), stack.back(2)) } func memoryReturnDataCopy(stack *Stack) (uint64, bool) { - return calcMemSize64(stack.Back(0), stack.Back(2)) + return calcMemSize64(stack.back(0), stack.back(2)) } func memoryCodeCopy(stack *Stack) (uint64, bool) { - return calcMemSize64(stack.Back(0), stack.Back(2)) + return calcMemSize64(stack.back(0), stack.back(2)) } func memoryExtCodeCopy(stack *Stack) (uint64, bool) { - return calcMemSize64(stack.Back(1), stack.Back(3)) + return calcMemSize64(stack.back(1), stack.back(3)) } func memoryMLoad(stack *Stack) (uint64, bool) { - return calcMemSize64WithUint(stack.Back(0), 32) + return calcMemSize64WithUint(stack.back(0), 32) } func memoryMStore8(stack *Stack) (uint64, bool) { - return calcMemSize64WithUint(stack.Back(0), 1) + return calcMemSize64WithUint(stack.back(0), 1) } func memoryMStore(stack *Stack) (uint64, bool) { - return calcMemSize64WithUint(stack.Back(0), 32) + return calcMemSize64WithUint(stack.back(0), 32) } func memoryMcopy(stack *Stack) (uint64, bool) { - mStart := stack.Back(0) // stack[0]: dest - if stack.Back(1).Gt(mStart) { - mStart = stack.Back(1) // stack[1]: source + mStart := stack.back(0) // stack[0]: dest + if stack.back(1).Gt(mStart) { + mStart = stack.back(1) // stack[1]: source } - return calcMemSize64(mStart, stack.Back(2)) // stack[2]: length + return calcMemSize64(mStart, stack.back(2)) // stack[2]: length } func memoryCreate(stack *Stack) (uint64, bool) { - return calcMemSize64(stack.Back(1), stack.Back(2)) + return calcMemSize64(stack.back(1), stack.back(2)) } func memoryCreate2(stack *Stack) (uint64, bool) { - return calcMemSize64(stack.Back(1), stack.Back(2)) + return calcMemSize64(stack.back(1), stack.back(2)) } func memoryCall(stack *Stack) (uint64, bool) { - x, overflow := calcMemSize64(stack.Back(5), stack.Back(6)) + x, overflow := calcMemSize64(stack.back(5), stack.back(6)) if overflow { return 0, true } - y, overflow := calcMemSize64(stack.Back(3), stack.Back(4)) + y, overflow := calcMemSize64(stack.back(3), stack.back(4)) if overflow { return 0, true } @@ -80,11 +80,11 @@ func memoryCall(stack *Stack) (uint64, bool) { } func memoryDelegateCall(stack *Stack) (uint64, bool) { - x, overflow := calcMemSize64(stack.Back(4), stack.Back(5)) + x, overflow := calcMemSize64(stack.back(4), stack.back(5)) if overflow { return 0, true } - y, overflow := calcMemSize64(stack.Back(2), stack.Back(3)) + y, overflow := calcMemSize64(stack.back(2), stack.back(3)) if overflow { return 0, true } @@ -95,11 +95,11 @@ func memoryDelegateCall(stack *Stack) (uint64, bool) { } func memoryStaticCall(stack *Stack) (uint64, bool) { - x, overflow := calcMemSize64(stack.Back(4), stack.Back(5)) + x, overflow := calcMemSize64(stack.back(4), stack.back(5)) if overflow { return 0, true } - y, overflow := calcMemSize64(stack.Back(2), stack.Back(3)) + y, overflow := calcMemSize64(stack.back(2), stack.back(3)) if overflow { return 0, true } @@ -110,13 +110,13 @@ func memoryStaticCall(stack *Stack) (uint64, bool) { } func memoryReturn(stack *Stack) (uint64, bool) { - return calcMemSize64(stack.Back(0), stack.Back(1)) + return calcMemSize64(stack.back(0), stack.back(1)) } func memoryRevert(stack *Stack) (uint64, bool) { - return calcMemSize64(stack.Back(0), stack.Back(1)) + return calcMemSize64(stack.back(0), stack.back(1)) } func memoryLog(stack *Stack) (uint64, bool) { - return calcMemSize64(stack.Back(0), stack.Back(1)) + return calcMemSize64(stack.back(0), stack.back(1)) } diff --git a/core/vm/operations_acl.go b/core/vm/operations_acl.go index 313d03819e..86ac262a93 100644 --- a/core/vm/operations_acl.go +++ b/core/vm/operations_acl.go @@ -37,7 +37,7 @@ func makeGasSStoreFunc(clearingRefund uint64) gasFunc { } // Gas sentry honoured, do the actual gas calculation based on the stored value var ( - y, x = stack.Back(1), stack.peek() + y, x = stack.back(1), stack.peek() slot = common.Hash(x.Bytes32()) current, original = evm.StateDB.GetStateAndCommittedState(contract.Address(), slot) cost = uint64(0) @@ -158,7 +158,7 @@ func gasEip2929AccountCheck(evm *EVM, contract *Contract, stack *Stack, mem *Mem func makeCallVariantGasCallEIP2929(oldCalculator gasFunc, addressPosition int) gasFunc { return func(evm *EVM, contract *Contract, stack *Stack, mem *Memory, memorySize uint64) (GasCosts, error) { - addr := common.Address(stack.Back(addressPosition).Bytes20()) + addr := common.Address(stack.back(addressPosition).Bytes20()) // Check slot presence in the access list warmAccess := evm.StateDB.AddressInAccessList(addr) // The WarmStorageReadCostEIP2929 (100) is already deducted in the form of a constant cost, so @@ -269,7 +269,7 @@ func gasCallEIP7702(evm *EVM, contract *Contract, stack *Stack, mem *Memory, mem // Although it's checked in `gasCall`, EIP-7702 loads the target's code before // to determine if it is resolving a delegation. This could incorrectly record // the target in the block access list (BAL) if the call later fails. - transfersValue := !stack.Back(2).IsZero() + transfersValue := !stack.back(2).IsZero() if evm.readOnly && transfersValue { return GasCosts{}, ErrWriteProtection } @@ -281,7 +281,7 @@ func makeCallVariantGasCallEIP7702(intrinsicFunc gasFunc) gasFunc { var ( eip2929Cost uint64 eip7702Cost uint64 - addr = common.Address(stack.Back(1).Bytes20()) + addr = common.Address(stack.back(1).Bytes20()) ) // Perform EIP-2929 checks (stateless), checking address presence // in the accessList and charge the cold access accordingly. @@ -330,7 +330,7 @@ func makeCallVariantGasCallEIP7702(intrinsicFunc gasFunc) gasFunc { } // Calculate the gas budget for the nested call. The costs defined by // EIP-2929 and EIP-7702 have already been applied. - evm.callGasTemp, err = callGas(evm.chainRules.IsEIP150, contract.Gas.RegularGas, intrinsicCost.RegularGas, stack.Back(0)) + evm.callGasTemp, err = callGas(evm.chainRules.IsEIP150, contract.Gas.RegularGas, intrinsicCost.RegularGas, stack.back(0)) if err != nil { return GasCosts{}, err } diff --git a/core/vm/operations_verkle.go b/core/vm/operations_verkle.go index d57f2c4dcf..4d3960a174 100644 --- a/core/vm/operations_verkle.go +++ b/core/vm/operations_verkle.go @@ -56,7 +56,7 @@ func gasExtCodeHash4762(evm *EVM, contract *Contract, stack *Stack, mem *Memory, func makeCallVariantGasEIP4762(oldCalculator gasFunc, withTransferCosts bool) gasFunc { return func(evm *EVM, contract *Contract, stack *Stack, mem *Memory, memorySize uint64) (GasCosts, error) { var ( - target = common.Address(stack.Back(1).Bytes20()) + target = common.Address(stack.back(1).Bytes20()) witnessGas uint64 _, isPrecompile = evm.precompile(target) isSystemContract = target == params.HistoryStorageAddress @@ -64,7 +64,7 @@ func makeCallVariantGasEIP4762(oldCalculator gasFunc, withTransferCosts bool) ga // If value is transferred, it is charged before 1/64th // is subtracted from the available gas pool. - if withTransferCosts && !stack.Back(2).IsZero() { + if withTransferCosts && !stack.back(2).IsZero() { wantedValueTransferWitnessGas := evm.AccessEvents.ValueTransferGas(contract.Address(), target, contract.Gas.RegularGas) if wantedValueTransferWitnessGas > contract.Gas.RegularGas { return GasCosts{RegularGas: wantedValueTransferWitnessGas}, nil @@ -168,8 +168,8 @@ func gasCodeCopyEip4762(evm *EVM, contract *Contract, stack *Stack, mem *Memory, gas := gasCost.RegularGas if !contract.IsDeployment && !contract.IsSystemCall { var ( - codeOffset = stack.Back(1) - length = stack.Back(2) + codeOffset = stack.back(1) + length = stack.back(2) ) uint64CodeOffset, overflow := codeOffset.Uint64WithOverflow() if overflow { diff --git a/core/vm/stack.go b/core/vm/stack.go index b65b002a0d..7d5e57f288 100644 --- a/core/vm/stack.go +++ b/core/vm/stack.go @@ -33,10 +33,15 @@ func newArena() *stackArena { return stackPool.New().(*stackArena) } +// 1025, because in stack() there is a condition check +// for the stack size that would fail if it was set to +// 1024. +const initialStackSize = 1025 + var stackPool = sync.Pool{ New: func() any { return &stackArena{ - data: make([]uint256.Int, 1025), + data: make([]uint256.Int, initialStackSize), } }, } @@ -93,9 +98,8 @@ func (s *Stack) Data() []uint256.Int { } func (s *Stack) push(d *uint256.Int) { - s.inner.data[s.inner.top] = *d - s.inner.top++ - s.size++ + elem := s.get() + *elem = *d } // get returns a pointer to a newly created element @@ -176,7 +180,7 @@ func (s *Stack) peek() *uint256.Int { return &s.inner.data[s.bottom+s.size-1] } -// Back returns the n'th item in stack -func (s *Stack) Back(n int) *uint256.Int { +// back returns the n'th item in stack +func (s *Stack) back(n int) *uint256.Int { return &s.inner.data[s.bottom+s.size-n-1] }