fix(relinker): find Windows guest TLS accesses in code no root reaches #1117
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/1117"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Imported from boykopovar/AnyPS5. Originally posted by TideOPL on 2026-10-07T13:13:41Z.
What
The Windows TLS rewriter only patches instructions that
CodeInstructionCollectorfinds: 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 withlea), and its unwind tables don't cover every function. TLS accesses in that code stayedmov 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 toscePthreadSetspecificin a function only reached through alea-loaded thread entry.The collector now also sweeps each executable segment linearly, starting at the first 16-byte boundary preceded by
int3padding, and adds every FS-prefixed instruction with an absolutedisp32operand 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
ctest: 393/393.tls_function_coveragecases: a padded function nothing references is patched (fails onmain), and the same load behind zero fill after padding is left unchanged. The existing case that keeps an unreached load at0x1850unpatched still passes.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.nopfill). 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
main; no other open PR implements the same functionsNotImplemented_nid_no_patch); silent stubs are listed in TechnicalDebt.mdfiles or images added to the repository (attach them to this PR)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.
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 itsFAPREventQueueListenerthread. It's the same pattern you describe: a66 66 66 64 48 8b 04 25 00 00 00 00load right after a call toscePthreadSetspecific, 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 unpatchedfs:loads remain in the relinked executable and the relink succeeds with all bundled modules.Thanks for this! 👍
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_coveragefails without the collector change and passes with it. The diff matches the description, and the sweep only adds FS-prefixeddisp32instructions 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):
nopinstead ofcc ccwon't be swept; no regression versusmain, just no gain there.CodeInstructionCollector.cppas #1326; whichever lands second will need a rebase.