Repository navigation
feat(relinker): execute supported guest GS accesses in macOS output - #18453
Conversation
Rewrite each supported guest GS memory instruction into a jump to an __APS5TEXT stub instead of UD2. The stub steps over the red zone, saves one scratch register (rbx, rsi or rdi, whichever the instruction does not use), reads the thread's guest GS base from the host TSD slot of the pthread key bound through _Aps5GuestGsKey_nid_no_patch, adds the index, displacement and base of the original addressing with LEA, replays the instruction with its original prefixes, opcode, register and immediate against [scratch], restores the scratch register and jumps back. No stub instruction other than the replay writes RFLAGS. Forms the stub cannot express (shorter than 5 bytes, RIP-relative or 67-prefixed addressing, VEX/EVEX/XOP, moffs beyond disp32, opcodes outside the replayed set, all three scratch candidates in use) keep the UD2 trap. Branch-target and entry-point checks are unchanged. libc on macOS creates the key and stores the base with pthread_setspecific; libkernel's sysarch AMD64_SET_GSBASE/GET_GSBASE and I386_GET_GSBASE use it, and scePthreadCreate children inherit their creator's base. A thread that never set a base reads 0 and faults. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
|
Windows check of head Setup: Windows 11 Enterprise 22621, WinLibs GCC 15.2 UCRT, Release. The hosted Windows check does not run these tests on pull requests.
AI-assisted: yes. |
|
Other open pull requests touch the same code. Merging one will break the other, or both implement the same thing. If this one builds on them, list them in Depends on.
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.
|
|
The hosted |
MarcZX10
left a comment
There was a problem hiding this comment.
Reviewed — no blocking findings
Nothing blocks this; the points below are optional.
What I ran
- Head
41312d2e03677d2f3f6ea1bf7fa963ede79b203donmain, PR #18453. - Read the diff hunks of all 11 files in
files.jsonreviewed. No full file text was read, and no file was excluded. - Searched the head with the reader for the GS helper symbols and the
<bit>include. - No build and no test ran here. The host is Linux, and the macOS output needs a macOS x86-64 run.
python3 repo_read.py search GuestGs core/libs/prx
python3 repo_read.py search "bit>" core/relinker/elfpatcher/src/macos/MacOsImage.cpp
Results
| Check | Result | Note |
|---|---|---|
| Diff of the 11 files | passed | Hunks only, no full files |
| Helper declarations and includes | passed | GuestGs.hpp and <bit> present |
| Relinker and libc build | not run | No macOS build here |
macos_guest_gs, macos_gs_traps |
not run | Author reports a macOS run |
Findings
Blocking
None.
Non-blocking
docs/dev/debt/relinker.md:56lists the MMX moves0F 6Fand0F 7Famong the GS forms that are rewritten.core/relinker/elfpatcher/src/macos/MacOsImage.cpp:335accepts0F 6E/6F/7E/7F/D6only with a66orF3prefix. A plain MMX0F 6Fload staysUD2, andtest_macos_gs_traps.pylistsgs-mmx-loadamong the trapped forms. Drop the MMX wording from the doc.core/relinker/relinker/tests/test_macos_guest_gs.py:312check_rewrittenonly checks that each original instruction's bytes are gone. It does not check that ajmp rel32now leads to a stub, which thecheck_rewrittenintest_macos_gs_traps.pydoes. On a Linux run, which skips the native part, this is the only check of the rewrite, and a broken stub would pass it.
Not verified
- Author-reported results: full suite 780 of 780, 9 of 11 mutations failing, and
check_conventions.py. Not reproduced here. - Native execution of the guest GS probe.
native()intest_macos_guest_gs.pyreturns without running off macOS. - The stub reads
gs:[key*8](MacOsImage.cpp:389), andcore/libs/prx/libc/src/specifics/macos/GuestGs.cpp:7does not check that the key stays in the fast TSD range. The author's macOS 27 run is the only evidence. - How
GsTrap.Entryinteracts with a rewrite in the entry-root path. I did not traceScanTlsor the entry handling. - Intel Macs, older macOS, and an arm64 host, where
sysarchthrows. - Negative control: not run here. The author reports the mutation check.
To get this to a good state
Nothing is required.
Optional: the two non-blocking items above.
MarcZX10
left a comment
There was a problem hiding this comment.
Reviewed — comments
The GS rewrite path appears internally consistent in the inspected code, but this review is incomplete because several changed files were not read and no checks could be run.
What I ran
Reviewed head 649abf7f431eb4e339482ae313174bce785cec2c. No commands or tests were run; repo_read.py review tools were used to inspect the prepared diff and selected source ranges.
Results
| Check | Result | Note |
|---|---|---|
| Diff inspection | passed | Prepared diff reviewed |
| Tests and build | not run | Not available through review tools |
| Full changed-file review | not run | Several changed files not read |
Findings
Blocking
None verified in the inspected code.
Non-blocking
No specific code change is requested. A complete review would require reading all changed files and checking the author-reported macOS build and test results independently.
Not verified
Read: description.md, commits.md, manifest.json, diff.patch; changed source ranges in core/relinker/elfpatcher/src/macos/MacOsImage.cpp, core/libs/prx/libkernel/System/src/Process.cpp, and core/libs/prx/libc/src/specifics/macos/GuestGs.cpp.
Not read: full text of the changed files in core/libs/prx/libc/CMakeLists.txt, core/libs/prx/libkernel/Pthread/src/Thread.cpp, core/relinker/CMakeLists.txt, core/relinker/relinker/tests/test_macos_gs_traps.py, core/relinker/relinker/tests/test_macos_guest_gs.py, core/relinker/relinker/tests/test_macos_tls_overlap.py, docs/dev/debt/libkernel.md, and docs/dev/debt/relinker.md. No changed files were excluded from the prepared manifest. macOS behavior and the author-reported tests and build were not independently confirmed.
To get this to a good state
Nothing is required. Optional: complete the changed-file review and independently run the relevant build and tests before making a formal decision.
|
Tested
AI-assisted: yes (Claude Code); the runs are automated, this summary was written from their reports. |
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
MarcZX10
left a comment
There was a problem hiding this comment.
Reviewed — no blocking findings
Nothing blocks this; the points below are optional.
What I ran
Reviewed head de6f94666405d1a1f597ae0bae6958a853aaa24b. No commands or tests were run; the author reports a macOS 27 Apple M1 run and syntax-only checks for changed libkernel/libc files.
Results
| Check | Result | Note |
|---|---|---|
| Source review | passed | All 11 changed files read |
| Test execution | not run | Author-reported results only |
| Excluded files | not applicable | No changed files excluded |
Findings
Blocking
None.
Non-blocking
PlanGsRewriteaccepts the two-byte opcode0xC1with any prefix state, while replaying its immediate unchanged. In the0F C1encoding, a mandatoryF2prefix selectsCMPXCHG16B; this instruction is a memory operation with implicit register operands and a fixed 16-byte operand, so treating the ModRM reg field as the sole register operand can select a scratch register that clobbers an implicit operand. Please rejectF2 0F C1(or fully model its implicit operands) before marking it rewriteable. Seecore/relinker/elfpatcher/src/macos/MacOsImage.cpp:201.
Not verified
- No tests or builds were run in this review; author-reported checks were not independently confirmed.
- Changed files read:
core/libs/prx/libc/CMakeLists.txt,core/libs/prx/libc/src/specifics/macos/GuestGs.cpp,core/libs/prx/libkernel/Pthread/src/Thread.cpp,core/libs/prx/libkernel/System/src/Process.cpp,core/relinker/CMakeLists.txt,core/relinker/elfpatcher/src/macos/MacOsImage.cpp,core/relinker/relinker/tests/test_macos_gs_traps.py,core/relinker/relinker/tests/test_macos_guest_gs.py,core/relinker/relinker/tests/test_macos_tls_overlap.py,docs/dev/debt/libkernel.md, anddocs/dev/debt/relinker.md. - No changed files were excluded by the review bundle. The review-folder
files.jsonwas not available; the manifest lists all 11 changed files as included. - The native macOS execution, other macOS versions, Intel Macs, and behavior of faults inside a rewritten stub were not independently verified.
To get this to a good state
Nothing is required. Optional: reject or correctly model F2 0F C1, and add a focused regression case.
A pull request no longer edits debt entries in place. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
MarcZX10
left a comment
There was a problem hiding this comment.
Reviewed — changes requested
Changes are required before merge: fix the GS rewriter’s decoding of SIB no-index operands when REX.X is set.
What I ran
Reviewed head cbcf5d0d43dad8ff29d5e5441e750ba428d1cb2e. I read the prepared description, commits, manifest, diff, and all nine changed repository files; the manifest lists all nine under reviewed. I did not run build or test commands. The author reports macOS tests and a relinker-only Release build; these were not run by me.
Results
| Check | Result | Note |
|---|---|---|
| Diff and changed-file review | passed | All nine changed files read |
| Build and tests | not run | Author-reported results only |
Findings
Blocking
- In
core/relinker/elfpatcher/src/macos/MacOsImage.cpp:349, the SIB index is extended withREX.Xbefore checking whether the encoded index bits mean “no index.” In x86-64, SIB index bits100mean no index only whenREX.Xis clear; withREX.X=1, the operand isr12. The current check therefore leavesr12inrewrite.Index, and the stub addsr12 * scaleto the effective address (:392-395) even though the original instruction had no index. Valid GS accesses using a SIB operand with no index andREX.X=1will access the wrong address. Detect the no-index encoding from the raw SIB index bits andREX.Xbefore extending it, and add a regression case.
Non-blocking
None.
Not verified
- No build or tests were run in this review. The description reports a macOS 27 Apple M1 run and notes that the changed libkernel/libc files were only syntax-checked; those author-reported results were not independently verified.
- No files were excluded by the manifest. Repository files read:
core/libs/prx/libc/CMakeLists.txt,core/libs/prx/libc/src/specifics/macos/GuestGs.cpp,core/libs/prx/libkernel/Pthread/src/Thread.cpp,core/libs/prx/libkernel/System/src/Process.cpp,core/relinker/CMakeLists.txt,core/relinker/elfpatcher/src/macos/MacOsImage.cpp,core/relinker/relinker/tests/test_macos_gs_traps.py,core/relinker/relinker/tests/test_macos_guest_gs.py,core/relinker/relinker/tests/test_macos_tls_overlap.py, anddocs/dev/debt/changes/feat-relinker-macos-guest-gs.md.
To get this to a good state
Correct the SIB no-index handling and add a test for REX.X=1 with index bits 100; rerun the relevant macOS guest GS and GS trap tests on the updated head.
SIB index 100 means no index only with REX.X clear; with REX.X set it is r12. SIB base 101 with mod 00 means disp32 with no base whatever REX.B says, and mod 00 rm 101 stays RIP-relative with REX.B set. PlanGsRewrite already decodes these forms this way. These cases pin that behavior: a decoder that reads the raw index bits drops r12, and one that checks the extended base adds r13. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
|
@MarcZX10 thanks for the close look at the SIB decode. I agree with the rule, from SDM Vol 2A §2.2.1.2, Table 2-5: index bits
Deciding "no index" from the raw bits would drop a real r12 index. I tried that change, and the existing To pin this down, b31c2e9 adds cases to
It also adds a trap case to |
|
@MarcZX10 I checked the SIB decode on b31c2e9. MacOsImage.cpp:349 folds REX.X into the index before line 350 compares it with 4, so index bits 100 mean "no index" only when REX.X is clear and decode as r12 when it is set. The new load64-r12-index-only and load64-r12-index-no-base-rexb cases pin that, and the relinker tests pass on Ubuntu and macOS. Could you recheck whether your change request still applies? |
MarcZX10
left a comment
There was a problem hiding this comment.
Reviewed — looks good
Nothing blocks this change; the review covers the changed source files and reports no verified defects.
What I ran
Reviewed head b31c2e99ff8fb0e9dd2190acba68f0913e03af9d using repo_read.py to inspect the prepared diff and targeted source ranges. No commands or tests were run.
Results
| Check | Result | Note |
|---|---|---|
| Changed code review | passed | 8 changed files read |
| Programmatic checks | not run | No build or tests run |
Findings
Blocking
None.
Non-blocking
None.
Not verified
Builds and runtime tests were not run. No files were excluded: all 8 files listed in the manifest were included and read. Author-reported test results were not independently verified.
To get this to a good state
Nothing is required.
What
In macOS output, supported guest GS-relative memory instructions now run through stubs instead of becoming
UD2. This narrows the relinker debt entry that blocks GS users such as Prospero Win's (PPSA99995) Winentdll.Relinker (
MacOsImage.cpp).PlanGsRewritedecides per instruction. A supported one becomesjmp rel32to an__APS5TEXTstub, padded with NOPs. The stub:rspdown past the red zone (lea rsp,[rsp-128]) and pushes one scratch register: the first ofrbx/rsi/rdithe instruction does not use.Aps5GuestGsKey_nid_no_patchand read withgs:[key*8], the mechanism the guest TLS stubs already use.lea; anrspbase gets +0x88.65), REX (minus X/B), opcode, reg field and immediates against[scratch].rsp, then jumps back.Only the replayed instruction writes RFLAGS. The branch-target, entry-point and AMD-trampoline checks and their messages are unchanged.
Runtime.
libc/src/specifics/macos/GuestGs.cppcreates the key at load time. Each thread's slot holds its guest GS base.sysarchnow handles set-GS (131), get-GS (130) andI386_GET_GSBASE(9), sharing the Windows branch. Get-FS still throws.scePthreadCreatechildren inherit the creator's base, as on FreeBSD.Still
UD2(recorded in the narrowed debt entry):67-prefixed addressingmoffsoutside signed 32 bitspush/pop/call/jmpthrough GS are not replayedrsp/esp/sp/spl)rbx,rsiandrdiThe debt entry also notes that a fault in a rewritten access is reported in the stub, with the stack 136 bytes lower.
Tested
macOS 27 on Apple M1, relinker-only Release build.
ctest --test-dir build-relinker -j 4: 780 of 780 on this branch, 779 of 779 on base.New
macos_guest_gstest (RUN_SERIAL on Apple). Guest code sets GS through the stubbedsysarch. A guest harness loads every general register, xmm0/xmm1 and RFLAGS, then runs 57 GS forms, each against a native reference without GS, under flag patterns0x8d7and0x202. It compares all registers,rsp, flags, xmm0/xmm1, the buffer and the guard bytes around it. The 57 forms cover:gs:dispandgs:[reg+disp]rspbaselock xaddmoffs, SSE movesIt also checks that two threads with different bases each read their own, and that a thread with no base faults with SIGSEGV on the main thread and on a new thread.
AssertionError: (-4, '', '')(SIGILL fromUD2).57 cases matched the reference under two flag patterns; two threads kept their own basesanda thread without a GS base faults with SIGSEGV.Updated tests.
test_macos_gs_traps.pyandtest_macos_tls_overlap.pynow expect rewrites for supported forms, and gain still-UD2cases. One is a stack-pointer register operand (mov rsp, gs:[x], a store fromrsp, a store fromspl): replaying it inside the stub would have corrupted the stub's own stack restore. Without the rejection, the gs_traps test fails withgs-load-into-rsp: the GS access was not replaced by ud2 and NOP padding.Mutation check. 9 of 11 hand mutations failed the new test:
The 2 survivors leave the output unchanged.
libkernel/libc. These do not build on macOS here, so the changed files were only checked with
-fsyntax-only -arch x86_64. The test's stub libkernel mirrors the key andsysarch. A Linux/Windows full build, and a macOS run with the real libkernel, are needed.Overlap. No other open PR implements GS on macOS; I grepped all 75 "cannot confirm" diffs.
sysarchand debt entries, so textual conflicts are possible.MacOsImage.cpp.Not verified:
cmpxchgin the equal case andmovupd, which are replayable but untestedChecklist
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)🤖 Generated with Claude Code