Repository navigation
fix(relinker): refuse GS traps that overwrite a code entry point in macOS output - #11883
Conversation
The macOS TLS scan asked the code collector for no entry roots, so it replaced an fs access with jmp rel32 and NOPs even when a pointer target taken as data lay inside it, and the relink succeeded with the referenced bytes changed. The Windows TLS builder has refused this since boykopovar#5890 and boykopovar#8166. The macOS scan now collects the same entry roots and fails with "TLS rewrite would overwrite an executable entry point" when one lies strictly inside the rewritten instruction. Every input it refuses is already refused by --windows; --macos used to rewrite it silently.
…gs-trap-entry-roots
…acOS output In macOS output, a guest GS memory instruction became UD2 and NOP padding even when a function symbol, a relocated pointer, a RIP-relative lea target or an initializer array entry pointed into it after its first byte, so that entry ran the padding instead of the guest code. The macOS image writer now refuses such a trap with "GS trap would overwrite an executable entry point" at the entry's file offset, the rule the Windows TLS builder applies since boykopovar#11090. The check runs after the existing "Branch enters a guest GS instruction" check, so branch targets keep their message. Outputs without such an entry are unchanged.
|
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.
Some 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.
|
rooklz
left a comment
There was a problem hiding this comment.
Reviewed exact head 566fbb17ef. Note for merge ordering: the branch contains #11488 (head 9ef8b4b75a, fix commit c13d32870f) merged with main 801d5b9bc1 at 17dffabc20, so until #11488 merges the PR diff folds both changesets; the GS-trap delta of this PR is the GsTrap::Entry field, the entryRoots collection in ScanTls, and the WriteMacOsImage refusal.
Checked line-by-line against the Windows builder (#11090):
ScanTlscollects the sameentryRootsfromCodeInstructionCollector(roots plusdataTargets, CodeInstructionCollector.cpp:570-572), so the debt line's "pointer target it takes as data" is covered. The trap'sEntryuses the identicalupper_bound(*instruction)/*entry - *instruction < Lengthguard andheader.Offset + *entry - header.MappedAddressfile-offset math as WindowsTlsBuilder.WriteMacOsImagerefuses in the same order as the WindowsgsTrapsloop: relative-branch target first (Branch enters a guest GS instruction), thenGS trap would overwrite an executable entry pointwith the entered byte's file offset.- The rewritten
fs:accesses keep the pre-existing check order (unsupported-encoding refusal first) and now throwTLS rewrite would overwrite an executable entry pointwhere the Windows builder callscheckProtectedEntry; aninsidePrevious0x65decode is skipped by the macOS loop guards in exactly the cases Windows's0x65 && !insidePreviouscondition skips it.
Verified locally (ANYPS5_RELINKER_ONLY, macOS): macos_gs_traps, sce_code_extent, windows_tls_protected_entries and gs_trap_entry_roots pass, full suite 361/361. The new check_entry_roots cases reuse the Windows protected-entry fixture for all four root kinds plus two negative roots, and the refusal offsets match the entered byte (0x214 inside the 9-byte GS load). The debt line mirrors the reviewed Windows wording accurately.
(AI-assisted: yes)
What
--macosreplaces a guest GS memory instruction with UD2 and NOPs and, of the code entries into it, refuses only a relative branch (#7706); since #11090,--windowsalso refuses any other code entry inside the trapped bytes, but--macosdoes not. On the untouched base 17dffab (#11488 at 9ef8b4b merged withmain801d5b9),65 48 8b 04 25 28 00 00 00 c3at 0x210 with an entry root at 0x214 (aSTT_FUNCsymbol, anR_X86_64_RELATIVEpointer, a REX.Wleatarget or aDT_INIT_ARRAYentry) converts with--macoswith exit 0, and the Mach-O holds0f 0b 90 90 90 90 90 90 90 c3at file offset 0x10210, so the entry at 0x214 runs NOPs andretinstead ofand eax,0x28; ret;--windowsrefuses all four inputs withGS trap would overwrite an executable entry point (offset 0x214). maximilianfv noted this gap in the review of #11488 and suggested a follow-up PR.ScanTlsinMacOsImage.cpprecords for each trapped GS memory instruction[start, start + length)the first collector entry root withstart < root < start + length, with the sameentryRoots.upper_boundlookup and file-offset convention asWindowsTlsBuilder::Build(fix(relinker): refuse GS traps that overwrite a code entry point in Windows output #11090). In theWriteMacOsImagetrap loop, after the existingBranch enters a guest GS instructioncheck and before theAMD-only instruction is also a guest GS accesscheck, such a root throwsGS trap would overwrite an executable entry point; the CLI exits 2 and writes no output.entryRootsalso contains relative branch targets, so the branch check stays first and keeps its message, whichmacos_gs_trapspins for--macos(its offset is pinned for--windowsbytls_function_coverage_rejected). A root at the instruction start is still trapped as before. The entry roots come from fix(relinker): protect macOS TLS code entry roots #11488, which makesScanTlspass&entryRootsinstead ofnullptrtoCodeInstructionCollector::Collect; this PR therefore depends on fix(relinker): protect macOS TLS code entry roots #11488, and its diff includes the changes of fix(relinker): protect macOS TLS code entry roots #11488 (ScanTlsand onetest_sce_code_extent.pycase) until fix(relinker): protect macOS TLS code entry roots #11488 merges. A new entry before the macOS GS entry indocs/dev/debt/relinker.mdlists both macOS messages, the entries each covers, and that no option skips either check; the Windows entry is unchanged.test_macos_gs_traps.py(macos_gs_traps) now also converts with--macosthe entry-root images oftest_windows_tls_protected_entries.py(make_image, whichtest_gs_trap_entry_roots.pyuses for--windows) with65 48 8b 04 25 28 00 00 00 c3at 0x210: rootsrelative,symbol,leaandinitializermust exit 2 with the message,(offset 0x214)and no output; controlssafeandaccess-startmust convert, with0f 0b 90 90 90 90 90 90 90 c3and without the original instruction in the executable ranges of the Mach-O.test_gs_trap_entry_roots.pyis unchanged. No new test file and no CMake change.pr_overlap.pyreports no shared lines with open pull requests that merge withmain; the macOS cases sit intest_macos_gs_traps.pyand the debt entry is a new bullet, so Felipebmaitan's feat(relinker): execute supported scalar guest GS accesses on Windows #11628, feat(relinker): support short scalar guest GS instructions #11661, feat(relinker): support SSE guest GS memory accesses #11689 and feat(relinker): support VEX guest GS memory accesses #11698, which rewritetest_gs_trap_entry_roots.pyand the Windows GS entry, merge with this branch without conflict. It cannot compare this branch with feat(aot): consolidate native ARM64 compiler, runtime and consumers #5021 (docs/dev/debt/relinker.md), fix: define decoded AGC state and bound size arithmetic #10249 (MacOsImage.cpp) and fix(relinker): analyze referenced code beyond SCE scan boundaries #10861 (MacOsImage.cpp,test_sce_code_extent.py,docs/dev/debt/relinker.md), because those do not merge withmain. Checked by hand: trial merges with feat(aot): consolidate native ARM64 compiler, runtime and consumers #5021 and fix: define decoded AGC state and bound size arithmetic #10249 conflict in exactly the filesmaindoes. With fix(relinker): analyze referenced code beyond SCE scan boundaries #10861 the only conflict outsideCodeInstructionCollector.cpp, which conflicts withmaintoo, is fix(relinker): protect macOS TLS code entry roots #11488'sentryRootsargument ofCollectinScanTls, the same conflict fix(relinker): protect macOS TLS code entry roots #11488 alone has; none of this PR's own lines is involved.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.MacOsImage.cppof the untouched base 17dffab and the extended test,macos_gs_trapsfails withentry-relative: expected the GS trap to be refused at offset 0x214: 0after all earlier cases passed. Run case by case, the four root inputs exited 0 with an output file, and both controls converted with the trapped bytes and without the original instruction. With the change (566fbb1) the four root inputs exit 2 withGS trap would overwrite an executable entry point (offset 0x214)and no output, the controls are unchanged, andmacos_gs_trapspasses in 1.1 s.ctest -R 'gs_trap|macos_gs|tls_function_coverage|sce_code_extent|macos_tls|windows_tls' -j 4on 566fbb1: 43 tests passed in 25.1 s, among themgs_trap_entry_roots(0.2 s),macos_gs_traps(1.0 s),sce_code_extent(1.7 s),windows_tls_protected_entries(0.4 s),tls_function_coverage_rejected(0.4 s),macos_tls_overlap,macos_tls_exports,macos_tls_alu,tls_function_coverage_gs_trapsandtls_function_coverage_gs_traps_implicit.ctest --test-dir build-relinker -j 8on 566fbb1: 369 tests, 366 passed,long_pathsskipped; failures known on this machine onmainas well:select_tests(Windows symlink fixture) andpatch_advisor_unit(path with spaces, which open test(tools): compare quoted and resolved paths in the patch advisor test #10667 fixes); both failed again alone.python tools/check_conventions.py --base upstream/mainwith this title and description passed on 566fbb1.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)