fix(relinker): drop unwind ranges that begin inside a decoded instruction #20387

Open
oneandonlydean wants to merge 4 commits from upstream-pr/20387 into main
oneandonlydean commented 2026-11-15 04:01:14 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by oneandonlydean on 2026-11-15T04:01:14Z.


What

The relinker stopped on a PPSA03977 build with Code analysis: Instruction truncated after prefixes (offset 0x3957a) at "Output image 19%". Address 0x3957a is byte 1 of a jmp rel32 (E9 64 FE FF FF at 0x39579), and its 0x64 is the low byte of the displacement.

Cause: an unwind function range whose begin lies inside an instruction that an enclosing function already decoded was decoded again from that byte, and its begin was also kept as a code root. A one byte range starting on the 0x64 leaves a tail that is only a segment prefix, so the decoder throws and the FS prefix rule in the tail check turns the throw into a failed relink. A longer range from the same byte fails later with overlapping instruction boundaries because the root decodes over the enclosing instruction.

Fix: CodeInstructionCollector drops such a range and its root before decoding it. It only checks ranges that start before the end of an earlier range, so ordinary functions pay nothing. A range with no enclosing decoded instruction is rejected as before.

The exact unwind data of the user's eboot was not available. The test builds the same bytes and ranges synthetically; which source produced the range in that build was not confirmed.

Tested

Linux, release build. New case unwindRangeInsideInstruction in relinker_game_patch_tests: E9 64 FE FF FF inside a function with a one byte range at the 0x64 (and a nested longer range) converts now. Without the change it fails with Instruction truncated after prefixes. A lone one byte range at the 0x64 is still rejected. relinker_conversion, control_flow_graph and relinker_game_patch pass.

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
  • Fixes: none
  • Unblocks: PPSA03977 (not confirmed on the user eboot)
  • GPUs tested: n/a
  • AI-assisted: yes

Summary by CodeRabbit

  • Bug Fixes
    • Improved handling of overlapping and nested unwind ranges during Windows conversion.
    • Conversion now reports failures when range boundaries or initializer targets cut through an instruction, including instructions with prefixes.
    • Invalid unwind entries that begin within an instruction are excluded, while valid entries are preserved when a surrounding range is discarded.
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5319282434 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/20387). Originally posted by [oneandonlydean](https://github.com/oneandonlydean) on 2026-11-15T04:01:14Z. --- ### What The relinker stopped on a PPSA03977 build with `Code analysis: Instruction truncated after prefixes (offset 0x3957a)` at "Output image 19%". Address 0x3957a is byte 1 of a `jmp rel32` (`E9 64 FE FF FF` at 0x39579), and its `0x64` is the low byte of the displacement. Cause: an unwind function range whose begin lies inside an instruction that an enclosing function already decoded was decoded again from that byte, and its begin was also kept as a code root. A one byte range starting on the `0x64` leaves a tail that is only a segment prefix, so the decoder throws and the FS prefix rule in the tail check turns the throw into a failed relink. A longer range from the same byte fails later with `overlapping instruction boundaries` because the root decodes over the enclosing instruction. Fix: `CodeInstructionCollector` drops such a range and its root before decoding it. It only checks ranges that start before the end of an earlier range, so ordinary functions pay nothing. A range with no enclosing decoded instruction is rejected as before. The exact unwind data of the user's eboot was not available. The test builds the same bytes and ranges synthetically; which source produced the range in that build was not confirmed. ### Tested Linux, release build. New case `unwindRangeInsideInstruction` in `relinker_game_patch_tests`: `E9 64 FE FF FF` inside a function with a one byte range at the `0x64` (and a nested longer range) converts now. Without the change it fails with `Instruction truncated after prefixes`. A lone one byte range at the `0x64` is still rejected. `relinker_conversion`, `control_flow_graph` and `relinker_game_patch` pass. ### Checklist - [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] Fixes: none - [x] Unblocks: PPSA03977 (not confirmed on the user eboot) - [x] GPUs tested: n/a - [x] AI-assisted: yes <!-- This is an auto-generated comment: release notes by coderabbit.ai --> <!-- coderabbit-ai-generated:v1 --> ## Summary by CodeRabbit * **Bug Fixes** * Improved handling of overlapping and nested unwind ranges during Windows conversion. * Conversion now reports failures when range boundaries or initializer targets cut through an instruction, including instructions with prefixes. * Invalid unwind entries that begin within an instruction are excluded, while valid entries are preserved when a surrounding range is discarded. <!-- end of auto-generated comment: release notes by coderabbit.ai -->
MarcZX10 commented 2026-11-15 04:41:43 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-11-15T04:41:43Z.

Review: APPROVED


Reviewed — looks good

Nothing in the inspected changes blocks merging; the new range filtering is covered by synthetic conversion cases.

What I ran

Reviewed head 082070994d64a5aa54da43b1d104610d7a3cba9d using repo_read.py. I did not run build or test commands.

Results

Check Result Note
Changed code and tests passed All changed lines read
Build and tests not run No commands run

Findings

Blocking

None.

Non-blocking

None.

Not verified

Build and test results reported in the PR description were not independently verified. The user's eboot and the source of its unwind range were not available for review. Both changed files listed in manifest.json were read; no files were excluded by the manifest.

To get this to a good state

Nothing is required.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5909121861 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/20387#pullrequestreview-5909121861). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-11-15T04:41:43Z. **Review:** APPROVED --- # Reviewed — looks good Nothing in the inspected changes blocks merging; the new range filtering is covered by synthetic conversion cases. ## What I ran Reviewed head `082070994d64a5aa54da43b1d104610d7a3cba9d` using `repo_read.py`. I did not run build or test commands. ## Results | Check | Result | Note | |---|---|---| | Changed code and tests | passed | All changed lines read | | Build and tests | not run | No commands run | ## Findings ### Blocking None. ### Non-blocking None. ## Not verified Build and test results reported in the PR description were not independently verified. The user's eboot and the source of its unwind range were not available for review. Both changed files listed in `manifest.json` were read; no files were excluded by the manifest. ## To get this to a good state Nothing is required.
PrivacyOnion commented 2026-11-15 05:03:27 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by PrivacyOnion on 2026-11-15T05:03:27Z.

Review: APPROVED


Verified on Linux (Ubuntu 24.04, GCC 13.3, Ninja, Release) against 082070994d:

  • Conventions: tools/check_conventions.py passed; git diff --check clean.
  • Build: relinker_game_patch_tests built cleanly.
  • CTest (relinker_game_patch):
    1/1 Test #5235: relinker_game_patch ..............   Passed    0.03 sec
    100% tests passed, 0 tests failed out of 1
    
  • Negative control: with core/relinker/relinker/src/analysis/CodeInstructionCollector.cpp reverted to main and the new tests kept, the suite fails:
    1/1 Test #5235: relinker_game_patch ..............***Failed    0.02 sec
    0% tests passed, 1 tests failed out of 1
    
    Restoring the change makes it pass again.
  • Code reading:
    • core/relinker/relinker/src/analysis/CodeInstructionCollector.cpp: Verified that insideEnclosingInstruction safely navigates reverse map iterators using outer != functions.begin() and --outer, eliminating iterator underflow or invalid dereferences when scanning enclosing ranges.
    • core/relinker/relinker/src/analysis/CodeInstructionCollector.cpp: Verified instruction slice safety, ensuring fileOffset(outerBegin, outerEnd - outerBegin) and decoder.DecodeInstruction(..., outerEnd - address) are strictly bounds-checked with length == 0 || length > outerEnd - address, and that Codegen::CodegenException is safely caught.
    • core/relinker/relinker/src/analysis/CodeInstructionCollector.cpp: Verified that roots.erase(begin) reliably strips invalid mid-instruction addresses from code entry roots, while ranges extending beyond outerEnd (end > outerEnd) correctly bypass filtering to trigger 'overlapping instruction boundaries'.
    • core/relinker/relinker/tests/GamePatchTests.cpp: Verified that unwindRangeInsideInstruction thoroughly exercises nested unwind ranges, exact-boundary ranges, and contiguous ranges, and validates negative controls rejecting lone truncated ranges ('Instruction truncated after prefixes') and overlapping ranges ('overlapping instruction boundaries').
    • AGENTS.md & Portability: Confirmed zero comments added to implementation and test files, consistent 64-bit unsigned integer arithmetic ensuring LP64 and LLP64 portability, and generic non-title-specific handling.
  • Note: the AI-assisted checkbox can stay.

Looks Good To Me

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5909199591 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/20387#pullrequestreview-5909199591). Originally posted by [PrivacyOnion](https://github.com/PrivacyOnion) on 2026-11-15T05:03:27Z. **Review:** APPROVED --- Verified on Linux (Ubuntu 24.04, GCC 13.3, Ninja, Release) against `082070994d`: - **Conventions**: `tools/check_conventions.py` passed; `git diff --check` clean. - **Build**: `relinker_game_patch_tests` built cleanly. - **CTest** (`relinker_game_patch`): ``` 1/1 Test #5235: relinker_game_patch .............. Passed 0.03 sec 100% tests passed, 0 tests failed out of 1 ``` - **Negative control**: with `core/relinker/relinker/src/analysis/CodeInstructionCollector.cpp` reverted to `main` and the new tests kept, the suite fails: ``` 1/1 Test #5235: relinker_game_patch ..............***Failed 0.02 sec 0% tests passed, 1 tests failed out of 1 ``` Restoring the change makes it pass again. - **Code reading**: - core/relinker/relinker/src/analysis/CodeInstructionCollector.cpp: Verified that insideEnclosingInstruction safely navigates reverse map iterators using outer != functions.begin() and --outer, eliminating iterator underflow or invalid dereferences when scanning enclosing ranges. - core/relinker/relinker/src/analysis/CodeInstructionCollector.cpp: Verified instruction slice safety, ensuring fileOffset(outerBegin, outerEnd - outerBegin) and decoder.DecodeInstruction(..., outerEnd - address) are strictly bounds-checked with length == 0 || length > outerEnd - address, and that Codegen::CodegenException is safely caught. - core/relinker/relinker/src/analysis/CodeInstructionCollector.cpp: Verified that roots.erase(begin) reliably strips invalid mid-instruction addresses from code entry roots, while ranges extending beyond outerEnd (end > outerEnd) correctly bypass filtering to trigger 'overlapping instruction boundaries'. - core/relinker/relinker/tests/GamePatchTests.cpp: Verified that unwindRangeInsideInstruction thoroughly exercises nested unwind ranges, exact-boundary ranges, and contiguous ranges, and validates negative controls rejecting lone truncated ranges ('Instruction truncated after prefixes') and overlapping ranges ('overlapping instruction boundaries'). - AGENTS.md & Portability: Confirmed zero comments added to implementation and test files, consistent 64-bit unsigned integer arithmetic ensuring LP64 and LLP64 portability, and generic non-title-specific handling. - Note: the AI-assisted checkbox can stay. Looks Good To Me
catminou4 commented 2026-11-25 06:56:06 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by catminou4 on 2026-11-25T06:56:06Z.

Review: CHANGES_REQUESTED


Reviewed at 082070994d, relinker-only Release build (GCC, Ubuntu 22.04).

  • On the PR head (base 4b5344fc89): relinker_game_patch passes.
  • Applied on current main 7a1c9bbccf: relinker_game_patch fails:
    accepted: expected `overlapping instruction boundaries`
    
    The last case of unwindRangeInsideInstruction ({0x1200, 0x1209}, {0x1205, 0x120f}) is now accepted. On main the overlapping instruction boundaries throw in CodeInstructionCollector.cpp (line 801) only fires when spanHasFsPrefix, so this expectation is stale. With CodeInstructionCollector.cpp from main the test fails as well, so I could not isolate the fix on current main.
  • No other relinker test regresses (full ctest: only relinker_game_patch and the environmental tools_headers_manifest).
  • No textual conflict with #20900, #25672 or #25897.

Not mergeable as is: rebase on main and update or drop the overlapping case.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:6053132246 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/20387#pullrequestreview-6053132246). Originally posted by [catminou4](https://github.com/catminou4) on 2026-11-25T06:56:06Z. **Review:** CHANGES_REQUESTED --- Reviewed at 082070994d, relinker-only Release build (GCC, Ubuntu 22.04). - On the PR head (base 4b5344fc89): `relinker_game_patch` passes. - **Applied on current `main` 7a1c9bbccf:** `relinker_game_patch` fails: ``` accepted: expected `overlapping instruction boundaries` ``` The last case of `unwindRangeInsideInstruction` (`{0x1200, 0x1209}, {0x1205, 0x120f}`) is now accepted. On `main` the `overlapping instruction boundaries` throw in `CodeInstructionCollector.cpp` (line 801) only fires when `spanHasFsPrefix`, so this expectation is stale. With `CodeInstructionCollector.cpp` from `main` the test fails as well, so I could not isolate the fix on current `main`. - No other relinker test regresses (full `ctest`: only `relinker_game_patch` and the environmental `tools_headers_manifest`). - No textual conflict with #20900, #25672 or #25897. Not mergeable as is: rebase on `main` and update or drop the overlapping case.
MarcZX10 commented 2026-11-30 00:23:37 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-11-30T00:23:37Z.

Review: APPROVED


Reviewed — looks good

The change at head 715ec7213a00bfcda03dd0ef7b9c62661f9b9fe8 adds filtering for unwind function entries that begin inside a decoded instruction; I found no verified blocking defect in the changed code. The prepared bundle reports tests, but I did not run them.

What I ran

Read the prepared description, commits, manifest, diff, and both changed files at the pinned head using repo_read.py. I did not execute tests or other commands.

Results

Check Result Note
Changed-file review passed Both reviewed files read
Tests not run Results in the PR description only

Findings

Blocking

None.

Non-blocking

None.

Not verified

I did not run the reported Linux release tests or independently check the behavior against the user’s eboot. The manifest lists two changed files and both were read: core/relinker/relinker/src/analysis/CodeInstructionCollector.cpp and core/relinker/relinker/tests/GamePatchTests.cpp. No changed files were excluded by the prepared manifest.

To get this to a good state

Nothing is required.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:6105610877 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/20387#pullrequestreview-6105610877). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-11-30T00:23:37Z. **Review:** APPROVED --- # Reviewed — looks good The change at head `715ec7213a00bfcda03dd0ef7b9c62661f9b9fe8` adds filtering for unwind function entries that begin inside a decoded instruction; I found no verified blocking defect in the changed code. The prepared bundle reports tests, but I did not run them. ## What I ran Read the prepared description, commits, manifest, diff, and both changed files at the pinned head using `repo_read.py`. I did not execute tests or other commands. ## Results | Check | Result | Note | |---|---|---| | Changed-file review | passed | Both reviewed files read | | Tests | not run | Results in the PR description only | ## Findings ### Blocking None. ### Non-blocking None. ## Not verified I did not run the reported Linux release tests or independently check the behavior against the user’s eboot. The manifest lists two changed files and both were read: `core/relinker/relinker/src/analysis/CodeInstructionCollector.cpp` and `core/relinker/relinker/tests/GamePatchTests.cpp`. No changed files were excluded by the prepared manifest. ## To get this to a good state Nothing is required.
coderabbitai[bot] commented 2026-11-30 00:27:35 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by coderabbitai[bot] on 2026-11-30T00:27:35Z.

Review: COMMENTED


Architecture findings (1)

Advisory architecture findings (1)

  • Trivial — At the merge base, a range-only address that survived code analysis was included in the rewrite guard's protection set. At the reviewed head, a qualifying interior address can be removed from that set, so a TLS or GS rewrite spanning it may proceed if no independent evidence restores protection. The source establishes the narrower guard, not that any discarded address is a live control-flow target.
Review info

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: defaults
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: e34cf942-77b6-4b88-bdd4-4bf824f6626d
Commits

Reviewing files that changed from the base of the PR and between 4b0f137c86 and 715ec7213a.

Files selected for processing: 2

  • core/relinker/relinker/src/analysis/CodeInstructionCollector.cpp
  • core/relinker/relinker/tests/GamePatchTests.cpp

Included review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 8 remain after this review.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:6105630365 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/20387#pullrequestreview-6105630365). Originally posted by [coderabbitai[bot]](https://github.com/apps/coderabbitai) on 2026-11-30T00:27:35Z. **Review:** COMMENTED --- <!-- coderabbit-ai-generated:v1 --> **Architecture findings (1)** <details> <summary>Advisory architecture findings (1)</summary> <dl> <dd> <br> - **Trivial** — At the merge base, a range-only address that survived code analysis was included in the rewrite guard's protection set. At the reviewed head, a qualifying interior address can be removed from that set, so a TLS or GS rewrite spanning it may proceed if no independent evidence restores protection. The source establishes the narrower guard, not that any discarded address is a live control-flow target. </dd> </dl> </details> <h6> <details> <summary><picture><img src="https://storage.googleapis.com/coderabbit_public_assets/review-comment-icons-v1/info.svg" width="12" height="12" align="absmiddle" alt=""></picture> Review info</summary> <dl> <dd> <br> <details> <summary><picture><source media="(prefers-color-scheme: dark)" srcset="https://storage.googleapis.com/coderabbit_public_assets/review-comment-icons-v1/cog-dark.svg"><img src="https://storage.googleapis.com/coderabbit_public_assets/review-comment-icons-v1/cog.svg" width="16" height="16" align="absmiddle" alt=""></picture> Run configuration</summary> <dl> <dd> <br> - **Source mode**: Auto - **Repository**: Unavailable - **Branch**: Unavailable - **File**: No configuration file from this repository applied - **Commit**: Unavailable - **Layers**: defaults - **Review profile**: CHILL - **Plan**: Advanced - **Run ID**: `e34cf942-77b6-4b88-bdd4-4bf824f6626d` </dd> </dl> </details> <details> <summary><picture><source media="(prefers-color-scheme: dark)" srcset="https://storage.googleapis.com/coderabbit_public_assets/review-comment-icons-v1/git-commit-horizontal-dark.svg"><img src="https://storage.googleapis.com/coderabbit_public_assets/review-comment-icons-v1/git-commit-horizontal.svg" width="16" height="16" align="absmiddle" alt=""></picture> Commits</summary> <dl> <dd> <br> Reviewing files that changed from the base of the PR and between 4b0f137c86c25689b04bcb9dfe62e75f264027aa and 715ec7213a00bfcda03dd0ef7b9c62661f9b9fe8. </dd> </dl> </details> <details> <summary><picture><source media="(prefers-color-scheme: dark)" srcset="https://storage.googleapis.com/coderabbit_public_assets/review-comment-icons-v1/file-plus-corner-dark.svg"><img src="https://storage.googleapis.com/coderabbit_public_assets/review-comment-icons-v1/file-plus-corner.svg" width="16" height="16" align="absmiddle" alt=""></picture> Files selected for processing: 2</summary> <dl> <dd> <br> * `core/relinker/relinker/src/analysis/CodeInstructionCollector.cpp` * `core/relinker/relinker/tests/GamePatchTests.cpp` </dd> </dl> </details> **Included review availability:** This review used your included allowance. Your plan provides up to 10 included reviews per hour; 8 remain after this review. </dd> </dl> </details> </h6> <!-- This is an auto-generated comment by CodeRabbit for review status --> <!-- coderabbit-review-publication v1 publication=96487ae1-f481-4087-9cd1-939300411382 attempt=3e9f7130-a271-46b4-8d12-b66ed44eb050 batch=1/1 -->
ligmaSec commented 2026-12-03 11:55:25 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by ligmaSec on 2026-12-03T11:55:25Z.

Review: COMMENTED


Built this head with ANYPS5_RELINKER_ONLY=ON and ran relinker_game_patch on Linux. It passed, including the branch’s unwind-range regression in GamePatchTests.cpp.

AI-assisted: yes.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:6169254647 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/20387#pullrequestreview-6169254647). Originally posted by [ligmaSec](https://github.com/ligmaSec) on 2026-12-03T11:55:25Z. **Review:** COMMENTED --- Built this head with `ANYPS5_RELINKER_ONLY=ON` and ran `relinker_game_patch` on Linux. It passed, including the branch’s unwind-range regression in `GamePatchTests.cpp`. AI-assisted: yes.
This pull request cannot be merged automatically due to conflicts.
Merge manually to resolve the conflicts.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin 'upstream-pr/20387:upstream-pr/20387'
git switch 'upstream-pr/20387'

Merge

Merge the changes and update on Forgejo.

Warning: The "Autodetect manual merge" setting is not enabled for this repository, you will have to mark this pull request as manually merged afterwards.

git switch 'main'
git merge --no-ff 'upstream-pr/20387'
git switch 'upstream-pr/20387'
git rebase 'main'
git switch 'main'
git merge --ff-only 'upstream-pr/20387'
git switch 'upstream-pr/20387'
git rebase 'main'
git switch 'main'
git merge --no-ff 'upstream-pr/20387'
git switch 'main'
git merge --squash 'upstream-pr/20387'
git switch 'main'
git merge --ff-only 'upstream-pr/20387'
git switch 'main'
git merge 'upstream-pr/20387'
git push origin 'main'
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!20387
No description provided.