Skip to content

runtime: _panic.fp makes a dead stack object reachable during Goexit/panic, so the GC marks a freed object #82394

Closed

Description

@dayamjz

Disclosure: a coding agent (Claude Code) did this investigation and wrote this report, and I reviewed it before filing. All output below comes from local runs.

Go version

go version go1.26.9 darwin/amd64

The same program also reproduces on go1.22.12, go1.24.13, go1.25.12, go1.26.10 and go1.27.3 (darwin/amd64), and on go1.26.9 linux/amd64. It does not reproduce on go1.21.13.

Output of go env in your module/workspace:

CGO_ENABLED='1'
GOAMD64='v1'
GOARCH='amd64'
GODEBUG=''
GOEXPERIMENT=''
GOFLAGS=''
GOHOSTARCH='amd64'
GOHOSTOS='darwin'
GOOS='darwin'
GOVERSION='go1.26.9'

(Intel Core i9-9980HK, macOS / Darwin 25.6.0.)

What did you do?

The program below has no unsafe, no cgo, no finalizers and no data races. f takes a struct larger than 128 bytes by value. A closure that is not inlined reads it, so the closure captures it by address and the argument becomes a stack object. After the closure's call, the argument is dead. f drops every other reference to the object the argument points at, collects (which frees that object), and then calls runtime.Goexit. f's deferred call collects again.

package main

import (
	"fmt"
	"runtime"
)

type big struct {
	p   *[4]int
	pad [32]int // keeps big above the 128 bytes a closure captures by value
}

var (
	sink  *[4]int
	value int
)

//go:noinline
func use(p *[4]int) { value = p[0] }

//go:noinline
func call(fn func()) { fn() }

//go:noinline
func f(w big) {
	defer func() {
		runtime.GC()
		runtime.GC()
	}()
	call(func() { use(w.p) }) // w is address-taken, and dead after this
	sink = nil
	runtime.GC() // frees *w.p: nothing live points to it
	runtime.GC()
	runtime.Goexit()
}

func main() {
	fmt.Println(runtime.Version(), runtime.GOOS+"/"+runtime.GOARCH)
	for i := 0; i < 200; i++ {
		done := make(chan struct{})
		go func() {
			defer close(done)
			p := new([4]int)
			p[0] = i
			sink = p
			f(big{p: p})
		}()
		<-done
	}
	fmt.Println("no crash")
}

GOMAXPROCS=4 go run . (any GOMAXPROCS works)

What did you see happen?

The program crashes on every run (5/5 on each version listed above). Depending on whether the freed slot was reused, it fails as either found pointer to free object or found bad pointer in Go heap.

With GOEXPERIMENT=nogreenteagc:

runtime: marked free object in span 0x12a8920, elemsize=32 freeindex=0 (bad use of unsafe.Pointer or having race conditions? try -d=checkptr or -race)
0x28f551d4e0c0 free  marked   zombie
000028f551d4e0c0: 00000000 00000000  00000000 00000000  ................
000028f551d4e0d0: 00000000 00000000  00000000 00000000  ................
fatal error: found pointer to free object

With the default (Green Tea) build:

runtime: pointer 0x1fee91bc4020 to unallocated span span.base()=0x1fee91bc4000 span.limit=0x1fee91bc5f80 span.state=0
runtime: found in object at *(0x1fee91b99ea0+0x0)
fatal error: found bad pointer in Go heap (incorrect use of unsafe or cgo?)

runtime stack:
runtime.throw({0x107a6305?, 0x6?})
	$GOROOT/src/runtime/panic.go:1229 +0x48
runtime.badPointer(0x1354c188, 0x1fee91bc4020, 0x1fee91b99ea0, 0x0)
	$GOROOT/src/runtime/mbitmap.go:1334 +0x165
runtime.findObject(0x700003b11c70?, 0x1fee91b99e88?, 0x108391c0?)
	$GOROOT/src/runtime/mbitmap.go:1386 +0x9e
runtime.scanblock(0x1fee91b99ea0, 0x8, 0x107a6ad0, 0x1fee91b51260, 0x700003b11d18)
	$GOROOT/src/runtime/mgcmark.go:1511 +0xdd
runtime.scanstack(0x1fee91b845a0, 0x1fee91b51260)
	$GOROOT/src/runtime/mgcmark.go:1038 +0x58e
runtime.markroot.func1()
	$GOROOT/src/runtime/mgcmark.go:306 +0xb1

mgcmark.go:1038 is the scanblock in scanstack's loop over reachable stack objects.

Controls on go1.26.9, 5 runs each:

  • Remove f's defer: 0/5 crashes.
  • Shrink pad to [4]int, so the closure captures w by value and there is no stack object: 0/5.
  • Replace runtime.Goexit() with panic(...), with or without a recover() in the deferred call: 5/5.
  • Build with -race: still crashes.

What did you expect to see?

no crash.

Analysis

This is our reading of the runtime source; we have not confirmed it with runtime instrumentation.

While Goexit (or a panic) runs f's deferred call, (*_panic).nextFrame has set p.fp = unsafe.Pointer(u.frame.fp) for f's frame. On amd64 frame.argp == frame.fp, so _panic.fp is exactly the address of f's stack-argument area, which is where the stack object w lives. scanstack calls state.putPtr(uintptr(unsafe.Pointer(gp._panic)), false), scans the _panic record precisely as a stack object, and so queues its fp (and sp, startSP, slotsPtr, ...) as pointers into the stack. findObject resolves fp to the stack object w, so w is treated as reachable and is scanned with its type even though it is dead at f's PC. w.p was not marked by the earlier cycle (w was dead and unreachable then), so it was freed; the scan now greys a pointer to a free slot. The sweeper then reports a zombie, or the marker hits an unallocated span if the span was freed.

In other words, the frame-position fields of _panic (which are only there so the runtime can resume unwinding) act as liveness roots for stack objects. That resurrects dead stack objects that may hold dangling heap pointers. Go 1.21 does not reproduce. The 1.22 panic/defer rewrite, which introduced these fields, looks like the likely origin, but we have not bisected.

How we found it

A real test suite hit this (race-built, darwin/amd64): t.Skipf → runtime.Goexit while a test-body frame holding a 224-byte struct argument was running its deferred Close of a database. GODEBUG=gccheckmark=1 reported checkmark found unmarked object, with found obj at *(base+0x18). That base was the test body's frame pointer, and the scan came from scanstack's stack-object loop. In practice this means any test that skips or fails (t.Skip*, t.Fatal*) out of such a frame can crash the test binary.

Not verified: arm64, where frame.argp = frame.fp + 8, so fp may not fall inside the argument the same way.

Possibly related: #80799 (under Green Tea, reportZombies prints every slot unmarked, which hid the zombie in our first crashes).

Activity

  1. dayamjz commented on Oct 21, 2026

    @dayamjz
    Author

    Additional data point from the test suite where this was found (darwin/amd64, go1.26.9, race build). With GOMAXPROCS=4 GOGC=20 GODEBUG=clobberfree=1,gccheckmark=1, running the three affected tests 50 times each crashed 6 of 6 runs with checkmark found unmarked object. Every crash reported found obj at *(base+0x18), where base equals the fp of the test body frame whose 224-byte argument is captured by address by a closure, and the scan length is 0xe0. The goroutine was in runtime.Goexit from t.Skipf, running that frame's deferred (*sql.DB).Close. Moving the skip to before that frame exists removes the crash (0 of 6 runs, 900 skips). We have not checked arm64 or whether -race changes the rate.

  2. dayamjz commented on Oct 21, 2026

    @dayamjz
    Author

    Negative data point on arm64: the minimal program above does not crash on darwin/arm64 (Apple M-series). 0 of 45 runs (5 at default settings, then 20 each with go1.26.9 and go1.26.4 under GOMAXPROCS=4 GOGC=20 GODEBUG=clobberfree=1,gccheckmark=1), each run 200 Goexit iterations; the same binary built for darwin/amd64 crashes 5 of 5. Consistent with _panic.fp landing on the dead argument only under amd64's frame layout.

  3. visiongaiatechnology commented on Oct 23, 2026

    @visiongaiatechnology

    Independent confirmation and a network trigger for this runtime memory-safety bug on current Go (1.27.3) and 1.26.10:

    • Standalone reproduction: the program pattern from the report fails on Linux/amd64 with
      untime: marked free object ... and atal error: found pointer to free object (exit 2), no unsafe/cgo/race.
    • Network trigger: a normal
      et/http handler with a >128-byte struct captured by an address-taking closure, a deferred
      untime.GC(), and a request-triggered panic crashes the whole process after one unauthenticated HTTP request.
      et/http's per-connection recover cannot help because the fatal abort happens in the runtime GC while _panic.fp is treated as a stack root. curl sees an empty reply and the server exits 2.

    This makes the issue a remote DoS for applications that panic inside such a frame, in addition to the standalone memory corruption. I have not demonstrated code execution. Full standalone and HTTP programs are available on request.

    — VisionGaiaTechnology

  4. gopherbot commented on Nov 2, 2026

    @gopherbot
    Contributor

    Change https://go.dev/cl/857961 mentions this issue: runtime: don't let a panic's frame pointer keep a stack object alive

  5. added
    FixPendingIssues that have a fix which has not yet been reviewed or submitted.
    on Nov 3, 2026
  6. added this to the Go1.28 milestone on Nov 3, 2026
  7. added a commit that references this issue on Nov 6, 2026
    739e336
  8. gopherbot commented on Nov 10, 2026

    @gopherbot
    Contributor

    Change https://go.dev/cl/861982 mentions this issue: runtime: hold _panic.sp and _panic.startSP as uintptr

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

FixPendingIssues that have a fix which has not yet been reviewed or submitted.compiler/runtimeIssues related to the Go compiler and/or runtime.

Type

No type

Projects

Relationships

None yet

Development

No branches or pull requests

Issue actions