Repository navigation
purego: load errno as a 32-bit int on darwin - #564
Merged
Merged
Conversation
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
approved these changes
Nov 8, 2026
hajimehoshi
left a comment
Member
There was a problem hiding this comment.
LGTM
@TotallyGamerJet PTAL
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What issue is this addressing?
Closes #561
What type of issue is this addressing?
bug
What this PR does | solves
The darwin
syscallXtrampolines insys_arm64.sandsys_amd64.sloaded errno with a 64-bit load, but__error()points to anint. The upper 32 bits picked up whatever came after errno, sosyscall.Errno(err)never matchedsyscall.ENOENTand friends.Both now do a 32-bit load with sign extension (
MOVWon arm64,MOVLQSXon 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.TestErrnoonly comparedstrerror(int32(errno)), which hid the bug. It now also checkssyscall.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