fix(relinker): trap only GS memory accesses in Windows and macOS output #8063
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/8063"
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 AasishDairelSahayaGrinspan on 2026-10-19T19:25:45Z.
What
#6458 (macOS) and #6102 (Windows) replace every guest instruction that carries a GS segment prefix by
UD2and NOP padding. A GS prefix on an instruction without a memory operand (65 90,65 48 89 c8,65 0f a2) is architecturally ignored and does not touch GS, so the instruction can run unchanged in the converted output, but it currently faults.docs/dev/debt/relinker.mdrecords the macOS case, with those two byte examples, as a known gap.DecodedInstructionInfogainsHasMemoryOperand: set for a ModRM memory operand (mod != 3, including the Group 5 indirect call/jump path and VEX/EVEX/XOP instructions) and for the encodings whose memory operand is implicit (INS/OUTS, MOV moffs, MOVS/CMPS/STOS/LODS/SCAS, XLATB, MASKMOVQ/MASKMOVDQU/VMASKMOVDQU, MONITOR/MONITORX, UMONITOR and CLZERO).HasMemoryOperandis set. A GS-prefixed instruction without a memory operand is left unchanged and executes with the ignored prefix.gs_trapsandgs_traps_implicitgroups andtest_macos_gs_traps.pygain65 90,65 48 89 c8,65 0f a2, the implicit-memory65 66 0f f7 c1,65 0f f7 c1,65 c5 f9 f7 c1,65 0f 01 c8,65 0f 01 fa,65 0f 01 fcand65 f3 0f ae f0, and the non-memory65 0f 01 c9(MWAIT),65 0f 01 fd(RDPRU) and65 f2 0f ae f0(UMWAIT) as negative cases. The decoder test also covers the three-byte VEX form65 c4 e1 79 f7 c1. The macOS debt entry no longer records the no-memory case.74 01 65 b8 2a 00 00 00 c3;65 b8 2a 00 00 00has no memory operand and no longer traps here, so if #7706 lands after this change its case needs a memory-operand instruction (for example65 8b 05 ...). Nothing else in #7706 changes.Tested
macOS 15.7.7, x86_64, AppleClang, relinker-only Release build (
-DANYPS5_RELINKER_ONLY=ON) on this branch rebased ontomain49fd13a74:ctest210/210 pass.gs_traps(9 cases) andgs_traps_implicit(10 cases) so each stays well under its 30 s CTest timeout; the combined group ran close to that limit on a loaded Windows host.65 66 0f f7 c1,65 0f f7 c1,65 c5 f9 f7 c1,65 0f 01 c8,65 0f 01 fa), themov rax, gs:[rax]control,UMONITOR(65 f3 0f ae f0) andCLZERO(65 0f 01 fc) convert toUD2and NOP padding in Windows output;65 90,65 0f 01 c9,65 0f 01 fd,65 f2 0f ae f0and65 66 0f ae f0remain unchanged. VEX2 and VEX3VMASKMOVDQUare covered by the decoder test.gs-nopagain, so the tests cover both sides.HasMemoryOperandwas compared against/usr/bin/objdump -dfor the GS-prefixed instructions of/usr/lib/dyld,libsystem_pthread.dylib,libsystem_kernel.dylib,libsystem_platform.dylib,libdispatch.dyliband/usr/lib/ignition(306 unique instructions): no instruction whose disassembly uses%gs:was reported as having no memory operand.python3 tools/check_conventions.py --base origin/mainpasses.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 github-actions[bot] on 2026-10-19T19:34:25Z.
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/relinker/tests/test_macos_gs_traps.pyL40Some 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.txtdocs/dev/debt/relinker.mdcore/relinker/CMakeLists.txtcore/relinker/CMakeLists.txtcore/relinker/CMakeLists.txtcore/relinker/CMakeLists.txtdocs/dev/debt/relinker.mdcore/relinker/CMakeLists.txtdocs/dev/debt/relinker.mddocs/dev/debt/relinker.mdcore/relinker/elfpatcher/src/macos/MacOsImage.cppImported from boykopovar/AnyPS5. Originally posted by AasishDairelSahayaGrinspan on 2026-10-19T19:35:31Z.
Could a maintainer run the manual Build workflow on
mainfor this PR (8063)? Locally I ran the relinker-only Release build on macOS 15.7.7 (x86_64, AppleClang):ctest208/208 pass, and the two new GS regression cases fail when only the Windows/macOS writer changes are reverted. The relinker matrix has not been covered by a local run.Imported from boykopovar/AnyPS5. Originally posted by wachi35676 on 2026-10-19T23:17:44Z.
Verdict: defect. Some GS-prefixed instructions with an implicit memory operand are no longer trapped.
HasMemoryOperandis set only for a ModRM memory form and for opcodes 6C-6F, A0-A7, AA-AF and D7. Some instructions use a register-form ModRM byte but still access memory at DS:rDI or DS:rAX, and a GS override applies to that access. This PR leaves them unchanged in Windows output, whilemaintraps them:main--windows--windows65 48 8b 00mov rax, gs:[rax](control)0f 0b 90 900f 0b 90 9065 66 0f f7 c1maskmovdqu xmm0, xmm1(stores to DS:rDI)0f 0b 90 90 9065 66 0f f7 c165 0f f7 c1maskmovq mm0, mm10f 0b 90 9065 c5 f9 f7 c1vmaskmovdqu xmm0, xmm10f 0b 90 90 9065 0f 01 c8monitor(DS:rAX)0f 0b 90 9065 0f 01 famonitorx(DS:rAX)0f 0b 90 9065 90nop0f 0b65 90(intended)Each image is
make_image("register", "unwind", body=<bytes> + c3)fromtest_tls_function_coverage.py, converted withrelinker --skip-sce-module --windows. The bytes shown are read back at RVA 0x11240.The prefix is honoured on hardware. A native MinGW program on this Ryzen 5 9600X ran
mov rdi, 0x28followed by65 66 0f f7 c1with an 8-byte mask, and that wrote TEB+0x28 (gs:[0x28]read 0 before anda7a6a5a4a3a2a1a0after). The Intel SDM entries for MASKMOVDQU/MASKMOVQ and MONITOR say the default DS segment can be overridden. Left unconverted, these instructions read or write relative to the host's GS (the Windows TEB) instead of faulting. One way to cover them: setHasMemoryOperandfor0F F7(legacy and VEX) and for0F 01 C8/0F 01 FA, and add those encodings togs_traps.The rest checks out here. Windows 11 Home 10.0.26300, Ryzen 5 9600X, WinLibs GCC 15.2.0 r7, relinker-only Release build of this PR merged onto
main9127ea36d:ctest -j4212/212 pass. The newgs-nop,gs-register-moveandgs-cpuidcases convert unchanged here, andmaintraps65 90.shader_cache_runtime_sources, also fails onmainin the same environment.mainand with this PR, using--windows --to-intel,--macos --to-rosettaand Linux--to-intel, all with--trap-sdk-syscall-stubs. The titles were PS5 OpenGL Showcase 1.0.2 (PPSA99005), PPSA47402, PS5LM Plus 0.8.4 (PPSA99581), and Prospero Win v0.2.0 (PPSA99995) with its 16 Winex86_64-unixmodules via--module-dir. All 12 output sets are byte-identical tomain, so the six GS instructions in Prospero Win'sntdllare memory forms and still trap.AI-assisted: yes (Claude Code); results above were run on this machine.
Imported from boykopovar/AnyPS5. Originally posted by AasishDairelSahayaGrinspan on 2026-10-20T04:45:54Z.
@wachi35676 thanks for the detailed check and the hardware validation. You are right:
MASKMOVQ/MASKMOVDQU/VMASKMOVDQUandMONITOR/MONITORXuse a register-form ModRM byte but still access memory atDS:rDIorDS:rAX, and the first version ofHasMemoryOperandmissed them.Fixed in
cde44070e, rebased ontomain49fd13a74:HasMemoryOperandis now set for legacy0F F7, VEX map-0F0F F7, and0F 01 C8/0F 01 FA.gs_trapsandtest_macos_gs_traps.pynow cover65 66 0f f7 c1,65 0f f7 c1,65 c5 f9 f7 c1,65 0f 01 c8and65 0f 01 fa;65 0f 01 c9(mwait, no memory operand) is a negative case. The decoder test also covers the three-byte VEX form65 c4 e1 79 f7 c1.0F 0Bplus NOP padding, while65 90and65 0f 01 c9stay unchanged.ctestis 209/209 on macOS after the rebase, andpython3 tools/check_conventions.py --base origin/mainpasses.I cannot run the Windows or Linux matrix locally, so a manual Build run on the new commit would still be useful. Could you re-test the Windows matrix when you have a chance?
Imported from boykopovar/AnyPS5. Originally posted by AasishDairelSahayaGrinspan on 2026-10-20T04:50:15Z.
One more from the same audit:
UMONITOR(F3 0F AE /6, register-form ModRM) takes its effective address from the register operand and uses DS with segment overrides per the Intel SDM, so it was in the same gap. Added ina58d560c9:65 f3 0f ae f0now traps; the register-form encoding is recognized by theF3prefix and/6.UMWAIT(65 f2 0f ae f0) andTPAUSE(65 66 0f ae f0) use the same opcode with different mandatory prefixes and have no memory operand, so they remain unchanged;gs-umwaitis the negative case in both test suites.Full relinker-only
ctestis still 209/209 on macOS, and the decoder tests coverUMONITOR,UMWAIT,TPAUSEandINCSSP.Imported from boykopovar/AnyPS5. Originally posted by wachi35676 on 2026-10-20T05:02:50Z.
I re-tested
a58d560c9merged ontomain329b9aa1a. All five cases from my earlier table now trap, and so does UMONITOR. One more register-form instruction is still missed: AMDCLZERO(0F 01 FC).main65 66 0f f7 c1/65 0f f7 c1/65 c5 f9 f7 c1/65 c4 e1 79 f7 c165 0f 01 c8/65 0f 01 fa65 f3 0f ae f065 f2 0f ae f0/65 66 0f ae f065 0f 01 c9/65 0f 01 fb65 90,65 48 89 c8,65 0f a2,65 0f ae f865 0f ae 38,65 66 0f 38 f8 01,65 f2 0f 38 f8 01,65 d7,65 a465 0f 01 fcThe method is the same as before:
make_image("register", "unwind", body=<bytes> + c3),relinker --skip-sce-module --windows, bytes read back at RVA 0x11240. Here "trap" means0f 0bplus NOP padding. Leaving UMWAIT and TPAUSE unchanged matches the SDM: the r32 operand is a control value and Mod must be 11B, so there is no memory operand. MOVDIR64B, ENQCMD and CLFLUSH have only memory-form ModRM encodings, so the existing ModRM check already covers them.The CLZERO gap is real on hardware. The AMD APM describes CLZERO as clearing "the cache line specified by the logical address in rAX" and says it performs the segmentation checks a store would. I ran a native MinGW program on this Ryzen 5 9600X (CPUID 8000_0008 EBX[0] = 1) with a 64-byte buffer filled with
0xAAandrax = buffer - TEB:0f 01 fcwithout a prefix faulted once and left the buffer untouched.65 0f 01 fcdid not fault and zeroed the buffer. The GS base is applied, so it clears a line relative to the host TEB.0f 01 fcwithrax = buffer(control) zeroed the buffer.Adding
info.ModRmByte == 0xFCnext to0xC8/0xFAin theTwoByteGrp7branch, with ags-clzerocase ings_trapsandtest_macos_gs_traps.py, would cover it.Everything else passes on this machine (Windows 11 Home 10.0.26300, Ryzen 5 9600X, WinLibs GCC 15.2.0, relinker-only Release):
ctest -j4: 214/214 pass onmainand 214/214 with the PR, includingdecoder_length,tls_function_coverage_gs_trapsandmacos_gs_traps.mainand 219/219 with the PR.--windows --to-intel,--macos --to-rosettaand Linux--to-intel, all with--trap-sdk-syscall-stubs. All 12 output sets are byte-identical tomain.AI-assisted: yes (Claude Code); results above were run on this machine.
Imported from boykopovar/AnyPS5. Originally posted by AasishDairelSahayaGrinspan on 2026-10-20T05:24:16Z.
@wachi35676 thanks for the hardware
CLZEROtest — you are right, the GS base is applied. Fixed ina0e591c93:0F 01 FCnow joins0F 01 C8/0F 01 FAin the group-7 implicit-memory set.gs-clzerois added togs_trapsandtest_macos_gs_traps.py;RDPRU(65 0f 01 fd) is the negative case.65 0f 01 fcnow converts to0F 0B+ NOP padding;65 0f 01 fdstays unchanged.ctestis 209/209 on macOS, andcheck_conventions.pypasses.Please re-run the Windows/Linux matrix on
a0e591c93when you have a chance.Imported from boykopovar/AnyPS5. Originally posted by wachi35676 on 2026-10-20T06:08:23Z.
I re-tested
a0e591c93merged ontomain1ed63f63c, and CLZERO is now trapped. I found nothing else.rdpruand two prefixed CLZERO forms, throughrelinker --skip-sce-module --windows. The bytes were read back at RVA 0x11240.0f 0b+ NOPs):65 0f 01 fc(clzero),65 67 0f 01 fc(32-bit address) and65 48 0f 01 fc(REX.W).65 0f 01 fd(rdpru).0f 01 fdand65 0f 01 fdwith ECX = 0 and 1 both return MPERF/APERF without faulting. The GS prefix has no effect.X64InstructionDecoder.cpphunk. Three tests then fail:decoder_length: "Wrong memory operand presence for 650f01fc"tls_function_coverage_gs_traps:('gs-clzero', '650f01fcc3')macos_gs_traps: "gs-clzero: the GS access was not replaced by ud2 and NOP padding"ctest -j4passes 215/215 onmainand 215/215 with the PR.mainand 220/220 with the PR.--windows --to-intel,--macos --to-rosettaand Linux--to-intel, all with--trap-sdk-syscall-stubs. All 12 output sets are byte-identical tomain.AI-assisted: yes (Claude Code); results above were run on this machine.
Imported from boykopovar/AnyPS5. Originally posted by AasishDairelSahayaGrinspan on 2026-10-20T08:26:42Z.
Thanks for the thorough re-test and the negative control — good to have the prefixed CLZERO forms covered too. No further changes on my side; the head is
a0e591c93. The only remaining item is the manual Build workflow and maintainer review.Imported from boykopovar/AnyPS5. Originally posted by screamm on 2026-10-20T13:43:02Z.
Windows run of
a0e591c93merged ontomaind50ec76da(merges cleanly).Host: Windows 11 Pro 26220, Ryzen 7 5700X, NVIDIA GeForce RTX 4070 driver 596.21, WinLibs MinGW-w64 GCC 15.2 r7, Release, relinker-only build (
-DANYPS5_RELINKER_ONLY=ON);libsfrom a full build of the samemainfor the title boots.decoder_length3/3 andmacos_gs_traps3/3 under ctest.tls_function_coverage_gs_trapspassed 3/3 run directly (26-30 s each). Under ctest it timed out once at its 30 sTIMEOUT, because the host was loaded by other builds. Onmainthe same group takes 10-18 s here; the PR grows it from 8 to 21 cases, so on a slow Windows box it now runs close to that limit.WindowsTlsBuilder.cpprestored frommain,gs_trapsfails withAssertionError: ('gs-nop', '0f0bc3'). With the PR's file back, it passes.eboot.elf, including Showcase PPSA99005 and Prospero Win PPSA99995 with its 16 Wine modules), default Linux target and--windows,mainvs PR. The 5 largest executables ran 3 times each and the rest once. Every output file (executables, guest modules, registry and needed JSON) has the same sha256 asmain, so none of these titles has a GS-prefixed instruction without a memory operand in its converted code. PPSA99360--windowsfails the same way onmainand on the PR (Base relocation target crosses a page boundary (offset 0x3243fff)).main, 123.5 s with the PR; user CPU 15.5 s vs 15.4 s.--windows: 844.3 s vs 843.3 s; user CPU 536.8 s vs 543.3 s. The largest per-title difference is 0.6 s (default) and 2.5 s (--windows). The two relinkers ran side by side for every sample.--windows, 60 s each, RTX 4070): PPSA99005 and PPSA99995 were still running at 60 s with bothmainand the PR (162/175 and 41/48 frames; low counts because the host was loaded), and neither printed a stop line.python tools/check_conventions.py --base origin/mainpasses, andgit diff origin/main --checkis clean.Not checked: macOS and Rosetta outputs, a hardware probe of the encodings on this CPU, and the full relinker suite under a quiet host (a
-j3run gave 190/223 with the other 33 at their hard-coded 20-300 s limits on this loaded host, and a serial rerun was cut short by a host problem).Written with Claude Code assistance (validation by a subagent, reviewed in this session).
Imported from boykopovar/AnyPS5. Originally posted by AasishDairelSahayaGrinspan on 2026-10-20T16:25:04Z.
Thanks for the Windows run and for catching the timing risk.
I split the combined case set so the 30 s limit is no longer in play:
tls_function_coverage_gs_traps: the original 8 cases plusgs-without-pt-tls(9 relinker runs).tls_function_coverage_gs_traps_implicit: the 7 implicit-memory positives plus the MWAIT/RDPRU/UMWAIT negatives (10 relinker runs).allstill runs both.Local macOS relinker-only
ctestis 210/210, and both groups pass when run directly. This adds one CTest entry, so the Windows count goes from 215 to 216. Head is0d3f6f726, one test-only commit on top ofa0e591c93; the functional code is unchanged. Could you re-run the Windows matrix and the ctest timing when you have a chance?