Go’s new LICM bug only hits a narrow unsafe.Pointer pattern — but it can crash at stack growth
A Go compiler bug hoists guarded unsafe.Pointer arithmetic in 1.27.x, with a narrow crash-prone pattern and confirmed version boundaries.
A new Go compiler bug report shows a surprising failure mode: code that is logically guarded by if o != nil can still leave an invalid pointer live across a call, and that live pointer can crash the runtime when the goroutine stack grows.
The report, filed Nov. 5 as golang/go#82865, says the problem appears in go1.27.3 on windows/amd64, and was also reproduced on linux/amd64 and darwin/arm64 in CI with Go 1.27.x. The reporter says go1.26.5 and go1.24.13 are not affected because those releases do not have the loop-invariant code motion pass involved in the bug. The issue was later closed as completed after a fix landed in a Go change referencing the report.
The key point is that this is not a broad “Go is corrupting memory everywhere” bug. The failure mode is specific: cmd/compile’s LICM pass hoists unsafe.Pointer arithmetic out of a guard, then register allocation spills that value to a stack slot that remains live across a call. When that call grows the goroutine stack, Go’s stack copier and pointer adjuster see a bad live pointer and abort with runtime: bad pointer in frame main.sum ... fatal error: invalid pointer found on stack.
What the reproducer does
The issue includes a minimal program that computes an offset only when o != nil:
func sideOf(o *obj) *side {
return (*side)(unsafe.Add(unsafe.Pointer(o), unsafe.Sizeof(obj{})))
}
//go:noinline
func sum(o *obj, n int) int {
t := 0
for i := 0; i < n; i++ {
if o != nil {
t += first(sideOf(o))
}
t += call(i)
}
return t
}
The report says the program fails on every run with go1.27.3, but prints ok with go1.26.5.
The runtime crash is not from the arithmetic itself being impossible. The reporter’s argument is that the pointer is only computed on the o != nil path, and only points within the same allocation when o is valid. The problem is that LICM moves the address computation into the loop preheader, before the guard, so the compiler materializes the pointer even when o is nil. Once spilled, that slot stays live across the call inside the loop.
Why stack growth turns this into a crash
The report identifies the decisive trigger: call(i) grows the goroutine’s stack. That matters because Go adjusts live pointer slots when it copies stack frames. A live slot containing 0x20 — the offset produced from nil pointer arithmetic — becomes visible to the stack scanner, which then rejects it.
The same issue can also be exposed by GC stack shrinking, the reporter says, because that path also scans and adjusts live pointers.
This makes the bug more than a missed optimization barrier. It is a correctness bug that can become a hard runtime failure when the optimized code crosses a safepoint.
The version boundary is narrower than “all current Go”
The clearest version statement in the issue is this:
- affected:
go1.27.3 - not affected:
go1.26.5andgo1.24.13 - reason given: those older releases did not have the LICM pass in question
That matters because the bug is tied to compiler internals, not a general runtime change. The issue also says the problem reproduces across GOARCH=amd64 and GOARCH=arm64, and on multiple operating systems in CI, so it is not a single-platform codegen oddity.
The issue does not prove that every Go 1.27.x build is affected in the same way, but it does document a reproduced 1.27.3 failure and the absence of the bug on the older versions cited by the reporter. The follow-up fix was referenced in a Go change, and the issue was closed as completed on Nov. 6.
The unsafe.Pointer detail is the important one
The compiler pass itself is supposed to avoid moving pointer arithmetic out of guarded blocks. The issue quotes the LICM code comment and the check that currently says pointer arithmetic should not be moved if it may materialize a bad pointer across a safepoint.
But the reporter says the implementation checks v.Type.IsPtr(), which only catches plain TPTR. unsafe.Add produces a value typed as unsafe.Pointer (TUNSAFEPTR), so the pass treats it as non-pointer arithmetic and hoists it.
That distinction is the heart of the bug:
unsafe.Pointer-typed arithmetic can be hoisted*T-typed pointer arithmetic expressed as a field overlay is left alone- the lowered SSA form no longer has enough type signal to distinguish the dangerous case unless the compiler checks a broader pointer-shaped property
The issue’s suggested fix is to test v.Type.IsPtrShaped() or v.Type.HasPointers() instead of v.Type.IsPtr() so that unsafe.Pointer arithmetic is not moved out of the block that guards it.
Which code patterns are actually at risk
The issue is not about ordinary range loops or ordinary pointer math. The reproducer needs several things to line up:
- the value must be in an innermost loop and eligible for LICM
- the operation must be one of the early-movable forms, such as
ADDQ,ADDQconst, orLEAQ*on amd64, or the corresponding forms on arm64 - the type must not be
TPTR—unsafe.Pointerslips through - the inputs must be defined outside the loop
- the address must be used as a standalone value, not folded into a load/store addressing mode
- the loop must contain a call, so the hoisted value stays live across a safepoint and may be spilled
The reporter also says -d=checkptr=2 makes many more additions eligible because instrumentation introduces extra checks, and a normal build may not exhibit the same hoist in unrelated code. In the original program, the bug was found in a path where a normal build had no hoist, but a checkptr build had many.
So the risk is concentrated: low-level code that uses unsafe.Add or similar unsafe.Pointer arithmetic inside a guarded loop, with a call in the loop body, is the pattern to inspect.
A workaround exists, but it changes the expression shape
The report gives a practical workaround: write the offset as a field access on a struct overlay, for example
&(*T)(unsafe.Pointer(p)).f
That produces a *F-typed value, which the current pass does not move in the same way.
That is useful, but it is not a general recommendation to prefer one unsafe style over another. It is a workaround for this bug’s interaction between LICM, type classification, and stack adjustment. It also means the source shape of unsafe code can determine whether the compiler leaves the guard in place.
Why this matters beyond one crash report
The issue links to older compiler work on LICM and to earlier bug reports about the same pass. That history matters because it shows the compiler is already accumulating special-case rules to keep the optimizer from moving values that become unsafe when they cross calls or safepoints.
The broader lesson is that in Go, unsafe code is not only constrained by the language rules you write by hand. It is also constrained by how the compiler lowers and classifies values after SSA transformations. A pattern that looks safe at the source level can become unsafe if the optimizer repositions it across a call and the runtime later has to adjust the stack.
For teams that use unsafe in performance-sensitive code, the practical response is targeted testing: build with the exact Go version in production, run with -d=checkptr=2 when possible, and inspect any loop that computes unsafe.Pointer values conditionally before a call. The issue’s own reproducer shows that the failure can be deterministic once the compiler emits the wrong placement.
The narrow answer to the assignment question is this: the documented risk is not broad runtime corruption in all Go 1.27 code. It is a compiler miscompilation in Go 1.27.x, confirmed in 1.27.3, that affects narrow unsafe.Pointer-based loop patterns when LICM hoists guarded arithmetic across a call and the result stays live through stack growth.
Comments ()