Fix stack alignment and auxv for APE loader on macOS #931
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.
Hi @jart,
Through extensive testing on another project, I have found that the Cosmopolitan APE loader sometimes crashes with SIGSEGV on x86_64 macOS for two reasons: 1) the startup stack pointer isn't automatically aligned by the kernel (it varies based on passed argv/envp strings), and 2) the macOS auxv vector isn't ELF compatible; they're passed as a pointer array similar to
char **environ
, rather than an ELF-compatible twin-pointer array. Depending on the arguments passed to the loader, the stack misalignment can cause a crash very early in startup.S when calling the loader entry pointApeLoader
, or later, when the aux vector is assumed to be ELF-compatible.These fixes (with the exception of the ARM64 startup) have been tested on macOS Mojave and Catalina using the loader in another project. I am unable to test on the latest Cosmopolitan tree, as
build/bootstrap/make.com
immediately segfaults, the reason for which is almost assuredly this same problem. After this fix is incorporated and make.com updated, I will test the Cosmopolitan build and report further.Thank you!