fix(relinker): refuse branches into trapped guest GS instructions on Windows #10710

Merged
roboterspinne merged 1 commit from upstream-pr/10710 into main 2026-10-25 08:50:26 +00:00
roboterspinne commented 2026-10-24 18:44:09 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by roboterspinne on 2026-10-24T18:44:09Z.


What

Windows output replaces each guest GS memory instruction with ud2 and NOP fill of its own length (#6102) but did not compare the decoded direct branch targets with the replaced bytes. A fixture whose function body is 74 01 65 48 8b 0c 25 78 03 00 00 c3 (je to the byte after the 0x65 prefix of mov rcx, gs:[0x378]) relinked with --windows with exit 0, and the PE held 74 01 0f 0b 90 90 90 90 90 90 90 c3 there, so the taken branch executes or edx, [rax-0x6f6f6f70] instead of the guest's mov rcx, [0x378]. --macos refuses the same input with FAIL: Branch enters a guest GS instruction (offset 0x11243) since #7706, merged 2026-10-24.

  • Rule. WindowsTlsBuilder::Build sorts and deduplicates the collected branch targets before the GS trap loop instead of before the FS rewrite loop, which keeps using them, and refuses a trap when a target lies strictly inside it with Branch enters a guest GS instruction at the target RVA: the check and message of MacOsImage.cpp from #7706, and the check this file already applies to FS rewrites (Branch enters a guest TLS instruction). A branch to the first byte still converts and reaches the ud2. The trap loop runs before the PT_TLS early returns, so images without PT_TLS are covered; guest modules go through the same Build call (WindowsGuestModuleWriter.cpp). No earlier stage refuses the input: CodeInstructionCollector rejects overlapping decodings only when the span contains an FS prefix. #6102 states that "branch targets inside it do not matter"; that holds only for a target at the first byte.
  • Tests. One case, branch-into-gs, at the end of the rejected group of test_tls_function_coverage.py, with the body of the macOS case in test_macos_gs_traps.py: --windows exits 2 with the message at offset 0x11243 and writes no output. On main the case fails because the relinker exits 0.
  • Overlap. pr_overlap.py reports no overlap with open pull requests.

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 on untouched main (d0e7fe7beb): tls_function_coverage_rejected fails on the new case with AssertionError at assert result.returncode == 2 and error in result.stderr and not output.exists(); the relinker exited 0 with empty stderr. The same build converts a branch into the last byte (74 08) on --windows and refuses it on --macos at 0x1124a.
  • With the change: --windows refuses 74 01 at 0x11243 and 74 08 at 0x1124a like --macos; a branch to the first byte (74 00) and one past the instruction (74 09) still convert on both, the Windows output holding 0f 0b and seven NOPs. tls_function_coverage_rejected 0.45 s, tls_function_coverage_gs_traps 0.85 s, tls_function_coverage_gs_traps_implicit 1.01 s, macos_gs_traps 0.73 s; ctest -R 'tls|gs|windows': 68 of 68 passed.
  • ctest --test-dir build-relinker -j 4: 244 tests, 241 passed, long_paths skipped; select_tests (FileExistsError, Windows symlink fixture) and patch_advisor_unit (build path with spaces, addressed by open #10667) fail on this machine on main as well.
  • Run on this host: with xor eax, eax; je +1; mov eax, gs:[rip+disp]; ret, the operand holding 42, the main Windows output exits with 0xC0000005; with the branch to the instruction start it exits with 0xC000001D (the intended ud2), and with 0x90 in place of the prefix with 42. With the change the first input is refused at 0x11245 and the other two are unchanged.
  • Prospero Win (PPSA99995), the only title with known guest GS instructions, was not re-converted; it is not available here.
  • python tools/check_conventions.py --base upstream/main with this title and description passed.
  • 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:4998125503 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/10710). Originally posted by [roboterspinne](https://github.com/roboterspinne) on 2026-10-24T18:44:09Z. --- ### What Windows output replaces each guest GS memory instruction with `ud2` and NOP fill of its own length (#6102) but did not compare the decoded direct branch targets with the replaced bytes. A fixture whose function body is `74 01 65 48 8b 0c 25 78 03 00 00 c3` (`je` to the byte after the `0x65` prefix of `mov rcx, gs:[0x378]`) relinked with `--windows` with exit 0, and the PE held `74 01 0f 0b 90 90 90 90 90 90 90 c3` there, so the taken branch executes `or edx, [rax-0x6f6f6f70]` instead of the guest's `mov rcx, [0x378]`. `--macos` refuses the same input with `FAIL: Branch enters a guest GS instruction (offset 0x11243)` since #7706, merged 2026-10-24. - **Rule.** `WindowsTlsBuilder::Build` sorts and deduplicates the collected branch targets before the GS trap loop instead of before the FS rewrite loop, which keeps using them, and refuses a trap when a target lies strictly inside it with `Branch enters a guest GS instruction` at the target RVA: the check and message of `MacOsImage.cpp` from #7706, and the check this file already applies to FS rewrites (`Branch enters a guest TLS instruction`). A branch to the first byte still converts and reaches the `ud2`. The trap loop runs before the `PT_TLS` early returns, so images without `PT_TLS` are covered; guest modules go through the same `Build` call (`WindowsGuestModuleWriter.cpp`). No earlier stage refuses the input: `CodeInstructionCollector` rejects overlapping decodings only when the span contains an FS prefix. #6102 states that "branch targets inside it do not matter"; that holds only for a target at the first byte. - **Tests.** One case, `branch-into-gs`, at the end of the `rejected` group of `test_tls_function_coverage.py`, with the body of the macOS case in `test_macos_gs_traps.py`: `--windows` exits 2 with the message at offset `0x11243` and writes no output. On `main` the case fails because the relinker exits 0. - **Overlap.** `pr_overlap.py` reports no overlap with open pull requests. ### 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 on untouched `main` (d0e7fe7beb): `tls_function_coverage_rejected` fails on the new case with `AssertionError` at `assert result.returncode == 2 and error in result.stderr and not output.exists()`; the relinker exited 0 with empty stderr. The same build converts a branch into the last byte (`74 08`) on `--windows` and refuses it on `--macos` at `0x1124a`. - With the change: `--windows` refuses `74 01` at `0x11243` and `74 08` at `0x1124a` like `--macos`; a branch to the first byte (`74 00`) and one past the instruction (`74 09`) still convert on both, the Windows output holding `0f 0b` and seven NOPs. `tls_function_coverage_rejected` 0.45 s, `tls_function_coverage_gs_traps` 0.85 s, `tls_function_coverage_gs_traps_implicit` 1.01 s, `macos_gs_traps` 0.73 s; `ctest -R 'tls|gs|windows'`: 68 of 68 passed. - `ctest --test-dir build-relinker -j 4`: 244 tests, 241 passed, `long_paths` skipped; `select_tests` (`FileExistsError`, Windows symlink fixture) and `patch_advisor_unit` (build path with spaces, addressed by open #10667) fail on this machine on `main` as well. - Run on this host: with `xor eax, eax; je +1; mov eax, gs:[rip+disp]; ret`, the operand holding 42, the `main` Windows output exits with `0xC0000005`; with the branch to the instruction start it exits with `0xC000001D` (the intended `ud2`), and with `0x90` in place of the prefix with 42. With the change the first input is refused at `0x11245` and the other two are unchanged. - Prospero Win (PPSA99995), the only title with known guest GS instructions, was not re-converted; it is not available here. - `python tools/check_conventions.py --base upstream/main` with this title and description passed. - 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-24 18:54:20 +00:00 (Migrated from github.com)

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

Review: APPROVED


Reviewed exact head 7424949b8b against current main. The branch-target range check rejects targets strictly inside the bytes replaced by the GS trap while allowing the instruction start, and the check runs before the no-PT_TLS return. The focused tls_function_coverage_rejected CTest passed locally (1/1); diff-check, current-main conventions and merge-tree pass. Hosted Linux, Windows, ASAN and macOS x86_64 jobs pass; macOS arm64 is still pending. No blocking source issue found.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5639503610 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/10710#pullrequestreview-5639503610). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-24T18:54:20Z. **Review:** APPROVED --- Reviewed exact head 7424949b8bb2dd04011a683dbc1949b8ee6cd7de against current main. The branch-target range check rejects targets strictly inside the bytes replaced by the GS trap while allowing the instruction start, and the check runs before the no-PT_TLS return. The focused `tls_function_coverage_rejected` CTest passed locally (1/1); diff-check, current-main conventions and merge-tree pass. Hosted Linux, Windows, ASAN and macOS x86_64 jobs pass; macOS arm64 is still pending. No blocking source issue found.
MarcZX10 commented 2026-10-25 00:12:38 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-25T00:12:38Z.

Review: APPROVED


The fix sorts and deduplicates decoded branch targets before the GS trap loop, then rejects only targets strictly inside a trapped instruction; a target at the first byte still reaches the existing ud2. The ordering also keeps the targets available for the later FS rewrite checks and covers the no-PT_TLS early return. I checked the new branch-into-prefix fixture, conventions, and git diff --check; hosted relinker and ASan suites pass across Linux, Windows, macOS and macOS x86_64. Local execution was unavailable because this checkout has no built relinker.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5640721099 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/10710#pullrequestreview-5640721099). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-25T00:12:38Z. **Review:** APPROVED --- The fix sorts and deduplicates decoded branch targets before the GS trap loop, then rejects only targets strictly inside a trapped instruction; a target at the first byte still reaches the existing `ud2`. The ordering also keeps the targets available for the later FS rewrite checks and covers the no-PT_TLS early return. I checked the new branch-into-prefix fixture, conventions, and `git diff --check`; hosted relinker and ASan suites pass across Linux, Windows, macOS and macOS x86_64. Local execution was unavailable because this checkout has no built relinker.
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!10710
No description provided.