fix(relinker): find Windows guest TLS accesses in code no root reaches #1117

Merged
TideOPL merged 1 commit from upstream-pr/1117 into main 2026-10-08 07:54:41 +00:00
TideOPL commented 2026-10-07 13:13:41 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by TideOPL on 2026-10-07T13:13:41Z.


What

The Windows TLS rewriter only patches instructions that CodeInstructionCollector finds: symbol and unwind function ranges, plus whatever their branches, relocated pointers and initializers reach. A fixed-address executable (ET_SCE_EXEC) has no relocations for function pointers held in data (vtables, a thread entry loaded with lea), and its unwind tables don't cover every function. TLS accesses in that code stayed mov reg, fs:[0], which reads address 0 on Windows.

PPSA16064 (Minecraft Dungeons II, Unreal Engine) has 3689 such loads in its executable segment; 478 were left unpatched. Its worker threads faulted on their first TLS access, e.g. 66 66 66 64 48 8b 04 25 00 00 00 00 (a relaxed general-dynamic access) right after a call to scePthreadSetspecific in a function only reached through a lea-loaded thread entry.

The collector now also sweeps each executable segment linearly, starting at the first 16-byte boundary preceded by int3 padding, and adds every FS-prefixed instruction with an absolute disp32 operand as a root. The sweep resynchronizes at the next padded 16-byte boundary when an instruction doesn't decode, crosses such a boundary, or starts with two zero bytes, so zero fill and undecodable bytes are never read as code. The rewriter's own checks still decide which forms it can patch. The collector is only used by the Windows TLS rewriter, so nothing else changes.

Tested

  • Windows 11, MinGW-w64 GCC 15.2.0 (WinLibs UCRT r7). Full ctest: 393/393.
  • New tls_function_coverage cases: a padded function nothing references is patched (fails on main), and the same load behind zero fill after padding is left unchanged. The existing case that keeps an unreached load at 0x1850 unpatched still passes.
  • PPSA16064: all 3689 FS loads are patched after conversion (0 left), and the title gets past the worker-thread start-up crash.

Asking for ideas

I'd welcome a second opinion on the sync rule. "16-byte boundary preceded by cc cc" matches clang's function padding in this title, but I haven't checked titles built with other padding (e.g. nop fill). If anyone knows a better anchor for fixed-address executables, such as something from the SCE dynamic tables, I'm happy to switch to it.

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 in TechnicalDebt
  • 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
  • AI-assisted: yes
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4773168262 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/1117). Originally posted by [TideOPL](https://github.com/TideOPL) on 2026-10-07T13:13:41Z. --- ### What The Windows TLS rewriter only patches instructions that `CodeInstructionCollector` finds: symbol and unwind function ranges, plus whatever their branches, relocated pointers and initializers reach. A fixed-address executable (`ET_SCE_EXEC`) has no relocations for function pointers held in data (vtables, a thread entry loaded with `lea`), and its unwind tables don't cover every function. TLS accesses in that code stayed `mov reg, fs:[0]`, which reads address 0 on Windows. PPSA16064 (Minecraft Dungeons II, Unreal Engine) has 3689 such loads in its executable segment; **478 were left unpatched**. Its worker threads faulted on their first TLS access, e.g. `66 66 66 64 48 8b 04 25 00 00 00 00` (a relaxed general-dynamic access) right after a call to `scePthreadSetspecific` in a function only reached through a `lea`-loaded thread entry. The collector now also sweeps each executable segment linearly, starting at the first 16-byte boundary preceded by `int3` padding, and adds every FS-prefixed instruction with an absolute `disp32` operand as a root. The sweep resynchronizes at the next padded 16-byte boundary when an instruction doesn't decode, crosses such a boundary, or starts with two zero bytes, so zero fill and undecodable bytes are never read as code. The rewriter's own checks still decide which forms it can patch. The collector is only used by the Windows TLS rewriter, so nothing else changes. ### Tested - Windows 11, MinGW-w64 GCC 15.2.0 (WinLibs UCRT r7). Full `ctest`: 393/393. - New `tls_function_coverage` cases: a padded function nothing references is patched (fails on `main`), and the same load behind zero fill after padding is left unchanged. The existing case that keeps an unreached load at `0x1850` unpatched still passes. - PPSA16064: all 3689 FS loads are patched after conversion (0 left), and the title gets past the worker-thread start-up crash. ### Asking for ideas I'd welcome a second opinion on the sync rule. "16-byte boundary preceded by `cc cc`" matches clang's function padding in this title, but I haven't checked titles built with other padding (e.g. `nop` fill). If anyone knows a better anchor for fixed-address executables, such as something from the SCE dynamic tables, I'm happy to switch to it. ### Checklist - [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 in [TechnicalDebt](https://github.com/boykopovar/AnyPS5/blob/main/docs/dev/TechnicalDebt.md#silent-stubs) - [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 - [x] AI-assisted: yes
TideOPL commented 2026-10-07 13:15:33 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by TideOPL on 2026-10-07T13:15:33Z.


Apologies for the spam of PRs above this one. I've been tinkering this morning with getting Balatro and Minecraft Dungeons 2 to work and play. I had great success with Balatro, even being fully playable without any crashes for prolonged gameplay.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6038738711 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/1117#issuecomment-6038738711). Originally posted by [TideOPL](https://github.com/TideOPL) on 2026-10-07T13:15:33Z. --- Apologies for the spam of PRs above this one. I've been tinkering this morning with getting Balatro and Minecraft Dungeons 2 to work and play. I had great success with Balatro, even being fully playable without any crashes for prolonged gameplay.
MathiasDevelopes commented 2026-10-07 17:05:03 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MathiasDevelopes on 2026-10-07T17:05:03Z.


Tested this on another Unreal Engine title: PPSA14632 (LEGO Horizon Adventures), Windows 11, MinGW-w64 GCC (WinLibs), Release.

Without this PR, 92 fs:[disp32] loads in the executable segment stay unpatched, and the title crashes during startup on its FAPREventQueueListener thread. It's the same pattern you describe: a 66 66 66 64 48 8b 04 25 00 00 00 00 load right after a call to scePthreadSetspecific, in a function only reached through a pointer in data, reading address 0.

With this PR cherry-picked onto current main (cea1ef13, it applies cleanly), 0 unpatched fs: loads remain in the relinked executable and the relink succeeds with all bundled modules.

Thanks for this! 👍

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6042769759 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/1117#issuecomment-6042769759). Originally posted by [MathiasDevelopes](https://github.com/MathiasDevelopes) on 2026-10-07T17:05:03Z. --- Tested this on another Unreal Engine title: PPSA14632 (LEGO Horizon Adventures), Windows 11, MinGW-w64 GCC (WinLibs), Release. Without this PR, 92 `fs:[disp32]` loads in the executable segment stay unpatched, and the title crashes during startup on its `FAPREventQueueListener` thread. It's the same pattern you describe: a `66 66 66 64 48 8b 04 25 00 00 00 00` load right after a call to `scePthreadSetspecific`, in a function only reached through a pointer in data, reading address 0. With this PR cherry-picked onto current `main` (`cea1ef13`, it applies cleanly), 0 unpatched `fs:` loads remain in the relinked executable and the relink succeeds with all bundled modules. Thanks for this! 👍
adriadam10 commented 2026-10-08 07:54:37 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-08T07:54:37Z.


Checked on main + this PR: build and full ctest green (423/423); tls_function_coverage fails without the collector change and passes with it. The diff matches the description, and the sweep only adds FS-prefixed disp32 instructions as roots, so the rewriter's own checks still decide what gets patched. Two independent Windows reports (PPSA16064, PPSA14632) in the thread. Merging once CI is green.

Notes (not blocking):

  • Titles padded with nop instead of cc cc won't be swept; no regression versus main, just no gain there.
  • This touches the same region of CodeInstructionCollector.cpp as #1326; whichever lands second will need a rebase.
<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6055386358 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/1117#issuecomment-6055386358). Originally posted by [adriadam10](https://github.com/adriadam10) on 2026-10-08T07:54:37Z. --- Checked on main + this PR: build and full ctest green (423/423); `tls_function_coverage` fails without the collector change and passes with it. The diff matches the description, and the sweep only adds FS-prefixed `disp32` instructions as roots, so the rewriter's own checks still decide what gets patched. Two independent Windows reports (PPSA16064, PPSA14632) in the thread. Merging once CI is green. Notes (not blocking): - Titles padded with `nop` instead of `cc cc` won't be swept; no regression versus `main`, just no gain there. - This touches the same region of `CodeInstructionCollector.cpp` as #1326; whichever lands second will need a rebase.
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!1117
No description provided.