Commit b5a438d2a4 for golang
commit b5a438d2a4c49f3c206c426663644be7068673cb
Author: Thomas Mangin <thomas@mangin.com>
Date: Tue Dec 1 16:41:01 2026 +0000
runtime: don't hexdump a frame that is not on the stack
tracebackHexdump clamps the window it dumps to the bounds of the stack
it is given. When the frame's SP is far from that stack, only one end
is clamped and the range is inverted. Since CL 681375, hexdumpWords
takes a length instead of an end address, so hi-lo wraps around and
unsafe.Slice throws "len out of range". Before that CL, hexdumpWords
looped while p+i < end and printed only an empty line.
The frame is not on the stack when a signal arrives while a goroutine
is in racecall, msancall, or asancall, which switch SP to the system
stack without switching g. A SIGQUIT traceback of such a goroutine
then dies with "fatal error: unsafe.Slice: len out of range" and
"panic during panic" instead of printing the goroutine dump. The
second traceback usually faults in turn: the process is killed by
SIGSEGV on linux and spins on darwin.
Return after printing the stack bounds when there is nothing to dump.
Fixes #83101
Change-Id: Iac33450c5a0c7414ea56cf444683a68b43e4efaa
GitHub-Last-Rev: df7874522d6b1ff3876cb34e580af080831a2532
GitHub-Pull-Request: golang/go#83102
Reviewed-on: https://go-review.googlesource.com/c/go/+/863961
Reviewed-by: Keith Randall <khr@google.com>
Reviewed-by: Dmitri Shuralyov <dmitshur@google.com>
Auto-Submit: Keith Randall <khr@golang.org>
LUCI-TryBot-Result: golang-scoped@luci-project-accounts.iam.gserviceaccount.com <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: Keith Randall <khr@golang.org>
diff --git a/src/runtime/export_test.go b/src/runtime/export_test.go
index 59f7f888de..b85c2fec42 100644
--- a/src/runtime/export_test.go
+++ b/src/runtime/export_test.go
@@ -2210,3 +2210,22 @@ func FuncNamePiecesForPrint(name string) (string, string, string, string, string
var InHeapOrStack = inHeapOrStack
func SysRSS() (rss, shared uint64, hasShared bool) { return sysRSS() }
+
+// TracebackHexdump calls tracebackHexdump on the calling goroutine's stack.
+// The frame's SP is on that stack if where is 0, below the stack if where
+// is negative, and above it if where is positive.
+func TracebackHexdump(where int) {
+ gp := getg()
+ sp := sys.GetCallerSP()
+ // Run on the system stack, as the callers of tracebackHexdump do,
+ // so that gp's stack cannot move while it is being dumped.
+ systemstack(func() {
+ frame := stkframe{sp: sp}
+ if where < 0 {
+ frame.sp = gp.stack.lo - 1<<20
+ } else if where > 0 {
+ frame.sp = gp.stack.hi + 1<<20
+ }
+ tracebackHexdump(gp.stack, &frame, 0)
+ })
+}
diff --git a/src/runtime/traceback.go b/src/runtime/traceback.go
index 5ad35c0b2e..f8108def9c 100644
--- a/src/runtime/traceback.go
+++ b/src/runtime/traceback.go
@@ -1493,6 +1493,13 @@ func tracebackHexdump(stk stack, frame *stkframe, bad uintptr) {
// Print the hex dump.
print("stack: frame={sp:", hex(frame.sp), ", fp:", hex(frame.fp), "} stack=[", hex(stk.lo), ",", hex(stk.hi), ")\n")
+ if hi <= lo {
+ // The frame is not on stk, so there is nothing to dump.
+ // This happens, for example, when a signal arrives while
+ // the goroutine is running on the system stack without
+ // having switched g, as in racecall, msancall, and asancall.
+ return
+ }
hexdumpWords(lo, hi-lo, func(p uintptr, m hexdumpMarker) {
if p == frame.fp {
m.start()
diff --git a/src/runtime/traceback_test.go b/src/runtime/traceback_test.go
index 9480cec9c1..9d1f4b6117 100644
--- a/src/runtime/traceback_test.go
+++ b/src/runtime/traceback_test.go
@@ -13,6 +13,7 @@ import (
"internal/msan"
"internal/race"
"internal/testenv"
+ "os"
"regexp"
"runtime"
"runtime/debug"
@@ -1018,3 +1019,40 @@ func TestTracebackGoroutineLabelsDisabledGODEBUG(t *testing.T) {
verifyLabels()
wg.Wait()
}
+
+// Test that tracebackHexdump does not throw when the frame's SP is not on
+// the stack it is given. That is the case when a signal arrives while a
+// goroutine is running C code on the system stack without having switched
+// g, as racecall, msancall, and asancall do.
+func TestTracebackHexdumpOffStack(t *testing.T) {
+ const env = "TEST_TRACEBACK_HEXDUMP"
+ switch os.Getenv(env) {
+ case "on":
+ runtime.TracebackHexdump(0)
+ return
+ case "below":
+ runtime.TracebackHexdump(-1)
+ return
+ case "above":
+ runtime.TracebackHexdump(1)
+ return
+ }
+
+ for _, where := range []string{"on", "below", "above"} {
+ cmd := testenv.CleanCmdEnv(testenv.Command(t, testenv.Executable(t), "-test.run=^TestTracebackHexdumpOffStack$"))
+ cmd.Env = append(cmd.Env, env+"="+where)
+ out, err := cmd.CombinedOutput()
+ if err != nil {
+ t.Errorf("SP %s the stack: %v\n%s", where, err, out)
+ continue
+ }
+ if want := "stack: frame={sp:"; !strings.Contains(string(out), want) {
+ t.Errorf("SP %s the stack: output does not contain %q:\n%s", where, want, out)
+ }
+ // The hexdump marks the word at SP.
+ const mark = "^ SP\n"
+ if got, want := strings.Contains(string(out), mark), where == "on"; got != want {
+ t.Errorf("SP %s the stack: hexdump printed = %v, want %v:\n%s", where, got, want, out)
+ }
+ }
+}