Skip to content

fix(relinker): guard length-changing rewrites around truncated branches - #7822

Merged
adriadam10 merged 2 commits into
boykopovar:mainfrom
yash27git:fix/refuse-truncated-branch-rewrites
Oct 24, 2026
Merged

adriadam10 merged 2 commits into
boykopovar:mainfrom
yash27git:fix/refuse-truncated-branch-rewrites

Conversation

@yash27git

@yash27git yash27git commented Oct 19, 2026 •

Copy link
Copy Markdown
Contributor

What

The fix for 16-bit-truncated relative branch targets adds TruncatesBranchTarget metadata and deliberately leaves HasBranchTarget false 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

  • Reviewer "@jotitadev" revalidated commit "1e30c5a6c" on Apple Silicon (M2 Max, macOS 27.2, native ARM64) using a relinker-only build with testing enabled.
  • "rewrite_boundaries": 19/19 passed, including regression cases for moving and unaffected truncated relative branches.
  • Full relinker test suite: 209/209 passed.
  • The reviewer confirmed that the narrowed guard rejects cases where the truncated branch would be affected while accepting a length-changing rewrite when the branch remains unaffected.

ctest -R '^rewrite_boundaries$' on the combined tree at 5563156935, including #7817 and #7746:

  • With this PR's change: passes 3 of 3.
  • With X64InstructionRewriter.cpp restored to main at fb0dc118f, while retaining the regression test: fails 3 of 3 with Length-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

  • Relinker build and tests: macOS (clang++, x86-64 and native), Ubuntu (g++) and Windows (g++) — successful.
  • Relinker AddressSanitizer checks — successful.
  • Conventions, PR eligibility and PR-label checks — successful.
  • Four build jobs were skipped: Linux, register-context, Windows and write-watch.

Not tested: relinking a real title or running the regression test manually on Linux.

Checklist

  • Based on current main; no other open PR implements the same functions
  • One topic per PR; follow-ups go in a new PR
  • No comments in code except technical debt
  • Unimplemented paths throw (NotImplemented_nid_no_patch); silent stubs are listed under Silent stubs in their library's technical debt file
  • No notes, investigation .md files or images added to the repository (attach them to this PR)
  • New third-party code is a submodule built from source
  • Generic behaviour, not specific to one title
  • Depends on: none
  • Unblocks: none
  • GPUs tested: n/a
  • AI-assisted: yes

🤖 Generated with ChatGPT

@github-actions

github-actions Bot commented Oct 19, 2026 •

Copy link
Copy Markdown

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.

Pull request Conflicting files (lines on main) Same exports Same new files
PR 9454 core/relinker/codegen/tests/RewriteBoundaryTests.cpp L21-26

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.

Pull request Shared files not compared
PR 5021 docs/dev/debt/relinker.md
PR 7173 docs/dev/debt/relinker.md
PR 8076 docs/dev/debt/relinker.md
PR 9228 docs/dev/debt/relinker.md
PR 9408 docs/dev/debt/relinker.md

@xXPBSXx

xXPBSXx commented Oct 19, 2026

Copy link
Copy Markdown
Contributor

Windows data point for 554691a0b merged onto main fb0dc118f (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$'.

  • With the PR: passes 3 of 3.
  • Same tree with core/relinker/codegen/src/x86/X64InstructionRewriter.cpp restored to main (git checkout fb0dc118f -- core/relinker/codegen/src/x86/X64InstructionRewriter.cpp; git status shows 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).

@yash27git

Copy link
Copy Markdown
Contributor Author

Windows data point for 554691a0b merged onto main fb0dc118f (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$'.

  • With the PR: passes 3 of 3.
  • Same tree with core/relinker/codegen/src/x86/X64InstructionRewriter.cpp restored to main (git checkout fb0dc118f -- core/relinker/codegen/src/x86/X64InstructionRewriter.cpp; git status shows 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).

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! 🙏

@jotitadev jotitadev left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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. Head 554691a0b merged onto main 8816fd772 (clean merge), together with #7363 and #7820 (no shared files).

  • rewrite_boundaries passes, the PR's RewriteBoundaryTests.cpp on main (rewriter unchanged) fails, and the non-GPU suite is 775/775, same as main.

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 a 66 EB/66 E9 anywhere, 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.

@yash27git

Copy link
Copy Markdown
Contributor Author

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. Head 554691a0b merged onto main 8816fd772 (clean merge), together with #7363 and #7820 (no shared files).

  • rewrite_boundaries passes, the PR's RewriteBoundaryTests.cpp on main (rewriter unchanged) fails, and the non-GPU suite is 775/775, same as main.

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 a 66 EB/66 E9 anywhere, 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.

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!

@yash27git
yash27git requested a review from jotitadev October 19, 2026 17:11
@adriadam10 adriadam10 self-assigned this Oct 19, 2026
@github-actions github-actions Bot added needs rebase Conflicts with main, rebase needed and removed needs rebase Conflicts with main, rebase needed labels Oct 20, 2026

@jotitadev jotitadev left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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: 1e30c5a6c

The narrowed guard now distinguishes a truncated relative branch that moves with the cut from one that stays put. rewrite_boundaries covers 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 C3 rewritten past the branch) is now accepted, producing 66 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.

@yash27git

yash27git commented Oct 22, 2026 •

Copy link
Copy Markdown
Contributor Author

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: 1e30c5a6c

The narrowed guard now distinguishes a truncated relative branch that moves with the cut from one that stays put. rewrite_boundaries covers 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 C3 rewritten past the branch) is now accepted, producing 66 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.

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 ?

@github-actions github-actions Bot added the needs rebase Conflicts with main, rebase needed label Oct 22, 2026
@yash27git
yash27git force-pushed the fix/refuse-truncated-branch-rewrites branch 2 times, most recently from a7904a0 to db35f9a Compare October 23, 2026 10:46
@github-actions github-actions Bot removed the needs rebase Conflicts with main, rebase needed label Oct 23, 2026
@yash27git
yash27git force-pushed the fix/refuse-truncated-branch-rewrites branch from db35f9a to 6e8f9dc Compare October 23, 2026 10:50
@BKlaasWerkman

Copy link
Copy Markdown
Contributor

Windows run of the current head: 6e8f9dc merged into main 990f45e (clean merge). Windows 11 Pro 10.0.26300, WinLibs GCC 15.2.0 r7, relinker-only BUILD_TESTING=ON, Release. The full suite passes 235 of 235, including rewrite_boundaries with the regression cases, and none changes result against a run of that main on the same machine.

AI-assisted: yes (Claude Code)

@yash27git

Copy link
Copy Markdown
Contributor Author

Windows run of the current head: 6e8f9dc merged into main 990f45e (clean merge). Windows 11 Pro 10.0.26300, WinLibs GCC 15.2.0 r7, relinker-only BUILD_TESTING=ON, Release. The full suite passes 235 of 235, including rewrite_boundaries with the regression cases, and none changes result against a run of that main on the same machine.

AI-assisted: yes (Claude Code)

Thanks for testing the current head on Windows and sharing the detailed results. Glad to see all 235 tests passing, including rewrite_boundaries. Appreciate the extra validation!

@github-actions github-actions Bot added the needs rebase Conflicts with main, rebase needed label Oct 24, 2026
@yash27git
yash27git force-pushed the fix/refuse-truncated-branch-rewrites branch from 6e8f9dc to eb3d0b1 Compare October 24, 2026 04:43
@github-actions github-actions Bot removed the needs rebase Conflicts with main, rebase needed label Oct 24, 2026
@adriadam10
adriadam10 force-pushed the fix/refuse-truncated-branch-rewrites branch from eb3d0b1 to e1fea2e Compare October 24, 2026 09:01
@adriadam10
adriadam10 merged commit 3fcacfb into boykopovar:main Oct 24, 2026
12 checks passed
@adriadam10

Copy link
Copy Markdown
Collaborator

Checked the guard against the rewriter (displacement decode for rel8/rel16, moving vs unaffected branch, 16-bit range) and the new rewrite_boundaries cases; the Linux, macOS and Windows runs from @jotitadev, @xXPBSXx and @BKlaasWerkman cover the rest.

Added on top as our own commit:

  • docs/dev/debt/relinker.md: the entry now also states that edits are rejected when a truncated branch would move relative to its target, not only the 16-bit range case.
  • Removed the stray leading blank line in RewriteBoundaryTests.cpp.

Rebased onto current main, relinker tests pass; merged.

@yash27git
yash27git deleted the fix/refuse-truncated-branch-rewrites branch October 24, 2026 12:33
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants