Skip to content

purego: load errno as a 32-bit int on darwin - #564

Merged
hajimehoshi merged 1 commit into
ebitengine:mainfrom
chinmayajha:darwin-errno-32bit
Nov 10, 2026
Merged

hajimehoshi merged 1 commit into
ebitengine:mainfrom
chinmayajha:darwin-errno-32bit

Conversation

@chinmayajha

Copy link
Copy Markdown
Contributor

What issue is this addressing?

Closes #561

What type of issue is this addressing?

bug

What this PR does | solves

The darwin syscallX trampolines in sys_arm64.s and sys_amd64.s loaded errno with a 64-bit load, but __error() points to an int. The upper 32 bits picked up whatever came after errno, so syscall.Errno(err) never matched syscall.ENOENT and friends.

Both now do a 32-bit load with sign extension (MOVW on arm64, MOVLQSX on amd64). I went with sign extension rather than the zero extension suggested in the issue so the value is the same as what the cgo path stores (args->a3 = errno). errno is never negative, so in practice both give the same result.

TestErrno only compared strerror(int32(errno)), which hid the bug. It now also checks syscall.Errno(errno) == syscall.ENOENT. On darwin/arm64 that check fails without the asm change (errno 0x6dddc00000000002, wanted 0x2) and passes with it, with and without cgo. I also ran it for darwin/amd64 under Rosetta.

cc @hajimehoshi

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 ebitengine#561

@hajimehoshi hajimehoshi left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@TotallyGamerJet PTAL

@TotallyGamerJet TotallyGamerJet left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@hajimehoshi
hajimehoshi merged commit cfb65d5 into ebitengine:main Nov 10, 2026
27 checks passed
@chinmayajha
chinmayajha deleted the darwin-errno-32bit branch November 10, 2026 17:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

SyscallN on darwin/arm64 returns errno with garbage in the upper 32 bits

3 participants