fix(relinker): keep FS and GS accesses out of AMD-only sites on Windows #27649

Merged
GiannisG1 merged 1 commit from upstream-pr/27649 into main 2026-11-27 09:29:09 +00:00
GiannisG1 commented 2026-11-26 21:40:27 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by GiannisG1 on 2026-11-26T21:40:27Z.


What

On Windows, WindowsPePatcher (and WindowsGuestModuleWriter for guest modules) runs WindowsTlsBuilder::Build before WindowsTrampolineBuilder::Build. The TLS builder rewrites guest fs:/gs: accesses in the section bytes, and the trampoline builder then checks that each AMD-only site still has its original bytes. The AMD-only converter only stays clear of segment-prefixed instructions when it is created with keepSegmentAccesses. Conversion.cpp and GuestModuleBuilder.cpp passed that for macOS only (options.Target == TargetSystem::MacOs, macos). So on Windows, an AMD-only instruction too short for its jump could absorb the fs: access after it. The TLS builder then rewrote that access inside the site, and the conversion failed:

FAIL: AMD-only site bytes changed before patching

This is the failure @akkadbakkad89 dumped for PPSA38500 in #27564 (issuecomment-7202722298): a 13-byte site of vrcpps xmm4, xmm2 (c5 f8 53 e2, the optional RCPPS lowering) and the following mov rax, fs:[0], whose bytes had become the TLS builder's e9 … 90 90 90 90. The converter is now created with segment accesses kept on Windows as on macOS, for the executable and for the guest modules. A short site then does not take the fs: access, so WindowsTlsBuilder alone owns it. An optional lowering without room is kept, as RCPPS already is wherever its neighbours cannot move. Linux, which does not rewrite segment accesses, is unchanged. The same collision in the --frame-red-zone path (a frame stub spanning an fs:/gs: access) is #27668. It touches RedZoneFramer.cpp only, so the two are independent.

Tested

Linux Mint 22.3, kernel 7.0.0, GCC 13.3, relinker-only build (-DANYPS5_RELINKER_ONLY=ON). Head on main 7c5d920df6; it merges cleanly with main 00b15fcbb0.

  • New fsAccessAfterShortAmdOnlySite in ConversionTests.cpp. It converts an executable whose code is vrcpps xmm4, xmm2; mov rax, fs:[0]; ret (with a PT_TLS header) with --to-intel, for Windows and for Linux. RED on main: the Windows conversion throws AMD-only site bytes changed before patching, the PPSA38500 error. GREEN 3/3 with the change.
  • All 1141 relinker tests pass, no skips (check_skips.py).
  • Windows --to-intel relink A/B of all 223 ps5-homebrew-catalog titles, from the same build tree with main's Conversion.cpp/GuestModuleBuilder.cpp and with this change, outputs compared byte for byte. 222 are identical. PPSA99193 fails on both with the branch-entry failure that #27560 fixes. So no catalog title has a short AMD-only site next to an fs:/gs: access, and the change only affects conversions like PPSA38500's.
  • Third-party run, not here (we have no copy): @akkadbakkad89 applied this change locally on top of #27560 e269ebc5d5 and relinked PPSA38500 on Windows. Its eboot now converts completely with --to-intel (full instruction scan, PRX conversion, output image written, no FAIL; #27564, issuecomment-7206089884). Before, it stopped at AMD-only site bytes changed before patching at RVA 0xfe10fd. Booted on the same machine with a warm shader cache, the converted title runs at 29.97 fps, against 0.90-0.92 fps when its AMD-only instructions are trapped and emulated at run time instead (issuecomment-7206202354): same boot depth, no new crashes.
  • check_conventions.py, check_test_registration.py (relinker build), check_compile.py (3 translation units) and check_main_sync.py are clean.
  • pr_overlap.py --branch: #24915 adds lines next to the same two MakeAmd64OnlyConverter calls (GuestModuleBuilder.cpp L522, Conversion.cpp L75) to record host CPU features. That is a textual conflict only, and whoever lands second keeps both. It could not compare #10861, #11525, #12678, #15245, #20389, #21877 and #24152, which conflict with main. Of these, #11525, #12678 and #20389 rewrite those call lines from older bases, and none of them changes which targets keep segment accesses.

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 and the licence notice of code adapted from upstream (see AGENTS.md)
  • 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 (independent of #27560; PPSA38500 needs both to convert past this point)
  • Fixes: #27643
  • Unblocks: PPSA38500's Windows --to-intel conversion past this site (with #27560), as reported in #27564 and Game bring-up #27523
  • GPUs tested: n/a
  • AI-assisted: yes

🤖 Generated with Claude Code

Summary by CodeRabbit

  • Bug Fixes
    • Improved Windows executable conversion compatibility for certain instruction sequences involving thread-local storage access.
    • Windows conversions now use the appropriate platform-specific conversion behavior, while Linux behavior remains unchanged.
  • Tests
    • Added coverage for Windows and Linux conversions involving these instruction sequences.
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5517509324 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/27649). Originally posted by [GiannisG1](https://github.com/GiannisG1) on 2026-11-26T21:40:27Z. --- ### What On Windows, `WindowsPePatcher` (and `WindowsGuestModuleWriter` for guest modules) runs `WindowsTlsBuilder::Build` before `WindowsTrampolineBuilder::Build`. The TLS builder rewrites guest `fs:`/`gs:` accesses in the section bytes, and the trampoline builder then checks that each AMD-only site still has its original bytes. The AMD-only converter only stays clear of segment-prefixed instructions when it is created with `keepSegmentAccesses`. `Conversion.cpp` and `GuestModuleBuilder.cpp` passed that for macOS only (`options.Target == TargetSystem::MacOs`, `macos`). So on Windows, an AMD-only instruction too short for its jump could absorb the `fs:` access after it. The TLS builder then rewrote that access inside the site, and the conversion failed: ``` FAIL: AMD-only site bytes changed before patching ``` This is the failure @akkadbakkad89 dumped for PPSA38500 in #27564 (issuecomment-7202722298): a 13-byte site of `vrcpps xmm4, xmm2` (`c5 f8 53 e2`, the optional RCPPS lowering) and the following `mov rax, fs:[0]`, whose bytes had become the TLS builder's `e9 … 90 90 90 90`. The converter is now created with segment accesses kept on Windows as on macOS, for the executable and for the guest modules. A short site then does not take the `fs:` access, so `WindowsTlsBuilder` alone owns it. An optional lowering without room is kept, as RCPPS already is wherever its neighbours cannot move. Linux, which does not rewrite segment accesses, is unchanged. The same collision in the `--frame-red-zone` path (a frame stub spanning an `fs:`/`gs:` access) is #27668. It touches `RedZoneFramer.cpp` only, so the two are independent. ### Tested Linux Mint 22.3, kernel 7.0.0, GCC 13.3, relinker-only build (`-DANYPS5_RELINKER_ONLY=ON`). Head on `main` `7c5d920df6`; it merges cleanly with `main` `00b15fcbb0`. - New `fsAccessAfterShortAmdOnlySite` in `ConversionTests.cpp`. It converts an executable whose code is `vrcpps xmm4, xmm2; mov rax, fs:[0]; ret` (with a `PT_TLS` header) with `--to-intel`, for Windows and for Linux. RED on `main`: the Windows conversion throws `AMD-only site bytes changed before patching`, the PPSA38500 error. GREEN 3/3 with the change. - All 1141 relinker tests pass, no skips (`check_skips.py`). - Windows `--to-intel` relink A/B of all 223 ps5-homebrew-catalog titles, from the same build tree with `main`'s `Conversion.cpp`/`GuestModuleBuilder.cpp` and with this change, outputs compared byte for byte. 222 are identical. PPSA99193 fails on both with the branch-entry failure that #27560 fixes. So no catalog title has a short AMD-only site next to an `fs:`/`gs:` access, and the change only affects conversions like PPSA38500's. - Third-party run, not here (we have no copy): @akkadbakkad89 applied this change locally on top of #27560 `e269ebc5d5` and relinked PPSA38500 on Windows. Its eboot now converts completely with `--to-intel` (full instruction scan, PRX conversion, output image written, no FAIL; #27564, issuecomment-7206089884). Before, it stopped at `AMD-only site bytes changed before patching` at RVA `0xfe10fd`. Booted on the same machine with a warm shader cache, the converted title runs at 29.97 fps, against 0.90-0.92 fps when its AMD-only instructions are trapped and emulated at run time instead (issuecomment-7206202354): same boot depth, no new crashes. - `check_conventions.py`, `check_test_registration.py` (relinker build), `check_compile.py` (3 translation units) and `check_main_sync.py` are clean. - `pr_overlap.py --branch`: #24915 adds lines next to the same two `MakeAmd64OnlyConverter` calls (`GuestModuleBuilder.cpp` L522, `Conversion.cpp` L75) to record host CPU features. That is a textual conflict only, and whoever lands second keeps both. It could not compare #10861, #11525, #12678, #15245, #20389, #21877 and #24152, which conflict with `main`. Of these, #11525, #12678 and #20389 rewrite those call lines from older bases, and none of them changes which targets keep segment accesses. ### 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) and the licence notice of code adapted from upstream (see AGENTS.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: none (independent of #27560; PPSA38500 needs both to convert past this point) - [x] Fixes: #27643 - [x] Unblocks: PPSA38500's Windows `--to-intel` conversion past this site (with #27560), as reported in #27564 and Game bring-up #27523 - [x] GPUs tested: n/a - [x] AI-assisted: yes 🤖 Generated with [Claude Code](https://claude.com/claude-code) <!-- This is an auto-generated comment: release notes by coderabbit.ai --> <!-- coderabbit-ai-generated:v1 --> ## Summary by CodeRabbit * **Bug Fixes** * Improved Windows executable conversion compatibility for certain instruction sequences involving thread-local storage access. * Windows conversions now use the appropriate platform-specific conversion behavior, while Linux behavior remains unchanged. * **Tests** * Added coverage for Windows and Linux conversions involving these instruction sequences. <!-- end of auto-generated comment: release notes by coderabbit.ai -->
MarcZX10 commented 2026-11-26 22:29:52 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-11-26T22:29:52Z.

Review: APPROVED


Reviewed — looks good

Nothing blocks this; the points below are optional.

What I ran

Reviewed head 9df408a0e57943ab5024dbb1377e337cb4dbb1ab. No test or build commands were run; the test results in the PR description are author-reported.

Results

Check Result Note
Diff and changed code passed Three changed files read
Tests and build not run Author reports 1141 tests passed

Findings

Blocking

None.

Non-blocking

None.

Not verified

The full 1141-test run, Windows behavior, guest-module behavior, and reported catalog comparison were not independently verified. All three changed files were read: core/relinker/relinker/src/guest/GuestModuleBuilder.cpp, core/relinker/relinker/src/pipeline/Conversion.cpp, and core/relinker/relinker/tests/ConversionTests.cpp. The manifest lists three reviewed files; none were excluded. Relevant converter code in core/relinker/codegen/src/Amd64OnlyConverter.cpp was also read for context.

To get this to a good state

Nothing is required.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:6081133916 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/27649#pullrequestreview-6081133916). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-11-26T22:29:52Z. **Review:** APPROVED --- # Reviewed — looks good Nothing blocks this; the points below are optional. ## What I ran Reviewed head `9df408a0e57943ab5024dbb1377e337cb4dbb1ab`. No test or build commands were run; the test results in the PR description are author-reported. ## Results | Check | Result | Note | |---|---|---| | Diff and changed code | passed | Three changed files read | | Tests and build | not run | Author reports 1141 tests passed | ## Findings ### Blocking None. ### Non-blocking None. ## Not verified The full 1141-test run, Windows behavior, guest-module behavior, and reported catalog comparison were not independently verified. All three changed files were read: `core/relinker/relinker/src/guest/GuestModuleBuilder.cpp`, `core/relinker/relinker/src/pipeline/Conversion.cpp`, and `core/relinker/relinker/tests/ConversionTests.cpp`. The manifest lists three reviewed files; none were excluded. Relevant converter code in `core/relinker/codegen/src/Amd64OnlyConverter.cpp` was also read for context. ## To get this to a good state Nothing is required.
fromthemoonx commented 2026-11-26 22:35:30 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by fromthemoonx on 2026-11-26T22:35:30Z.

Review: APPROVED


Windows-host run, since the Tested section is a Linux build:

  • Windows 11 Pro 10.0.26300, WinLibs GCC 15.2.0 r7 UCRT, Release, on main acf3fee1e5.
  • With only the new ConversionTests.cpp on main, relinker_conversion fails with AMD-only site bytes changed before patching.
  • With this PR merged, relinker_conversion passes, then passes again on 3 repeats.
  • tools/check_conventions.py passes on the head.

The two changed lines match: Conversion.cpp now uses its existing windows || macos locals (lines 55–56), and GuestModuleBuilder.cpp uses the same expression.

AI-assisted: yes (Claude Code).

<!-- greyfinch-sync:boykopovar/AnyPS5:review:6081167448 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/27649#pullrequestreview-6081167448). Originally posted by [fromthemoonx](https://github.com/fromthemoonx) on 2026-11-26T22:35:30Z. **Review:** APPROVED --- Windows-host run, since the Tested section is a Linux build: - Windows 11 Pro 10.0.26300, WinLibs GCC 15.2.0 r7 UCRT, Release, on `main` `acf3fee1e5`. - With only the new `ConversionTests.cpp` on `main`, `relinker_conversion` fails with `AMD-only site bytes changed before patching`. - With this PR merged, `relinker_conversion` passes, then passes again on 3 repeats. - `tools/check_conventions.py` passes on the head. The two changed lines match: `Conversion.cpp` now uses its existing `windows || macos` locals (lines 55–56), and `GuestModuleBuilder.cpp` uses the same expression. AI-assisted: yes (Claude Code).
Plyxen commented 2026-11-27 05:17:36 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by Plyxen on 2026-11-27T05:17:36Z.

Review: APPROVED


Checked head 9df408a0e5. I applied it to main ad7582bad4 together with #27680, #27668 and #27560, which touch different files. On Windows 11 with WinLibs GCC 15.2 (Release), tools/check_conventions.py --head on each head and tools/debt_fold.py --check passed, the build succeeded, and all 4,930 CPU tests passed (I left out the GPU tests). The ten relinker and codegen tests I ran for these four (amd64_only_converter, amd64_only_windows, red_zone_framer, red_zone_framer_execution, lane_split_lowering, relinker_conversion and four more) passed. Applied alone to main, it also passes the seven conversion tests (amd64_only_converter, amd64_only_windows, relinker_conversion, conversion_diagnostics, mulx_conversion, relr_conversion and replaced_plt_stubs_conversion).

With main's GuestModuleBuilder.cpp and Conversion.cpp, relinker_conversion fails.

Both MakeAmd64OnlyConverter call sites (GuestModuleBuilder.cpp and Conversion.cpp) now pass windows || macos as keepSegmentAccesses, which was macos only before. So Windows keeps FS and GS accesses out of AMD-only sites, as macOS already did. fsAccessAfterShortAmdOnlySite converts the same code for Windows and Linux.

AI-assisted: yes (Claude Code)

<!-- greyfinch-sync:boykopovar/AnyPS5:review:6083794105 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/27649#pullrequestreview-6083794105). Originally posted by [Plyxen](https://github.com/Plyxen) on 2026-11-27T05:17:36Z. **Review:** APPROVED --- Checked head `9df408a0e5`. I applied it to `main` `ad7582bad4` together with #27680, #27668 and #27560, which touch different files. On Windows 11 with WinLibs GCC 15.2 (Release), `tools/check_conventions.py --head` on each head and `tools/debt_fold.py --check` passed, the build succeeded, and all 4,930 CPU tests passed (I left out the GPU tests). The ten relinker and codegen tests I ran for these four (`amd64_only_converter`, `amd64_only_windows`, `red_zone_framer`, `red_zone_framer_execution`, `lane_split_lowering`, `relinker_conversion` and four more) passed. Applied alone to `main`, it also passes the seven conversion tests (`amd64_only_converter`, `amd64_only_windows`, `relinker_conversion`, `conversion_diagnostics`, `mulx_conversion`, `relr_conversion` and `replaced_plt_stubs_conversion`). With `main`'s `GuestModuleBuilder.cpp` and `Conversion.cpp`, `relinker_conversion` fails. Both `MakeAmd64OnlyConverter` call sites (`GuestModuleBuilder.cpp` and `Conversion.cpp`) now pass `windows || macos` as `keepSegmentAccesses`, which was `macos` only before. So Windows keeps FS and GS accesses out of AMD-only sites, as macOS already did. `fsAccessAfterShortAmdOnlySite` converts the same code for Windows and Linux. AI-assisted: yes (Claude Code)
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!27649
No description provided.