Commit aa4949ab3d for golang
commit aa4949ab3d66d4279e5a1deec584ceba87c17023
Author: David Chase <drchase@google.com>
Date: Sat Nov 28 12:03:10 2026 -0500
runtime: check wg.elem bounds before adding elemsize in findwghi
findwghi previously computed p := wg.elem + elemsize and checked
stk.lo <= p && p < stk.hi. If wg.elem points to a heap object
immediately below stk.lo such that wg.elem + elemsize == stk.lo,
the check passes and findwghi returns stk.lo even though the channel
element is not on the stack.
During stack shrinking, syncAdjustWaitingGChan then computes
wgsize = adjinfo.wghi - oldBot with oldBot > stk.lo, underflowing
wgsize and corrupting the stack copy in copystack.
Check stk.lo <= elem && elem < stk.hi on wg.elem itself before adding
elemsize.
The added test skips on Darwin because it tickles a runtime static
lock order bug.
Updates #65587
See also CL 612416
Fixes #83465
Cq-Include-Trybots: luci.golang.try:gotip-darwin-arm64-longtest,gotip-linux-amd64-longtest,gotip-linux-amd64-longtest-race
Change-Id: Ia427e84230880e98eba1ecbf8c692f8a90206f8e
Reviewed-on: https://go-review.googlesource.com/c/go/+/868121
LUCI-TryBot-Result: golang-scoped@luci-project-accounts.iam.gserviceaccount.com <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: Keith Randall <khr@google.com>
Reviewed-by: Keith Randall <khr@golang.org>
diff --git a/src/runtime/stack.go b/src/runtime/stack.go
index 2d5b87ce2e..164bcc6fb0 100644
--- a/src/runtime/stack.go
+++ b/src/runtime/stack.go
@@ -884,9 +884,11 @@ func fillstack(stk stack, b byte) {
func findwghi(gp *g, stk stack) uintptr {
var wghi uintptr
for wg := gp.waiting; wg != nil; wg = wg.waitlink {
- p := wg.elem.uintptr() + uintptr(wg.c.get().elemsize)
- if stk.lo <= p && p < stk.hi && p > wghi {
- wghi = p
+ elem := wg.elem.uintptr()
+ if stk.lo <= elem && elem < stk.hi {
+ if p := elem + uintptr(wg.c.get().elemsize); p > wghi {
+ wghi = p
+ }
}
}
return wghi
diff --git a/test/fixedbugs/issue83465.go b/test/fixedbugs/issue83465.go
new file mode 100644
index 0000000000..f810a9ca03
--- /dev/null
+++ b/test/fixedbugs/issue83465.go
@@ -0,0 +1,150 @@
+// run
+
+// Copyright 2026 The Go Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+package main
+
+import (
+ "fmt"
+ "runtime"
+ "strconv"
+ "strings"
+)
+type span32K [4096]uint64 // 32 KiB (4 runtime pages, noscan)
+var (
+ wallSink []*span32K
+ holeSink []*span32K
+)
+//go:noinline
+func wordSink(x uint64) {}
+
+// addrOf safely obtains an address.
+func addrOf(p *span32K) uint64 {
+ s := strings.TrimPrefix(fmt.Sprintf("%p", p), "0x")
+ v, err := strconv.ParseUint(s, 16, 64)
+ if err != nil {
+ panic(err)
+ }
+ return v
+}
+
+// captureStackAddr keeps p live across runtime.Stack without escaping it,
+// so runtime.Stack formats p's actual stack address in the argument list.
+//
+//go:noinline
+func captureStackAddr(p *uint64, buf []byte) int {
+ n := runtime.Stack(buf, false)
+ *p++
+ return n
+}
+
+func currentStackAddr() uint64 {
+ var anchor uint64
+ buf := make([]byte, 4096)
+ n := captureStackAddr(&anchor, buf)
+ wordSink(anchor)
+ s := string(buf[:n])
+ const marker = "captureStackAddr(0x"
+ i := strings.Index(s, marker)
+ if i < 0 {
+ panic("could not find stack address in:\n" + s)
+ }
+ s = s[i+len(marker):]
+ end := strings.IndexAny(s, ",?)")
+ v, err := strconv.ParseUint(s[:end], 16, 64)
+ if err != nil {
+ panic(err)
+ }
+ return v
+}
+func growStackFrames(frames int, hold <-chan struct{}, ready chan<- struct{}) uint64 {
+ var frame [128]uint64
+ frame[0] = 0x12345678
+ if frames > 1 {
+ return frame[0] ^ growStackFrames(frames-1, hold, ready)
+ }
+ ready <- struct{}{}
+ <-hold
+ return frame[0]
+}
+func main() {
+ prev := runtime.GOMAXPROCS(1)
+ defer func() {
+ wallSink = nil
+ holeSink = nil
+ runtime.GOMAXPROCS(prev)
+ }()
+ const targetRun = 896
+ wallSink = make([]*span32K, 0, 4096)
+ var prevAddr uint64
+ contigRun := 0
+ for {
+ blk := new(span32K)
+ wallSink = append(wallSink, blk)
+ addr := addrOf(blk)
+ const arenaMask = (64 << 20) - 1
+ if addr == prevAddr+32768 {
+ contigRun++
+ } else {
+ contigRun = 1
+ }
+ prevAddr = addr
+ if len(wallSink) >= 1536 && contigRun >= targetRun && (addr&arenaMask)+(24<<20) <= (64<<20) {
+ break
+ }
+ }
+ holeHold := make(chan struct{})
+ holeReady := make(chan struct{}, 1)
+ go func() { wordSink(growStackFrames(40, holeHold, holeReady)) }()
+ <-holeReady
+ var best *span32K
+ trigHold := make(chan struct{})
+ trigReady := make(chan struct{}, 1)
+ trigSPCh := make(chan uint64, 1)
+ casesReady := make(chan struct{})
+ trigParked := make(chan struct{}, 1)
+ go func() {
+ wordSink(growStackFrames(40, trigHold, trigReady))
+ trigSPCh <- currentStackAddr()
+ <-casesReady
+ trigParked <- struct{}{}
+ make(chan span32K) <- *best
+ }()
+ <-trigReady
+ close(holeHold)
+ for range 20 {
+ runtime.Gosched()
+ }
+ runtime.GC()
+ runtime.GC()
+ // Still allocate all 32 blocks so the hole is filled up to stk.lo.
+ const holeSpans = 32
+ holeSink = make([]*span32K, holeSpans)
+ for i := range holeSpans {
+ holeSink[i] = new(span32K)
+ }
+ close(trigHold)
+ trigSP := <-trigSPCh
+ // Pick the single block with the highest address strictly below trig's stack.
+ var bestAddr uint64
+ for _, blk := range holeSink {
+ a := addrOf(blk)
+ if a < trigSP && a > bestAddr {
+ bestAddr = a
+ best = blk
+ }
+ }
+ if best == nil {
+ panic("no hole block found below trig stack")
+ }
+ close(casesReady)
+ <-trigParked
+ for range 40 {
+ runtime.Gosched()
+ }
+ runtime.GC()
+ runtime.GC()
+}
+