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

Merged
roboterspinne merged 3 commits from upstream-pr/11090 into main 2026-10-27 05:32:03 +00:00
roboterspinne commented 2026-10-25 17:26:31 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by roboterspinne on 2026-10-25T17:26:31Z.


What

--windows replaces a guest GS memory instruction with UD2 and NOPs and refuses only a relative branch into it (#10710). On untouched 4017310678, 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) converted with exit 0, and the PE held 0f 0b 90 90 90 90 90 90 90 c3 at RVA 0x10210, so the entry at 0x214 ran NOPs and ret instead of and eax,0x28; ret, which Linux output keeps. CodeInstructionCollector does not refuse this layout: its overlap rule fires only when the overlapped span holds a 0x64 byte or the previous instruction has an FS prefix, and the same layout with 64 already fails with Code analysis: overlapping instruction boundaries.

  • Rule. WindowsTlsBuilder::Build records for each trapped GS memory instruction [start, start + length) the first collector entry root with start < root < start + length, found with the same entryRoots.upper_bound lookup that TLS rewrites use (#5890). In the trap loop, after the existing Branch enters a guest GS instruction check, such a root throws GS trap would overwrite an executable entry point at the root's file offset; the CLI exits 2 and writes no output. entryRoots also contains relative branch targets, so the branch check stays first and keeps its message and offset. A root at the instruction start is still trapped as before. entryRoots also holds pointer targets the collector takes as data, so such a target inside a trapped instruction is refused too, as TLS rewrites already do. checkProtectedEntry is not reused because its message names a TLS rewrite. Windows only; the macOS counterpart in MacOsImage.cpp is a follow-up PR, because ScanTls collects without entryRoots on the line #10861 rewrites. A new entry in docs/dev/debt/relinker.md, before the null-symbol relocation entry, states both refusals, that the messages give the position of the entered byte, that no option skips the checks, and that Linux output keeps the instruction with libkernel's sysarch setting the GS base; the Windows GS entry and the macOS GS entry are unchanged.
  • Tests. New test_gs_trap_entry_roots.py, built on make_image and pe_bytes_at from test_windows_tls_protected_entries.py, with the GS load at 0x210: roots relative, symbol, lea and initializer must fail 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 at RVA 0x10210. Registered in core/relinker/CMakeLists.txt after windows_tls_protected_entries.
  • Overlap. Checked by hand on main f95acfc948: 32 other open pull requests touch core/relinker/CMakeLists.txt or WindowsTlsBuilder.cpp. All of them except #5021, #6713 and #10861 merge with main and with this branch without conflicts. Those three conflict with main, none of them in lines of this PR. #5021 adds macos_fixtures after macos_output, more than 100 lines below gs_trap_entry_roots (line 554, after windows_tls_protected_entries). #6713 adds windows_process_parameters after elf_header and conflicts with main only in docs/dev/debt/libc.md. #10861 changes core/relinker/CMakeLists.txt at its lines 248 and 774. In WindowsTlsBuilder.cpp it changes the signature of Build and the Collect call three lines below the gsTraps declaration that this PR changes. It conflicts with main in GuestModuleBuilder.cpp, RelinkerPipeline.cpp and docs/dev/debt/relinker.md. Its extraEntries become collector entry roots, so after both merge, a GS trap that holds one after its first byte is refused as well. docs/dev/debt/relinker.md is touched by 27 open pull requests. With the entry inserted before the null-symbol entry, git merge-tree --write-tree against each of them from its merge base with this branch reports the same conflicting files as without the entry, and the existing conflict hunk of #10861 in that file is unchanged; an extension of the Windows GS entry in place would have conflicted with #10518, which inserts an entry directly before it.

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, relinked outputs not run.

  • Counter-check (run on 4017310678, the previous base; rebased onto 3a2c51afd2 with no relinker changes since): gs_trap_entry_roots on untouched 4017310678 fails with AssertionError: ('relative', 0, ''); run case by case, all four root inputs exited 0 with an output file and 0f 0b 90 90 90 90 90 90 90 c3 at RVA 0x10210, and both controls passed. With the change all four exit 2 with FAIL: GS trap would overwrite an executable entry point (offset 0x214) and no output, the controls are unchanged, and the test passes in 0.4 s.
  • On 3a2c51afd2 with the change: ctest -R 'gs_trap|tls_function_coverage|windows_tls|windows_gs' -j 4: 38 tests passed in 26.4 s, among them gs_trap_entry_roots (0.8 s), windows_tls_protected_entries, windows_tls_zero_fill, windows_tls_thread_pointer, tls_function_coverage_gs_traps and tls_function_coverage_gs_traps_implicit. macos_gs_traps also passed.
  • Merged current main (f95acfc948) into the branch without conflicts (66f6fa3a32); since 3a2c51afd2, main changed WindowsTlsBuilder.cpp only through #11000 (an empty-TLS check after the trap loop) and core/relinker/CMakeLists.txt away from this PR's registration. On 66f6fa3a32, ctest -R 'gs_trap|tls_function_coverage|windows_tls|windows_gs' -j 6: 39 tests passed, among them gs_trap_entry_roots (1.3 s), windows_tls_protected_entries, windows_tls_zero_fill, windows_tls_thread_pointer, macos_gs_traps, tls_function_coverage_gs_traps and tls_function_coverage_gs_traps_implicit.
  • Full suite on 66f6fa3a32: ctest --test-dir build-relinker -j 6: 311 tests, 308 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.
  • The debt entry is a separate commit (823251ddf5) that changes only docs/dev/debt/relinker.md; the test runs above are on 66f6fa3a32, whose code and tests are the same. python tools/check_conventions.py --base upstream/main with this title and description passed on 823251ddf5.
  • 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: none
  • Fixes: none
  • Unblocks: none
  • GPUs tested: n/a
  • AI-assisted: yes (Claude Code)
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5006972470 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/11090). Originally posted by [roboterspinne](https://github.com/roboterspinne) on 2026-10-25T17:26:31Z. --- ### What `--windows` replaces a guest GS memory instruction with UD2 and NOPs and refuses only a relative branch into it (#10710). On untouched 4017310678, `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) converted with exit 0, and the PE held `0f 0b 90 90 90 90 90 90 90 c3` at RVA 0x10210, so the entry at 0x214 ran NOPs and `ret` instead of `and eax,0x28; ret`, which Linux output keeps. `CodeInstructionCollector` does not refuse this layout: its overlap rule fires only when the overlapped span holds a `0x64` byte or the previous instruction has an FS prefix, and the same layout with `64` already fails with `Code analysis: overlapping instruction boundaries`. - **Rule.** `WindowsTlsBuilder::Build` records for each trapped GS memory instruction `[start, start + length)` the first collector entry root with `start < root < start + length`, found with the same `entryRoots.upper_bound` lookup that TLS rewrites use (#5890). In the trap loop, after the existing `Branch enters a guest GS instruction` check, such a root throws `GS trap would overwrite an executable entry point` at the root's file offset; the CLI exits 2 and writes no output. `entryRoots` also contains relative branch targets, so the branch check stays first and keeps its message and offset. A root at the instruction start is still trapped as before. `entryRoots` also holds pointer targets the collector takes as data, so such a target inside a trapped instruction is refused too, as TLS rewrites already do. `checkProtectedEntry` is not reused because its message names a TLS rewrite. Windows only; the macOS counterpart in `MacOsImage.cpp` is a follow-up PR, because `ScanTls` collects without `entryRoots` on the line #10861 rewrites. A new entry in `docs/dev/debt/relinker.md`, before the null-symbol relocation entry, states both refusals, that the messages give the position of the entered byte, that no option skips the checks, and that Linux output keeps the instruction with libkernel's `sysarch` setting the GS base; the Windows GS entry and the macOS GS entry are unchanged. - **Tests.** New `test_gs_trap_entry_roots.py`, built on `make_image` and `pe_bytes_at` from `test_windows_tls_protected_entries.py`, with the GS load at 0x210: roots `relative`, `symbol`, `lea` and `initializer` must fail 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` at RVA 0x10210. Registered in `core/relinker/CMakeLists.txt` after `windows_tls_protected_entries`. - **Overlap.** Checked by hand on `main` f95acfc948: 32 other open pull requests touch `core/relinker/CMakeLists.txt` or `WindowsTlsBuilder.cpp`. All of them except #5021, #6713 and #10861 merge with `main` and with this branch without conflicts. Those three conflict with `main`, none of them in lines of this PR. #5021 adds `macos_fixtures` after `macos_output`, more than 100 lines below `gs_trap_entry_roots` (line 554, after `windows_tls_protected_entries`). #6713 adds `windows_process_parameters` after `elf_header` and conflicts with `main` only in `docs/dev/debt/libc.md`. #10861 changes `core/relinker/CMakeLists.txt` at its lines 248 and 774. In `WindowsTlsBuilder.cpp` it changes the signature of `Build` and the `Collect` call three lines below the `gsTraps` declaration that this PR changes. It conflicts with `main` in `GuestModuleBuilder.cpp`, `RelinkerPipeline.cpp` and `docs/dev/debt/relinker.md`. Its `extraEntries` become collector entry roots, so after both merge, a GS trap that holds one after its first byte is refused as well. `docs/dev/debt/relinker.md` is touched by 27 open pull requests. With the entry inserted before the null-symbol entry, `git merge-tree --write-tree` against each of them from its merge base with this branch reports the same conflicting files as without the entry, and the existing conflict hunk of #10861 in that file is unchanged; an extension of the Windows GS entry in place would have conflicted with #10518, which inserts an entry directly before it. ### 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, relinked outputs not run. - Counter-check (run on 4017310678, the previous base; rebased onto 3a2c51afd2 with no relinker changes since): `gs_trap_entry_roots` on untouched 4017310678 fails with `AssertionError: ('relative', 0, '')`; run case by case, all four root inputs exited 0 with an output file and `0f 0b 90 90 90 90 90 90 90 c3` at RVA 0x10210, and both controls passed. With the change all four exit 2 with `FAIL: GS trap would overwrite an executable entry point (offset 0x214)` and no output, the controls are unchanged, and the test passes in 0.4 s. - On 3a2c51afd2 with the change: `ctest -R 'gs_trap|tls_function_coverage|windows_tls|windows_gs' -j 4`: 38 tests passed in 26.4 s, among them `gs_trap_entry_roots` (0.8 s), `windows_tls_protected_entries`, `windows_tls_zero_fill`, `windows_tls_thread_pointer`, `tls_function_coverage_gs_traps` and `tls_function_coverage_gs_traps_implicit`. `macos_gs_traps` also passed. - Merged current `main` (f95acfc948) into the branch without conflicts (66f6fa3a32); since 3a2c51afd2, `main` changed `WindowsTlsBuilder.cpp` only through #11000 (an empty-TLS check after the trap loop) and `core/relinker/CMakeLists.txt` away from this PR's registration. On 66f6fa3a32, `ctest -R 'gs_trap|tls_function_coverage|windows_tls|windows_gs' -j 6`: 39 tests passed, among them `gs_trap_entry_roots` (1.3 s), `windows_tls_protected_entries`, `windows_tls_zero_fill`, `windows_tls_thread_pointer`, `macos_gs_traps`, `tls_function_coverage_gs_traps` and `tls_function_coverage_gs_traps_implicit`. - Full suite on 66f6fa3a32: `ctest --test-dir build-relinker -j 6`: 311 tests, 308 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. - The debt entry is a separate commit (823251ddf5) that changes only `docs/dev/debt/relinker.md`; the test runs above are on 66f6fa3a32, whose code and tests are the same. `python tools/check_conventions.py --base upstream/main` with this title and description passed on 823251ddf5. - 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 --> none - [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)
MarcZX10 commented 2026-10-25 18:21:54 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-25T18:21:54Z.

Review: APPROVED


Reviewed exact head 33b48ba39f0155d466ae028e6d838a8611e83bb1. The Windows GS trap now checks the next executable entry root strictly inside the instruction span before replacing the bytes, while preserving the earlier branch-target error path. The fixture covers relocation, symbol, LEA and initializer roots, plus an entry at the instruction start and a safe trap. I ran gs_trap_entry_roots and windows_tls_protected_entries locally (2/2 pass), git diff --check upstream/main...HEAD, and tools/check_conventions.py --base upstream/main --head HEAD (pass). Hosted Linux, Windows, macOS/arm64 and ASan relinker checks pass. Hosted macOS/x86_64 reports one failure in unrelated masked_store_lowering (“Masked store stub changed the flags”); the PR’s new and adjacent tests passed there. I have not run a macOS build locally.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5644630771 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/11090#pullrequestreview-5644630771). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-25T18:21:54Z. **Review:** APPROVED --- Reviewed exact head `33b48ba39f0155d466ae028e6d838a8611e83bb1`. The Windows GS trap now checks the next executable entry root strictly inside the instruction span before replacing the bytes, while preserving the earlier branch-target error path. The fixture covers relocation, symbol, LEA and initializer roots, plus an entry at the instruction start and a safe trap. I ran `gs_trap_entry_roots` and `windows_tls_protected_entries` locally (2/2 pass), `git diff --check upstream/main...HEAD`, and `tools/check_conventions.py --base upstream/main --head HEAD` (pass). Hosted Linux, Windows, macOS/arm64 and ASan relinker checks pass. Hosted macOS/x86_64 reports one failure in unrelated `masked_store_lowering` (“Masked store stub changed the flags”); the PR’s new and adjacent tests passed there. I have not run a macOS build locally.
klosixgaming-arch commented 2026-10-25 21:45:53 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by klosixgaming-arch on 2026-10-25T21:45:53Z.

Review: APPROVED


Reviewed head 33b48ba39f by reading the diff and the existing review thread. I did not build or run this, so everything below is from the code, not from a test of mine.

What it does. The Windows TLS builder used to replace a guest GS-prefixed memory instruction with a trap (ud2 plus padding) without checking whether an executable entry point lay inside the bytes it overwrote. Now, if an entry root falls strictly inside the instruction (after its first byte), conversion fails with "GS trap would overwrite an executable entry point" and the offset; a root exactly at the start of the instruction is still fine.

Checked: entryRoots.upper_bound(*instruction) gives the first root after the instruction start, and *entry - *instruction < info.Length limits it to inside the instruction, so a root at the start is excluded by upper_bound and one at start + Length is excluded by the comparison. The file offset is computed from the section header the same way as the neighbouring code. The new gs_trap_entry_roots test covers four ways an entry root can land there (relative, symbol, LEA, initializer) and two cases that must still convert (a safe image and an entry root at the instruction start), and it checks the trapped bytes.

Notes:

  • This turns a silent corruption into a hard conversion failure. That is the right call, but it also means an image that used to convert (and then misbehave) will now be refused with no workaround. A debt entry or a line in the relinker docs saying what to do when you hit it would help the next user.
  • The hosted macOS x86_64 job reported one failure, masked_store_lowering. The author says the PR does not touch it, and the diff agrees (only WindowsTlsBuilder.cpp, a new test and its registration). MarcZX10 ran the new test locally from the head. I'd still re-run that job once to confirm it was a flake, because a "pre-existing failure" is easy to claim and cheap to check.

Approving.

AI-assisted: yes. Written with AI assistance and reviewed by me.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5645480370 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/11090#pullrequestreview-5645480370). Originally posted by [klosixgaming-arch](https://github.com/klosixgaming-arch) on 2026-10-25T21:45:53Z. **Review:** APPROVED --- Reviewed head `33b48ba39f` by reading the diff and the existing review thread. I did not build or run this, so everything below is from the code, not from a test of mine. **What it does.** The Windows TLS builder used to replace a guest GS-prefixed memory instruction with a trap (`ud2` plus padding) without checking whether an executable entry point lay inside the bytes it overwrote. Now, if an entry root falls strictly inside the instruction (after its first byte), conversion fails with "GS trap would overwrite an executable entry point" and the offset; a root exactly at the start of the instruction is still fine. **Checked:** `entryRoots.upper_bound(*instruction)` gives the first root *after* the instruction start, and `*entry - *instruction < info.Length` limits it to inside the instruction, so a root at the start is excluded by `upper_bound` and one at `start + Length` is excluded by the comparison. The file offset is computed from the section header the same way as the neighbouring code. The new `gs_trap_entry_roots` test covers four ways an entry root can land there (relative, symbol, LEA, initializer) and two cases that must still convert (a safe image and an entry root at the instruction start), and it checks the trapped bytes. **Notes:** - This turns a silent corruption into a hard conversion failure. That is the right call, but it also means an image that used to convert (and then misbehave) will now be refused with no workaround. A debt entry or a line in the relinker docs saying what to do when you hit it would help the next user. - The hosted macOS x86_64 job reported one failure, `masked_store_lowering`. The author says the PR does not touch it, and the diff agrees (only `WindowsTlsBuilder.cpp`, a new test and its registration). MarcZX10 ran the new test locally from the head. I'd still re-run that job once to confirm it was a flake, because a "pre-existing failure" is easy to claim and cheap to check. Approving. AI-assisted: yes. Written with AI assistance and reviewed by me.
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!11090
No description provided.