fix(relinker): trap guest GS accesses in macOS output instead of refusing the module #6458
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/6458"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Imported from boykopovar/AnyPS5. Originally posted by DavidSamir on 2026-10-15T16:18:36Z.
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
ntdllthe way the Windows writer did:ntdllhas 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 c5in the stubs). So the scan inMacOsImage.cppcannot 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
ud2and 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'sCRYPTOGAMS by <[email protected]>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:
fix(relinker): trap guest GS accesses in macOS output instead of refusing the module. Themacos_tls_overlaptest from #6410 checked that a GS access at an instruction start is refused; it now checks that it is trapped.test(relinker): cover GS loads, stores and ALU reads trapped in macOS output: a newmacos_gs_trapstest converts the two loads Wine'sntdlluses, a store, an ALU read and a 4-byte register-address form with--macos, and checks that each becomesud2and NOPs before the next instruction, with none of the original bytes left.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-emptyPT_TLS; a GS access in a module without one must still be trapped.docs(debt): record that guest GS accesses trap in macOS output.Tested
Windows host (
ee644bde66on #6410 onmain0afa6a607e, Windows 11 10.0.26300, WinLibs GCC 15.2.0 r7, Release). The relinker writes macOS output on any host:tools/check_conventions.py --base origin/main: passed.tools/pr_overlap.py: no overlap with open pull requests.-DANYPS5_RELINKER_ONLY=ON -DBUILD_TESTING=ON), the relinker builds at each of the four commits (on #6410) and every test that runs passes. The 27 that fail are the same at every commit and come from that build: 25 test programs it does not compile,windows_diagnostic_fixture_isolation, which copies one of them, andcheck_main_sync, which reads git's UTF-8 output as cp1252 on this host (sp ace é.txt) and fails the same way onmain. The change is inMacOsImage.cpp, which only the relinker links, so the relinker-only build runs every test that can see it.macos_gs_trapsfails without the fix: with #6410's relinker,gs-load-rcxstops withUnsupported macOS guest TLS instruction (bytes: 65 48 8b 0c 25 78 03 00 00).macos|tlstests pass (guest_force_tls_destructoris not run because only the relinker target was built).ntdll.prx, none of the six GS instructions is left (65 48 8b 0c 25 78 03 00 00×3,65 4c 8b 2c 25 30 00 00 00×2,65 4c 8b 14 25 30 03 00 00×1 in the input), andud2followed by seven NOPs occurs exactly six times. With #6410, #6419 and #6448, every one of the 79 catalog titles then converts for macOS.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
main; no other open PR implements the same functionsNotImplemented_nid_no_patch); silent stubs are listed under Silent stubs in their library's technical debt file.mdfiles or images added to the repository (attach them to this PR)Imported from boykopovar/AnyPS5. Originally posted by github-actions[bot] on 2026-10-15T16:37:15Z.
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.
core/relinker/CMakeLists.txtcore/relinker/CMakeLists.txtdocs/dev/debt/relinker.mdcore/relinker/CMakeLists.txtcore/relinker/CMakeLists.txtcore/relinker/CMakeLists.txtdocs/dev/debt/relinker.mdcore/relinker/CMakeLists.txtdocs/dev/debt/relinker.mdcore/relinker/CMakeLists.txtcore/relinker/elfpatcher/src/macos/MacOsImage.cppdocs/dev/debt/relinker.mdcore/relinker/CMakeLists.txtdocs/dev/debt/relinker.mdcore/relinker/CMakeLists.txtImported from boykopovar/AnyPS5. Originally posted by jotitadev on 2026-10-15T16:39:25Z.
Review: COMMENTED
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:
ee644bde6. Merges onto currentmain(0029e62f9) cleanly, no conflicts (onlycore/relinker/CMakeLists.txtanddocs/dev/debt/relinker.mdauto-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:
pr_overlap_tools,release_package,linux_fixed_base) are the known Python 3.9 incompatibilities (str.write_text(newline=)andzip(strict=)are 3.10+), unrelated to this PR.macos_gs_trapsandmacos_tls_overlaprun on macOS (not skipped) and pass on both architectures.The new tests protect against the regression. Running only the two test scripts against a
mainrelinker 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, wheremainwrongly treats a GS-prefixed decoding that starts inside amovabsimmediate as a real TLS access.Real behavior, GS fixture (
mov rcx, gs:[0x378], 9 bytes) converted with--macos:main:FAIL: Unsupported macOS guest TLS instruction ... (offset 0x1240), the #6102 refusal, exit 2.0f 0b 90 90 90 90 90 90 90 c3—UD2+ sevenNOP(matching the 9-byte original) + the followingRET. 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_overlapthemovabs rcx, 0x6e69622e65000000immediate (48 b9 00 00 00 65 2e 62 69 6e, the same shape as OpenSSL's "CRYPTOGAMS by [email protected]" literal) contains a0x65byte 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;mainrefuses 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.
Imported from boykopovar/AnyPS5. Originally posted by DavidSamir on 2026-10-16T04:25:39Z.
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) frommain5d9dcc42dalone and with this branch merged; catalog releases, sha256 checked, eboot and modules unwrapped withtools/unwrap_self.py, converted with--macos --to-rosetta. For the runs, libraries from a local tree ofmaince991f1ecwith #4627, #4630, #6124, #6028, #6135 and mugurc's macOS branches merged, so they build on macOS; MoltenVK from the LunarG SDK 1.4.363.--trap-sdk-syscall-stubs --module-dir win/wine/lib/wine/x86_64-unix: the relinkers ofmain, #6410, #6419 and #6448 each fail withUnsupported macOS guest TLS instruction (bytes: 65 48 8b 0c 25 78 03 00 00) (offset 0x3214c); this branch converts all 17 modules.app0and the converted modules over them: still running at 90 s, swapchain created (2008x1130 on the M2's display) and the mouse attached through Cocoa. The log also showslibc++abi: terminating due to uncaught exception of type std::__1::system_error: mutex lock failed: Invalid argumenttwice while the title keeps running; I haven't traced where that comes from yet.AI-assisted: yes
Imported from boykopovar/AnyPS5. Originally posted by Destroyers212x on 2026-10-18T11:09:22Z.
Rosetta results for this PR (
ee644bd), M1 with macOS 26.6.2, relinker-only Release build:CMAKE_OSX_ARCHITECTURES=x86_64, tests running under Rosetta): 165/165 passed, includingmacos_tls_overlapandmacos_gs_traps.Both new tests only check the converted output (no execution step), so they ran in full on both builds.
One issue:
ScanTlstraps 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 withmake_image("register", "unwind", body=instruction + b"\xc3")fromtest_tls_function_coverage.py, thenrelinker --macos --skip-sce-module(x86-64 build):65 90(gs nop)0f 0b(UD2)65 48 89 c8(mov rax, rcx)0f 0b 90 9065 48 b8 <imm64>(movabs rax, imm64)0f 0b+ 9 NOPsExpected: these are left unchanged. I checked that they run correctly under Rosetta in an x86-64 test binary:
movandmovabsgive the expected values, and thenopruns. 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)