Repository navigation
fix(relinker): trap guest GS accesses in macOS output instead of refusing the module - #6458
Conversation
|
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.
|
jotitadev
left a comment
There was a problem hiding this comment.
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=)andzip(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, 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:
- 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+ 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_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.
|
Hardware run on Apple silicon. macOS 26.7.1, Apple M2 (8 GB), Rosetta 2, AppleClang 21.0.0. Relinkers built (
AI-assisted: yes |
…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>
|
Rosetta results for this PR (ee644bd), M1 with macOS 26.6.2, relinker-only Release build:
Both new tests only check the converted output (no execution step), so they ran in full on both builds. One issue:
Expected: these are left unchanged. I checked that they run correctly under Rosetta in an x86-64 test binary: AI-assisted: yes (Claude Code) |
…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.
…hout a memory operand too
ee644bd to
da5b7e3
Compare
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>
… 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>
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 <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:
fix(relinker): trap guest GS accesses in macOS output instead of refusing the module. Themacos_tls_overlaptest 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.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 (ee644bd on #6410 on
main0afa6a6, 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 fix(relinker): skip prefixed decodings inside an earlier instruction in the macOS TLS scan #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 fix(relinker): skip prefixed decodings inside an earlier instruction in the macOS TLS scan #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 fix(relinker): skip prefixed decodings inside an earlier instruction in the macOS TLS scan #6410, fix(relinker): rewrite ALU reads of guest TLS through %fs in macOS output #6419 and fix(relinker): convert stack-guard and thread-pointer reads with an empty PT_TLS for macOS #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)