Skip to content

purego: fix ARM callback trampoline spacing below ARMv7 - #563

Merged
hajimehoshi merged 5 commits into
ebitengine:mainfrom
yuhaiin:codex/fix-arm-callback-trampolines
Nov 7, 2026
Merged

hajimehoshi merged 5 commits into
ebitengine:mainfrom
yuhaiin:codex/fix-arm-callback-trampolines

Conversation

@Asutorufa

@Asutorufa Asutorufa commented Nov 6, 2026 •

Copy link
Copy Markdown
Contributor

What issue is this addressing?

On Linux/ARM below ARMv7 (GOARM=5 and GOARM=6), loading a callback index
with MOVW can require a literal pool when the value cannot be encoded as a
rotated immediate. Only GOARM=7 gets the 16-bit MOVW. The assembler inserts
these pools into callbackasm, while callbackasmAddr assumes every entry
occupies exactly eight bytes. After enough callback registrations, the computed
callback address can point into literal data and cause an illegal instruction.

This is the ARM correctness fix separated from #562 as requested in
the review discussion.
It applies directly to current main and contains no callback-dispatch optimization.

What type of issue is this addressing?

Bug.

What this PR does | solves

Build each callback index from two encodable immediates using MOVW and ORR,
followed by the branch. Update the ARM entry stride to twelve bytes and regenerate
the trampoline table with go generate. All ARM targets use the same layout.
The large assembly diff is generated; the handwritten production changes are in
the generator and address calculation.

Add two tests:

  • TestCallbackEntries re-executes the test binary, registers maxCB callbacks
    that each return their own index, and calls them all. It covers every
    architecture with callback support, does not depend on instruction encodings,
    and leaves the parent's callback slots untouched. It is skipped when the binary
    cannot be re-executed, such as under a user-mode emulator without binfmt_misc.
  • TestCallbackTrampolineEncoding (linux/arm) checks all entries without
    registering callbacks. It verifies the instruction opcodes, decodes the rotated
    immediates to check the callback index, and checks that every branch reaches
    callbackasm1. It reports every bad entry, up to a limit.

Validation

  • Linux ARMv5, ARMv6 and ARMv7 under QEMU 10.0, CGO disabled: both new tests
    pass. TestCallbackEntries was run in child mode directly there, because the
    local QEMU setup has no binfmt_misc and the parent skips.
  • Negative check: against main's eight-byte layout, TestCallbackTrampolineEncoding fails
    from entry 0 and TestCallbackEntries fails with wrong indices
    (for example, callback 445 returns 444).
  • Complete Linux amd64 suites pass with CGO enabled and disabled.
  • go vet passes with CGO enabled and disabled, gofmt is clean, and rerunning
    go generate ./... leaves the committed tree unchanged.

Split callback indices into two encodable immediates to prevent ARMv5 literal pools from breaking trampoline address calculation. Use a twelve-byte stride and regenerate the assembly table.

Verify the instructions, loaded callback index, and branch destination for all 2,000 entries without consuming callback registrations.

@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.

Posted by Claude (Claude Code) on behalf of hajimehoshi. All of these are suggestions, and none is a confirmed failure. The PR's code was not built or run for this review; the findings come from reading the code and assembling zcallback_arm.s with go tool asm.

Comment thread wincallback.go Resolved
Comment thread callback_arm_test.go Resolved
Comment thread callback_arm_test.go Resolved
Comment thread callback_arm_test.go Resolved
Comment thread callback_arm_test.go Resolved
Comment thread callback_arm_test.go Resolved
Comment thread callback_arm_test.go Resolved
GOARM=6 needs the split MOVW/ORR load too: only GOARM=7 gets the 16-bit
MOVW, so describe the affected targets as below ARMv7.

Rework the ARM encoding test as an external test in
callback_linux_arm_test.go. It now checks that every branch reaches
callbackasm1, not only that all branches agree, so a shared wrong
destination such as a linker trampoline would fail. It reports every bad
entry, up to a limit, instead of stopping at the first one, and drops the
stride check, which only compared two hard-coded constants.

Add TestCallbackEntries, which re-executes the test binary, registers
maxCB callbacks that each return their own index, and calls them all.
This covers every architecture without depending on the instruction
encodings, and leaves the callback slots of the parent process
untouched. It is skipped when the binary cannot be re-executed, such as
under a user-mode emulator without binfmt_misc.
@Asutorufa Asutorufa changed the title purego: fix ARMv5 callback trampoline spacing purego: fix ARM callback trampoline spacing below ARMv7 Nov 6, 2026
@hajimehoshi

Copy link
Copy Markdown
Member

Fix the test failure.

On the arm64 CI runner the linux/arm child failed to start with
"libdl.so.2: cannot open shared object file". QEMU_LD_PREFIX only tells
qemu-user where the target's shared libraries are, but the child is not
always run by the same emulator: an arm64 host with AArch32 support runs
it natively, and the dynamic loader then searches the host's directories.

When QEMU_LD_PREFIX is set, also pass its lib directory in
LD_LIBRARY_PATH to the child. Under qemu-user the path resolves to the
same libraries.
The arm64 runner can execute linux/arm binaries natively. The child
process of TestCallbackEntries therefore runs outside QEMU, and the
kernel looks for the cgo test binary's dynamic linker,
/lib/ld-linux-armhf.so.3, on the host, where only /lib/ld-linux.so.3 was
linked. Link it as well.
@Asutorufa

Copy link
Copy Markdown
Contributor Author

Fixed in a9b1911 and 628c659. The arm64 runner executes linux/arm binaries natively, so the child process of TestCallbackEntries ran outside QEMU and QEMU_LD_PREFIX did not apply to it:

  • The test now passes $QEMU_LD_PREFIX/lib to the child in LD_LIBRARY_PATH.
  • The workflow also links /lib/ld-linux-armhf.so.3, which the armhf Cgo test binary requests, next to the existing /lib/ld-linux.so.3 link.

The full workflow passes on my fork: https://github.com/yuhaiin/purego/actions/runs/42329129160. In the ubuntu-24.04-arm jobs, TestCallbackEntries passes for all four linux/arm binaries (ARMv7/ARMv5, with and without Cgo) and is not skipped. CI here is waiting for workflow approval.

@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.

Posted by Claude (Claude Code) on behalf of hajimehoshi. This is a suggestion, and it comes from the CI job logs; the PR's code was not built or run for this review.

Comment thread callback_entries_test.go Resolved
…ctures

binfmt_misc is not registered on the runner that tests loong64, ppc64le,
riscv64 and s390x under QEMU, so TestCallbackEntries cannot re-execute
the test binary and skips. Run its child mode directly after each test
binary so these callback tables are exercised too.

@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 3716594 into ebitengine:main Nov 7, 2026
27 checks passed
@hajimehoshi

Copy link
Copy Markdown
Member

I'm sorry but the squashed commit message included Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> trailer accidentally even though this PR's commits didn't indicate that. I should have been more careful.

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