Skip to content

Commit 7aafad2

Browse files
authored
core/vm: better error-info for vm errors (#29354)
1 parent 8876868 commit 7aafad2

File tree

6 files changed

+20
-6
lines changed

6 files changed

+20
-6
lines changed

core/vm/errors.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ var (
3131
ErrContractAddressCollision = errors.New("contract address collision")
3232
ErrExecutionReverted = errors.New("execution reverted")
3333
ErrMaxCodeSizeExceeded = errors.New("max code size exceeded")
34+
ErrMaxInitCodeSizeExceeded = errors.New("max initcode size exceeded")
3435
ErrInvalidJump = errors.New("invalid jump destination")
3536
ErrWriteProtection = errors.New("write protection")
3637
ErrReturnDataOutOfBounds = errors.New("return data out of bounds")

core/vm/gas_table.go

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ package vm
1818

1919
import (
2020
"errors"
21+
"fmt"
2122

2223
"github.com/ethereum/go-ethereum/common"
2324
"github.com/ethereum/go-ethereum/common/math"
@@ -310,9 +311,12 @@ func gasCreateEip3860(evm *EVM, contract *Contract, stack *Stack, mem *Memory, m
310311
return 0, err
311312
}
312313
size, overflow := stack.Back(2).Uint64WithOverflow()
313-
if overflow || size > params.MaxInitCodeSize {
314+
if overflow {
314315
return 0, ErrGasUintOverflow
315316
}
317+
if size > params.MaxInitCodeSize {
318+
return 0, fmt.Errorf("%w: size %d", ErrMaxInitCodeSizeExceeded, size)
319+
}
316320
// Since size <= params.MaxInitCodeSize, these multiplication cannot overflow
317321
moreGas := params.InitCodeWordGas * ((size + 31) / 32)
318322
if gas, overflow = math.SafeAdd(gas, moreGas); overflow {
@@ -326,9 +330,12 @@ func gasCreate2Eip3860(evm *EVM, contract *Contract, stack *Stack, mem *Memory,
326330
return 0, err
327331
}
328332
size, overflow := stack.Back(2).Uint64WithOverflow()
329-
if overflow || size > params.MaxInitCodeSize {
333+
if overflow {
330334
return 0, ErrGasUintOverflow
331335
}
336+
if size > params.MaxInitCodeSize {
337+
return 0, fmt.Errorf("%w: size %d", ErrMaxInitCodeSizeExceeded, size)
338+
}
332339
// Since size <= params.MaxInitCodeSize, these multiplication cannot overflow
333340
moreGas := (params.InitCodeWordGas + params.Keccak256WordGas) * ((size + 31) / 32)
334341
if gas, overflow = math.SafeAdd(gas, moreGas); overflow {

core/vm/gas_table_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ package vm
1818

1919
import (
2020
"bytes"
21+
"errors"
2122
"math"
2223
"math/big"
2324
"sort"
@@ -98,7 +99,7 @@ func TestEIP2200(t *testing.T) {
9899
vmenv := NewEVM(vmctx, TxContext{}, statedb, params.AllEthashProtocolChanges, Config{ExtraEips: []int{2200}})
99100

100101
_, gas, err := vmenv.Call(AccountRef(common.Address{}), address, nil, tt.gaspool, new(uint256.Int))
101-
if err != tt.failure {
102+
if !errors.Is(err, tt.failure) {
102103
t.Errorf("test %d: failure mismatch: have %v, want %v", i, err, tt.failure)
103104
}
104105
if used := tt.gaspool - gas; used != tt.used {

core/vm/interpreter.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
package vm
1818

1919
import (
20+
"fmt"
21+
2022
"github.com/ethereum/go-ethereum/common"
2123
"github.com/ethereum/go-ethereum/common/math"
2224
"github.com/ethereum/go-ethereum/core/tracing"
@@ -255,7 +257,10 @@ func (in *EVMInterpreter) Run(contract *Contract, input []byte, readOnly bool) (
255257
var dynamicCost uint64
256258
dynamicCost, err = operation.dynamicGas(in.evm, contract, stack, mem, memorySize)
257259
cost += dynamicCost // for tracing
258-
if err != nil || !contract.UseGas(dynamicCost, in.evm.Config.Tracer, tracing.GasChangeIgnored) {
260+
if err != nil {
261+
return nil, fmt.Errorf("%w: %v", ErrOutOfGas, err)
262+
}
263+
if !contract.UseGas(dynamicCost, in.evm.Config.Tracer, tracing.GasChangeIgnored) {
259264
return nil, ErrOutOfGas
260265
}
261266

eth/tracers/internal/tracetest/testdata/call_tracer_flat/callcode_precompiled_throw.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@
5757
"gas": "0x1a034",
5858
"init": "0x36600060003760406103e8366000600060095af26001556103e8516002556104085160035500"
5959
},
60-
"error": "out of gas",
60+
"error": "out of gas: not enough gas for reentrancy sentry",
6161
"traceAddress": [],
6262
"subtraces": 1,
6363
"transactionPosition": 117,

eth/tracers/internal/tracetest/testdata/call_tracer_flat/nested_create_action_gas.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@
5757
"gas": "0x19ee4",
5858
"init": "0x5a600055600060006000f0505a60015500"
5959
},
60-
"error": "out of gas",
60+
"error": "out of gas: not enough gas for reentrancy sentry",
6161
"traceAddress": [],
6262
"subtraces": 1,
6363
"transactionPosition": 63,

0 commit comments

Comments
 (0)