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

Merged
yash27git merged 2 commits from upstream-pr/7822 into main 2026-10-24 12:14:55 +00:00
yash27git commented 2026-10-19 09:02:17 +00:00 (Migrated from github.com)

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

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4918458633 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/7822). Originally posted by [yash27git](https://github.com/yash27git) on 2026-10-19T09:02:17Z. --- ### 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 <!-- Rules behind each item: https://github.com/boykopovar/AnyPS5/blob/main/CONTRIBUTING.md --> - [x] Based on current `main`; no other open PR implements the same functions - [x] One topic per PR; follow-ups go in a new PR - [x] No comments in code except [technical debt](https://github.com/boykopovar/AnyPS5/blob/main/docs/dev/CONVENTIONS.md) - [x] Unimplemented paths throw (`NotImplemented_nid_no_patch`); silent stubs are listed under Silent stubs in their library's [technical debt](https://github.com/boykopovar/AnyPS5/tree/main/docs/dev/debt) file - [x] No notes, investigation `.md` files or images added to the repository (attach them to this PR) - [x] New third-party code is a submodule built from source - [x] Generic behaviour, not specific to one title - [x] Depends on: none - [x] Unblocks: none - [x] GPUs tested: n/a - [x] AI-assisted: yes 🤖 Generated with [ChatGPT](https://chatgpt.com)
xXPBSXx commented 2026-10-19 09:40:35 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by xXPBSXx on 2026-10-19T09:40:35Z.


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).

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6264263529 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/7822#issuecomment-6264263529). Originally posted by [xXPBSXx](https://github.com/xXPBSXx) on 2026-10-19T09:40:35Z. --- 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 commented 2026-10-19 10:00:38 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by yash27git on 2026-10-19T10:00:38Z.


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

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6264622184 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/7822#issuecomment-6264622184). Originally posted by [yash27git](https://github.com/yash27git) on 2026-10-19T10:00:38Z. --- > 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 commented 2026-10-19 10:28:27 +00:00 (Migrated from github.com)

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. 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.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5570214887 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/7822#pullrequestreview-5570214887). Originally posted by [jotitadev](https://github.com/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. 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 commented 2026-10-19 15:05:50 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by yash27git on 2026-10-19T15:05:50Z.


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!

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6270437663 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/7822#issuecomment-6270437663). Originally posted by [yash27git](https://github.com/yash27git) on 2026-10-19T15:05:50Z. --- > 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!
jotitadev commented 2026-10-21 23:19:43 +00:00 (Migrated from github.com)

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: 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.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5607414703 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/7822#pullrequestreview-5607414703). Originally posted by [jotitadev](https://github.com/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: `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 commented 2026-10-22 03:45:06 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by yash27git on 2026-10-22T03:45:06Z.


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 ?

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6332164164 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/7822#issuecomment-6332164164). Originally posted by [yash27git](https://github.com/yash27git) on 2026-10-22T03:45:06Z. --- > 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 ?
Sign in to join this conversation.
No reviewers
No labels
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Reference
boykopovar/AnyPS5!7822
No description provided.