Skip to content

fix(relinker): trap guest GS accesses in macOS output instead of refusing the module - #6458

Merged
adriadam10 merged 5 commits into
boykopovar:mainfrom
DavidSamir:fix/relinker-trap-macos-guest-gs
Oct 19, 2026
Merged

adriadam10 merged 5 commits into
boykopovar:mainfrom
DavidSamir:fix/relinker-trap-macos-guest-gs

Conversation

@DavidSamir

Copy link
Copy Markdown
Contributor

What

Prospero Win (PPSA99995, the catalog's Wine port) cannot be converted for macOS. As jotitadev measured on an M2 Max in #6102, the macOS writer refuses its Wine ntdll the way the Windows writer did:

FAIL: Unsupported macOS guest TLS instruction (bytes: 65 48 8b 0c 25 78 03 00 00) (offset 0x3214c)

ntdll has six instructions that address memory through GS (mov %gs:0x378,%rcx ×3, mov %gs:0x30,%r13 ×2, mov %gs:0x330,%r10). On x86-64 macOS, under Rosetta too, the host owns GS: it holds the pthread TSD base, which the writer's own TLS stubs read (65 48 8b 04 c5 in the stubs). So the scan in MacOsImage.cpp cannot translate a GS access the way it rewrites FS accesses, and it refuses the whole module.

This is the macOS half of #6102, as a follow-up PR: the scan now records each GS-prefixed instruction, and the writer overwrites it in place with ud2 and NOP padding of its own length before the sections become segments. The module converts, and an instruction that is reached faults. Only a GS-prefixed decoding at an instruction start is trapped: one that starts inside an earlier collected instruction is skipped first, as #6410 does (and as the Windows rewriter does), so bytes inside another instruction, such as OpenSSL's CRYPTOGAMS by <appro@openssl.org> string in 24 catalog titles, are never overwritten. A GS instruction that is also an AMD-only trampoline site is refused, as a TLS access there already is. FS accesses are unchanged. The change is independent of #6102.

This branch is stacked on #6410, so its first three commits are #6410's; this PR adds four:

  1. fix(relinker): trap guest GS accesses in macOS output instead of refusing the module. The macos_tls_overlap test from fix(relinker): skip prefixed decodings inside an earlier instruction in the macOS TLS scan #6410 checked that a GS access at an instruction start is refused; it now checks that it is trapped.
  2. test(relinker): cover GS loads, stores and ALU reads trapped in macOS output: a new macos_gs_traps test converts the two loads Wine's ntdll uses, a store, an ALU read and a 4-byte register-address form with --macos, and checks that each becomes ud2 and NOPs before the next instruction, with none of the original bytes left.
  3. test(relinker): trap a GS access in a macOS module without a TLS segment: the writer sets up guest TLS only for a module with a non-empty PT_TLS; a GS access in a module without one must still be trapped.
  4. docs(debt): record that guest GS accesses trap in macOS output.

Tested

Windows host (ee644bd on #6410 on main 0afa6a6, Windows 11 10.0.26300, WinLibs GCC 15.2.0 r7, Release). The relinker writes macOS output on any host:

Not run on macOS: I have no Mac, so neither that a converted module loads there nor that a trap is reported there is checked.

Checklist

@github-actions

github-actions Bot commented Oct 15, 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 2780 core/relinker/CMakeLists.txt
PR 5021 core/relinker/CMakeLists.txt
docs/dev/debt/relinker.md
PR 6044 core/relinker/CMakeLists.txt
PR 6879 core/relinker/CMakeLists.txt
PR 6977 core/relinker/CMakeLists.txt
docs/dev/debt/relinker.md
PR 7173 core/relinker/CMakeLists.txt
docs/dev/debt/relinker.md
PR 7450 core/relinker/CMakeLists.txt
core/relinker/elfpatcher/src/macos/MacOsImage.cpp
docs/dev/debt/relinker.md
PR 7531 core/relinker/CMakeLists.txt
docs/dev/debt/relinker.md
PR 7594 core/relinker/CMakeLists.txt

@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.

Tested on Apple silicon. This is the macOS follow-up of #6102, and it builds directly on the M2 Max measurement I reported there, so I validated it on the same machine.

Machine: Apple M2 Max, macOS 27.2.
Head: ee644bd. Merges onto current main (0029e62) cleanly, no conflicts (only core/relinker/CMakeLists.txt and docs/dev/debt/relinker.md auto-merge). The stack applies as expected: 3 commits from #6410 plus the 4 of this PR.
Build: relinker-only (-DANYPS5_RELINKER_ONLY=ON -DBUILD_TESTING=ON), both arm64 and x86_64 (-DCMAKE_OSX_ARCHITECTURES=x86_64 -DPython3_EXECUTABLE=/usr/bin/python3, Python 3.9.6).

Results:

  • arm64: 174/174 pass, including the two new tests.
  • x86_64 + Python 3.9.6: 171/174. The three failures (pr_overlap_tools, release_package, linux_fixed_base) are the known Python 3.9 incompatibilities (str.write_text(newline=) and zip(strict=) are 3.10+), unrelated to this PR.

macos_gs_traps and macos_tls_overlap run on macOS (not skipped) and pass on both architectures.

The new tests protect against the regression. Running only the two test scripts against a main relinker built without the fix, both fail with the original refusal:

  • macos_gs_traps: FAIL: Unsupported macOS guest TLS instruction (bytes: 65 48 8b 0c 25 78 03 00 00).
  • macos_tls_overlap: FAIL: Unsupported macOS guest TLS instruction (bytes: 65 2e 62 69 6e c3 c3 c3) — the split-function case, where main wrongly treats a GS-prefixed decoding that starts inside a movabs immediate as a real TLS access.

Real behavior, GS fixture (mov rcx, gs:[0x378], 9 bytes) converted with --macos:

  • On main: FAIL: Unsupported macOS guest TLS instruction ... (offset 0x1240), the #6102 refusal, exit 2.
  • With this PR: converts, exit 0. At the site the Mach-O holds 0f 0b 90 90 90 90 90 90 90 c3 — UD2 + seven NOP (matching the 9-byte original) + the following RET. So the module loads and runs until a GS access is actually reached, then faults, as the debt note describes.

The case the PR is careful about is covered: in test_macos_tls_overlap the movabs rcx, 0x6e69622e65000000 immediate (48 b9 00 00 00 65 2e 62 69 6e, the same shape as OpenSSL's "CRYPTOGAMS by appro@openssl.org" literal) contains a 0x65 byte that would decode as a GS-prefixed instruction if the scan started mid-instruction. The PR skips prefixed decodings that start inside an earlier instruction, so the immediate is left intact; main refuses the whole module. Confirmed: the immediate survives the conversion with the fix and the module is refused without it.

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

@DavidSamir

Copy link
Copy Markdown
Contributor Author

Hardware run on Apple silicon.

macOS 26.7.1, Apple M2 (8 GB), Rosetta 2, AppleClang 21.0.0. Relinkers built (-DANYPS5_RELINKER_ONLY=ON, Release) from main 5d9dcc4 alone and with this branch merged; catalog releases, sha256 checked, eboot and modules unwrapped with tools/unwrap_self.py, converted with --macos --to-rosetta. For the runs, libraries from a local tree of main ce991f1 with #4627, #4630, #6124, #6028, #6135 and mugurc's macOS branches merged, so they build on macOS; MoltenVK from the LunarG SDK 1.4.363.

AI-assisted: yes

ycarogabriel-lgtm added a commit to ycarogabriel-lgtm/AnyPS5 that referenced this pull request Oct 18, 2026
…guest GS accesses in macOS output instead of refusing the module (@DavidSamir)

Taken from the open upstream pull request so the macOS integration branch carries every piece of the port that is already under review, instead of re-implementing it.

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

Copy link
Copy Markdown
Contributor

Rosetta results for this PR (ee644bd), M1 with macOS 26.6.2, relinker-only Release build:

  • x86-64 build (CMAKE_OSX_ARCHITECTURES=x86_64, tests running under Rosetta): 165/165 passed, including macos_tls_overlap and macos_gs_traps.
  • Native arm64 build: 165/165 passed, including both new tests.

Both new tests only check the converted output (no execution step), so they ran in full on both builds.

One issue: ScanTls traps every instruction with a 0x65 prefix, including ones with no memory operand. A segment override only selects the segment for a memory operand (Intel SDM Vol. 2A §2.1.1, Group 2 prefixes), so these instructions never touch GS and don't need trapping. Reproduction with make_image("register", "unwind", body=instruction + b"\xc3") from test_tls_function_coverage.py, then relinker --macos --skip-sce-module (x86-64 build):

instruction main this PR
65 90 (gs nop) refused: "Unsupported macOS guest TLS instruction" 0f 0b (UD2)
65 48 89 c8 (mov rax, rcx) refused 0f 0b 90 90
65 48 b8 <imm64> (movabs rax, imm64) refused 0f 0b + 9 NOPs

Expected: these are left unchanged. I checked that they run correctly under Rosetta in an x86-64 test binary: mov and movabs give the expected values, and the nop runs. Before this PR, such a module failed at conversion. Now it converts and then faults at runtime on an instruction that would have worked. Only trapping decodings that have a memory operand (ModRM with mod != 3, or the moffs forms A0–A3) would avoid this.

AI-assisted: yes (Claude Code)

@github-actions github-actions Bot added the unblocks title Names a title ID on the Unblocks line label Oct 18, 2026
@adriadam10 adriadam10 self-assigned this Oct 19, 2026
DavidSamir and others added 5 commits October 19, 2026 02:06
…sing the module

On x86-64 macOS, including under Rosetta, the host owns GS (it holds the
pthread TSD base, which the macOS TLS stubs read), so a guest instruction
that reads or writes through GS cannot be translated, and the TLS scan
refused the whole module. Prospero Win's (PPSA99995) Wine ntdll has six.
Overwrite each in place with ud2 and NOP padding of its own length, as the
Windows writer does in boykopovar#6102, and refuse one that is also an AMD-only
trampoline site.
… output

macos_gs_traps converts the forms Wine's ntdll in Prospero Win (PPSA99995) uses, a store, an ALU read and a register-address form shorter than a jump, and checks that each becomes ud2 and NOP padding of its own length before the next instruction, with none of the original bytes left.
The macOS writer sets up guest TLS only for a module with a non-empty PT_TLS. A GS access in a module without one must still be trapped.
@adriadam10
adriadam10 force-pushed the fix/relinker-trap-macos-guest-gs branch from ee644bd to da5b7e3 Compare October 19, 2026 00:09
@adriadam10
adriadam10 merged commit 42d8666 into boykopovar:main Oct 19, 2026
21 of 22 checks passed
ycarogabriel-lgtm added a commit to ycarogabriel-lgtm/AnyPS5 that referenced this pull request Oct 19, 2026
The branch no longer merged cleanly with main, which jotitadev reported
after rechecking 57bccfb on the M2 Max; this brings in 183 upstream
commits, among them boykopovar#6410, boykopovar#6419 and boykopovar#6458, which this branch carried.

Conflicts:
- recompile request serialization: main moved to version 34 for pixel
  sample iteration and locations, this branch to 33 for the fragment
  barycentric built-ins flag. Both fields are kept and the version
  becomes 35; the barycentric flag is read from 35 on, so a version 34
  request written by main still parses. The serializer tests, and
  main's new version 30 and 32 cases in ShaderMemory, account for the
  extra trailing byte.
- ShaderInfoCollector: main folds the center and sample barycentric
  kinds into one input and adds gl_SampleID for sample iteration; the
  gl_FragCoord input that keeps Apple GPUs' barycentrics normalized is
  kept for all three kinds.
- QuadSampleMasks keeps this branch's per-sample masks beside main's
  sample iteration fields; DynamicLoader keeps the macOS image field
  beside main's Windows load order; the relinker keeps its macOS
  fixture and fixed-base tests; the GS trap debt line is main's newer
  wording from boykopovar#6458.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
ycarogabriel-lgtm added a commit to ycarogabriel-lgtm/AnyPS5 that referenced this pull request Oct 19, 2026
… guest GS instructions on macOS (@Destroyers212x)

This branch carries boykopovar#6458's GS traps, which overwrite a GS-prefixed
guest instruction with UD2 and NOPs without checking whether a branch
lands inside it; a taken branch into the middle then ran the padding
as a different instruction. boykopovar#7706 refuses such a branch with the same
BranchTargets check the TLS stubs already use, and its test is
registered on this branch, so the integration gets the fix now rather
than when both land.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

build docs relinker unblocks title Names a title ID on the Unblocks line

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants