Skip to content

Commit b7df8b1

Browse files
authored
debug: do not include inlined sources in frame counts (#2199)
Signed-off-by: Takeshi Yoneda <[email protected]>
1 parent 60dbe96 commit b7df8b1

File tree

2 files changed

+23
-6
lines changed

2 files changed

+23
-6
lines changed

internal/wasmdebug/debug.go

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,10 @@ func NewErrorBuilder() ErrorBuilder {
109109
}
110110

111111
type stackTrace struct {
112-
frames []string
112+
// frameCount is the number of stack frame currently pushed into lines.
113+
frameCount int
114+
// lines contains the stack trace and possibly the inlined source code information.
115+
lines []string
113116
}
114117

115118
// GoRuntimeErrorTracePrefix is the prefix coming before the Go runtime stack trace included in the face of runtime.Error.
@@ -125,7 +128,7 @@ func (s *stackTrace) FromRecovered(recovered interface{}) error {
125128
return exitErr
126129
}
127130

128-
stack := strings.Join(s.frames, "\n\t")
131+
stack := strings.Join(s.lines, "\n\t")
129132

130133
// If the error was internal, don't mention it was recovered.
131134
if wasmErr, ok := recovered.(*wasmruntime.Error); ok {
@@ -152,12 +155,16 @@ const MaxFrames = 30
152155

153156
// AddFrame implements ErrorBuilder.AddFrame
154157
func (s *stackTrace) AddFrame(funcName string, paramTypes, resultTypes []api.ValueType, sources []string) {
158+
if s.frameCount == MaxFrames {
159+
return
160+
}
161+
s.frameCount++
155162
sig := signature(funcName, paramTypes, resultTypes)
156-
s.frames = append(s.frames, sig)
163+
s.lines = append(s.lines, sig)
157164
for _, source := range sources {
158-
s.frames = append(s.frames, "\t"+source)
165+
s.lines = append(s.lines, "\t"+source)
159166
}
160-
if len(s.frames) == MaxFrames {
161-
s.frames = append(s.frames, "... maybe followed by omitted frames")
167+
if s.frameCount == MaxFrames {
168+
s.lines = append(s.lines, "... maybe followed by omitted frames")
162169
}
163170
}

internal/wasmdebug/debug_test.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,3 +157,13 @@ func (e testRuntimeErr) RuntimeError() {}
157157
func (e testRuntimeErr) Error() string {
158158
return string(e)
159159
}
160+
161+
func Test_AddFrame_MaxFrame(t *testing.T) {
162+
builder := NewErrorBuilder().(*stackTrace)
163+
for i := 0; i < MaxFrames+10; i++ {
164+
builder.AddFrame("x.y", nil, nil, []string{"a.go:1:2", "b.go:3:4"})
165+
}
166+
require.Equal(t, MaxFrames, builder.frameCount)
167+
require.Equal(t, MaxFrames*3 /* frame + two inlined sources */ +1, len(builder.lines))
168+
require.Equal(t, "... maybe followed by omitted frames", builder.lines[len(builder.lines)-1])
169+
}

0 commit comments

Comments
 (0)