fix(relinker): refuse branches into trapped guest GS instructions on Windows #10710
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/10710"
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 roboterspinne on 2026-10-24T18:44:09Z.
What
Windows output replaces each guest GS memory instruction with
ud2and NOP fill of its own length (#6102) but did not compare the decoded direct branch targets with the replaced bytes. A fixture whose function body is74 01 65 48 8b 0c 25 78 03 00 00 c3(jeto the byte after the0x65prefix ofmov rcx, gs:[0x378]) relinked with--windowswith exit 0, and the PE held74 01 0f 0b 90 90 90 90 90 90 90 c3there, so the taken branch executesor edx, [rax-0x6f6f6f70]instead of the guest'smov rcx, [0x378].--macosrefuses the same input withFAIL: Branch enters a guest GS instruction (offset 0x11243)since #7706, merged 2026-10-24.WindowsTlsBuilder::Buildsorts and deduplicates the collected branch targets before the GS trap loop instead of before the FS rewrite loop, which keeps using them, and refuses a trap when a target lies strictly inside it withBranch enters a guest GS instructionat the target RVA: the check and message ofMacOsImage.cppfrom #7706, and the check this file already applies to FS rewrites (Branch enters a guest TLS instruction). A branch to the first byte still converts and reaches theud2. The trap loop runs before thePT_TLSearly returns, so images withoutPT_TLSare covered; guest modules go through the sameBuildcall (WindowsGuestModuleWriter.cpp). No earlier stage refuses the input:CodeInstructionCollectorrejects overlapping decodings only when the span contains an FS prefix. #6102 states that "branch targets inside it do not matter"; that holds only for a target at the first byte.branch-into-gs, at the end of therejectedgroup oftest_tls_function_coverage.py, with the body of the macOS case intest_macos_gs_traps.py:--windowsexits 2 with the message at offset0x11243and writes no output. Onmainthe case fails because the relinker exits 0.pr_overlap.pyreports no overlap with open pull requests.Tested
Windows 11, GCC 15.2.0 (WinLibs UCRT), relinker-only Release build (
-DANYPS5_RELINKER_ONLY=ON -DBUILD_TESTING=ON); synthetic fixtures only, no title or homebrew.main(d0e7fe7beb):tls_function_coverage_rejectedfails on the new case withAssertionErroratassert result.returncode == 2 and error in result.stderr and not output.exists(); the relinker exited 0 with empty stderr. The same build converts a branch into the last byte (74 08) on--windowsand refuses it on--macosat0x1124a.--windowsrefuses74 01at0x11243and74 08at0x1124alike--macos; a branch to the first byte (74 00) and one past the instruction (74 09) still convert on both, the Windows output holding0f 0band seven NOPs.tls_function_coverage_rejected0.45 s,tls_function_coverage_gs_traps0.85 s,tls_function_coverage_gs_traps_implicit1.01 s,macos_gs_traps0.73 s;ctest -R 'tls|gs|windows': 68 of 68 passed.ctest --test-dir build-relinker -j 4: 244 tests, 241 passed,long_pathsskipped;select_tests(FileExistsError, Windows symlink fixture) andpatch_advisor_unit(build path with spaces, addressed by open #10667) fail on this machine onmainas well.xor eax, eax; je +1; mov eax, gs:[rip+disp]; ret, the operand holding 42, themainWindows output exits with0xC0000005; with the branch to the instruction start it exits with0xC000001D(the intendedud2), and with0x90in place of the prefix with 42. With the change the first input is refused at0x11245and the other two are unchanged.python tools/check_conventions.py --base upstream/mainwith this title and description passed.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 MarcZX10 on 2026-10-24T18:54:20Z.
Review: APPROVED
Reviewed exact head
7424949b8bagainst current main. The branch-target range check rejects targets strictly inside the bytes replaced by the GS trap while allowing the instruction start, and the check runs before the no-PT_TLS return. The focusedtls_function_coverage_rejectedCTest passed locally (1/1); diff-check, current-main conventions and merge-tree pass. Hosted Linux, Windows, ASAN and macOS x86_64 jobs pass; macOS arm64 is still pending. No blocking source issue found.Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-25T00:12:38Z.
Review: APPROVED
The fix sorts and deduplicates decoded branch targets before the GS trap loop, then rejects only targets strictly inside a trapped instruction; a target at the first byte still reaches the existing
ud2. The ordering also keeps the targets available for the later FS rewrite checks and covers the no-PT_TLS early return. I checked the new branch-into-prefix fixture, conventions, andgit diff --check; hosted relinker and ASan suites pass across Linux, Windows, macOS and macOS x86_64. Local execution was unavailable because this checkout has no built relinker.