Repository navigation
internal/fakecgo: align the stack for C calls on ppc64le - #573
Merged
hajimehoshi merged 1 commit intoNov 24, 2026
Merged
Conversation
ELFv2 requires R1 to be 16-byte aligned at a call, but Go frames on ppc64le are only 8-byte aligned and call5 only subtracted a constant 32 from R1. The C function could therefore run on a misaligned stack, where stvx and lvx silently drop the low address bits of vector stack slots. Align R1 down to 16 bytes below the Go caller's frame, and keep the original R1 in R22, which C preserves. R14 is not used for this because rt0_go keeps its stack pointer there across _cgo_init. Store the back chain before moving R1 so that 0(R1) is valid at any point, and keep the 32-byte adjustment visible to the assembler so that the nosplit stack accounting does not change. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Member
Author
|
@TotallyGamerJet PTAL |
Contributor
There was a problem hiding this comment.
🟡 Changes recommended
The new ppc64le alignment behavior lacks a regression test covering both incoming stack alignments, the callee’s alignment and back chain, and restoration of the original stack pointer.
1 open issue
What changed in this PR
This PR fixes a latent stack-alignment issue in fakecgo’s ppc64le C-call bridge, bringing call5 into compliance with ELFv2’s 16-byte alignment requirement.
Changes:
- Aligns the stack before C calls while preserving a valid back chain.
- Saves the original stack pointer in
R22and restores it before storing the return value.
| File | Description |
|---|---|
internal/fakecgo/trampolines_ppc64le.s |
Updates call5 to align the C-call stack and restore the original stack pointer afterward. |
Review effort: Balanced
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.

What issue is this addressing?
No issue is filed. This was found by review, and no failure has been reported.
What type of issue is this addressing?
bug
What this PR does | solves
Problem. ELFv2 requires
R1to be 16-byte aligned at a C call, but Go frames on ppc64le are only 8-byte aligned, and fakecgo'scall5only subtracted 32 fromR1. Depending on the Go caller's frame size, libc functions ran on a stack that is off by 8. This is latent: no crash was observed. The risk isstvx/lvx, which ignore the low 4 address bits, in C code that uses vector stack slots.Fix.
call5alignsR1down to 16 bytes below the caller's frame and restores the original value after the call.R1is kept inR22, which C preserves.R14is not used becausert0_gokeeps its stack pointer there across_cgo_init.R1moves, so0(R1)is always valid.call5is still 32 and the stack check results do not change. The up to 8 bytes of alignment padding are not counted.call5is now marked SPWRITE by the assembler.setg_trampolineis unchanged. It calls the runtime'ssetg_gcc, which is Go assembly that makes no C call.Testing. linux/ppc64le under qemu-user 10.0.13 (Debian 13, Podman), cross-built with Go 1.27.4, default and PIE unless noted. No ppc64le hardware was used.
call5(not part of this PR) recordedR1&15right before the C call. It was 0 and 8 without the fix, and only 0 with it. The back chain was always the caller'sR1.LD_PRELOADlibrary interposed libc functions on unprobed fakecgo test binaries. Without the fix,pthread_attr_init,pthread_attr_getstacksize,sigfillsetandpthread_sigmaskwere entered withR1&15 == 8whileTestThreadEntryReturnran. With it, all the interposed calls were entered with 0.TestCallbackEntriesin child mode passes. The fakecgo tests pass 50 times.go vetreports the same two findings as onmain(setg_trampoline,call5).-gcflags=all=-N -lbuilds still fail to link with the same 16-byte nosplit overflow inx_cgo_thread_startas onmain.The tests above ran on top of 2fc8a24, before #570. After rebasing onto #570, whose changes do not overlap with this one, only the builds,
go vetand gofmt were rerun.No test is added: it would need an assembly callee, and Go has no test-only assembly files.
Filed by Claude (Claude Code), on behalf of @hajimehoshi.
🤖 Generated with Claude Code