Skip to content

feat(relinker): execute supported guest GS accesses in macOS output - #18453

Merged
oneandonlydean merged 7 commits into
boykopovar:mainfrom
SHOnnay:feat/relinker-macos-guest-gs
Nov 16, 2026
Merged

oneandonlydean merged 7 commits into
boykopovar:mainfrom
SHOnnay:feat/relinker-macos-guest-gs

Conversation

@SHOnnay

@SHOnnay SHOnnay commented Nov 11, 2026

Copy link
Copy Markdown
Contributor

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) Wine ntdll.

Relinker (MacOsImage.cpp). PlanGsRewrite decides per instruction. A supported one becomes jmp rel32 to an __APS5TEXT stub, padded with NOPs. The stub:

  1. Moves rsp down past the red zone (lea rsp,[rsp-128]) and pushes one scratch register: the first of rbx/rsi/rdi the instruction does not use.
  2. Reads the guest GS base for the current thread from the host TSD slot of a pthread key. The key is exported by libc as Aps5GuestGsKey_nid_no_patch and read with gs:[key*8], the mechanism the guest TLS stubs already use.
  3. Forms base + index×scale + disp with lea; an rsp base gets +0x88.
  4. Replays the original prefixes (minus 65), REX (minus X/B), opcode, reg field and immediates against [scratch].
  5. Restores the register and 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.

  • New libc/src/specifics/macos/GuestGs.cpp creates the key at load time. Each thread's slot holds its guest GS base.
  • On Apple x86-64, sysarch now handles set-GS (131), get-GS (130) and I386_GET_GSBASE (9), sharing the Windows branch. Get-FS still throws.
  • scePthreadCreate children inherit the creator's base, as on FreeBSD.
  • A thread that never set a base reads 0, so an access faults with SIGSEGV, as with FreeBSD's default base of 0.

Still UD2 (recorded in the narrowed debt entry):

  • instructions shorter than 5 bytes
  • RIP-relative or 67-prefixed addressing
  • moffs outside signed 32 bits
  • VEX, EVEX or XOP encodings
  • opcodes outside the replayed set; push/pop/call/jmp through GS are not replayed
  • a register operand that is the stack pointer (rsp/esp/sp/spl)
  • instructions using all of rbx, rsi and rdi

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

  • Full suite: ctest --test-dir build-relinker -j 4: 780 of 780 on this branch, 779 of 779 on base.

New macos_guest_gs test (RUN_SERIAL on Apple). Guest code sets GS through the stubbed sysarch. 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 patterns 0x8d7 and 0x202. It compares all registers, rsp, flags, xmm0/xmm1, the buffer and the guard bytes around it. The 57 forms cover:

  • gs:disp and gs:[reg+disp]
  • SIB with index, negative displacements, rsp base
  • byte and high-byte registers
  • ALU reads, read-modify-write, lock xadd
  • moffs, SSE moves

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

  • Base: AssertionError: (-4, '', '') (SIGILL from UD2).
  • Branch: 57 cases matched the reference under two flag patterns; two threads kept their own bases and a thread without a GS base faults with SIGSEGV.

Updated tests. test_macos_gs_traps.py and test_macos_tls_overlap.py now expect rewrites for supported forms, and gain still-UD2 cases. One is a stack-pointer register operand (mov rsp, gs:[x], a store from rsp, a store from spl): replaying it inside the stub would have corrupted the stub's own stack restore. Without the rejection, the gs_traps test fails with gs-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:

  • wrong stack shift, no base add, no scale
  • scratch register ignoring the reg operand
  • REX.X/B kept, missing pop
  • unsigned disp8
  • no opcode-consistency check
  • MMX allowed

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 and sysarch. A Linux/Windows full build, and a macOS run with the real libkernel, are needed.

$ python3 tools/check_conventions.py --base upstream/main
All conventions checks passed.

Overlap. No other open PR implements GS on macOS; I grepped all 75 "cannot confirm" diffs.

Not verified:

  • whether Prospero Win's six instructions are in the supported set
  • Intel Macs and older macOS
  • a fault inside a stub reaching the write watch and signal handling
  • cmpxchg in the equal case and movupd, which are replayable but untested

Checklist

  • Based on current main; no other open PR implements the same functions
  • One topic per PR; follow-ups go in a new PR
  • No comments in code except technical debt
  • Unimplemented paths throw (NotImplemented_nid_no_patch); silent stubs are listed under Silent stubs in their library's technical debt file
  • No notes, investigation .md files or images added to the repository (attach them to this PR)
  • New third-party code is a submodule built from source
  • Generic behaviour, not specific to one title
  • Depends on: none
  • Fixes: none
  • Unblocks: none
  • GPUs tested: n/a
  • AI-assisted: yes (Claude Code)

🤖 Generated with Claude Code

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

JJenkx commented Nov 11, 2026

Copy link
Copy Markdown
Contributor

Windows check of head 41312d2e03, merged onto main f2473212d8 (3db01b7511, clean merge).

Setup: Windows 11 Enterprise 22621, WinLibs GCC 15.2 UCRT, Release. The hosted Windows check does not run these tests on pull requests.

  • macos_gs_traps, macos_guest_gs, macos_tls_overlap pass in 3 of 3 runs, none skipped.

AI-assisted: yes.

@github-actions

github-actions Bot commented Nov 11, 2026 •

Copy link
Copy Markdown

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.

Pull request Conflicting files (lines on main) Same exports Same new files
PR 21156 core/libs/prx/libc/CMakeLists.txt L145

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 4563 core/libs/prx/libkernel/Pthread/src/Thread.cpp
PR 6149 core/relinker/CMakeLists.txt
PR 7149 core/libs/prx/libkernel/Pthread/src/Thread.cpp
PR 7447 core/libs/prx/libc/CMakeLists.txt
PR 7531 core/relinker/CMakeLists.txt
PR 7715 core/libs/prx/libc/CMakeLists.txt
PR 8150 core/libs/prx/libc/CMakeLists.txt
PR 8592 core/libs/prx/libkernel/Pthread/src/Thread.cpp
PR 8965 core/libs/prx/libc/CMakeLists.txt
PR 9466 core/libs/prx/libc/CMakeLists.txt
PR 9938 core/libs/prx/libkernel/Pthread/src/Thread.cpp
PR 10249 core/relinker/elfpatcher/src/macos/MacOsImage.cpp
PR 10792 core/relinker/CMakeLists.txt
PR 10861 core/relinker/CMakeLists.txt
core/relinker/elfpatcher/src/macos/MacOsImage.cpp
PR 11628 core/relinker/CMakeLists.txt
PR 11661 core/relinker/CMakeLists.txt
PR 11689 core/relinker/CMakeLists.txt
PR 11698 core/relinker/CMakeLists.txt
PR 12642 core/libs/prx/libkernel/Pthread/src/Thread.cpp
PR 12678 core/relinker/CMakeLists.txt
PR 12682 core/libs/prx/libc/CMakeLists.txt
core/libs/prx/libkernel/Pthread/src/Thread.cpp
PR 12688 core/libs/prx/libc/CMakeLists.txt
core/libs/prx/libkernel/Pthread/src/Thread.cpp
PR 12700 core/libs/prx/libc/CMakeLists.txt
core/libs/prx/libkernel/Pthread/src/Thread.cpp
PR 12717 core/libs/prx/libc/CMakeLists.txt
PR 12849 core/libs/prx/libc/CMakeLists.txt
PR 12965 core/libs/prx/libc/CMakeLists.txt
core/libs/prx/libkernel/Pthread/src/Thread.cpp
PR 13486 core/relinker/CMakeLists.txt
PR 13716 core/relinker/CMakeLists.txt
PR 15245 core/relinker/CMakeLists.txt
PR 15642 core/relinker/CMakeLists.txt
PR 15896 core/relinker/CMakeLists.txt
PR 16790 core/libs/prx/libkernel/Pthread/src/Thread.cpp
PR 16791 core/libs/prx/libc/CMakeLists.txt
PR 17354 core/libs/prx/libc/CMakeLists.txt
PR 17451 core/relinker/CMakeLists.txt
PR 17473 core/relinker/CMakeLists.txt
PR 17557 core/libs/prx/libc/CMakeLists.txt
core/relinker/CMakeLists.txt
PR 17558 core/libs/prx/libc/CMakeLists.txt
core/relinker/CMakeLists.txt
PR 19181 core/libs/prx/libc/CMakeLists.txt
core/libs/prx/libkernel/Pthread/src/Thread.cpp

@SHOnnay

SHOnnay commented Nov 11, 2026

Copy link
Copy Markdown
Contributor Author

The hosted relinker (windows-latest, g++) failure on this head is pr_intake_reopen, a PR-intake tooling test whose setUpClass timed out after 61 s. The other 867 tests passed, including macos_guest_gs, macos_gs_traps and macos_tls_overlap. This PR doesn't touch the intake tooling. JJenkx's Windows run above passes the three GS tests 3 of 3. A collaborator re-run of that job should clear it.

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

Reviewed — no blocking findings

Nothing blocks this; the points below are optional.

What I ran

  • Head 41312d2e03677d2f3f6ea1bf7fa963ede79b203d on main, PR #18453.
  • Read the diff hunks of all 11 files in files.json reviewed. 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

  1. docs/dev/debt/relinker.md:56 lists the MMX moves 0F 6F and 0F 7F among the GS forms that are rewritten. core/relinker/elfpatcher/src/macos/MacOsImage.cpp:335 accepts 0F 6E/6F/7E/7F/D6 only with a 66 or F3 prefix. A plain MMX 0F 6F load stays UD2, and test_macos_gs_traps.py lists gs-mmx-load among the trapped forms. Drop the MMX wording from the doc.
  2. core/relinker/relinker/tests/test_macos_guest_gs.py:312 check_rewritten only checks that each original instruction's bytes are gone. It does not check that a jmp rel32 now leads to a stub, which the check_rewritten in test_macos_gs_traps.py does. 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() in test_macos_guest_gs.py returns without running off macOS.
  • The stub reads gs:[key*8] (MacOsImage.cpp:389), and core/libs/prx/libc/src/specifics/macos/GuestGs.cpp:7 does not check that the key stays in the fast TSD range. The author's macOS 27 run is the only evidence.
  • How GsTrap.Entry interacts with a rewrite in the entry-root path. I did not trace ScanTls or the entry handling.
  • Intel Macs, older macOS, and an arm64 host, where sysarch throws.
  • 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 MarcZX10 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.

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.

@ycbuzi

ycbuzi commented Nov 14, 2026

Copy link
Copy Markdown
Contributor

Tested 649abf7f43 merged into main, with the whole ctest suite on each machine below. Nothing fails that does not also fail on main on the same machine.

  • Windows Server 2022 (GitHub-hosted runner), full build, WinLibs GCC 15.2.0: 99% tests passed, 2 tests failed out of 5401; 99% tests passed, 2 tests failed out of 5401. 2 of the failing tests fail on main too. run
  • Windows Server 2025 (GitHub-hosted runner), full build, WinLibs GCC 15.2.0: 99% tests passed, 1 tests failed out of 5401; 100% tests passed, 0 tests failed out of 5401. run

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

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

  • PlanGsRewrite accepts the two-byte opcode 0xC1 with any prefix state, while replaying its immediate unchanged. In the 0F C1 encoding, a mandatory F2 prefix selects CMPXCHG16B; 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 reject F2 0F C1 (or fully model its implicit operands) before marking it rewriteable. See core/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, and docs/dev/debt/relinker.md.
  • No changed files were excluded by the review bundle. The review-folder files.json was 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.

SHOnnay and others added 2 commits November 15, 2026 10:45
A pull request no longer edits debt entries in place.

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

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

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 with REX.X before checking whether the encoded index bits mean “no index.” In x86-64, SIB index bits 100 mean no index only when REX.X is clear; with REX.X=1, the operand is r12. The current check therefore leaves r12 in rewrite.Index, and the stub adds r12 * scale to the effective address (:392-395) even though the original instruction had no index. Valid GS accesses using a SIB operand with no index and REX.X=1 will access the wrong address. Detect the no-index encoding from the raw SIB index bits and REX.X before 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, and docs/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>
@SHOnnay

SHOnnay commented Nov 15, 2026

Copy link
Copy Markdown
Contributor Author

@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 100 mean "no index" only when REX.X is clear, and with REX.X=1 the operand is indexed by r12. That is what the code does. MacOsImage.cpp:349 combines the index bits with REX.X first, and :350 then treats only the result 4 as no index:

  • REX.X=0 with bits 100 gives 4, so no index.
  • REX.X=1 with bits 100 gives 12, so the r12 index is kept, as the SDM requires.

Deciding "no index" from the raw bits would drop a real r12 index. I tried that change, and the existing load64-r13-r12 case catches it (gs 815c3712edc8a37e reference a9845f3a15f0cba6).

To pin this down, b31c2e9 adds cases to test_macos_guest_gs.py, each run natively and compared with a reference:

  • no index with REX.X=0
  • an r12-only index (REX.X=1, bits 100, no base)
  • SIB base 101 with mod 00 and REX.B (disp32 with no base, not r13)
  • an r12 base, and an r13 base with disp8

It also adds a trap case to test_macos_gs_traps.py: a RIP-relative operand with REX.B must stay trapped. All pass on macOS, and the full relinker suite passes 834 of 834. If you have an encoding that decodes wrongly, please post the bytes and I'll add it.

@oneandonlydean

Copy link
Copy Markdown
Collaborator

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

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.

@oneandonlydean
oneandonlydean merged commit 4ad2866 into boykopovar:main Nov 16, 2026
22 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants