From 60947c8d3299044c6ba3b100a6ef24a00706ba10 Mon Sep 17 00:00:00 2001 From: AnyCPU Date: Tue, 17 Nov 2026 08:22:46 +0000 Subject: [PATCH 1/2] internal/fakecgo: reserve threadentry's result slot on arm --- .github/workflows/test.yml | 4 ++ internal/fakecgo/threadentry_arm.go | 12 ++++ internal/fakecgo/threadentry_arm_test.go | 52 ++++++++++++++++ internal/fakecgo/trampolines_arm.s | 77 ++++++++++++++---------- 4 files changed, 112 insertions(+), 33 deletions(-) create mode 100644 internal/fakecgo/threadentry_arm.go create mode 100644 internal/fakecgo/threadentry_arm_test.go diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 825df892..5c77c71b 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -185,6 +185,8 @@ jobs: go env -w CXX=arm-linux-gnueabihf-g++ env GOOS=linux GOARCH=arm CGO_ENABLED=0 go test -c -o=purego-test-nocgo . env QEMU_LD_PREFIX=/usr/arm-linux-gnueabihf qemu-arm ./purego-test-nocgo -test.shuffle=on -test.v -test.count=10 + env GOOS=linux GOARCH=arm CGO_ENABLED=0 go test -c -o=fakecgo-test-nocgo ./internal/fakecgo + env QEMU_LD_PREFIX=/usr/arm-linux-gnueabihf qemu-arm ./fakecgo-test-nocgo -test.shuffle=on -test.v -test.count=10 env GOOS=linux GOARCH=arm CGO_ENABLED=1 go test -c -o=purego-test-cgo . env QEMU_LD_PREFIX=/usr/arm-linux-gnueabihf qemu-arm ./purego-test-cgo -test.shuffle=on -test.v -test.count=10 go env -u CC @@ -196,6 +198,8 @@ jobs: go env -w CXX=arm-linux-gnueabi-g++ env GOOS=linux GOARCH=arm GOARM=5 CGO_ENABLED=0 go test -c -o=purego-test-nocgo . env QEMU_LD_PREFIX=/usr/arm-linux-gnueabi qemu-arm ./purego-test-nocgo -test.shuffle=on -test.v -test.count=10 + env GOOS=linux GOARCH=arm GOARM=5 CGO_ENABLED=0 go test -c -o=fakecgo-test-nocgo ./internal/fakecgo + env QEMU_LD_PREFIX=/usr/arm-linux-gnueabi qemu-arm ./fakecgo-test-nocgo -test.shuffle=on -test.v -test.count=10 env GOOS=linux GOARCH=arm GOARM=5 CGO_ENABLED=1 go test -c -o=purego-test-cgo . env QEMU_LD_PREFIX=/usr/arm-linux-gnueabi qemu-arm ./purego-test-cgo -test.shuffle=on -test.v -test.count=10 go env -u CC diff --git a/internal/fakecgo/threadentry_arm.go b/internal/fakecgo/threadentry_arm.go new file mode 100644 index 00000000..78ffbe34 --- /dev/null +++ b/internal/fakecgo/threadentry_arm.go @@ -0,0 +1,12 @@ +// SPDX-License-Identifier: Apache-2.0 +// SPDX-FileCopyrightText: 2026 The Ebitengine Authors + +//go:build !cgo && (freebsd || linux) + +package fakecgo + +// callThreadEntryTrampoline calls threadentry_trampoline like a C caller that +// passes arg in R0 and keeps r4 in R4 across the call. It returns R4 as the +// trampoline leaves it. +// It is only used by tests. Implemented in trampolines_arm.s. +func callThreadEntryTrampoline(arg, r4 uintptr) uintptr diff --git a/internal/fakecgo/threadentry_arm_test.go b/internal/fakecgo/threadentry_arm_test.go new file mode 100644 index 00000000..fa5d0524 --- /dev/null +++ b/internal/fakecgo/threadentry_arm_test.go @@ -0,0 +1,52 @@ +// SPDX-License-Identifier: Apache-2.0 +// SPDX-FileCopyrightText: 2026 The Ebitengine Authors + +//go:build !cgo && linux + +package fakecgo + +import ( + "testing" + "unsafe" +) + +// On 32-bit ELF the internal linker records a needed library only for a _ _ +// import, not for the library named with each symbol in zsymbols_linux.go. A +// test binary of fakecgo alone then has no DT_NEEDED and fails to start, which +// is why threadentry_test.go skips arm. Naming libc here lets this one start. +// +//go:cgo_import_dynamic _ _ "libc.so.6" + +// resultProbeArg marks the call made by TestThreadEntryTrampolinePreservesR4. +var resultProbeArg byte + +// resultProbe stands in for threadentry during the test and, like it, returns +// nil, which ABI0 stores in its result slot. Threads the runtime starts in the +// meantime still get the real threadentry. They have no g yet, which is why +// resultProbe is nosplit and norace like threadentry. +// +//go:nosplit +//go:norace +func resultProbe(v unsafe.Pointer) unsafe.Pointer { + if v != unsafe.Pointer(&resultProbeArg) { + return threadentry(v) + } + return nil +} + +// TestThreadEntryTrampolinePreservesR4 checks that threadentry_trampoline +// keeps the C caller's R4, which AAPCS makes callee-saved, out of the result +// slot of threadentry. threadentry is ABI0, so its argument is at 4(R13) and +// its result at 8(R13) of the trampoline's frame. +func TestThreadEntryTrampolinePreservesR4(t *testing.T) { + const r4 uintptr = 0x89abcdef + arg := uintptr(unsafe.Pointer(&resultProbeArg)) + + old := threadentry_call + threadentry_call = resultProbe + defer func() { threadentry_call = old }() + + if got := callThreadEntryTrampoline(arg, r4); got != r4 { + t.Errorf("R4 = %#x after threadentry_trampoline, want %#x", got, r4) + } +} diff --git a/internal/fakecgo/trampolines_arm.s b/internal/fakecgo/trampolines_arm.s index 0aa43ee0..483b4300 100644 --- a/internal/fakecgo/trampolines_arm.s +++ b/internal/fakecgo/trampolines_arm.s @@ -54,34 +54,36 @@ TEXT ·setg_trampoline(SB), NOSPLIT, $0-8 BL (R12) RET -TEXT threadentry_trampoline(SB), NOSPLIT, $104-0 +TEXT threadentry_trampoline(SB), NOSPLIT, $108-0 // Save C callee-saved registers at C-to-Go boundary. // See crosscall2 in asm_arm.s. // ARM AAPCS callee-saved: R4-R11 (includes g=R10), D8-D15. // LR is saved/restored by the Go-managed frame prologue/epilogue. + // threadentry is ABI0: its argument is at 4(R13) and its result at + // 8(R13), so the saved registers start at 12(R13). MOVW R0, 4(R13) // arg for threadentry_call - MOVW R4, 8(R13) - MOVW R5, 12(R13) - MOVW R6, 16(R13) - MOVW R7, 20(R13) - MOVW R8, 24(R13) - MOVW R9, 28(R13) - MOVW g, 32(R13) // R10 - MOVW R11, 36(R13) + MOVW R4, 12(R13) + MOVW R5, 16(R13) + MOVW R6, 20(R13) + MOVW R7, 24(R13) + MOVW R8, 28(R13) + MOVW R9, 32(R13) + MOVW g, 36(R13) // R10 + MOVW R11, 40(R13) // Skip floating point registers if runtime.goarmsoftfp!=0. MOVB runtime·goarmsoftfp(SB), R11 CMP $0, R11 BNE skipfpsave - MOVD F8, 40(R13) - MOVD F9, 48(R13) - MOVD F10, 56(R13) - MOVD F11, 64(R13) - MOVD F12, 72(R13) - MOVD F13, 80(R13) - MOVD F14, 88(R13) - MOVD F15, 96(R13) + MOVD F8, 48(R13) + MOVD F9, 56(R13) + MOVD F10, 64(R13) + MOVD F11, 72(R13) + MOVD F12, 80(R13) + MOVD F13, 88(R13) + MOVD F14, 96(R13) + MOVD F15, 104(R13) skipfpsave: MOVW ·threadentry_call(SB), R12 @@ -91,27 +93,36 @@ skipfpsave: MOVB runtime·goarmsoftfp(SB), R11 CMP $0, R11 BNE skipfprest - MOVD 40(R13), F8 - MOVD 48(R13), F9 - MOVD 56(R13), F10 - MOVD 64(R13), F11 - MOVD 72(R13), F12 - MOVD 80(R13), F13 - MOVD 88(R13), F14 - MOVD 96(R13), F15 + MOVD 48(R13), F8 + MOVD 56(R13), F9 + MOVD 64(R13), F10 + MOVD 72(R13), F11 + MOVD 80(R13), F12 + MOVD 88(R13), F13 + MOVD 96(R13), F14 + MOVD 104(R13), F15 skipfprest: - MOVW 8(R13), R4 - MOVW 12(R13), R5 - MOVW 16(R13), R6 - MOVW 20(R13), R7 - MOVW 24(R13), R8 - MOVW 28(R13), R9 - MOVW 32(R13), g - MOVW 36(R13), R11 + MOVW 12(R13), R4 + MOVW 16(R13), R5 + MOVW 20(R13), R6 + MOVW 24(R13), R7 + MOVW 28(R13), R8 + MOVW 32(R13), R9 + MOVW 36(R13), g + MOVW 40(R13), R11 RET +// func callThreadEntryTrampoline(arg, r4 uintptr) uintptr +// Test helper; see threadentry_arm_test.go. +TEXT ·callThreadEntryTrampoline(SB), NOSPLIT, $0-12 + MOVW arg+0(FP), R0 + MOVW r4+4(FP), R4 + CALL threadentry_trampoline(SB) + MOVW R4, ret+8(FP) + RET + TEXT ·call5(SB), NOSPLIT, $0-28 MOVW fn+0(FP), R12 MOVW a1+4(FP), R0 From 263e0c4a991bc1b670309004f43478f0acc04825 Mon Sep 17 00:00:00 2001 From: AnyCPU Date: Mon, 23 Nov 2026 19:01:03 +0000 Subject: [PATCH 2/2] internal/fakecgo: remove threadentry spill slot test --- .github/workflows/test.yml | 4 -- internal/fakecgo/threadentry_arm.go | 12 ------ internal/fakecgo/threadentry_arm_test.go | 52 ------------------------ internal/fakecgo/trampolines_arm.s | 14 ++----- 4 files changed, 3 insertions(+), 79 deletions(-) delete mode 100644 internal/fakecgo/threadentry_arm.go delete mode 100644 internal/fakecgo/threadentry_arm_test.go diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 5c77c71b..825df892 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -185,8 +185,6 @@ jobs: go env -w CXX=arm-linux-gnueabihf-g++ env GOOS=linux GOARCH=arm CGO_ENABLED=0 go test -c -o=purego-test-nocgo . env QEMU_LD_PREFIX=/usr/arm-linux-gnueabihf qemu-arm ./purego-test-nocgo -test.shuffle=on -test.v -test.count=10 - env GOOS=linux GOARCH=arm CGO_ENABLED=0 go test -c -o=fakecgo-test-nocgo ./internal/fakecgo - env QEMU_LD_PREFIX=/usr/arm-linux-gnueabihf qemu-arm ./fakecgo-test-nocgo -test.shuffle=on -test.v -test.count=10 env GOOS=linux GOARCH=arm CGO_ENABLED=1 go test -c -o=purego-test-cgo . env QEMU_LD_PREFIX=/usr/arm-linux-gnueabihf qemu-arm ./purego-test-cgo -test.shuffle=on -test.v -test.count=10 go env -u CC @@ -198,8 +196,6 @@ jobs: go env -w CXX=arm-linux-gnueabi-g++ env GOOS=linux GOARCH=arm GOARM=5 CGO_ENABLED=0 go test -c -o=purego-test-nocgo . env QEMU_LD_PREFIX=/usr/arm-linux-gnueabi qemu-arm ./purego-test-nocgo -test.shuffle=on -test.v -test.count=10 - env GOOS=linux GOARCH=arm GOARM=5 CGO_ENABLED=0 go test -c -o=fakecgo-test-nocgo ./internal/fakecgo - env QEMU_LD_PREFIX=/usr/arm-linux-gnueabi qemu-arm ./fakecgo-test-nocgo -test.shuffle=on -test.v -test.count=10 env GOOS=linux GOARCH=arm GOARM=5 CGO_ENABLED=1 go test -c -o=purego-test-cgo . env QEMU_LD_PREFIX=/usr/arm-linux-gnueabi qemu-arm ./purego-test-cgo -test.shuffle=on -test.v -test.count=10 go env -u CC diff --git a/internal/fakecgo/threadentry_arm.go b/internal/fakecgo/threadentry_arm.go deleted file mode 100644 index 78ffbe34..00000000 --- a/internal/fakecgo/threadentry_arm.go +++ /dev/null @@ -1,12 +0,0 @@ -// SPDX-License-Identifier: Apache-2.0 -// SPDX-FileCopyrightText: 2026 The Ebitengine Authors - -//go:build !cgo && (freebsd || linux) - -package fakecgo - -// callThreadEntryTrampoline calls threadentry_trampoline like a C caller that -// passes arg in R0 and keeps r4 in R4 across the call. It returns R4 as the -// trampoline leaves it. -// It is only used by tests. Implemented in trampolines_arm.s. -func callThreadEntryTrampoline(arg, r4 uintptr) uintptr diff --git a/internal/fakecgo/threadentry_arm_test.go b/internal/fakecgo/threadentry_arm_test.go deleted file mode 100644 index fa5d0524..00000000 --- a/internal/fakecgo/threadentry_arm_test.go +++ /dev/null @@ -1,52 +0,0 @@ -// SPDX-License-Identifier: Apache-2.0 -// SPDX-FileCopyrightText: 2026 The Ebitengine Authors - -//go:build !cgo && linux - -package fakecgo - -import ( - "testing" - "unsafe" -) - -// On 32-bit ELF the internal linker records a needed library only for a _ _ -// import, not for the library named with each symbol in zsymbols_linux.go. A -// test binary of fakecgo alone then has no DT_NEEDED and fails to start, which -// is why threadentry_test.go skips arm. Naming libc here lets this one start. -// -//go:cgo_import_dynamic _ _ "libc.so.6" - -// resultProbeArg marks the call made by TestThreadEntryTrampolinePreservesR4. -var resultProbeArg byte - -// resultProbe stands in for threadentry during the test and, like it, returns -// nil, which ABI0 stores in its result slot. Threads the runtime starts in the -// meantime still get the real threadentry. They have no g yet, which is why -// resultProbe is nosplit and norace like threadentry. -// -//go:nosplit -//go:norace -func resultProbe(v unsafe.Pointer) unsafe.Pointer { - if v != unsafe.Pointer(&resultProbeArg) { - return threadentry(v) - } - return nil -} - -// TestThreadEntryTrampolinePreservesR4 checks that threadentry_trampoline -// keeps the C caller's R4, which AAPCS makes callee-saved, out of the result -// slot of threadentry. threadentry is ABI0, so its argument is at 4(R13) and -// its result at 8(R13) of the trampoline's frame. -func TestThreadEntryTrampolinePreservesR4(t *testing.T) { - const r4 uintptr = 0x89abcdef - arg := uintptr(unsafe.Pointer(&resultProbeArg)) - - old := threadentry_call - threadentry_call = resultProbe - defer func() { threadentry_call = old }() - - if got := callThreadEntryTrampoline(arg, r4); got != r4 { - t.Errorf("R4 = %#x after threadentry_trampoline, want %#x", got, r4) - } -} diff --git a/internal/fakecgo/trampolines_arm.s b/internal/fakecgo/trampolines_arm.s index 483b4300..06c514f5 100644 --- a/internal/fakecgo/trampolines_arm.s +++ b/internal/fakecgo/trampolines_arm.s @@ -59,8 +59,9 @@ TEXT threadentry_trampoline(SB), NOSPLIT, $108-0 // See crosscall2 in asm_arm.s. // ARM AAPCS callee-saved: R4-R11 (includes g=R10), D8-D15. // LR is saved/restored by the Go-managed frame prologue/epilogue. - // threadentry is ABI0: its argument is at 4(R13) and its result at - // 8(R13), so the saved registers start at 12(R13). + // Go's arm calling convention is stack-based: threadentry's argument is + // at 4(R13) and its result at 8(R13), so the saved registers start at + // 12(R13). 44(R13) is padding that keeps the MOVD slots 8-byte aligned. MOVW R0, 4(R13) // arg for threadentry_call MOVW R4, 12(R13) @@ -114,15 +115,6 @@ skipfprest: RET -// func callThreadEntryTrampoline(arg, r4 uintptr) uintptr -// Test helper; see threadentry_arm_test.go. -TEXT ·callThreadEntryTrampoline(SB), NOSPLIT, $0-12 - MOVW arg+0(FP), R0 - MOVW r4+4(FP), R4 - CALL threadentry_trampoline(SB) - MOVW R4, ret+8(FP) - RET - TEXT ·call5(SB), NOSPLIT, $0-28 MOVW fn+0(FP), R12 MOVW a1+4(FP), R0