Repository navigation
Description
Disclosure: this issue was investigated and written by a coding agent (Claude Code) and reviewed by
me before filing. Every command output below comes from real local runs.
Go version
go version go1.26.8 linux/amd64
It also reproduces on go1.24.0, go1.24.13, go1.25.7, go1.26.1, go1.27.0 and on tip
(go1.28-devel_0b6b8381b6, 2026-10-05). It does not reproduce on go1.23.12.
Output of go env in your module/workspace:
AR='ar'
CC='gcc'
CGO_CFLAGS='-O2 -g'
CGO_CPPFLAGS=''
CGO_CXXFLAGS='-O2 -g'
CGO_ENABLED='1'
CGO_FFLAGS='-O2 -g'
CGO_LDFLAGS='-O2 -g'
CXX='g++'
GCCGO='gccgo'
GO111MODULE=''
GOAMD64='v1'
GOARCH='amd64'
GOAUTH='netrc'
GOBIN=''
GOCACHE='/home/lestrrat/.cache/go-build'
GOCACHEPROG=''
GODEBUG=''
GOENV='/home/lestrrat/.config/go/env'
GOEXE=''
GOEXPERIMENT=''
GOFIPS140='off'
GOFLAGS=''
GOGCCFLAGS='-fPIC -m64 -pthread -Wl,--no-gc-sections -fmessage-length=0 -ffile-prefix-map=/tmp/go-build1697543423=/tmp/go-build -gno-record-gcc-switches'
GOHOSTARCH='amd64'
GOHOSTOS='linux'
GOINSECURE=''
GOMOD='/home/lestrrat/repro-cse/go.mod'
GOMODCACHE='/home/lestrrat/go/pkg/mod'
GONOPROXY=''
GONOSUMDB=''
GOOS='linux'
GOPATH='/home/lestrrat/go'
GOPRIVATE=''
GOPROXY='https://proxy.golang.org,direct'
GOROOT='/home/lestrrat/.local/go/1.26.8'
GOSUMDB='sum.golang.org'
GOTELEMETRY='local'
GOTELEMETRYDIR='/home/lestrrat/.config/go/telemetry'
GOTMPDIR=''
GOTOOLCHAIN='auto'
GOTOOLDIR='/home/lestrrat/.local/go/1.26.8/pkg/tool/linux_amd64'
GOVCS=''
GOVERSION='go1.26.8'
GOWORK=''
PKG_CONFIG='pkg-config'
What did you do?
The program below has no unsafe, no cgo, no finalizers and no goroutines. Function f copies
its two arguments into a local [2]T array, reads arr[m].p with a variable index, calls a
function that runs the GC, then reads arr[m] again. The *payload values are reachable only
through arr while churn runs.
package main
import (
"fmt"
"os"
"runtime"
)
type payload struct{ v [64]int }
type T struct {
p *payload
pad [8]int // keeps T out of SSA registers
flag *int
}
var sink []*payload
//go:noinline
func churn() {
runtime.GC()
for i := 0; i < 64; i++ {
sink = append(sink[:0], &payload{})
}
runtime.GC()
}
//go:noinline
func newPayload() *payload {
p := &payload{}
p.v[7] = 42
return p
}
//go:noinline
func use(t T) int { return t.p.v[7] }
//go:noinline
func f(a, b T, pick int) int {
arr := [2]T{a, b}
m := pick & 1
if arr[m].p == nil { // &arr[m] computed here
return -1
}
churn() // GC runs; arr is reported dead
return use(arr[m]) // reads arr[m] through the earlier &arr[m]
}
func main() {
bad := 0
for i := 0; i < 2000; i++ {
a := T{p: newPayload()}
b := T{p: newPayload()}
if got := f(a, b, i); got != 42 {
bad++
}
}
fmt.Println("bad =", bad)
if bad != 0 {
os.Exit(1)
}
}$ go build -o repro . && ./repro
$ go build -gcflags=-N -o repro-N . && ./repro-N
$ GODEBUG=clobberfree=1 ./repro
What did you see happen?
f reads freed memory. The freed payload is reused by churn's allocations, so use sees
v[7] == 0. The failure is deterministic, with no GODEBUG settings needed:
$ ./repro
bad = 2000
$ ./repro-N
bad = 0
| Toolchain | default build | -gcflags=-N |
|---|---|---|
| go1.23.12 | bad = 0 |
bad = 0 |
| go1.24.0 | bad = 1999 |
bad = 0 |
| go1.24.13 | bad = 1999 |
bad = 0 |
| go1.25.7 | bad = 2000 |
bad = 0 |
| go1.26.1 | bad = 2000 |
bad = 0 |
| go1.26.8 | bad = 2000 |
bad = 0 |
| go1.27.0 | bad = 2000 |
bad = 0 |
tip (go1.28-devel_0b6b8381b6) |
bad = 2000 |
bad = 0 |
-race and -gcflags=all=-d=checkptr builds also print bad = 2000, with no race report and
no checkptr failure. In a larger program, the same pattern showed up as
fatal error: found pointer to free object from runtime.(*mspan).reportZombies, and as
0xdeadbeefdeadbeef words under GODEBUG=clobberfree=1.
Liveness
arr is dead at the call to churn on go1.24+. Only a spill slot holding &arr[m] is live.
Line numbers refer to the program above (f is at line 39, the call to churn at line 45):
$ go build -gcflags=-live . # go1.26.8
./main.go:39:6: live at entry to f: a b
./main.go:45:7: live at call to churn: .autotmp_8
$ go build -gcflags=-live . # tip, go1.28-devel_0b6b8381b6
./main.go:39:6: live at entry to f: a b
./main.go:45:7: live at call to churn: .autotmp_8
$ go build -gcflags=-live . # go1.23.12
./main.go:39:6: live at entry to f: a b
./main.go:45:7: live at call to churn: arr
arr is not address-taken, so it is not a stack object. The GC does not follow .autotmp_8 into
it either, so nothing scans arr[*].p during churn.
SSA (GOSSAFUNC='f+', go1.26.8)
Before generic cse, the read at line 46 has its own LocalAddr, which takes the memory state
after the call (v37 is the memory result of churn):
(40) v11 = LocalAddr <*[2]T> {arr} v2 v10
(42) v22 = LocalAddr <*[2]T> {arr} v2 v20
(+45) v36 = StaticLECall <mem> {AuxCall{main.churn}} v20
(45) v37 = SelectN <mem> [0] v36
(46) v38 = LocalAddr <*[2]T> {arr} v2 v37
(42) v26 = AddPtr <*T> v22 v33
(46) v43 = AddPtr <*T> v38 v7
(46) v47 = Move <mem> {T} [80] v46 v43 v45
After generic cse, every LocalAddr {arr} is merged into v11 (line 40). The Move at
line 46 reads through v26, the address computed at line 42 before the call. The LocalAddr
taken after the call is dead:
(40) v11 = LocalAddr <*[2]T> {arr} v2 v10
(42) v22 = LocalAddr <*[2]T> {arr} v2 v20 DEAD
(46) v38 = LocalAddr <*[2]T> {arr} v2 v37 DEAD
(42) v26 = AddPtr <*T> v11 v33
(46) v43 = AddPtr <*T> v11 v33 DEAD
(46) v47 = Move <mem> {T} [80] v46 v26 v45
In the generated code, &arr[m] becomes LEAQ main.arr+off(SP)(CX*1), CX. That value cannot be
rematerialized, so regalloc spills it to .autotmp_8 across the CALL main.churn.
Cause
CSE skips the memory argument of OpLocalAddr when it partitions values
(if v.Op == OpLocalAddr && k == 1 { continue }). The check is in
src/cmd/compile/internal/ssa/cse.go in go1.24 through go1.27, and in
src/cmd/compile/internal/ssacompile/cse.go (around line 150) on master. So CSE merges
LocalAddrs taken at different memory states, including across calls. liveness.valueEffects counts a SymAddr
op as a use of the variable ("Addr is a read also"). After CSE, the only remaining use of arr is
the LocalAddr before the call, so arr is dead across the call. The derived pointer is still
dereferenced afterwards.
That special case is absent in go1.23.12 and present from go1.24.0 on. It was possibly
introduced by c4e6ab9 ("cmd/compile: modify CSE to remove redundant OpLocalAddrs", CL 628075,
for #70300), the only change to cse.go between those releases that touches it. I found this by
reading the source, not by bisecting. The check is unchanged on master and on
release-branch.go1.24 through release-branch.go1.27.
Possibly related: #70300, https://go.dev/cl/628075.
What did you expect to see?
bad = 0. arr should stay live, or be scanned, for as long as an address derived from it is
dereferenced. Equivalently, CSE should not merge OpLocalAddr values across a memory state that
contains a call (a safepoint) when the variable has pointers and is not address-taken.
Workarounds
All three of these remove the failure. The first two were confirmed with this program.
- Build with
-gcflags=-N. - Make the array address-taken so it becomes a stack object, for example by adding
runtime.KeepAlive(&arr)after the last use. - Avoid a variable-indexed local array of pointer-containing structs. Use named locals and pick
between them with a branch.
Metadata
Metadata
Assignees
Labels
Type
Projects
- StatusShow more project fieldsIn Progress
Activity
dmitshur commentedon Oct 5, 2026
CC @golang/compiler.
gopherbot commentedon Oct 6, 2026
Change https://go.dev/cl/845625 mentions this issue:
cmd/compile: do not CSE OpLocalAddr across memory states for pointer-containing typesthepudds commentedon Oct 7, 2026
FWIW, a bisect seemed to land on c4e6ab9:
CC @fengyoulin
gopherbot commentedon Oct 7, 2026
Change https://go.dev/cl/846325 mentions this issue:
cmd/compile: retain OpLocalAddrs for pointer-containing types in CSEfengyoulin commentedon Oct 7, 2026
The bug reproduces with the following code in Go 1.23.12 and 1.22.12.
fengyoulin commentedon Oct 7, 2026
So I would prefer to keep the variable alive rather than retain
OpLocalAddrs.fengyoulin commentedon Oct 8, 2026
I think the root cause of this problem is that we index into an array using a non‑constant index, and the array’s element type contains pointer types that may point to heap‑allocated objects. This non‑constant index later gets lowered to a dynamically computed offset, which is also non‑constant. As a result, liveness analysis cannot locate the original variable based on this non‑constant offset.
This code uses constant indices and has no such problem.
So in my CL, I am trying to keep the original variable alive when indexing into the array with a non‑constant index.
randall77 commentedon Oct 22, 2026
I agree there is a bug here, but I don't think I agree with the described cause.
I think the problem is that
arris not treated as a stack object. At the call tochurn, the address ofarris spilled and later restored. The stack slot it spills to is correctly marked as containing a pointer. So that pointer should keep all ofarrlive across the call.It is ok that
arritself is marked dead at the call. That is intentional to how stack objects work. Only direct accesses are used to compute the liveness maps. Indirect accesses (through a derived pointer, for example) should use the stack object mechanism to keeparrlive.But since
arris not marked as a stack object, the stack scanner does not realize it should track and scan it if there is a live pointer to it.Using dumpgc, no stack objects are listed for
f. There should be one.randall77 commentedon Oct 22, 2026
This patch does fix the immediate problem, but I'm not sure it is a complete solution. Probably we need to handle PPARAM/PPARAMOUT cases as well.