Repository navigation
purego: fix ARM callback trampoline spacing below ARMv7 #563
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
hajimehoshi
merged 5 commits into
ebitengine:main
from
yuhaiin:codex/fix-arm-callback-trampolines
Nov 7, 2026
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
3eb10ce
purego: keep ARM callback trampoline entries a fixed size
Asutorufa 8b07e60
purego: test that every callback entry reaches its own function
Asutorufa a9b1911
purego: let the TestCallbackEntries child find the target's libraries
Asutorufa 628c659
.github/workflows: provide the armhf dynamic linker for native children
Asutorufa b8b6fc2
.github/workflows: run the TestCallbackEntries child on minor archite…
Asutorufa File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,76 @@ | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
| // SPDX-FileCopyrightText: 2026 The Ebitengine Authors | ||
|
|
||
| //go:build darwin || (linux && (386 || amd64 || arm || arm64 || loong64 || ppc64le || riscv64 || (s390x && (cgo || go1.27)))) | ||
|
|
||
| package purego_test | ||
|
|
||
| import ( | ||
| "errors" | ||
| "os" | ||
| "os/exec" | ||
| "path/filepath" | ||
| "syscall" | ||
| "testing" | ||
|
|
||
| "github.com/ebitengine/purego" | ||
| ) | ||
|
|
||
| const callbackEntriesChildEnv = "PUREGO_TEST_CALLBACK_ENTRIES_CHILD" | ||
|
|
||
| // TestCallbackEntries calls every callback entry and checks that each one | ||
| // reaches its own Go function. Callback slots are never released, so the | ||
| // callbacks are registered in a child process to keep the slots of this | ||
| // process available to other tests. | ||
| func TestCallbackEntries(t *testing.T) { | ||
| if os.Getenv(callbackEntriesChildEnv) == "1" { | ||
| callEveryCallbackEntry(t) | ||
| return | ||
| } | ||
| exe, err := os.Executable() | ||
| if err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| cmd := exec.Command(exe, "-test.run=^TestCallbackEntries$", "-test.v") | ||
| cmd.Env = append(os.Environ(), callbackEntriesChildEnv+"=1") | ||
| if prefix := os.Getenv("QEMU_LD_PREFIX"); prefix != "" { | ||
| // QEMU_LD_PREFIX only tells qemu-user where the target's shared | ||
| // libraries are. The child might not run under the same emulator: | ||
| // an arm64 host with AArch32 support runs a linux/arm binary natively, | ||
| // and the dynamic loader then searches the host's directories. Point | ||
| // the loader at the target's libraries directly so the child starts | ||
| // either way. Under qemu-user the path still resolves to the same | ||
| // libraries. | ||
| libs := filepath.Join(prefix, "lib") | ||
| if old := os.Getenv("LD_LIBRARY_PATH"); old != "" { | ||
| libs += string(filepath.ListSeparator) + old | ||
| } | ||
| cmd.Env = append(cmd.Env, "LD_LIBRARY_PATH="+libs) | ||
| } | ||
| out, err := cmd.CombinedOutput() | ||
| if errors.Is(err, syscall.ENOEXEC) { | ||
| // For example, the test binary runs under a user-mode emulator without | ||
| // binfmt_misc registered, so the child cannot be started. | ||
| t.Skipf("cannot re-execute the test binary: %v", err) | ||
| } | ||
| if err != nil { | ||
| t.Fatalf("child process failed: %v\n%s", err, out) | ||
| } | ||
| } | ||
|
|
||
| func callEveryCallbackEntry(t *testing.T) { | ||
| callbacks := make([]uintptr, purego.MaxCB) | ||
| for i := range callbacks { | ||
| callbacks[i] = purego.NewCallback(func() uintptr { return uintptr(i) }) | ||
| } | ||
| const maxFailures = 20 | ||
| failures := 0 | ||
| for i, cb := range callbacks { | ||
| if got, _, _ := purego.SyscallN(cb); got != uintptr(i) { | ||
| t.Errorf("callback %d returned %d", i, got) | ||
| if failures++; failures >= maxFailures { | ||
| t.Fatalf("stopping after %d failures", failures) | ||
| } | ||
| } | ||
| } | ||
| } | ||
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,54 @@ | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
| // SPDX-FileCopyrightText: 2026 The Ebitengine Authors | ||
|
|
||
| package purego_test | ||
|
|
||
| import ( | ||
| "math/bits" | ||
| "testing" | ||
| "unsafe" | ||
|
|
||
| "github.com/ebitengine/purego" | ||
| ) | ||
|
|
||
| func TestCallbackTrampolineEncoding(t *testing.T) { | ||
| // Check every entry without consuming the process-wide callback slots. | ||
| // Below ARMv7, a MOVW of a large immediate can insert a literal pool into | ||
| // the table and make a computed callback address point at data instead. | ||
| const maxFailures = 20 | ||
| failures := 0 | ||
| fail := func(format string, args ...any) { | ||
| t.Helper() | ||
| t.Errorf(format, args...) | ||
| if failures++; failures >= maxFailures { | ||
| t.Fatalf("stopping after %d failures", failures) | ||
| } | ||
| } | ||
| target := purego.Callbackasm1Addr() | ||
| for i := range purego.MaxCB { | ||
| pc := purego.CallbackasmAddr(i) | ||
| // pc identifies executable native memory, never a Go allocation. | ||
| instructions := (*[3]uint32)(*(*unsafe.Pointer)(unsafe.Pointer(&pc))) | ||
| mov, orr, branch := instructions[0], instructions[1], instructions[2] | ||
| if mov&0xfffff000 != 0xe3a0c000 || orr&0xfffff000 != 0xe38cc000 { | ||
| fail("callback %d: expected MOV/ORR immediates into R12, got %#08x/%#08x", i, mov, orr) | ||
| continue | ||
| } | ||
| // ARM immediates are an 8-bit value rotated right by an even count. | ||
| low := bits.RotateLeft32(mov&0xff, -2*int((mov>>8)&0xf)) | ||
| high := bits.RotateLeft32(orr&0xff, -2*int((orr>>8)&0xf)) | ||
| if index := low | high; index != uint32(i) { | ||
| fail("callback %d: MOV/ORR load index %d", i, index) | ||
| } | ||
| if branch&0xff000000 != 0xea000000 { | ||
| fail("callback %d: expected an unconditional branch at %#x, got %#08x", i, pc+8, branch) | ||
| continue | ||
| } | ||
| // ARM branches use a signed 24-bit word offset from their own address | ||
| // plus 8. The branch is at pc+8. | ||
| destination := pc + 16 + uintptr(int32(branch<<8)>>6) | ||
| if destination != target { | ||
| fail("callback %d: branch target %#x, want callbackasm1 at %#x", i, destination, target) | ||
| } | ||
| } | ||
| } |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
| // SPDX-FileCopyrightText: 2026 The Ebitengine Authors | ||
|
|
||
| package purego | ||
|
|
||
| import "unsafe" | ||
|
|
||
| //go:linkname __callbackasm1 callbackasm1 | ||
| var __callbackasm1 byte | ||
|
|
||
| // CallbackasmAddr re-exports callbackasmAddr for external tests. | ||
| func CallbackasmAddr(i int) uintptr { | ||
| return callbackasmAddr(i) | ||
| } | ||
|
|
||
| // Callbackasm1Addr returns the address that every callbackasm entry branches to. | ||
| func Callbackasm1Addr() uintptr { | ||
| return uintptr(unsafe.Pointer(&__callbackasm1)) | ||
| } |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
| // SPDX-FileCopyrightText: 2026 The Ebitengine Authors | ||
|
|
||
| //go:build darwin || freebsd || (linux && (386 || amd64 || arm || arm64 || loong64 || ppc64le || riscv64 || (s390x && (cgo || go1.27)))) || netbsd | ||
|
|
||
| package purego | ||
|
|
||
| // MaxCB re-exports maxCB for external tests. | ||
| const MaxCB = maxCB |
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
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
Oops, something went wrong.
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.