fix(relinker): guard length-changing rewrites around truncated branches #7822
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/7822"
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 yash27git on 2026-10-19T09:02:17Z.
What
The fix for 16-bit-truncated relative branch targets adds
TruncatesBranchTargetmetadata and deliberately leavesHasBranchTargetfalse when the decoder cannot safely provide a relocatable target.X64InstructionRewriter::Rewrite()could still perform length-changing edits without accounting for a surviving truncated relative branch. If the edit shifts code before or after that branch, its effective destination may change even though the rewriter cannot update the branch target.This change conservatively rejects length-changing rewrites when a truncated relative branch survives outside the instruction being replaced.
Changes:
X64InstructionRewriter.cpp: reject length-changing edits when a surviving truncated relative branch cannot be safely preserved.RewriteBoundaryTests.cpp: add regression cases covering truncated branches whose source or destination would be affected by resizing.docs/dev/debt/relinker.md: document the rewriter limitation and safeguard.Tested
Windows 10 (10.0.19045), MinGW-w64 (WinLibs) GCC 15.2.0, Release
Independent review validation
ctest -R '^rewrite_boundaries$'on the combined tree at5563156935, including #7817 and #7746:X64InstructionRewriter.cpprestored tomainatfb0dc118f, while retaining the regression test: fails 3 of 3 withLength-changing rewrite accepted a section with a truncated relative branch.The comparison confirms that the regression test detects the unsafe behaviour when the fix is removed.
GitHub Actions
clang++, x86-64 and native), Ubuntu (g++) and Windows (g++) — successful.Not tested: relinking a real title or running the regression test manually on Linux.
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)🤖 Generated with ChatGPT
Imported from boykopovar/AnyPS5. Originally posted by xXPBSXx on 2026-10-19T09:40:35Z.
Windows data point for
554691a0bmerged ontomainfb0dc118f(together with #7817 and #7746 in one tree,5563156935; the three PRs touch different files): Windows 10 (10.0.19045), MinGW-w64 (WinLibs) GCC 15.2.0, Release,ctest -R '^rewrite_boundaries$'.core/relinker/codegen/src/x86/X64InstructionRewriter.cpprestored tomain(git checkout fb0dc118f -- core/relinker/codegen/src/x86/X64InstructionRewriter.cpp;git statusshows only that file staged, 12 deletions; the new test kept): fails 3 of 3 (exit 8),Length-changing rewrite accepted a section with a truncated relative branch.Not tested: relinking a real title, Linux.
AI-assisted (Claude Code).
Imported from boykopovar/AnyPS5. Originally posted by yash27git on 2026-10-19T10:00:38Z.
Thank you for testing this on Windows and providing the regression comparison! I really appreciate you taking the time to verify that the test passes with the fix and fails when the change is reverted. This is valuable evidence that the fix addresses the issue. Thanks again for the detailed test results! 🙏
Imported from boykopovar/AnyPS5. Originally posted by jotitadev on 2026-10-19T10:28:27Z.
Review: COMMENTED
Linux run of
554691a0b, next to xXPBSXx's Windows data, plus one question about how wide the guard is.Machine: Linux x86_64, i7-8700, Ubuntu 26.04, GCC 15.2, Release,
BUILD_TESTING=ON, full build. Head554691a0bmerged ontomain8816fd772(clean merge), together with #7363 and #7820 (no shared files).rewrite_boundariespasses, the PR'sRewriteBoundaryTests.cpponmain(rewriter unchanged) fails, and the non-GPU suite is 775/775, same asmain.The guard decodes every instruction in the section and throws as soon as any truncated branch is outside the cut, without looking at where it sits. With a local probe on this head,
{66 EB 00, 90, 90, C3}rewritten at offset 4 (90->66 90) is rejected, although that branch starts before the cut and its target (offset 3) is before it too, so neither moves. So any length-changing rewrite now fails in a section that contains a66 EB/66 E9anywhere, including bytes that only decode that way. If that is the intended conservative choice, fine. Otherwise the before/after-cut test that the relocation adjustment above already uses (instrShifts/targetShifts) would narrow it to branches whose source or target moves. I did not relink a real title.Written with AI assistance (Claude Code). I ran the builds and tests above on my machine.
Imported from boykopovar/AnyPS5. Originally posted by yash27git on 2026-10-19T15:05:50Z.
Thank you for testing this on Linux and for the detailed review! I appreciate you checking the unaffected case as well.
You're right that the current guard is broader than necessary.
I'll revise the guard to reject only cases where the rewrite changes the position of a surviving truncated branch or its effective target.
I'll add a regression test for the unaffected case you identified and rerun the relevant tests.
Thanks again for catching this!
Imported from boykopovar/AnyPS5. Originally posted by jotitadev on 2026-10-21T23:19:43Z.
Review: COMMENTED
Revalidated the new head on Apple Silicon (M2 Max, macOS 27.2), arm64 native, relinker-only build (
-DANYPS5_RELINKER_ONLY=ON -DBUILD_TESTING=ON).Head:
1e30c5a6cThe narrowed guard now distinguishes a truncated relative branch that moves with the cut from one that stays put.
rewrite_boundariescovers it (19/19): the two new moving-branch cases (66 EB 01 90 C3,90 66 EB FC C3) are rejected, and the new unaffected case (66 EB 00 90 90 C3rewritten past the branch) is now accepted, producing66 EB 00 90 66 90 C3. That resolves the question from my earlier note — a length change beyond an unaffected truncated branch no longer errors.Full relinker suite: 209/209.
Written with AI assistance (Claude Code). I ran the builds and tests above on my machine.
Imported from boykopovar/AnyPS5. Originally posted by yash27git on 2026-10-22T03:45:06Z.
Thanks for taking the time to test this on your machine and verify the fix! Really appreciate the thorough review and for confirming that all 209 tests pass.
Query: Are you a Bot ?