Skip to content

Commit e4a4161

Browse files
committed
runtime: non-strict InlTreeIndex lookup in Frames.Next
When using cgo, some of the frames can be provided by cgoTraceback, a cgo-provided function to generate C tracebacks. Unlike Go tracebacks, cgoTraceback has no particular guarantees that it produces valid tracebacks. If one of the (invalid) frames happens to put the PC in the alignment region at the end of a function (filled with int 3's on amd64), then Frames.Next will find a valid funcInfo for the PC, but pcdatavalue will panic because PCDATA doesn't cover this PC. Tolerate this case by doing a non-strict PCDATA lookup. We'll still show a bogus frame, but at least avoid throwing. Fixes #44971 Change-Id: I9eed728470d6f264179a7615bd19845c941db78c Reviewed-on: https://go-review.googlesource.com/c/go/+/301369 Trust: Michael Pratt <[email protected]> Run-TryBot: Michael Pratt <[email protected]> TryBot-Result: Go Bot <[email protected]> Reviewed-by: Cherry Zhang <[email protected]>
1 parent a81b5e4 commit e4a4161

File tree

2 files changed

+88
-1
lines changed

2 files changed

+88
-1
lines changed

src/runtime/symtab.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,9 @@ func (ci *Frames) Next() (frame Frame, more bool) {
102102
name := funcname(funcInfo)
103103
if inldata := funcdata(funcInfo, _FUNCDATA_InlTree); inldata != nil {
104104
inltree := (*[1 << 20]inlinedCall)(inldata)
105-
ix := pcdatavalue(funcInfo, _PCDATA_InlTreeIndex, pc, nil)
105+
// Non-strict as cgoTraceback may have added bogus PCs
106+
// with a valid funcInfo but invalid PCDATA.
107+
ix := pcdatavalue1(funcInfo, _PCDATA_InlTreeIndex, pc, nil, false)
106108
if ix >= 0 {
107109
// Note: entry is not modified. It always refers to a real frame, not an inlined one.
108110
f = nil

src/runtime/symtab_test.go

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"runtime"
99
"strings"
1010
"testing"
11+
"unsafe"
1112
)
1213

1314
func TestCaller(t *testing.T) {
@@ -165,3 +166,87 @@ func TestNilName(t *testing.T) {
165166
t.Errorf("Name() = %q, want %q", got, "")
166167
}
167168
}
169+
170+
var dummy int
171+
172+
func inlined() {
173+
// Side effect to prevent elimination of this entire function.
174+
dummy = 42
175+
}
176+
177+
// A function with an InlTree. Returns a PC within the function body.
178+
//
179+
// No inline to ensure this complete function appears in output.
180+
//
181+
//go:noinline
182+
func tracebackFunc(t *testing.T) uintptr {
183+
// This body must be more complex than a single call to inlined to get
184+
// an inline tree.
185+
inlined()
186+
inlined()
187+
188+
// Acquire a PC in this function.
189+
pc, _, _, ok := runtime.Caller(0)
190+
if !ok {
191+
t.Fatalf("Caller(0) got ok false, want true")
192+
}
193+
194+
return pc
195+
}
196+
197+
// Test that CallersFrames handles PCs in the alignment region between
198+
// functions (int 3 on amd64) without crashing.
199+
//
200+
// Go will never generate a stack trace containing such an address, as it is
201+
// not a valid call site. However, the cgo traceback function passed to
202+
// runtime.SetCgoTraceback may not be completely accurate and may incorrect
203+
// provide PCs in Go code or the alignement region between functions.
204+
//
205+
// Go obviously doesn't easily expose the problematic PCs to running programs,
206+
// so this test is a bit fragile. Some details:
207+
//
208+
// * tracebackFunc is our target function. We want to get a PC in the
209+
// alignment region following this function. This function also has other
210+
// functions inlined into it to ensure it has an InlTree (this was the source
211+
// of the bug in issue 44971).
212+
//
213+
// * We acquire a PC in tracebackFunc, walking forwards until FuncForPC says
214+
// we're in a new function. The last PC of the function according to FuncForPC
215+
// should be in the alignment region (assuming the function isn't already
216+
// perfectly aligned).
217+
//
218+
// This is a regression test for issue 44971.
219+
func TestFunctionAlignmentTraceback(t *testing.T) {
220+
pc := tracebackFunc(t)
221+
222+
// Double-check we got the right PC.
223+
f := runtime.FuncForPC(pc)
224+
if !strings.HasSuffix(f.Name(), "tracebackFunc") {
225+
t.Fatalf("Caller(0) = %+v, want tracebackFunc", f)
226+
}
227+
228+
// Iterate forward until we find a different function. Back up one
229+
// instruction is (hopefully) an alignment instruction.
230+
for runtime.FuncForPC(pc) == f {
231+
pc++
232+
}
233+
pc--
234+
235+
// Is this an alignment region filler instruction? We only check this
236+
// on amd64 for simplicity. If this function has no filler, then we may
237+
// get a false negative, but will never get a false positive.
238+
if runtime.GOARCH == "amd64" {
239+
code := *(*uint8)(unsafe.Pointer(pc))
240+
if code != 0xcc { // INT $3
241+
t.Errorf("PC %v code got %#x want 0xcc", pc, code)
242+
}
243+
}
244+
245+
// Finally ensure that Frames.Next doesn't crash when processing this
246+
// PC.
247+
frames := runtime.CallersFrames([]uintptr{pc})
248+
frame, _ := frames.Next()
249+
if frame.Func != f {
250+
t.Errorf("frames.Next() got %+v want %+v", frame.Func, f)
251+
}
252+
}

0 commit comments

Comments
 (0)