Skip to content

internal/fakecgo: reserve threadentry's result slot on arm - #569

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

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

Conversation

@AnyCPU

@AnyCPU AnyCPU commented Nov 17, 2026 •

Copy link
Copy Markdown
Contributor

What issue is this addressing?

@hajimehoshi hey,

this is the arm follow-up from #568: threadentry_trampoline saves the C caller's R4 in threadentry's result slot, so libc gets R4 = 0 back after every thread exit.

What type of issue is this addressing?

updates #571.

What this PR does | solves

Cause. threadentry is called as ABI0 on arm, so its argument is at 4(R13) and its result at 8(R13) of the trampoline's frame. The trampoline saves R4, callee-saved under AAPCS, at 8(R13), and return nil overwrites it. This happens in every build, not only with -N.

It is latent with glibc: start_thread doesn't use R4 after the start routine returns (glibc 2.41 armhf), and TestThreadEntryReturn passes with or without the fix. The new test is what shows it.

Fix. The saved registers move past the result slot: R4-R11 to 12..40(R13), F8-F15 to 48..104(R13), frame $108. The argument stays at 4(R13) and the result slot at 8(R13) is left to threadentry, the same layout as trampolines_386.s. With $108 the doubles are also 8-byte aligned. Moving only R4, to 104(R13), would be a three-line change, but it leaves R4 apart from the other saves; I can switch to it if you prefer.

Test. TestThreadEntryTrampolinePreservesR4, the arm counterpart of #568's test:

  • It swaps threadentry_call for a probe that returns nil like threadentry, and forwards any other call to the real threadentry. Like threadentry, it is nosplit and norace.
  • It calls the trampoline through a small asm helper, callThreadEntryTrampoline, that puts a known value in R4 and returns R4 afterwards. Its Go declaration is in threadentry_arm.go, next to the asm, as in internal/fakecgo: reserve threadentry's spill slot on amd64 #568.
  • A fakecgo test binary can't start on arm today: on 32-bit ELF, the internal linker writes DT_NEEDED only for a _ _ import (cmd/link/internal/ld/elf.go), so pthread_attr_init is unresolved. That's why threadentry_test.go skips arm. The test file adds //go:cgo_import_dynamic _ _ "libc.so.6", which fixes it for this binary.

CI. The qemu jobs build only the root package, so the arm step gets two lines per float ABI to build and run the fakecgo test binary (hard-float and GOARM=5). Without them the test doesn't run in CI.

Testing. linux/arm 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 with GOARM=5, 6 and 7, default and -gcflags=all=-N -l (R4 = 0x0). With it, it passes 20 times in each.
  • The root package's suite, built as CI does (CC set to the cross compiler), passes for GOARM=7 and GOARM=5.
  • go vet for linux/arm and gofmt are clean.

Notes.

  • With the libc import in place, TestThreadEntryReturn also passes on linux/arm under qemu, so its !arm exclusion could be lifted later.
  • freebsd is in trampolines_arm.s's build tag, but fakecgo doesn't build for freebsd/arm on main (go_freebsd.go is amd64 and arm64 only), so the test is Linux only.

@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 2fc8a24 into ebitengine:main Nov 24, 2026
27 checks passed
@AnyCPU
AnyCPU deleted the fix/trampoline_arm_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