Repository navigation
internal/fakecgo: reserve threadentry's spill slot on amd64 - #568
Conversation
|
The arm and ppc64le overlaps from the Notes reproduce under qemu-user 10.0.13 with glibc 2.41:
Neither shows up as a crash in Would you like to include these in this PR, or should they go in a separate one? Reproduced by Claude (Claude Code). |
hajimehoshi
left a comment
There was a problem hiding this comment.
Posted by Claude (Claude Code) on behalf of hajimehoshi. All of these are suggestions.
|
@hajimehoshi hey, thanks for the review, all suggestions are applied. i'd keep arm and ppc64le out of this mr. |
|
Can you file an issue for this and for related PRs? Thanks, |
|
@hajimehoshi done |
hajimehoshi
left a comment
There was a problem hiding this comment.
LGTM
@TotallyGamerJet PTAL
TotallyGamerJet
left a comment
There was a problem hiding this comment.
The code change looks good. I don't think we actually need the test for this though. It adds more assembly and a complicated looking test when the change itself is 4 lines. I'd prefer to have it removed but willing to keep it if @hajimehoshi prefers
|
I'm leaning toward removing tests. @AnyCPU Could you remove the test file? Thanks, |
What issue is this addressing?
On darwin/amd64, CI's
CGO_ENABLED=0 go test "-gcflags=all=-N -l" ./...hangs ininternal/fakecgo'sTestThreadEntryReturn(added in #486). It reproduces onmain(cfb65d5) with Go 1.27.4 and 1.25.0:Optimized builds and cgo builds pass. CI's
macos-latestrunner is arm64, which is why CI hasn't caught it.What type of issue is this addressing?
updates #571.
What this PR does | solves
Cause.
threadentryis an ABIInternal function with one register argument, and the ABI requires its callerto reserve that argument's spill slot ("the caller also reserves spill space on the stack for all register-based
arguments",
cmd/compile/abi-internal.md).threadentry_trampolinereserves none. The slot is then thetrampoline's
0(SP), wherePUSH_REGS_HOST_TO_ABI0()savedBX. Built with-N,threadentrystores itsargument there (
MOVQ AX, 0x48(SP)after a 0x40-byte frame), andPOP_REGS_HOST_TO_ABI0()hands the C callerthe freed
ThreadStartpointer inBX.macOS's
_pthread_startkeeps the thread'spthread_tinrbxacross the start routine and passes it to_pthread_exitafterwards._pthread_terminatethen faults on the boguspthread_twhile holding_pthread_list_lock. The thread has nog, so the fault repeats at full CPU, every laterpthread_createblocks on the lock, no new M starts, and the test waits forever. The same clobber happens on the other amd64 systems, but glibc doesn't read the thread fromBXafter the start routine returns, which would explain why Linux CI passes.This is the thread-exit path of #485 (fixed by #486), with a different register.
Fix. Reserve the slot with
ADJSParound the call, after the registers are pushed, asruntime·sigtrampdoes before callingsigtrampgo("Reserve space for spill slots",runtime/sys_darwin_amd64.s).before
PUSH_REGS_HOST_TO_ABI0(), and the slot at theCALLwould still be the savedBX.ADJSPrather thanSUBQ/ADDQ: the assembler tracks SP only throughPUSH/POP/ADJSP, andSUBQ $n, SPwould mark the trampolineSPWRITE.Test.
TestThreadEntryTrampolinePreservesBXcatches the bug on every amd64 system in every build mode, not only unoptimized builds on darwin/amd64.threadentry_callfor a probe that takes the address of its argument, which makes even an optimizedbuild store it in its spill slot.
callThreadEntryTrampoline, that puts a known value inBXand returnsBXafterwards.threadentry, which keeps threads the runtime starts during thetest working. Like
threadentry, it is nosplit and norace.BX.threadentry_amd64.go, because vet's asmdecl checks the non-test package.The linker drops it from normal binaries.
Testing.
-shuffle=on -count=10with and without cgo,-gcflags=all=-N -lwith and without cgo,-race -shuffle=on -count=10with and without cgo.-N -land-racebuilds (BXequals the argument). With it, it passes.-N -lbuilds, whileTestThreadEntryReturnpasses.-N -l.-stdflag.Notes.
macos-15-intelrunner would cover darwin/amd64 in CI, if you'd like one.go test -gcflags=-N(package only, notall=) fails to link with a nosplit stack overflow inx_cgo_thread_start. It fails the same way onmainand is unrelated to this change.threadentry_trampolinesavesR4at8(R13), which is ABI0threadentry's result slot, andreturn nilrestoresR4 = 0to libc in every build. On ppc64le,SAVE_GPR(32)putsR14inthreadentry's spill slot with-N, which is latent today because-Nbuilds don't link there.