Skip to content

Commit ee9ef1f

Browse files
committed
eth/tracers: fix panics occurring for invalid params in js-tracers
1 parent b09b9f5 commit ee9ef1f

File tree

2 files changed

+17
-15
lines changed

2 files changed

+17
-15
lines changed

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: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -63,44 +63,37 @@ 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
6667
func TestRegressionPanicSlice(t *testing.T) {
6768
tracer, err := New("{depths: [], step: function(log) { this.depths.push(log.memory.slice(-1,-2)); }, fault: function() {}, result: function() { return this.depths; }}")
6869
if err != nil {
6970
t.Fatal(err)
7071
}
71-
72-
ret, err := runTrace(tracer)
73-
if err != nil {
72+
if _, err = runTrace(tracer); err != nil {
7473
t.Fatal(err)
7574
}
76-
t.Logf("Got result %s", string(ret))
7775
}
7876

77+
// TestRegressionPanicSlice tests that we don't panic on bad arguments to stack peeks
7978
func TestRegressionPanicPeek(t *testing.T) {
8079
tracer, err := New("{depths: [], step: function(log) { this.depths.push(log.stack.peek(-1)); }, fault: function() {}, result: function() { return this.depths; }}")
8180
if err != nil {
8281
t.Fatal(err)
8382
}
84-
85-
ret, err := runTrace(tracer)
86-
if err != nil {
83+
if _, err = runTrace(tracer); err != nil {
8784
t.Fatal(err)
8885
}
89-
t.Logf("Expected return value to be [0,1,2], got %s", string(ret))
9086
}
9187

92-
88+
// TestRegressionPanicSlice tests that we don't panic on bad arguments to memory getUint
9389
func TestRegressionPanicGetUint(t *testing.T) {
9490
tracer, err := New("{ depths: [], step: function(log, db) { this.depths.push(log.memory.getUint(-64));}, fault: function() {}, result: function() { return this.depths; }}")
9591
if err != nil {
9692
t.Fatal(err)
9793
}
98-
99-
ret, err := runTrace(tracer)
100-
if err != nil {
94+
if _, err = runTrace(tracer); err != nil {
10195
t.Fatal(err)
10296
}
103-
t.Logf("Expected return value to be [0,1,2], got %s", string(ret))
10497
}
10598

10699
func TestTracing(t *testing.T) {

0 commit comments

Comments
 (0)