fix(relinker): trap guest GS accesses in Windows output instead of refusing the module #6102
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/6102"
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 DavidSamir on 2026-10-14T22:11:03Z.
What
Prospero Win (PPSA99995, the catalog's Wine port) cannot be converted for Windows. The relinker refuses its Wine
ntdllmodule:65 48 8b 0c 25 78 03 00 00ismov rcx, gs:[0x378]. The module has six instructions that address memory through GS (gs:0x30,gs:0x330,gs:0x378, objdump of its executable segment), and no FS access. On FreeBSD a program sets its own GS base withsysarch(AMD64_SET_GSBASE); on Windows the host owns GS (the thread environment block), soWindowsTlsBuildercannot translate a GS access the way it rewrites FS thread-pointer accesses, and it refuses the whole module, so the title cannot start at all.The builder now overwrites each GS-prefixed instruction in place with
ud2and NOP padding of the instruction's own length. The module converts, and an instruction that is reached faults withSTATUS_ILLEGAL_INSTRUCTION, which the crash reporter prints with its address and bytes. This follows--trap-sdk-syscall-stubs, which turns SDK syscall stubs intoud2the same way. Because the instruction is replaced in place, no jump or stub is needed, branch targets inside it do not matter, and forms shorter than a jump are covered too. FS accesses are unchanged.Four commits:
fix(relinker): trap guest GS accesses in Windows output instead of refusing the module: the builder change, and thegs-loadcase oftls_function_coverage_rejected, which encoded the refusal.test(relinker): cover GS loads, stores and ALU reads trapped in Windows output: a newtls_function_coverage_gs_trapsgroup. Each form (the two loads Wine'sntdlluses, a store, an ALU read and a 4-byte register-address form) converts, and its bytes becomeud2and NOPs with the next instruction in place.test(relinker): trap a GS access in a module without a TLS segment: the builder returns early for a module withoutPT_TLS; a GS access there must still be trapped.docs(debt): record that guest GS accesses trap in Windows output.Tested
Windows (
84f72fc05onmainfa539dd9b, Windows 11 10.0.26300, WinLibs GCC 15.2.0 r7, Release):--trap-sdk-syscall-stubsas the catalog sweep converts it, run withmain's libraries:main's relinker exits 2 with theFAILabove; nothing is written.ntdllare 9-byteud2and NOP sites in the output and none of the original bytes are left. The title then stops at load (exit 0xc0000139) on its next imports:sysarch(b7uXQmnfB2s, #6028) and four libSceVideoOut cursor functions,sceVideoOutCursorEnable,sceVideoOutCursorDisable,sceVideoOutCursorSetImageAddressandsceVideoOutCursorSetPosition, which no library exports yet. So it moves from conversion to its next stop; it does not run on Windows yet.main's relinker and the branch's, same arguments: every output file of 50 titles is byte-identical. PPSA99995 is the only difference.WindowsTlsBuilder.cppas onmain,tls_function_coverage_gs_trapsfails at its first case withUnsupported Windows guest TLS instruction (bytes: 65 48 8b 0c 25 78 03 00 00). With the trap applied after the builder's early return for a module withoutPT_TLS, the third commit's case fails: the instruction is left in place.tls_function_coverage_*,tls_diagnostics,guest_tls,empty_tls) pass, 34 at the first commit and 35 from the second.ctest --parallel 4on a full build: 1233 of 1234 pass.check_main_syncfails on this machine at a file name withéread back through the console code page; it passes withPYTHONUTF8=1, and the branch changes nothing undertools/. Once,tls_function_coverage_register_address_loadshit its 30 s timeout while the overlap check ran alongside; alone it passes 3 of 3 in about 5 s.tools/check_conventions.py --base origin/mainpasses.tools/pr_overlap.py --branchreports no overlap with open pull requests.Linux (same commit, WSL2 Ubuntu 24.04, GCC 13.3.0, Release): the relinker builds and the same 35 TLS tests pass.
Not checked on a console. The guest GS base is not emulated; the debt note says so.
Checklist
main; no other open PR implements the same functionsNotImplemented_nid_no_patch); silent stubs are listed under Silent stubs in their library's technical debt file.mdfiles or images added to the repository (attach them to this PR)Imported from boykopovar/AnyPS5. Originally posted by DavidSamir on 2026-10-14T23:02:47Z.
With this PR, #6028 (
sysarch) and #6135 (the four VideoOut cursor imports) merged locally onmain922abe6a4, Prospero Win (PPSA99995) reaches its launcher on Windows: still running at 60 s in two runs, and its session log records the keyboard and mouse opening, an empty game library andlauncher video=ok pad=ok hid=ok frame=1 apps=0. With only this PR it converts and stops at load on those imports, as described above.AI-assisted: yes
Imported from boykopovar/AnyPS5. Originally posted by jotitadev on 2026-10-14T23:34:07Z.
Review: COMMENTED
Static note from macOS, head
84f72fc05: the macOS writer has the same refusal, so with this PR PPSA99995'sntdllstill won't convert with--macos.MacOsImage.cpp(the TLS scan around L230-247, unchanged on currentmain) accepts only64-prefixed loads/stores and throws for anything else with a segment prefix. I fed thegs-load-rcxbody from this PR'sgs_trapsgroup (65 48 8b 0c 25 78 03 00 00, viamake_image("register", "unwind", ...)) to an existing macOS relinker build (M2 Max, macOS 27.2; mugurc'smacos-port@ b7eb6b6b, whoseMacOsImage.cppscan is identical tomain's):An FS load in the same fixture converts fine. On x86-64 macOS the host also owns GS (it holds the pthread TSD base, under Rosetta too), so the reason given in the debt note applies there as well, and the same in-place
ud2trap would fit the macOS scan. Not asking to widen this PR, since it's Windows-scoped. The debt line could say "Windows output" for now and a follow-up could cover macOS. I didn't build this PR's head; the macOS result above is from the existing build.Written with AI assistance (Claude Code). I ran the builds and tests above on my machine.
Imported from boykopovar/AnyPS5. Originally posted by screamm on 2026-10-15T02:00:24Z.
Cross-check on Windows: #6102 head
84f72fc05merged ontomain6239ceffc; the merge is clean.Host: Windows 11 Pro 26220, WinLibs MinGW-w64 GCC 15.2.0 r7, Ninja, Release, NVIDIA RTX 4070 (Ryzen 7 5700X).
cmake --build build -- -k 0) andlibsboth succeed. One observation:WindowsTlsBuilder.cppnow gives 8 GCC 15.2-Wstringop-overflowwarnings, wheremain's build of the same file gives none:warning: writing 8 bytes into a region of size between 5 and 6once, andwriting 1 byte into a region of size 07 times;WindowsTlsBuilder.cpp:190:50,movedBytes.insert(movedBytes.end(), 4, 0), inlined intoWindowsTlsBuilder::Build. That line ismain's code, so this looks like a false positive that the changed inlining exposes. It is not an error.--repeat until-fail:3:tls_function_coverage_gs_trapsandtls_function_coverage_rejected: 3/3;tls_function_coverage_*,tls_diagnostics,guest_tls,empty_tls): 3/3 each, 105 of 105 runs.ctest -j8 --timeout 120 -E '^agc_driver_|^video_out_flip': 809 tests (main's 808 plusgs_traps). 808 pass, including the 2 skipped.hardware_oracle_comparetimed out under host load and passes alone (8.75 s).Prospero Win (PPSA99995). Catalog release, converted with
relinker --windows --trap-sdk-syscall-stubs --module-dir win/wine/lib/wine/x86_64-unix --registry unused-filter=0 src6/eboot.elf ...and run with the display on.main: the relinker exits 2 and writes nothing:This PR: it converts, with 54 output files. In the input
ntdll.prx, objdump of the executable segment finds six GS instructions, at 0x2e14c, 0x2e1a0, 0x2e45f, 0x2e504, 0x2e52b and 0x2e56b:mov %gs:0x378,%rcx×3;mov %gs:0x30,%r13×2;mov %gs:0x330,%r10×1.In the output
ntdll.prx.guest.prx, each is0f 0bfollowed by 790, with the 8 bytes on each side unchanged, and none of the original 9-byte sequences is left.The title then stops at load, after 0.9 s, with exit
0xC0000139:Those are:
sysarch, imported byntdllandwow64native;sceVideoOutCursorDisable,sceVideoOutCursorSetImageAddress,sceVideoOutCursorEnableandsceVideoOutCursorSetPosition, imported bylibvulkan.No frame is presented.
Verdict: confirmed. Conversion now succeeds, and the six GS sites become 9-byte
ud2/NOP traps. The next stop is load, onsysarch(#6028) and the foursceVideoOutCursor*imports, exactly as the PR describes.AI-assisted: yes (Claude Code)
Imported from boykopovar/AnyPS5. Originally posted by DavidSamir on 2026-10-15T05:23:17Z.
@jotitadev thanks for checking the macOS writer. The debt line already reads "In Windows output, …", so it stays accurate for this PR. I'll do the same in-place trap for
MacOsImage.cpp's TLS scan as a follow-up PR, as CONTRIBUTING asks, and link it here.@screamm thanks for the cross-check. I'll look at the
-Wstringop-overflowwarnings onWindowsTlsBuilder.cpp:190with GCC 15.2 and report back here.AI-assisted: yes
Imported from boykopovar/AnyPS5. Originally posted by DavidSamir on 2026-10-15T05:53:03Z.
@screamm confirmed: recompiling
WindowsTlsBuilder.cppalone gives 8-Wstringop-overflowwarnings atmovedBytes.insert(movedBytes.end(), 4, 0)with this PR and none onmain(WinLibs GCC 15.2.0 r7, Release). Applying the traps inside the scan loop instead of after it did not change that.f1735f59fwrites that line asmovedBytes.resize(movedBytes.size() + 4), which appends the same four zero bytes, and the file then builds without warnings. With it:84f72fc05, Prospero Win included;tools/check_conventions.pypasses.AI-assisted: yes
Imported from boykopovar/AnyPS5. Originally posted by screamm on 2026-10-15T08:53:29Z.
Re-check of head
f1735f59fon Windows, merged ontomainc0be1f69b(clean).84f72fc05was merged onto the samemainfor comparison.Windows 11 Pro 26220, Ryzen 7 5700X, RTX 4070 (driver 596.21), WinLibs MinGW-w64 GCC 15.2.0 r7, Ninja, Release.
84f72fc05, recompilingWindowsTlsBuilder.cppstill gives the 8-Wstringop-overflowwarnings atWindowsTlsBuilder.cpp:190:50. Withf1735f59fthe file compiles with no warnings, and the full build andlibsafterwards have none either.ctest -R '^(tls_function_coverage_.*|tls_diagnostics|guest_tls|empty_tls)$' --repeat until-fail:3: 35/35, 3 of 3 (105 runs),gs_trapsandrejectedincluded.gs_trapswithmain's relinker still fails on65 48 8b 0c 25 78 03 00 00.tools/check_conventions.py --base origin/mainandgit diff --check: pass.84f72fc05againstf1735f59f, each with the catalog's flags and the same output path, every file hashed. All 114 output files of 9 titles are byte-identical:--trap-sdk-syscall-stubs --module-dir win/wine/lib/wine/x86_64-unix, 54 files);--module-dir Media/Modules --module-dir Media/Plugins, 15);--executable picker/picker.bin, 9);--windows-fixed-base 0x400000, 6);--trap-sdk-syscall-stubs, 6);Verdict: confirmed. The warnings are gone and the conversion output is unchanged, so the earlier Prospero Win result still holds for this head.
AI-assisted (Claude Code).
Imported from boykopovar/AnyPS5. Originally posted by github-actions[bot] on 2026-10-15T12:14:43Z.
Other open pull requests touch the same code. Merging one will break the other, or both implement the same thing. If this one builds on them, list them in Depends on.
main)core/relinker/elfpatcher/src/windows/WindowsTlsBuilder.cppL100-101Some comparisons could not be completed because a pull request conflicts with the base branch. Review these shared files manually before concluding that there is no overlap.
core/relinker/CMakeLists.txtcore/relinker/CMakeLists.txtdocs/dev/debt/relinker.mdcore/relinker/CMakeLists.txtdocs/dev/debt/relinker.mdcore/relinker/CMakeLists.txtdocs/dev/debt/relinker.mdcore/relinker/CMakeLists.txtcore/relinker/CMakeLists.txtdocs/dev/debt/relinker.mdImported from boykopovar/AnyPS5. Originally posted by DavidSamir on 2026-10-15T16:18:53Z.
@jotitadev the macOS half is up as #6458: GS accesses in macOS output become
ud2and NOP padding in place, the same rule as this PR, and Prospero Win (PPSA99995) converts for macOS with it. It sits on #6410, which first had to stop the macOS scan from refusing OpenSSL'sCRYPTOGAMS by <[email protected]>string as a GS access (a decoding that starts inside an earlier instruction, the case90dcd6256already skips for Windows); trapping those bytes would have overwritten code in 27 catalog titles. With #6410, #6419, #6448 and #6458, all 79 catalog titles convert for macOS (47 onmain).AI-assisted: yes
Imported from boykopovar/AnyPS5. Originally posted by screamm on 2026-10-16T08:24:13Z.
Re-check of head
f1735f59fon current main5d9dcc42d, alone and stacked with #6028 (fb0f83ddf) and #6135 (10bc2f05a). My earlier two comments covered conversion and byte-identity on older main; this one runs the title with the libraries.Windows 11 Pro 26220, Ryzen 7 5700X, NVIDIA RTX 4070 (596.21), WinLibs MinGW-w64 GCC 15.2.0 r7, Ninja, Release.
WindowsTlsBuilder.cppcompiles without warnings. Full build with-k 0on the stack: onlyagc_driver_gpu_indirect_arguments_misaligned_writer_ahead_testsfails (the 64 KiBalignasthat also fails onmain, #6452/#6478);libsbuilds.tls_function_coverage_*,tls_diagnostics,guest_tls,empty_tls),--repeat until-fail:3: 105 of 105.gs_trapswithmain's relinker fails withUnsupported Windows guest TLS instruction (bytes: 65 48 8b 0c 25 78 03 00 00), and passes with this PR's.-j8 --timeout 120 -E '^agc_driver_|^video_out_flip'): 837 of 840 pass, 2 skipped,hardware_oracle_comparetimed out under load and passes alone (11.2 s).tools/check_conventions.py --base origin/mainandgit diff --check: pass.Prospero Win (PPSA99995),
--trap-sdk-syscall-stubs --module-dir win/wine/lib/wine/x86_64-unix, display on, 125 s limit:main: the relinker exits 2 withFAIL: Unsupported Windows guest TLS instruction (bytes: 65 48 8b 0c 25 78 03 00 00) (offset 0x3214c).main's libraries), 2 runs: it converts; the six GS instructions inntdllare six0f 0b+ 7x90sites in the output. The title then exits0xC0000139after about 3 s, 0 frames:libSceVideoOut.prxlines remain.session-0.loghaslauncher video=ok pad=ok hid=ok frame=1 apps=0. No trapped GS instruction was reached in that time (no illegal-instruction fault).Verdict: confirmed on current main; with #6028 and #6135 this moves Prospero Win from refused conversion to its launcher on Windows.
Not checked: Linux, macOS, a console, any program run inside Prospero Win.
Written with Claude Code assistance (validation by a subagent, reviewed in this session).
Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-16T23:12:17Z.
Checked the merged tree: build and the relinker ctest scope green (
autorun_exit_statustimed out once under load and passes alone), andtls_function_coverage_gs_trapsfails without theWindowsTlsBuilder.cppchange. The debt line is in the right section. Windows evidence from @screamm covers the conversion on that platform; merging.