eth/tracers/native: guard agains stack underflow in keccak256PreimageTracer

This commit is contained in:
Dragan Milic 2025-09-23 23:35:21 +02:00
parent 4520ec0e71
commit cf80c3754a
2 changed files with 10 additions and 6 deletions

View file

@ -55,8 +55,14 @@ func newKeccak256preimageTracer(ctx *tracers.Context, cfg json.RawMessage, chain
func (t *keccak256preimageTracer) OnOpcode(pc uint64, op byte, gas, cost uint64, scope tracing.OpContext, rData []byte, depth int, err error) { func (t *keccak256preimageTracer) OnOpcode(pc uint64, op byte, gas, cost uint64, scope tracing.OpContext, rData []byte, depth int, err error) {
if op == byte(vm.KECCAK256) { if op == byte(vm.KECCAK256) {
dataOffset := peepStack(scope.StackData(), 0).Uint64() sd := scope.StackData()
dataLength := peepStack(scope.StackData(), 1).Uint64() // it turns out that sometimes the stack is empty, evm will fail in this case, but we should not panic here
if len(sd) < 2 {
return
}
dataOffset := peepStack(sd, 0).Uint64()
dataLength := peepStack(sd, 1).Uint64()
preimage, err := internal.GetMemoryCopyPadded(scope.MemoryData(), int64(dataOffset), int64(dataLength)) preimage, err := internal.GetMemoryCopyPadded(scope.MemoryData(), int64(dataOffset), int64(dataLength))
if err != nil { if err != nil {
log.Warn("erc7562Tracer: failed to copy keccak preimage from memory", "err", err) log.Warn("erc7562Tracer: failed to copy keccak preimage from memory", "err", err)

View file

@ -397,10 +397,8 @@ func TestKeccak256PreimageTracerInsufficientStack(t *testing.T) {
stack: stack, stack: stack,
} }
// This should panic due to insufficient stack, but we're testing it doesn't crash the test // This should not panic due to insufficient stack
require.Panics(t, func() {
tracer.OnOpcode(0, byte(vm.KECCAK256), 0, 0, mockScope, nil, 0, nil) tracer.OnOpcode(0, byte(vm.KECCAK256), 0, 0, mockScope, nil, 0, nil)
})
} }
func TestKeccak256PreimageTracerLargeData(t *testing.T) { func TestKeccak256PreimageTracerLargeData(t *testing.T) {