Skip to content

Commit 33791db

Browse files
tintinwebholiman
andauthored
tracers: avoid panic on invalid arguments (#20612)
* add regression tests for #20611 * eth/tracers: fix panics occurring for invalid params in js-tracers Co-authored-by: Martin Holst Swende <martin@swende.se>
1 parent 5a9c964 commit 33791db

2 files changed

Lines changed: 44 additions & 2 deletions

File tree

‎eth/tracers/tracer.go‎

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,15 @@ type memoryWrapper struct {
9393

9494
// slice returns the requested range of memory as a byte slice.
9595
func (mw *memoryWrapper) slice(begin, end int64) []byte {
96+
if end == begin {
97+
return []byte{}
98+
}
99+
if end < begin || begin < 0 {
100+
// TODO(karalabe): We can't js-throw from Go inside duktape inside Go. The Go
101+
// runtime goes belly up https://github.com/golang/go/issues/15639.
102+
log.Warn("Tracer accessed out of bound memory", "offset", begin, "end", end)
103+
return nil
104+
}
96105
if mw.memory.Len() < int(end) {
97106
// TODO(karalabe): We can't js-throw from Go inside duktape inside Go. The Go
98107
// runtime goes belly up https://github.com/golang/go/issues/15639.
@@ -104,7 +113,7 @@ func (mw *memoryWrapper) slice(begin, end int64) []byte {
104113

105114
// getUint returns the 32 bytes at the specified address interpreted as a uint.
106115
func (mw *memoryWrapper) getUint(addr int64) *big.Int {
107-
if mw.memory.Len() < int(addr)+32 {
116+
if mw.memory.Len() < int(addr)+32 || addr < 0 {
108117
// TODO(karalabe): We can't js-throw from Go inside duktape inside Go. The Go
109118
// runtime goes belly up https://github.com/golang/go/issues/15639.
110119
log.Warn("Tracer accessed out of bound memory", "available", mw.memory.Len(), "offset", addr, "size", 32)
@@ -147,7 +156,7 @@ type stackWrapper struct {
147156

148157
// peek returns the nth-from-the-top element of the stack.
149158
func (sw *stackWrapper) peek(idx int) *big.Int {
150-
if len(sw.stack.Data()) <= idx {
159+
if len(sw.stack.Data()) <= idx || idx < 0 {
151160
// TODO(karalabe): We can't js-throw from Go inside duktape inside Go. The Go
152161
// runtime goes belly up https://github.com/golang/go/issues/15639.
153162
log.Warn("Tracer accessed out of bound stack", "size", len(sw.stack.Data()), "index", idx)

‎eth/tracers/tracer_test.go‎

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,39 @@ func runTrace(tracer *Tracer) (json.RawMessage, error) {
6363
return tracer.GetResult()
6464
}
6565

66+
// TestRegressionPanicSlice tests that we don't panic on bad arguments to memory access
67+
func TestRegressionPanicSlice(t *testing.T) {
68+
tracer, err := New("{depths: [], step: function(log) { this.depths.push(log.memory.slice(-1,-2)); }, fault: function() {}, result: function() { return this.depths; }}")
69+
if err != nil {
70+
t.Fatal(err)
71+
}
72+
if _, err = runTrace(tracer); err != nil {
73+
t.Fatal(err)
74+
}
75+
}
76+
77+
// TestRegressionPanicSlice tests that we don't panic on bad arguments to stack peeks
78+
func TestRegressionPanicPeek(t *testing.T) {
79+
tracer, err := New("{depths: [], step: function(log) { this.depths.push(log.stack.peek(-1)); }, fault: function() {}, result: function() { return this.depths; }}")
80+
if err != nil {
81+
t.Fatal(err)
82+
}
83+
if _, err = runTrace(tracer); err != nil {
84+
t.Fatal(err)
85+
}
86+
}
87+
88+
// TestRegressionPanicSlice tests that we don't panic on bad arguments to memory getUint
89+
func TestRegressionPanicGetUint(t *testing.T) {
90+
tracer, err := New("{ depths: [], step: function(log, db) { this.depths.push(log.memory.getUint(-64));}, fault: function() {}, result: function() { return this.depths; }}")
91+
if err != nil {
92+
t.Fatal(err)
93+
}
94+
if _, err = runTrace(tracer); err != nil {
95+
t.Fatal(err)
96+
}
97+
}
98+
6699
func TestTracing(t *testing.T) {
67100
tracer, err := New("{count: 0, step: function() { this.count += 1; }, fault: function() {}, result: function() { return this.count; }}")
68101
if err != nil {

0 commit comments

Comments
 (0)