Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,10 @@ jobs:

# Create symlink for the dynamic linker that QEMU expects
sudo ln -sf /usr/arm-linux-gnueabihf/lib/ld-linux-armhf.so.3 /lib/ld-linux.so.3
# TestCallbackEntries re-executes the test binary, and this AArch32-capable
# host runs that child natively instead of under QEMU. Cgo binaries
# request this dynamic linker.
sudo ln -sf /usr/arm-linux-gnueabihf/lib/ld-linux-armhf.so.3 /lib/ld-linux-armhf.so.3
go env -w CC=arm-linux-gnueabihf-gcc
go env -w CXX=arm-linux-gnueabihf-g++
env GOOS=linux GOARCH=arm CGO_ENABLED=0 go test -c -o=purego-test-nocgo .
Expand Down Expand Up @@ -262,8 +266,12 @@ jobs:
go env -w CXX=loongarch64-linux-gnu-g++-14
env GOOS=linux GOARCH=loong64 CGO_ENABLED=0 go test -c -o=purego-test-nocgo .
env QEMU_LD_PREFIX=/usr/loongarch64-linux-gnu qemu-loongarch64 ./purego-test-nocgo -test.shuffle=on -test.v -test.count=10
# binfmt_misc is not registered here, so TestCallbackEntries cannot re-execute
# the test binary. Run its child mode directly.
env QEMU_LD_PREFIX=/usr/loongarch64-linux-gnu PUREGO_TEST_CALLBACK_ENTRIES_CHILD=1 qemu-loongarch64 ./purego-test-nocgo -test.run='^TestCallbackEntries$' -test.v
env GOOS=linux GOARCH=loong64 CGO_ENABLED=1 go test -c -o=purego-test-cgo .
env QEMU_LD_PREFIX=/usr/loongarch64-linux-gnu qemu-loongarch64 ./purego-test-cgo -test.shuffle=on -test.v -test.count=10
env QEMU_LD_PREFIX=/usr/loongarch64-linux-gnu PUREGO_TEST_CALLBACK_ENTRIES_CHILD=1 qemu-loongarch64 ./purego-test-cgo -test.run='^TestCallbackEntries$' -test.v
go env -u CC
go env -u CXX

Expand All @@ -273,10 +281,15 @@ jobs:
go env -w CXX=powerpc64le-linux-gnu-g++
env GOOS=linux GOARCH=ppc64le CGO_ENABLED=0 go test -c -o=purego-test-nocgo .
env QEMU_LD_PREFIX=/usr/powerpc64le-linux-gnu qemu-ppc64le ./purego-test-nocgo -test.shuffle=on -test.v -test.count=10
# binfmt_misc is not registered here, so TestCallbackEntries cannot re-execute
# the test binary. Run its child mode directly.
env QEMU_LD_PREFIX=/usr/powerpc64le-linux-gnu PUREGO_TEST_CALLBACK_ENTRIES_CHILD=1 qemu-ppc64le ./purego-test-nocgo -test.run='^TestCallbackEntries$' -test.v
env GOOS=linux GOARCH=ppc64le CGO_ENABLED=0 go test -c -buildmode=pie -ldflags=-linkmode=internal -o=purego-test-nocgo-pie .
env QEMU_LD_PREFIX=/usr/powerpc64le-linux-gnu qemu-ppc64le ./purego-test-nocgo-pie -test.shuffle=on -test.v -test.count=10
env QEMU_LD_PREFIX=/usr/powerpc64le-linux-gnu PUREGO_TEST_CALLBACK_ENTRIES_CHILD=1 qemu-ppc64le ./purego-test-nocgo-pie -test.run='^TestCallbackEntries$' -test.v
env GOOS=linux GOARCH=ppc64le CGO_ENABLED=1 go test -c -o=purego-test-cgo .
env QEMU_LD_PREFIX=/usr/powerpc64le-linux-gnu qemu-ppc64le ./purego-test-cgo -test.shuffle=on -test.v -test.count=10
env QEMU_LD_PREFIX=/usr/powerpc64le-linux-gnu PUREGO_TEST_CALLBACK_ENTRIES_CHILD=1 qemu-ppc64le ./purego-test-cgo -test.run='^TestCallbackEntries$' -test.v
go env -u CC
go env -u CXX

Expand All @@ -288,8 +301,12 @@ jobs:
sudo ln -sf /usr/riscv64-linux-gnu/lib/ld-linux-riscv64-lp64d.so.1 /lib/ld.so.1
env GOOS=linux GOARCH=riscv64 CGO_ENABLED=0 go test -c -o=purego-test-nocgo .
env QEMU_LD_PREFIX=/usr/riscv64-linux-gnu qemu-riscv64 ./purego-test-nocgo -test.shuffle=on -test.v -test.count=10
# binfmt_misc is not registered here, so TestCallbackEntries cannot re-execute
# the test binary. Run its child mode directly.
env QEMU_LD_PREFIX=/usr/riscv64-linux-gnu PUREGO_TEST_CALLBACK_ENTRIES_CHILD=1 qemu-riscv64 ./purego-test-nocgo -test.run='^TestCallbackEntries$' -test.v
env GOOS=linux GOARCH=riscv64 CGO_ENABLED=1 go test -c -o=purego-test-cgo .
env QEMU_LD_PREFIX=/usr/riscv64-linux-gnu qemu-riscv64 ./purego-test-cgo -test.shuffle=on -test.v -test.count=10
env QEMU_LD_PREFIX=/usr/riscv64-linux-gnu PUREGO_TEST_CALLBACK_ENTRIES_CHILD=1 qemu-riscv64 ./purego-test-cgo -test.run='^TestCallbackEntries$' -test.v
go env -u CC
go env -u CXX

Expand All @@ -301,9 +318,13 @@ jobs:
if [[ "${{ matrix.go }}" != 1.25.* && "${{ matrix.go }}" != 1.26.* ]]; then
env GOOS=linux GOARCH=s390x CGO_ENABLED=0 go test -c -o=purego-test-nocgo .
env QEMU_LD_PREFIX=/usr/s390x-linux-gnu qemu-s390x ./purego-test-nocgo -test.shuffle=on -test.v -test.count=10
# binfmt_misc is not registered here, so TestCallbackEntries cannot re-execute
# the test binary. Run its child mode directly.
env QEMU_LD_PREFIX=/usr/s390x-linux-gnu PUREGO_TEST_CALLBACK_ENTRIES_CHILD=1 qemu-s390x ./purego-test-nocgo -test.run='^TestCallbackEntries$' -test.v
fi
env GOOS=linux GOARCH=s390x CGO_ENABLED=1 go test -c -o=purego-test-cgo .
env QEMU_LD_PREFIX=/usr/s390x-linux-gnu qemu-s390x ./purego-test-cgo -test.shuffle=on -test.v -test.count=10
env QEMU_LD_PREFIX=/usr/s390x-linux-gnu PUREGO_TEST_CALLBACK_ENTRIES_CHILD=1 qemu-s390x ./purego-test-cgo -test.run='^TestCallbackEntries$' -test.v
go env -u CC
go env -u CXX

Expand Down
76 changes: 76 additions & 0 deletions callback_entries_test.go
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)
Comment thread
Asutorufa marked this conversation as resolved.
Resolved
}
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)
}
}
}
}
54 changes: 54 additions & 0 deletions callback_linux_arm_test.go
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)
}
}
}
19 changes: 19 additions & 0 deletions export_linux_arm_test.go
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))
}
9 changes: 9 additions & 0 deletions export_unix_test.go
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
10 changes: 5 additions & 5 deletions syscall_unix.go
Original file line number Diff line number Diff line change
Expand Up @@ -377,9 +377,7 @@ func callbackArgFromSlotBigEndian(slotPtr unsafe.Pointer, inType reflect.Type) r
// and we want callback to arrive at
// correspondent call instruction instead of start of
// runtime.callbackasm.
// On ARM, runtime.callbackasm is a series of mov and branch instructions.
// R12 is loaded with the callback index. Each entry is two instructions,
// hence 8 bytes.
// On ARM, each entry loads R12 with MOVW and ORR before branching, hence 12 bytes.
// On PPC64LE, every callback has a separate global-entry symbol so that an
// external call initializes the TOC. callbackasmAddrs stores their addresses.
func callbackasmAddr(i int) uintptr {
Expand All @@ -395,8 +393,10 @@ func callbackasmAddr(i int) uintptr {
entrySize = 10
case "ppc64le":
return callbackasmAddrs[i]
case "arm", "arm64", "loong64", "riscv64":
// On ARM, ARM64, Loong64 and RISCV64, each entry is a MOV instruction
case "arm":
entrySize = 12
case "arm64", "loong64", "riscv64":
// On ARM64, Loong64 and RISCV64, each entry is a MOV instruction
// followed by a branch instruction
entrySize = 8
case "s390x":
Expand Down
10 changes: 7 additions & 3 deletions wincallback.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,8 @@ func genasmArm() {
//go:build linux

// External code calls into callbackasm at an offset corresponding
// to the callback index. Callbackasm is a table of MOVW and B instructions.
// The MOVW instruction loads R12 with the callback index, and the
// to the callback index. Callbackasm is a table of MOVW, ORR and B instructions.
// The MOVW and ORR instructions load R12 with the callback index, and the
// B instruction branches to callbackasm1.
// callbackasm1 takes the callback index from R12 and
// indexes into an array that stores information about each callback.
Expand All @@ -91,7 +91,11 @@ func genasmArm() {
TEXT callbackasm(SB), NOSPLIT|NOFRAME, $0
`)
for i := range maxCallback {
fmt.Fprintf(&buf, "\tMOVW $%d, R12\n", i)
// Below ARMv7, MOVW cannot encode every callback index in one instruction.
// Splitting the index into two encodable immediates prevents literal
// pools from changing the fixed entry size assumed by callbackasmAddr.
fmt.Fprintf(&buf, "\tMOVW $%d, R12\n", i&0xff)
fmt.Fprintf(&buf, "\tORR $%d, R12\n", i&^0xff)
buf.WriteString("\tB callbackasm1(SB)\n")
}
if err := os.WriteFile("zcallback_arm.s", buf.Bytes(), 0644); err != nil {
Expand Down
Loading
Loading