From 1928f29840d8a1a609c7fcd348bea57bf7146d47 Mon Sep 17 00:00:00 2001 From: Chinmay Jha Date: Sun, 8 Nov 2026 11:16:09 +0530 Subject: [PATCH] purego: load errno as a 32-bit int on darwin The darwin syscallX trampolines read the int that __error() points to with a 64-bit load, so the upper half of the returned errno held whatever followed errno in memory and syscall.Errno(err) never matched a constant. Load 32 bits and sign-extend, which matches what the cgo path stores. Closes #561 --- sys_amd64.s | 2 +- sys_arm64.s | 2 +- syscall_test.go | 4 ++++ 3 files changed, 6 insertions(+), 2 deletions(-) diff --git a/sys_amd64.s b/sys_amd64.s index 9a4414bb..4ce15a35 100644 --- a/sys_amd64.s +++ b/sys_amd64.s @@ -125,7 +125,7 @@ TEXT syscallX(SB), NOSPLIT, $STACK_SIZE #ifdef GOOS_darwin CALL purego_error(SB) MOVQ PTR_ADDRESS(SP), DI // reload (DI clobbered by call) - MOVQ (AX), AX + MOVLQSX (AX), AX MOVQ AX, syscallArgs_a3(DI) // save errno #else MOVQ $0, syscallArgs_a3(DI) // no errno here: clear the input argument diff --git a/sys_arm64.s b/sys_arm64.s index e8435712..4baed650 100644 --- a/sys_arm64.s +++ b/sys_arm64.s @@ -125,7 +125,7 @@ TEXT syscallX(SB), NOSPLIT, $0 #ifdef GOOS_darwin BL purego_error(SB) MOVD PTR_ADDRESS(RSP), R2 // reload (R2 clobbered by call) - MOVD (R0), R0 + MOVW (R0), R0 MOVD R0, syscallArgs_a3(R2) // save errno #else MOVD $0, syscallArgs_a3(R2) // no errno here: clear the input argument diff --git a/syscall_test.go b/syscall_test.go index d697cc8f..ab2882e4 100644 --- a/syscall_test.go +++ b/syscall_test.go @@ -7,6 +7,7 @@ import ( "os" "path/filepath" "runtime" + "syscall" "testing" "github.com/ebitengine/purego" @@ -66,6 +67,9 @@ func TestErrno(t *testing.T) { if int32(r1) != -1 { t.Errorf("setErrno returned %d, wanted -1", r1) } + if syscall.Errno(errno) != syscall.ENOENT { + t.Errorf("SyscallN returned errno %#x, wanted %#x", errno, uintptr(syscall.ENOENT)) + } libcName, err := getSystemLibrary() if err != nil {