Skip to content

fix(relinker): initialize MXCSR in macOS guest entry - #7741

Merged
adriadam10 merged 1 commit into
boykopovar:mainfrom
MarcZX10:codex/macos-main-thread-mxcsr
Oct 24, 2026
Merged

adriadam10 merged 1 commit into
boykopovar:mainfrom
MarcZX10:codex/macos-main-thread-mxcsr

Conversation

@MarcZX10

Copy link
Copy Markdown
Contributor

What

The macOS Mach-O LC_MAIN entry stub calls the bound guest-start routine without applying the measured PS5 payload MXCSR to the main guest thread. Save the host MXCSR in aligned stack storage, load guest MXCSR 0x9fc0 immediately before the start call, and restore the host value if the call returns. The guest stack pointer is captured before the temporary slot is allocated. This complements #7594's Linux and Windows main-entry paths and #7565's created-thread path.

Tested

Linux x86_64, GCC 12.2.0, relinker-only Release build. Full CTest: 210/210 passed. The macos_output test checks the emitted setup and restore instructions, the guest entry target, and the bound start slot. python3 tools/check_conventions.py --base upstream/main passed. python3 tools/pr_overlap.py --branch codex/macos-main-thread-mxcsr reported no overlap. No native macOS runtime test was run.

Checklist

@jotitadev jotitadev left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Native macOS run of the new stub, since the description has none. Apple M2 Max, macOS 27.2, Apple clang 21, head b5d55bd80 merged on main 4ffb0cd12.

  • Relinker-only Release, Python 3.9.6: ctest 201/201 on arm64 and on x86_64 under Rosetta 2. On the base relinker, macos_output with the new check fails with entry stub does not save host MXCSR and load guest MXCSR 0x9fc0, and it passes with the fix.

  • Rosetta 2 runtime: an x86_64 probe that runs the exact stub bytes (mov rsi, rsp, sub rsp, 0x10 / stmxcsr / mov dword [rsp+4], 0x9fc0 / ldmxcsr, call [rip+slot], ldmxcsr [rsp] / add rsp, 0x10) around a C callee:

    • the callee reads MXCSR 0x9fc0;
    • DAZ takes effect: a denormal f32 input times 1.0 gives 0, and the host gives 1e-39;
    • FTZ takes effect: 1.5e-38 * 0.5 gives 0;
    • after the call returns, the host MXCSR (0x1f80 plus its sticky flags) is restored.

    So Rosetta 2 accepts the DAZ bit in ldmxcsr without a fault and applies both modes.

Not tested: a full relinked title through LC_MAIN.

Written with AI assistance (Claude Code). I ran the builds and tests above on my machine.

ycarogabriel-lgtm added a commit to ycarogabriel-lgtm/AnyPS5 that referenced this pull request Oct 19, 2026
…est entry (@MarcZX10)

This branch gathers the open macOS work, and this PR is part of it: it is merged here with its author credited, so the integration carries it now and merging it first in main only shrinks this branch.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@MarcZX10

Copy link
Copy Markdown
Contributor Author

Thanks for the native macOS and Rosetta validation. The relinker-only suite passes 201/201 on both arm64 and x86_64, and the negative control fails with the expected missing guest MXCSR setup. The runtime probe also confirms the stub restores host MXCSR on return. This fills the native macOS evidence gap for the current change; I’ll keep the exact tested head and scope in the PR record.

@adriadam10 adriadam10 self-assigned this Oct 20, 2026
@MarcZX10

Copy link
Copy Markdown
Contributor Author

Manual overlap check for #8658 against #7741, using the current GitHub heads (b5d55bd804a9 and fc7d30780fac). The two PR heads merge together without a conflict. In MacOsImage.cpp, #7741 extends the EntryStub instructions and updates EntryStubStartDisplacement; #8658 changes the stubOffset calculation to reserve space after load commands. These hunks are separate and compose cleanly. #8658's current merge conflict with main is in docs/dev/debt/libkernel.md, outside this overlap. I don't see a dependency or code change needed in #7741.

@github-actions

github-actions Bot commented Oct 21, 2026 •

Copy link
Copy Markdown

Some comparisons could not be completed because a pull request conflicts with the base branch. Review these shared files manually before concluding that there is no overlap.

Pull request Shared files not compared
PR 9408 core/relinker/elfpatcher/src/macos/MacOsImage.cpp

@adriadam10
adriadam10 merged commit 50aa179 into boykopovar:main Oct 24, 2026
13 checks passed
@adriadam10

Copy link
Copy Markdown
Collaborator

Checked the stub bytes on current main (77e3121): the merge is clean, the start-slot displacement (0x84) matches the call, and the host MXCSR restore follows the call with 16-byte alignment kept. macos_output fails without the change; native macOS and Rosetta runs by @jotitadev cover the runtime; merged.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants