Skip to content

internal/fakecgo: reserve threadentry's spill slot on ppc64le - #570

Merged
hajimehoshi merged 2 commits into
ebitengine:mainfrom
AnyCPU:fix/trampoline_ppc_1
Nov 24, 2026
Merged

hajimehoshi merged 2 commits into
ebitengine:mainfrom
AnyCPU:fix/trampoline_ppc_1

Conversation

@AnyCPU

@AnyCPU AnyCPU commented Nov 17, 2026 •

Copy link
Copy Markdown
Contributor

What issue is this addressing?

@hajimehoshi hey,

this is the ppc64le follow-up from #568: threadentry_trampoline saves the C caller's R14 in the spill slot of threadentry's register argument, and a threadentry that spills v hands libc R14 = v.

What type of issue is this addressing?

updates #571.

What this PR does | solves

Cause. threadentry is ABIInternal with one register argument, and its caller reserves that argument's spill slot right after the 32-byte fixed frame, at 32(R1). The trampoline's SAVE_GPR(32) puts R14, callee-saved under ELFv2, in that slot.

It is latent today: the optimized threadentry doesn't spill v, -N builds don't link on ppc64le (nosplit overflow in x_cgo_thread_start, also on main), and glibc's start_thread doesn't read R14 after the start routine returns. It is an ABI violation that only a probe shows, as in #568.

Fix. Reserve 8 bytes for the slot: the saves start at 40(R1), and the frame grows from 320 to 336 bytes, rounded up to keep R1 16-byte aligned.

Test. TestThreadEntryTrampolinePreservesR14, the same shape as #568's test:

  • A probe that takes the address of v, which makes the compiler store it in its spill slot, and forwards any other call to the real threadentry.
  • An asm helper, callThreadEntryTrampoline, that puts a known value in R14, calls the trampoline and returns R14. The trampoline returns to C with CR's value in R0, so the helper clears R0, which Go code expects to be 0.
  • A second check guards against a vacuous pass: the word above the slot must be the saved R14.

CI. The qemu jobs build only the root package, so the ppc64le step gets lines that build and run the fakecgo test binary, default and PIE. Without them the test doesn't run in CI.

Testing. linux/ppc64le under qemu-user 10.0.13 (Docker, Debian 13, glibc 2.41), Go 1.27.3 and 1.25.0:

  • Without the fix, the new test fails, default and PIE (R14 equals the argument). With it, it passes 20 times in each, and TestThreadEntryReturn still passes.
  • The root package's suite, built as CI does, passes, default and PIE.
  • gofmt is clean. go vet reports the same two findings as on main (setg_trampoline, call5), which this PR doesn't touch.

Notes.

  • The arm follow-up is internal/fakecgo: reserve threadentry's result slot on arm #569. The two PRs touch different files and different hunks of test.yml, so either can merge first.
  • I kept the hand-written save sequence rather than switching to STACK_AND_SAVE_HOST_TO_GO_ABI, which crosscall2 uses. The trampoline doesn't save V20-V31, which are callee-saved under ELFv2, and Go assembly does use them (crypto, bytealg). It is latent the same way: start_thread uses no vector registers after the call. Switching to the macro would fix that too, but it changes the frame to 528 bytes and the test; I can send it separately if you'd like.

@AnyCPU

AnyCPU commented Nov 17, 2026

Copy link
Copy Markdown
Contributor Author

@hajimehoshi please review

@TotallyGamerJet TotallyGamerJet left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@hajimehoshi hajimehoshi left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@hajimehoshi
hajimehoshi merged commit 01e51a8 into ebitengine:main Nov 24, 2026
53 of 54 checks passed
@AnyCPU
AnyCPU deleted the fix/trampoline_ppc_1 branch November 24, 2026 04:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants