Commit 946bb12a54 for golang
commit 946bb12a54ab27499a4c21a733ab16638af92263
Author: David Chase <drchase@google.com>
Date: Sat Nov 28 12:48:02 2026 -0500
cmd/compile: don't stack-allocate named return slices in functions with defer
The slice stack-allocation pass tracks named return slices and inserts
move-to-heap transitions at ORETURN statements. If the function has a
defer statement, a deferred recover can return the named result
variables without executing an ORETURN, leaving the returned slice
pointing at a dead stack frame.
Stop tracking named return slices when an ODEFER statement is present.
Fixes #83466
Cq-Include-Trybots: luci.golang.try:gotip-darwin-arm64-longtest,gotip-linux-amd64-longtest,gotip-linux-amd64-longtest-race
Change-Id: I382b75b614a82131ebd310cc29bf95f7b7dddee1
Reviewed-on: https://go-review.googlesource.com/c/go/+/865629
Reviewed-by: Keith Randall <khr@google.com>
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/cmd/compile/internal/slice/slice.go b/src/cmd/compile/internal/slice/slice.go
index b689fe124f..416f0cb8de 100644
--- a/src/cmd/compile/internal/slice/slice.go
+++ b/src/cmd/compile/internal/slice/slice.go
@@ -365,6 +365,12 @@ func analyze(fn *ir.Func) {
}
break
}
+ case ir.ODEFER:
+ // A deferred recover can return named results without
+ // executing an explicit ORETURN's move2heap copy.
+ for _, x := range namedRets {
+ x.Opt = nil
+ }
case ir.ORETURN:
n := n.(*ir.ReturnStmt)
for _, x := range n.Results {
diff --git a/test/fixedbugs/issue83466.go b/test/fixedbugs/issue83466.go
new file mode 100644
index 0000000000..5eed95b554
--- /dev/null
+++ b/test/fixedbugs/issue83466.go
@@ -0,0 +1,68 @@
+// 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"
+ "os"
+)
+
+const (
+ goodVal = 0x12345678
+ badVal = 0xdeadbeef
+)
+
+var fence int
+
+func tick() int {
+ fence++
+ return fence
+}
+
+//go:noinline
+func makeSlice(doPanic bool) (s []uintptr) {
+ defer func() {
+ recover()
+ }()
+ var sink int
+ for i := range 4 {
+ s, sink = append(s, uintptr(goodVal+i)), tick()
+ }
+ _ = sink
+ if doPanic {
+ panic("unwind")
+ }
+ return
+}
+
+//go:noinline
+func clobberStack(s []uintptr) uintptr {
+ defer func() { recover() }()
+ var buf [64]uintptr
+ for i := range buf {
+ buf[i] = badVal
+ }
+ if buf[0] == 0 {
+ return 0
+ }
+ return s[0]
+}
+
+func main() {
+ normal := makeSlice(false)
+ if got := clobberStack(normal); got != goodVal {
+ fmt.Printf("normal return: got %#x, want %#x\n", got, uint64(goodVal))
+ os.Exit(1)
+ }
+
+ unwound := makeSlice(true)
+ if got := clobberStack(unwound); got != goodVal {
+ fmt.Printf("panic/recover return: got %#x, want %#x (dangling stack slice)\n", got, uint64(goodVal))
+ os.Exit(1)
+ }
+}
+