Repository navigation
Description
PureGo Version
v0.11.2+
Operating System
- Windows
- macOS
- Linux
- FreeBSD
- illumos
- NetBSD
- Android
- iOS
Go Version (go version)
go1.25.0+
What steps will reproduce the problem?
Before #568, on an Intel Mac, an unoptimized build without cgo hung in TestThreadEntryReturn:
CGO_ENABLED=0 go test "-gcflags=all=-N -l" -run '^TestThreadEntryReturn$' -count=1 ./internal/fakecgo/CI runs this test unoptimized on Linux, FreeBSD, NetBSD and illumos amd64, where it passes. Its macOS runner is arm64. Nothing fails on arm or ppc64le either.
Each of the three PRs first came with a probe test, which showed the wrong register in a default build on its architecture. The tests have since been removed from all three (see the comments).
What is the expected result?
The test passes, and threadentry_trampoline returns to libc with every callee-saved register as it received it.
What happens instead?
threadentry_trampoline saves the C caller's callee-saved registers on its stack and calls threadentry. On amd64 (until #568), arm and ppc64le, one of them is saved in a slot that threadentry is allowed to write, so libc gets a wrong value back when the thread exits:
| Register | Saved in | Overwritten | Effect | Fix | |
|---|---|---|---|---|---|
| amd64 | BX |
the spill slot of threadentry's argument |
when threadentry spills it, as -N builds do |
before #568: hang on darwin, nothing visible elsewhere | #568 (merged) |
| arm | R4 |
threadentry's result slot |
always: return nil stores 0 |
nothing visible with glibc | #569 |
| ppc64le | R14 |
the spill slot of threadentry's argument |
when threadentry spills it |
nothing visible: optimized builds don't spill, and -N builds don't link |
#570 |
On macOS, _pthread_start keeps the thread's pthread_t in rbx and passes it to _pthread_exit after the start routine returns. With the wrong value the thread faulted while holding a libpthread lock, and the process hung. #568 has the details.
Anything else you feel useful to add?
TestThreadEntryReturncame with internal/fakecgo: preserve frame pointer across mstart call in threadentry #486, which fixed internal/fakecgo: threadentry SIGSEGVs in its LEAVE epilogue when a fakecgo-created OS thread exits (amd64, Go 1.28 frame-pointer epilogue) #485: the same thread-exit path, with a different register.- I also read the trampolines for 386, arm64, loong64, riscv64 and s390x. There the slot holds the argument or nothing, not a saved register. I didn't run them.
- internal/fakecgo: reserve threadentry's spill slot on amd64 #568 is merged. internal/fakecgo: reserve threadentry's result slot on arm #569 and internal/fakecgo: reserve threadentry's spill slot on ppc64le #570 each change one assembly file, so they can merge in either order.
Activity
AnyCPU commentedon Nov 17, 2026
@hajimehoshi fyi,
by reading only, s390x's threadentry_trampoline doesn't save F8-F15, which the s390x C ABI makes callee-saved (purego's crosscall2 and Go's gcc_s390x.S do save them). Like ppc64le's V20-V31, it's a missing save, not an overlap.
a separate report, if wanted.
internal/fakecgo: reserve threadentry's spill slot on amd64 (#568)
AnyCPU commentedon Nov 23, 2026
@hajimehoshi @TotallyGamerJet shall i remove tests for ppc and arm too?
hajimehoshi commentedon Nov 23, 2026
@AnyCPU I think yes.
@TotallyGamerJet Wdyt?
TotallyGamerJet commentedon Nov 23, 2026
Yes they can be deleted too
AnyCPU commentedon Nov 23, 2026
@hajimehoshi @TotallyGamerJet mrs are updated
internal/fakecgo: reserve threadentry's result slot on arm (#569)
internal/fakecgo: reserve threadentry's spill slot on ppc64le (ebiten…