Skip to content

Commit 78b5cd1

Browse files
seidroid[bot]alexander-seicursoragent
authored
Backport release/v6.8: Stop printing precompile errors and cap the recorded message at 1024 bytes (#4680)
Backport of #4654 to `release/v6.8`. Co-authored-by: alexander-sei <alexanderh@seinetwork.io> Co-authored-by: Cursor <cursoragent@cursor.com>
1 parent 96b6741 commit 78b5cd1

2 files changed

Lines changed: 73 additions & 2 deletions

File tree

‎precompiles/common/precompiles.go‎

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ import (
66
"errors"
77
"fmt"
88
"math/big"
9+
"strings"
10+
"unicode/utf8"
911

1012
"github.com/ethereum/go-ethereum/accounts/abi"
1113
"github.com/ethereum/go-ethereum/common"
@@ -21,6 +23,10 @@ import (
2123

2224
const UnknownMethodCallGas uint64 = 3000
2325

26+
// MaxPrecompileErrorLength is the longest precompile error message, in bytes, that
27+
// HandlePrecompileError records on the StateDB.
28+
const MaxPrecompileErrorLength = 1024
29+
2430
type Contexter interface {
2531
Ctx() sdk.Context
2632
}
@@ -92,12 +98,27 @@ func (p Precompile) Run(evm *vm.EVM, caller common.Address, callingContract comm
9298
func HandlePrecompileError(err error, evm *vm.EVM, operation string) {
9399
if err != nil {
94100
if sdb := state.GetDBImpl(evm.StateDB); sdb != nil {
95-
sdb.SetPrecompileError(err)
101+
sdb.SetPrecompileError(truncateError(err))
96102
}
97103
metrics.IncrementErrorMetrics(operation, err)
98104
}
99105
}
100106

107+
// truncateError returns err when its message is at most MaxPrecompileErrorLength bytes, and
108+
// otherwise a new error whose message is a copy of err's, cut on a UTF-8 boundary to fit. The new
109+
// error does not wrap err and shares no memory with err's message.
110+
func truncateError(err error) error {
111+
msg := err.Error()
112+
if len(msg) <= MaxPrecompileErrorLength {
113+
return err
114+
}
115+
cut := MaxPrecompileErrorLength
116+
for cut > 0 && !utf8.RuneStart(msg[cut]) {
117+
cut--
118+
}
119+
return errors.New(strings.Clone(msg[:cut]))
120+
}
121+
101122
func (p Precompile) Prepare(evm *vm.EVM, input []byte) (sdk.Context, *abi.Method, []interface{}, error) {
102123
ctxer := state.GetDBImpl(evm.StateDB)
103124
if ctxer == nil {
@@ -162,7 +183,6 @@ func (d DynamicGasPrecompile) RunAndCalculateGas(evm *vm.EVM, caller common.Addr
162183
}
163184
HandlePrecompileError(err, evm, operation)
164185
if err != nil {
165-
fmt.Printf("precompile %s encountered error: %v\n", d.name, err)
166186
err = vm.ErrExecutionReverted
167187
}
168188
}()

‎precompiles/common/precompiles_test.go‎

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,15 @@ package common_test
33
import (
44
"bytes"
55
"errors"
6+
"fmt"
67
"math/big"
78
"os"
9+
"runtime"
10+
"strings"
811
"testing"
12+
"time"
13+
"unicode/utf8"
14+
"unsafe"
915

1016
sdk "github.com/sei-protocol/sei-chain/sei-cosmos/types"
1117
"github.com/sei-protocol/sei-chain/x/evm/state"
@@ -54,6 +60,51 @@ func TestHandlePrecompileError(t *testing.T) {
5460
common.HandlePrecompileError(errors.New("other error"), evm, "other")
5561
}
5662

63+
func TestHandlePrecompileErrorTruncatesLongMessage(t *testing.T) {
64+
k := &testkeeper.EVMTestApp.EvmKeeper
65+
ctx := testkeeper.EVMTestApp.GetContextForDeliverTx(nil)
66+
stateDB := state.NewDBImpl(ctx, k, false)
67+
evm := &vm.EVM{StateDB: stateDB}
68+
69+
short := errors.New("short error")
70+
common.HandlePrecompileError(short, evm, "short")
71+
require.Same(t, short, stateDB.GetPrecompileError())
72+
73+
// "€" is three bytes wide, so the cut lands inside a character.
74+
long := fmt.Errorf("wrapped: %w", errors.New(strings.Repeat("€", common.MaxPrecompileErrorLength)))
75+
common.HandlePrecompileError(long, evm, "long")
76+
stored := stateDB.GetPrecompileError()
77+
require.LessOrEqual(t, len(stored.Error()), common.MaxPrecompileErrorLength)
78+
require.Greater(t, len(stored.Error()), common.MaxPrecompileErrorLength-utf8.UTFMax)
79+
require.True(t, utf8.ValidString(stored.Error()))
80+
require.True(t, strings.HasPrefix(long.Error(), stored.Error()))
81+
}
82+
83+
func TestHandlePrecompileErrorDoesNotRetainFullMessage(t *testing.T) {
84+
k := &testkeeper.EVMTestApp.EvmKeeper
85+
ctx := testkeeper.EVMTestApp.GetContextForDeliverTx(nil)
86+
stateDB := state.NewDBImpl(ctx, k, false)
87+
88+
released := recordLongPrecompileError(&vm.EVM{StateDB: stateDB})
89+
runtime.GC()
90+
select {
91+
case <-released:
92+
case <-time.After(10 * time.Second):
93+
t.Fatal("the StateDB keeps the full message of a truncated precompile error reachable")
94+
}
95+
require.Error(t, stateDB.GetPrecompileError())
96+
}
97+
98+
// recordLongPrecompileError records an error with an over-long message on evm's StateDB and returns
99+
// a channel that is closed once that message has been garbage collected.
100+
func recordLongPrecompileError(evm *vm.EVM) <-chan struct{} {
101+
err := fmt.Errorf("wrapped: %w", errors.New(strings.Repeat("€", common.MaxPrecompileErrorLength)))
102+
released := make(chan struct{})
103+
runtime.AddCleanup(unsafe.StringData(err.Error()), func(c chan struct{}) { close(c) }, released)
104+
common.HandlePrecompileError(err, evm, "long")
105+
return released
106+
}
107+
57108
type MockPrecompileExecutor struct {
58109
throw bool
59110
}

0 commit comments

Comments
 (0)