From e8efbe9c43d8d53b5295e6651d0b3bd04b81d9a5 Mon Sep 17 00:00:00 2001 From: AnyCPU Date: Tue, 17 Nov 2026 00:48:18 +0200 Subject: [PATCH 1/4] internal/fakecgo: reserve threadentry's spill slot on amd64 --- internal/fakecgo/threadentry_amd64.go | 6 ++ internal/fakecgo/threadentry_amd64_test.go | 70 ++++++++++++++++++++++ internal/fakecgo/trampolines_amd64.s | 15 +++++ 3 files changed, 91 insertions(+) create mode 100644 internal/fakecgo/threadentry_amd64_test.go diff --git a/internal/fakecgo/threadentry_amd64.go b/internal/fakecgo/threadentry_amd64.go index 1bbf9292..8e6b5309 100644 --- a/internal/fakecgo/threadentry_amd64.go +++ b/internal/fakecgo/threadentry_amd64.go @@ -13,3 +13,9 @@ package fakecgo //go:nosplit //go:norace func callThreadEntryFn(fn uintptr) + +// callThreadEntryTrampoline calls threadentry_trampoline like a C caller that +// passes arg in DI and keeps bx in BX across the call. It returns BX as the +// trampoline leaves it. +// It is only used by tests. Implemented in trampolines_amd64.s. +func callThreadEntryTrampoline(arg, bx uintptr) uintptr diff --git a/internal/fakecgo/threadentry_amd64_test.go b/internal/fakecgo/threadentry_amd64_test.go new file mode 100644 index 00000000..d101763a --- /dev/null +++ b/internal/fakecgo/threadentry_amd64_test.go @@ -0,0 +1,70 @@ +// SPDX-License-Identifier: Apache-2.0 +// SPDX-FileCopyrightText: 2026 The Ebitengine Authors + +//go:build !cgo && amd64 && (darwin || freebsd || illumos || linux || netbsd) + +package fakecgo + +import ( + "testing" + "unsafe" +) + +// spillProbeArg marks the call made by TestThreadEntryTrampolinePreservesBX. +var spillProbeArg byte + +// spillProbeAbove is the word just above spillProbe's spill slot. +var spillProbeAbove uintptr + +// spillProbe stands in for threadentry during the test. Taking the address of +// v makes even an optimized build store v in its spill slot, as threadentry +// does when built with -N. Threads the runtime starts in the meantime still +// get the real threadentry. They have no g yet, which is why spillProbe is +// nosplit and norace like threadentry. +// +//go:nosplit +//go:norace +func spillProbe(v unsafe.Pointer) unsafe.Pointer { + if v != unsafe.Pointer(&spillProbeArg) { + return threadentry(v) + } + readAboveSlot(&v) + return nil +} + +// readAboveSlot records the word just above *p in spillProbeAbove. It must be +// nosplit: otherwise it could be preempted for a stack scan, which cannot +// unwind through threadentry_trampoline and would crash the test binary. It is +// noinline, which makes spillProbe store v before the call. +// +//go:nosplit +//go:norace +//go:noinline +func readAboveSlot(p *unsafe.Pointer) { + spillProbeAbove = *(*uintptr)(unsafe.Add(unsafe.Pointer(p), unsafe.Sizeof(*p))) +} + +// TestThreadEntryTrampolinePreservesBX checks that threadentry_trampoline +// reserves the spill slot of threadentry's register argument. Without the +// reservation, the slot is where the trampoline saved BX for its C caller, and on darwin/amd64 +// _pthread_start reads the thread's pthread_t from BX after threadentry +// returns. +func TestThreadEntryTrampolinePreservesBX(t *testing.T) { + const bx uintptr = 0x0123456789abcdef + arg := uintptr(unsafe.Pointer(&spillProbeArg)) + + old := threadentry_call + threadentry_call = spillProbe + defer func() { threadentry_call = old }() + + spillProbeAbove = 0 + if got := callThreadEntryTrampoline(arg, bx); got != bx { + t.Fatalf("BX = %#x after threadentry_trampoline, want %#x (the argument was %#x)", got, bx, arg) + } + // The saved BX sits just above the reserved slot. If it isn't there, v is + // not in that slot, because the compiler or the trampoline's layout changed, + // and this test checks nothing. + if spillProbeAbove != bx { + t.Fatalf("word above the spill slot = %#x, want the saved BX %#x: v is not in the reserved slot (the compiler or the trampoline's layout changed)", spillProbeAbove, bx) + } +} diff --git a/internal/fakecgo/trampolines_amd64.s b/internal/fakecgo/trampolines_amd64.s index 98a362ef..71993b15 100644 --- a/internal/fakecgo/trampolines_amd64.s +++ b/internal/fakecgo/trampolines_amd64.s @@ -78,7 +78,13 @@ TEXT threadentry_trampoline(SB), NOSPLIT, $0 MOVQ DI, AX MOVQ ·threadentry_call(SB), R11 MOVQ (R11), R11 + // Reserve the spill slot for threadentry's register argument, as + // runtime·sigtramp does: threadentry is ABIInternal, which leaves that to + // the caller. Otherwise the slot is 0(SP), where PUSH_REGS_HOST_TO_ABI0 + // saved BX, and threadentry built with -N overwrites it with v. + ADJSP $8 CALL R11 + ADJSP $-8 POP_REGS_HOST_TO_ABI0() RET @@ -100,6 +106,15 @@ TEXT ·callThreadEntryFn(SB), NOSPLIT, $0-8 POP_REGS_HOST_TO_ABI0() RET +// func callThreadEntryTrampoline(arg, bx uintptr) uintptr +// Test helper; see threadentry_amd64_test.go. +TEXT ·callThreadEntryTrampoline(SB), NOSPLIT, $0-24 + MOVQ arg+0(FP), DI + MOVQ bx+8(FP), BX + CALL threadentry_trampoline(SB) + MOVQ BX, ret+16(FP) + RET + TEXT ·call5(SB), NOSPLIT, $0-56 MOVQ fn+0(FP), R11 MOVQ a1+8(FP), DI From 9052a0696939958147450c81ce9f7093cba556cb Mon Sep 17 00:00:00 2001 From: AnyCPU Date: Tue, 17 Nov 2026 06:48:27 +0000 Subject: [PATCH 2/4] internal/fakecgo: apply review suggestions --- internal/fakecgo/threadentry_amd64.go | 2 +- internal/fakecgo/threadentry_amd64_test.go | 10 +++++----- internal/fakecgo/trampolines_amd64.s | 7 +++---- 3 files changed, 9 insertions(+), 10 deletions(-) diff --git a/internal/fakecgo/threadentry_amd64.go b/internal/fakecgo/threadentry_amd64.go index 8e6b5309..8e2b492c 100644 --- a/internal/fakecgo/threadentry_amd64.go +++ b/internal/fakecgo/threadentry_amd64.go @@ -1,7 +1,7 @@ // SPDX-License-Identifier: Apache-2.0 // SPDX-FileCopyrightText: 2026 The Ebitengine Authors -//go:build !cgo && amd64 && (darwin || freebsd || illumos || linux || netbsd) +//go:build !cgo && (darwin || freebsd || illumos || linux || netbsd) package fakecgo diff --git a/internal/fakecgo/threadentry_amd64_test.go b/internal/fakecgo/threadentry_amd64_test.go index d101763a..8dbbca34 100644 --- a/internal/fakecgo/threadentry_amd64_test.go +++ b/internal/fakecgo/threadentry_amd64_test.go @@ -1,7 +1,7 @@ // SPDX-License-Identifier: Apache-2.0 // SPDX-FileCopyrightText: 2026 The Ebitengine Authors -//go:build !cgo && amd64 && (darwin || freebsd || illumos || linux || netbsd) +//go:build !cgo && (darwin || freebsd || illumos || linux || netbsd) package fakecgo @@ -46,9 +46,9 @@ func readAboveSlot(p *unsafe.Pointer) { // TestThreadEntryTrampolinePreservesBX checks that threadentry_trampoline // reserves the spill slot of threadentry's register argument. Without the -// reservation, the slot is where the trampoline saved BX for its C caller, and on darwin/amd64 -// _pthread_start reads the thread's pthread_t from BX after threadentry -// returns. +// reservation, the slot is where the trampoline saved BX for its C caller, +// and on darwin/amd64 _pthread_start reads the thread's pthread_t from BX +// after threadentry returns. func TestThreadEntryTrampolinePreservesBX(t *testing.T) { const bx uintptr = 0x0123456789abcdef arg := uintptr(unsafe.Pointer(&spillProbeArg)) @@ -65,6 +65,6 @@ func TestThreadEntryTrampolinePreservesBX(t *testing.T) { // not in that slot, because the compiler or the trampoline's layout changed, // and this test checks nothing. if spillProbeAbove != bx { - t.Fatalf("word above the spill slot = %#x, want the saved BX %#x: v is not in the reserved slot (the compiler or the trampoline's layout changed)", spillProbeAbove, bx) + t.Errorf("word above the spill slot = %#x, want the saved BX %#x: v is not in the reserved slot (the compiler or the trampoline's layout changed)", spillProbeAbove, bx) } } diff --git a/internal/fakecgo/trampolines_amd64.s b/internal/fakecgo/trampolines_amd64.s index 71993b15..e94cfed6 100644 --- a/internal/fakecgo/trampolines_amd64.s +++ b/internal/fakecgo/trampolines_amd64.s @@ -78,10 +78,9 @@ TEXT threadentry_trampoline(SB), NOSPLIT, $0 MOVQ DI, AX MOVQ ·threadentry_call(SB), R11 MOVQ (R11), R11 - // Reserve the spill slot for threadentry's register argument, as - // runtime·sigtramp does: threadentry is ABIInternal, which leaves that to - // the caller. Otherwise the slot is 0(SP), where PUSH_REGS_HOST_TO_ABI0 - // saved BX, and threadentry built with -N overwrites it with v. + // threadentry is ABIInternal, so the caller reserves the spill slot for + // its register argument, as runtime·sigtramp does. The slot must not + // overlap the registers saved by PUSH_REGS_HOST_TO_ABI0. ADJSP $8 CALL R11 ADJSP $-8 From 9452666d3ca9bfffe634fe1d2f6817c48b711b79 Mon Sep 17 00:00:00 2001 From: AnyCPU Date: Mon, 23 Nov 2026 18:20:17 +0200 Subject: [PATCH 3/4] internal/fakecgo: remove threadentry spill slot test --- internal/fakecgo/threadentry_amd64.go | 8 +-- internal/fakecgo/threadentry_amd64_test.go | 70 ---------------------- internal/fakecgo/trampolines_amd64.s | 9 --- 3 files changed, 1 insertion(+), 86 deletions(-) delete mode 100644 internal/fakecgo/threadentry_amd64_test.go diff --git a/internal/fakecgo/threadentry_amd64.go b/internal/fakecgo/threadentry_amd64.go index 8e2b492c..1bbf9292 100644 --- a/internal/fakecgo/threadentry_amd64.go +++ b/internal/fakecgo/threadentry_amd64.go @@ -1,7 +1,7 @@ // SPDX-License-Identifier: Apache-2.0 // SPDX-FileCopyrightText: 2026 The Ebitengine Authors -//go:build !cgo && (darwin || freebsd || illumos || linux || netbsd) +//go:build !cgo && amd64 && (darwin || freebsd || illumos || linux || netbsd) package fakecgo @@ -13,9 +13,3 @@ package fakecgo //go:nosplit //go:norace func callThreadEntryFn(fn uintptr) - -// callThreadEntryTrampoline calls threadentry_trampoline like a C caller that -// passes arg in DI and keeps bx in BX across the call. It returns BX as the -// trampoline leaves it. -// It is only used by tests. Implemented in trampolines_amd64.s. -func callThreadEntryTrampoline(arg, bx uintptr) uintptr diff --git a/internal/fakecgo/threadentry_amd64_test.go b/internal/fakecgo/threadentry_amd64_test.go deleted file mode 100644 index 8dbbca34..00000000 --- a/internal/fakecgo/threadentry_amd64_test.go +++ /dev/null @@ -1,70 +0,0 @@ -// SPDX-License-Identifier: Apache-2.0 -// SPDX-FileCopyrightText: 2026 The Ebitengine Authors - -//go:build !cgo && (darwin || freebsd || illumos || linux || netbsd) - -package fakecgo - -import ( - "testing" - "unsafe" -) - -// spillProbeArg marks the call made by TestThreadEntryTrampolinePreservesBX. -var spillProbeArg byte - -// spillProbeAbove is the word just above spillProbe's spill slot. -var spillProbeAbove uintptr - -// spillProbe stands in for threadentry during the test. Taking the address of -// v makes even an optimized build store v in its spill slot, as threadentry -// does when built with -N. Threads the runtime starts in the meantime still -// get the real threadentry. They have no g yet, which is why spillProbe is -// nosplit and norace like threadentry. -// -//go:nosplit -//go:norace -func spillProbe(v unsafe.Pointer) unsafe.Pointer { - if v != unsafe.Pointer(&spillProbeArg) { - return threadentry(v) - } - readAboveSlot(&v) - return nil -} - -// readAboveSlot records the word just above *p in spillProbeAbove. It must be -// nosplit: otherwise it could be preempted for a stack scan, which cannot -// unwind through threadentry_trampoline and would crash the test binary. It is -// noinline, which makes spillProbe store v before the call. -// -//go:nosplit -//go:norace -//go:noinline -func readAboveSlot(p *unsafe.Pointer) { - spillProbeAbove = *(*uintptr)(unsafe.Add(unsafe.Pointer(p), unsafe.Sizeof(*p))) -} - -// TestThreadEntryTrampolinePreservesBX checks that threadentry_trampoline -// reserves the spill slot of threadentry's register argument. Without the -// reservation, the slot is where the trampoline saved BX for its C caller, -// and on darwin/amd64 _pthread_start reads the thread's pthread_t from BX -// after threadentry returns. -func TestThreadEntryTrampolinePreservesBX(t *testing.T) { - const bx uintptr = 0x0123456789abcdef - arg := uintptr(unsafe.Pointer(&spillProbeArg)) - - old := threadentry_call - threadentry_call = spillProbe - defer func() { threadentry_call = old }() - - spillProbeAbove = 0 - if got := callThreadEntryTrampoline(arg, bx); got != bx { - t.Fatalf("BX = %#x after threadentry_trampoline, want %#x (the argument was %#x)", got, bx, arg) - } - // The saved BX sits just above the reserved slot. If it isn't there, v is - // not in that slot, because the compiler or the trampoline's layout changed, - // and this test checks nothing. - if spillProbeAbove != bx { - t.Errorf("word above the spill slot = %#x, want the saved BX %#x: v is not in the reserved slot (the compiler or the trampoline's layout changed)", spillProbeAbove, bx) - } -} diff --git a/internal/fakecgo/trampolines_amd64.s b/internal/fakecgo/trampolines_amd64.s index e94cfed6..8b85a9bd 100644 --- a/internal/fakecgo/trampolines_amd64.s +++ b/internal/fakecgo/trampolines_amd64.s @@ -105,15 +105,6 @@ TEXT ·callThreadEntryFn(SB), NOSPLIT, $0-8 POP_REGS_HOST_TO_ABI0() RET -// func callThreadEntryTrampoline(arg, bx uintptr) uintptr -// Test helper; see threadentry_amd64_test.go. -TEXT ·callThreadEntryTrampoline(SB), NOSPLIT, $0-24 - MOVQ arg+0(FP), DI - MOVQ bx+8(FP), BX - CALL threadentry_trampoline(SB) - MOVQ BX, ret+16(FP) - RET - TEXT ·call5(SB), NOSPLIT, $0-56 MOVQ fn+0(FP), R11 MOVQ a1+8(FP), DI From 16a01e99c9ca8c3944218cb7aac5ae1c078ea2d1 Mon Sep 17 00:00:00 2001 From: AnyCPU Date: Mon, 23 Nov 2026 16:31:19 +0000 Subject: [PATCH 4/4] internal/fakecgo: simplify build tag --- internal/fakecgo/threadentry_amd64.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/internal/fakecgo/threadentry_amd64.go b/internal/fakecgo/threadentry_amd64.go index 1bbf9292..0e9868f2 100644 --- a/internal/fakecgo/threadentry_amd64.go +++ b/internal/fakecgo/threadentry_amd64.go @@ -1,7 +1,7 @@ // SPDX-License-Identifier: Apache-2.0 // SPDX-FileCopyrightText: 2026 The Ebitengine Authors -//go:build !cgo && amd64 && (darwin || freebsd || illumos || linux || netbsd) +//go:build !cgo && (darwin || freebsd || illumos || linux || netbsd) package fakecgo