fix(relinker): refuse GS traps that overwrite a code entry point in macOS output #11883

Merged
roboterspinne merged 5 commits from upstream-pr/11883 into main 2026-11-02 12:27:44 +00:00
roboterspinne commented 2026-10-27 11:14:26 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by roboterspinne on 2026-10-27T11:14:26Z.


What

--macos replaces a guest GS memory instruction with UD2 and NOPs and, of the code entries into it, refuses only a relative branch (#7706); since #11090, --windows also refuses any other code entry inside the trapped bytes, but --macos does not. On the untouched base 17dffabc20 (#11488 at 9ef8b4b75a merged with main 801d5b9bc1), 65 48 8b 04 25 28 00 00 00 c3 at 0x210 with an entry root at 0x214 (a STT_FUNC symbol, an R_X86_64_RELATIVE pointer, a REX.W lea target or a DT_INIT_ARRAY entry) converts with --macos with exit 0, and the Mach-O holds 0f 0b 90 90 90 90 90 90 90 c3 at file offset 0x10210, so the entry at 0x214 runs NOPs and ret instead of and eax,0x28; ret; --windows refuses all four inputs with GS trap would overwrite an executable entry point (offset 0x214). maximilianfv noted this gap in the review of #11488 and suggested a follow-up PR.

  • Rule. ScanTls in MacOsImage.cpp records for each trapped GS memory instruction [start, start + length) the first collector entry root with start < root < start + length, with the same entryRoots.upper_bound lookup and file-offset convention as WindowsTlsBuilder::Build (#11090). In the WriteMacOsImage trap loop, after the existing Branch enters a guest GS instruction check and before the AMD-only instruction is also a guest GS access check, such a root throws GS trap would overwrite an executable entry point; the CLI exits 2 and writes no output. entryRoots also contains relative branch targets, so the branch check stays first and keeps its message, which macos_gs_traps pins for --macos (its offset is pinned for --windows by tls_function_coverage_rejected). A root at the instruction start is still trapped as before. The entry roots come from #11488, which makes ScanTls pass &entryRoots instead of nullptr to CodeInstructionCollector::Collect; this PR therefore depends on #11488, and its diff includes the changes of #11488 (ScanTls and one test_sce_code_extent.py case) until #11488 merges. A new entry before the macOS GS entry in docs/dev/debt/relinker.md lists both macOS messages, the entries each covers, and that no option skips either check; the Windows entry is unchanged.
  • Tests. test_macos_gs_traps.py (macos_gs_traps) now also converts with --macos the entry-root images of test_windows_tls_protected_entries.py (make_image, which test_gs_trap_entry_roots.py uses for --windows) with 65 48 8b 04 25 28 00 00 00 c3 at 0x210: roots relative, symbol, lea and initializer must exit 2 with the message, (offset 0x214) and no output; controls safe and access-start must convert, with 0f 0b 90 90 90 90 90 90 90 c3 and without the original instruction in the executable ranges of the Mach-O. test_gs_trap_entry_roots.py is unchanged. No new test file and no CMake change.
  • Overlap. pr_overlap.py reports no shared lines with open pull requests that merge with main; the macOS cases sit in test_macos_gs_traps.py and the debt entry is a new bullet, so Felipebmaitan's #11628, #11661, #11689 and #11698, which rewrite test_gs_trap_entry_roots.py and the Windows GS entry, merge with this branch without conflict. It cannot compare this branch with #5021 (docs/dev/debt/relinker.md), #10249 (MacOsImage.cpp) and #10861 (MacOsImage.cpp, test_sce_code_extent.py, docs/dev/debt/relinker.md), because those do not merge with main. Checked by hand: trial merges with #5021 and #10249 conflict in exactly the files main does. With #10861 the only conflict outside CodeInstructionCollector.cpp, which conflicts with main too, is #11488's entryRoots argument of Collect in ScanTls, the same conflict #11488 alone has; none of this PR's own lines is involved.

Tested

Windows 11, GCC 15.2.0 (WinLibs UCRT), relinker-only Release build (-DANYPS5_RELINKER_ONLY=ON -DBUILD_TESTING=ON); synthetic fixtures only, no title or homebrew.

  • Counter-check: with MacOsImage.cpp of the untouched base 17dffabc20 and the extended test, macos_gs_traps fails with entry-relative: expected the GS trap to be refused at offset 0x214: 0 after all earlier cases passed. Run case by case, the four root inputs exited 0 with an output file, and both controls converted with the trapped bytes and without the original instruction. With the change (566fbb17ef) the four root inputs exit 2 with GS trap would overwrite an executable entry point (offset 0x214) and no output, the controls are unchanged, and macos_gs_traps passes in 1.1 s.
  • ctest -R 'gs_trap|macos_gs|tls_function_coverage|sce_code_extent|macos_tls|windows_tls' -j 4 on 566fbb17ef: 43 tests passed in 25.1 s, among them gs_trap_entry_roots (0.2 s), macos_gs_traps (1.0 s), sce_code_extent (1.7 s), windows_tls_protected_entries (0.4 s), tls_function_coverage_rejected (0.4 s), macos_tls_overlap, macos_tls_exports, macos_tls_alu, tls_function_coverage_gs_traps and tls_function_coverage_gs_traps_implicit.
  • Full suite: ctest --test-dir build-relinker -j 8 on 566fbb17ef: 369 tests, 366 passed, long_paths skipped; failures known on this machine on main as well: select_tests (Windows symlink fixture) and patch_advisor_unit (path with spaces, which open #10667 fixes); both failed again alone.
  • python tools/check_conventions.py --base upstream/main with this title and description passed on 566fbb17ef.
  • Not tested on Linux or macOS; CI runs the tests on all three hosts.

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: #11488
  • Fixes: none
  • Unblocks: none
  • GPUs tested: n/a
  • AI-assisted: yes (Claude Code)
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5032959282 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/11883). Originally posted by [roboterspinne](https://github.com/roboterspinne) on 2026-10-27T11:14:26Z. --- ### What `--macos` replaces a guest GS memory instruction with UD2 and NOPs and, of the code entries into it, refuses only a relative branch (#7706); since #11090, `--windows` also refuses any other code entry inside the trapped bytes, but `--macos` does not. On the untouched base 17dffabc20 (#11488 at 9ef8b4b75a merged with `main` 801d5b9bc1), `65 48 8b 04 25 28 00 00 00 c3` at 0x210 with an entry root at 0x214 (a `STT_FUNC` symbol, an `R_X86_64_RELATIVE` pointer, a REX.W `lea` target or a `DT_INIT_ARRAY` entry) converts with `--macos` with exit 0, and the Mach-O holds `0f 0b 90 90 90 90 90 90 90 c3` at file offset 0x10210, so the entry at 0x214 runs NOPs and `ret` instead of `and eax,0x28; ret`; `--windows` refuses all four inputs with `GS trap would overwrite an executable entry point (offset 0x214)`. maximilianfv noted this gap in the review of #11488 and suggested a follow-up PR. - **Rule.** `ScanTls` in `MacOsImage.cpp` records for each trapped GS memory instruction `[start, start + length)` the first collector entry root with `start < root < start + length`, with the same `entryRoots.upper_bound` lookup and file-offset convention as `WindowsTlsBuilder::Build` (#11090). In the `WriteMacOsImage` trap loop, after the existing `Branch enters a guest GS instruction` check and before the `AMD-only instruction is also a guest GS access` check, such a root throws `GS trap would overwrite an executable entry point`; the CLI exits 2 and writes no output. `entryRoots` also contains relative branch targets, so the branch check stays first and keeps its message, which `macos_gs_traps` pins for `--macos` (its offset is pinned for `--windows` by `tls_function_coverage_rejected`). A root at the instruction start is still trapped as before. The entry roots come from #11488, which makes `ScanTls` pass `&entryRoots` instead of `nullptr` to `CodeInstructionCollector::Collect`; this PR therefore depends on #11488, and its diff includes the changes of #11488 (`ScanTls` and one `test_sce_code_extent.py` case) until #11488 merges. A new entry before the macOS GS entry in `docs/dev/debt/relinker.md` lists both macOS messages, the entries each covers, and that no option skips either check; the Windows entry is unchanged. - **Tests.** `test_macos_gs_traps.py` (`macos_gs_traps`) now also converts with `--macos` the entry-root images of `test_windows_tls_protected_entries.py` (`make_image`, which `test_gs_trap_entry_roots.py` uses for `--windows`) with `65 48 8b 04 25 28 00 00 00 c3` at 0x210: roots `relative`, `symbol`, `lea` and `initializer` must exit 2 with the message, `(offset 0x214)` and no output; controls `safe` and `access-start` must convert, with `0f 0b 90 90 90 90 90 90 90 c3` and without the original instruction in the executable ranges of the Mach-O. `test_gs_trap_entry_roots.py` is unchanged. No new test file and no CMake change. - **Overlap.** `pr_overlap.py` reports no shared lines with open pull requests that merge with `main`; the macOS cases sit in `test_macos_gs_traps.py` and the debt entry is a new bullet, so Felipebmaitan's #11628, #11661, #11689 and #11698, which rewrite `test_gs_trap_entry_roots.py` and the Windows GS entry, merge with this branch without conflict. It cannot compare this branch with #5021 (`docs/dev/debt/relinker.md`), #10249 (`MacOsImage.cpp`) and #10861 (`MacOsImage.cpp`, `test_sce_code_extent.py`, `docs/dev/debt/relinker.md`), because those do not merge with `main`. Checked by hand: trial merges with #5021 and #10249 conflict in exactly the files `main` does. With #10861 the only conflict outside `CodeInstructionCollector.cpp`, which conflicts with `main` too, is #11488's `entryRoots` argument of `Collect` in `ScanTls`, the same conflict #11488 alone has; none of this PR's own lines is involved. ### Tested <!-- OS, title or homebrew, what was checked. "Not tested" is a valid answer. A performance change: the summary lines of main and of this PR from the same machine and scene, with the traces attached, captured as the Performance report issue form describes. --> Windows 11, GCC 15.2.0 (WinLibs UCRT), relinker-only Release build (`-DANYPS5_RELINKER_ONLY=ON -DBUILD_TESTING=ON`); synthetic fixtures only, no title or homebrew. - Counter-check: with `MacOsImage.cpp` of the untouched base 17dffabc20 and the extended test, `macos_gs_traps` fails with `entry-relative: expected the GS trap to be refused at offset 0x214: 0` after all earlier cases passed. Run case by case, the four root inputs exited 0 with an output file, and both controls converted with the trapped bytes and without the original instruction. With the change (566fbb17ef) the four root inputs exit 2 with `GS trap would overwrite an executable entry point (offset 0x214)` and no output, the controls are unchanged, and `macos_gs_traps` passes in 1.1 s. - `ctest -R 'gs_trap|macos_gs|tls_function_coverage|sce_code_extent|macos_tls|windows_tls' -j 4` on 566fbb17ef: 43 tests passed in 25.1 s, among them `gs_trap_entry_roots` (0.2 s), `macos_gs_traps` (1.0 s), `sce_code_extent` (1.7 s), `windows_tls_protected_entries` (0.4 s), `tls_function_coverage_rejected` (0.4 s), `macos_tls_overlap`, `macos_tls_exports`, `macos_tls_alu`, `tls_function_coverage_gs_traps` and `tls_function_coverage_gs_traps_implicit`. - Full suite: `ctest --test-dir build-relinker -j 8` on 566fbb17ef: 369 tests, 366 passed, `long_paths` skipped; failures known on this machine on `main` as well: `select_tests` (Windows symlink fixture) and `patch_advisor_unit` (path with spaces, which open #10667 fixes); both failed again alone. - `python tools/check_conventions.py --base upstream/main` with this title and description passed on 566fbb17ef. - Not tested on Linux or macOS; CI runs the tests on all three hosts. ### Checklist <!-- Rules behind each item: https://github.com/boykopovar/AnyPS5/blob/main/CONTRIBUTING.md --> - [x] Based on current `main`; no other open PR implements the same functions - [x] One topic per PR; follow-ups go in a new PR - [x] No comments in code except [technical debt](https://github.com/boykopovar/AnyPS5/blob/main/docs/dev/CONVENTIONS.md) - [x] Unimplemented paths throw (`NotImplemented_nid_no_patch`); silent stubs are listed under Silent stubs in their library's [technical debt](https://github.com/boykopovar/AnyPS5/tree/main/docs/dev/debt) file - [x] No notes, investigation `.md` files or images added to the repository (attach them to this PR) - [x] New third-party code is a submodule built from source - [x] Generic behaviour, not specific to one title - [x] Depends on: <!-- #PR, or none --> #11488 - [x] Fixes: <!-- issue it closes as Fixes #N, or none --> none - [x] Unblocks: <!-- title ID and Game bring-up discussion, e.g. PPSA01325 #3232, or none --> none - [x] GPUs tested: <!-- for AGC, shader recompiler or VideoOut changes: vendor, model, driver and OS of each GPU the GPU tests ran on; otherwise n/a --> n/a - [x] AI-assisted: <!-- yes / no --> yes (Claude Code)
rooklz commented 2026-10-27 11:43:01 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by rooklz on 2026-10-27T11:43:01Z.

Review: APPROVED


Reviewed exact head 566fbb17ef. Note for merge ordering: the branch contains #11488 (head 9ef8b4b75a, fix commit c13d32870f) merged with main 801d5b9bc1 at 17dffabc20, so until #11488 merges the PR diff folds both changesets; the GS-trap delta of this PR is the GsTrap::Entry field, the entryRoots collection in ScanTls, and the WriteMacOsImage refusal.

Checked line-by-line against the Windows builder (#11090):

  • ScanTls collects the same entryRoots from CodeInstructionCollector (roots plus dataTargets, CodeInstructionCollector.cpp:570-572), so the debt line's "pointer target it takes as data" is covered. The trap's Entry uses the identical upper_bound(*instruction) / *entry - *instruction < Length guard and header.Offset + *entry - header.MappedAddress file-offset math as WindowsTlsBuilder.
  • WriteMacOsImage refuses in the same order as the Windows gsTraps loop: relative-branch target first (Branch enters a guest GS instruction), then GS trap would overwrite an executable entry point with the entered byte's file offset.
  • The rewritten fs: accesses keep the pre-existing check order (unsupported-encoding refusal first) and now throw TLS rewrite would overwrite an executable entry point where the Windows builder calls checkProtectedEntry; an insidePrevious 0x65 decode is skipped by the macOS loop guards in exactly the cases Windows's 0x65 && !insidePrevious condition skips it.

Verified locally (ANYPS5_RELINKER_ONLY, macOS): macos_gs_traps, sce_code_extent, windows_tls_protected_entries and gs_trap_entry_roots pass, full suite 361/361. The new check_entry_roots cases reuse the Windows protected-entry fixture for all four root kinds plus two negative roots, and the refusal offsets match the entered byte (0x214 inside the 9-byte GS load). The debt line mirrors the reviewed Windows wording accurately.

(AI-assisted: yes)

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5666499548 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/11883#pullrequestreview-5666499548). Originally posted by [rooklz](https://github.com/rooklz) on 2026-10-27T11:43:01Z. **Review:** APPROVED --- Reviewed exact head `566fbb17ef`. Note for merge ordering: the branch contains #11488 (head `9ef8b4b75a`, fix commit `c13d32870f`) merged with main `801d5b9bc1` at `17dffabc20`, so until #11488 merges the PR diff folds both changesets; the GS-trap delta of this PR is the `GsTrap::Entry` field, the `entryRoots` collection in `ScanTls`, and the `WriteMacOsImage` refusal. Checked line-by-line against the Windows builder (#11090): - `ScanTls` collects the same `entryRoots` from `CodeInstructionCollector` (roots plus `dataTargets`, CodeInstructionCollector.cpp:570-572), so the debt line's "pointer target it takes as data" is covered. The trap's `Entry` uses the identical `upper_bound(*instruction)` / `*entry - *instruction < Length` guard and `header.Offset + *entry - header.MappedAddress` file-offset math as WindowsTlsBuilder. - `WriteMacOsImage` refuses in the same order as the Windows `gsTraps` loop: relative-branch target first (`Branch enters a guest GS instruction`), then `GS trap would overwrite an executable entry point` with the entered byte's file offset. - The rewritten `fs:` accesses keep the pre-existing check order (unsupported-encoding refusal first) and now throw `TLS rewrite would overwrite an executable entry point` where the Windows builder calls `checkProtectedEntry`; an `insidePrevious` `0x65` decode is skipped by the macOS loop guards in exactly the cases Windows's `0x65 && !insidePrevious` condition skips it. Verified locally (`ANYPS5_RELINKER_ONLY`, macOS): `macos_gs_traps`, `sce_code_extent`, `windows_tls_protected_entries` and `gs_trap_entry_roots` pass, full suite 361/361. The new `check_entry_roots` cases reuse the Windows protected-entry fixture for all four root kinds plus two negative roots, and the refusal offsets match the entered byte (0x214 inside the 9-byte GS load). The debt line mirrors the reviewed Windows wording accurately. (AI-assisted: yes)
Sign in to join this conversation.
No reviewers
No labels
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Reference
boykopovar/AnyPS5!11883
No description provided.