loader: extend Windows red-zone protection to functions missing from .eh_frame (Saros) #2099

Open
github-bot wants to merge 2 commits from github-bot/gh-2099 into main AGit
Owner

@⁠joaocaparroz on GitHub · 2026-11-04 04:20 UTC · original
joaocaparroz:redzone-unlisted-functions → main · +131 −2 · 1 files · 2 commits · open

Game

Saros (PPSA07631, 01.005.002). Refs #1053.

Problem

On Windows the game crashed within about a minute of gameplay, both with and without --redzone, with:

Unhandled host exception: type=1 code=3221225477 pc=0x00000009001ff0fc access=2 address=0x...
 in src/loader/runtimeLinker.cpp:721

It also crashes on build 02b17af, so this is not a recent regression.

The faulting code is a leaf function in the game (0x9001feffe) that copies 28-byte records into a locked GPU buffer. It keeps its loop count at [rbp-0x40], which is inside the SysV red zone (rsp = rbp - 0x28 in that function). When a write to tracked GPU memory raises an exception, Windows builds the exception frame on the guest stack and overwrites that count. The loop then runs past the end of the buffer into an unmapped page. Dumping the caller's state at the fault showed a count of 1 while the loop was at record 16384.

--redzone already handles exactly this situation, but three gaps kept it from reaching the functions involved:

  1. Functions missing from .eh_frame were never analyzed. 0x9001feffe isn't listed, so it was decoded as part of the preceding function, and that decode never reached it.
  2. Only RSP-relative red-zone operands were recognized. This function reaches its red zone through RBP.
  3. Jump tables outside the executable segment were rejected. The tables normally sit in .rodata. A second function (0x9032ead00, reached when pressing X in game) dispatches through such a table before reloading a pointer from [rsp-0x30]. That part of the function was never decoded, the pointer came back as null, and the game crashed.

Changes

All changes are in src/loader/guestInstructionPatcher.cpp:

  • Direct call targets found in decoded code are added as function starts, repeating until no new ones appear. They only come from decoded control flow, never from raw byte scans. Tail jumps are left out, since they can land in the middle of a function.
  • New DetectFramePointerRedZone: it gets the RBP-to-RSP distance from the prologue (push rbp; mov rbp, rsp, then pushes and sub rsp, imm). If any [rbp-d] access falls in the red zone, the function is protected conservatively. It does nothing if RBP is written outside the epilogue.
  • ResolveBoundedJumpTable accepts a table anywhere in the loaded module image. Every target still has to be inside the function.

The function discovery only runs with --redzone, so default runs behave as before. The jump-table change also affects the --amd-cpu patch path, where it only allows more switch bodies to be decoded.

Testing

  • Saros on Windows 11 with --redzone. Before the fix, four runs crashed after 27–70 s. With a diagnostic build I confirmed both faulting instructions now get rewritten. After the fix, the game runs past both points, pressing X no longer crashes, and it exits cleanly.
  • The CI test set (ctest, 32 tests) passes locally with clang-cl.
  • Performance: gameplay runs at about 15 FPS with --redzone. Without the flag the game crashes too early to compare, so I couldn't isolate the overhead of the extra protected sites. I haven't measured the load-time cost of the extra decode pass either.

AI use

I used an AI coding assistant for most of the investigation and the implementation: disassembling the game's code, adding temporary diagnostics to trace the fault, and writing the patcher changes and this description. I reviewed the final diff and tested the game on my machine before and after the change, as described above. The temporary diagnostics are not part of this PR.


Thanks for the existing --redzone protection; it made this fix much smaller than it would otherwise have been. I'm happy to adjust anything, split the changes, or test other games if that helps.

**@⁠joaocaparroz** on GitHub · 2026-11-04 04:20 UTC · [original](https://github.com/KytyPS5/KytyPS5/pull/2099) `joaocaparroz:redzone-unlisted-functions` → `main` · +131 −2 · 1 files · 2 commits · open ## Game Saros (PPSA07631, 01.005.002). Refs #1053. ## Problem On Windows the game crashed within about a minute of gameplay, both with and without `--redzone`, with: ``` Unhandled host exception: type=1 code=3221225477 pc=0x00000009001ff0fc access=2 address=0x... in src/loader/runtimeLinker.cpp:721 ``` It also crashes on build `02b17af`, so this is not a recent regression. The faulting code is a leaf function in the game (`0x9001feffe`) that copies 28-byte records into a locked GPU buffer. It keeps its loop count at `[rbp-0x40]`, which is inside the SysV red zone (`rsp = rbp - 0x28` in that function). When a write to tracked GPU memory raises an exception, Windows builds the exception frame on the guest stack and overwrites that count. The loop then runs past the end of the buffer into an unmapped page. Dumping the caller's state at the fault showed a count of 1 while the loop was at record 16384. `--redzone` already handles exactly this situation, but three gaps kept it from reaching the functions involved: 1. **Functions missing from `.eh_frame` were never analyzed.** `0x9001feffe` isn't listed, so it was decoded as part of the preceding function, and that decode never reached it. 2. **Only RSP-relative red-zone operands were recognized.** This function reaches its red zone through RBP. 3. **Jump tables outside the executable segment were rejected.** The tables normally sit in `.rodata`. A second function (`0x9032ead00`, reached when pressing X in game) dispatches through such a table before reloading a pointer from `[rsp-0x30]`. That part of the function was never decoded, the pointer came back as null, and the game crashed. ## Changes All changes are in `src/loader/guestInstructionPatcher.cpp`: - Direct `call` targets found in decoded code are added as function starts, repeating until no new ones appear. They only come from decoded control flow, never from raw byte scans. Tail jumps are left out, since they can land in the middle of a function. - New `DetectFramePointerRedZone`: it gets the RBP-to-RSP distance from the prologue (`push rbp; mov rbp, rsp`, then pushes and `sub rsp, imm`). If any `[rbp-d]` access falls in the red zone, the function is protected conservatively. It does nothing if RBP is written outside the epilogue. - `ResolveBoundedJumpTable` accepts a table anywhere in the loaded module image. Every target still has to be inside the function. The function discovery only runs with `--redzone`, so default runs behave as before. The jump-table change also affects the `--amd-cpu` patch path, where it only allows more `switch` bodies to be decoded. ## Testing - Saros on Windows 11 with `--redzone`. Before the fix, four runs crashed after 27–70 s. With a diagnostic build I confirmed both faulting instructions now get rewritten. After the fix, the game runs past both points, pressing X no longer crashes, and it exits cleanly. - The CI test set (`ctest`, 32 tests) passes locally with clang-cl. - Performance: gameplay runs at about 15 FPS with `--redzone`. Without the flag the game crashes too early to compare, so I couldn't isolate the overhead of the extra protected sites. I haven't measured the load-time cost of the extra decode pass either. ## AI use I used an AI coding assistant for most of the investigation and the implementation: disassembling the game's code, adding temporary diagnostics to trace the fault, and writing the patcher changes and this description. I reviewed the final diff and tested the game on my machine before and after the change, as described above. The temporary diagnostics are not part of this PR. --- Thanks for the existing `--redzone` protection; it made this fix much smaller than it would otherwise have been. I'm happy to adjust anything, split the changes, or test other games if that helps.
--redzone only analyzed functions listed in .eh_frame, only recognized
RSP-relative red-zone operands, and dropped jump tables stored outside the
executable segment. Saros (PPSA07631) hits all three: leaf functions that are
not in .eh_frame keep locals below RSP, a Windows exception raised by a
tracked GPU write inside them overwrites those locals, and the game crashes
(a loop count turns into garbage and the loop runs past its buffer, or a
pointer reloads as null).

- Discover functions from the direct call targets of decoded code, to a
  fixpoint, so unlisted functions are analyzed instead of being decoded as
  the tail of the preceding function.
- Detect red-zone use through RBP in frame-pointer functions
  (push rbp; mov rbp, rsp; push/sub rsp) and protect such functions
  conservatively.
- Resolve jump tables that live anywhere in the loaded module (usually
  .rodata); targets are still required to be inside the function.
Direct calls into the current function's range were recorded only as
callees. When --redzone is off (--amd-cpu or the TSC-only path),
AddDecodedCallees does not run, so the function is never split there and
the entry of an unlisted function could end up inside a relocated span.
Record the target as a branch target as well, as before.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 10:14:11 +00:00
Author
Owner

@⁠coderabbitai[bot] on GitHub · 2026-11-04 04:24 UTC · original · edited 2026-11-04 04:46 UTC

Review in Change Stack →

No actionable comments were generated in the recent review.

Walkthrough

The patch records direct-call targets during function decoding and expands known function starts when memory protection is enabled. It accepts jump tables within a registered module image, while resolved targets must remain inside the function. During red-zone analysis, it detects certain RBP-relative accesses below RSP and marks the function for conservative tracking.

Suggested reviewers: nmzik, ew-sudo

Change: Bug fix

Merge Risk: Minimal · up to 52235

The change extends Windows red-zone protection to functions missing from .eh_frame and to RBP-relative accesses. No merge-blocking risk remains. A rare frame-pointer pattern with a later stack adjustment could stay unprotected, but this is a narrow edge case that can be handled as a follow-up.

Pre-merge checks: Passed 4 · Failed 1

Failed Failed checks: 1 warning

Check name Status Explanation Resolution
Docstring Coverage Warning Docstring coverage is 40.00% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff with cyclomatic complexity of at least 2. Documenta… Write docstrings for the functions missing them to satisfy the coverage threshold.
Passed Passed checks: 4 passed
Check name Status Explanation
Title check Passed The title names the main change: extending Windows red-zone protection to functions missing from .eh_frame, with the game (Saros) named. The changeset adds call-target function discovery, RBP-relative…
Description check Passed The description covers the game affected, the crash symptoms, the root causes (missing .eh_frame functions, RBP-relative red-zone operands, rejected out-of-segment jump tables), the specific changes i…
Linked Issues check Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check Passed Check skipped because no linked issues were found for this pull request.
Full details: Title check

Explanation

The title names the main change: extending Windows red-zone protection to functions missing from .eh_frame, with the game (Saros) named. The changeset adds call-target function discovery, RBP-relative red-zone detection, and jump-table bound changes in guestInstructionPatcher.cpp. The title covers the function-discovery part, which is the first of three changes, so it is a partial match. It is specific enough to identify the primary change. The title is 86 characters, slightly above the short-title guideline, but it is a single sentence.

Full details: Description check

Explanation

The description covers the game affected, the crash symptoms, the root causes (missing .eh_frame functions, RBP-relative red-zone operands, rejected out-of-segment jump tables), the specific changes in guestInstructionPatcher.cpp, testing done, and AI usage. It is directly related to the changeset.

Full details: Docstring Coverage

Explanation

Docstring coverage is 40.00% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff with cyclomatic complexity of at least 2. Documentation-only edits and deletions within surviving functions are included; unchanged diff context and fully deleted functions are excluded. Analyzed 5 functions across 1 files.

Resolution

Write docstrings for the functions missing them to satisfy the coverage threshold.

Tip

🎯 Customize your risk assessments

Guide how CodeRabbit assesses merge risk and security architecture risk. Highlight sensitive changes and explain your security boundaries using Merge Risk Instructions and Security Risk Instructions in your review settings. Security architecture risk assessments require the Advanced plan or above.

✅ Automatic approvals, with your rules

Let CodeRabbit approve pull requests once required threads are resolved, pre-merge checks pass, and your risk limits are met. Enable Automatic approvals in review settings, with overrides for teams or file paths.

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share
Recent review info
Run configuration
Commits

Reviewing files that changed from the base of the PR and between ea342ad7a1 and 52235b3884.

Files selected for processing: 1
  • src/loader/guestInstructionPatcher.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.

**@⁠coderabbitai[bot]** on GitHub · 2026-11-04 04:24 UTC · [original](https://github.com/KytyPS5/KytyPS5/pull/2099#issuecomment-6622510596) · edited 2026-11-04 04:46 UTC <!-- coderabbit-ai-generated:v1 --> <!-- This is an auto-generated comment: summarize by coderabbit.ai --> <!-- review_stack_entry_start --> <a href="https://app.coderabbit.ai/change-stack/KytyPS5/KytyPS5/pull/2099?scope=ghh_OusAJGA4HG8ljoOV0wJvVx8NmnZIjc2DdjmxOu0xZWA&amp;cs_source=review_comment"><picture><source media="(prefers-color-scheme: dark)" srcset="https://storage.googleapis.com/coderabbit_public_assets/review-stack-in-coderabbit-ui-dark.svg?v=3"><source media="(prefers-color-scheme: light)" srcset="https://storage.googleapis.com/coderabbit_public_assets/review-stack-in-coderabbit-ui.svg?v=3"><img src="https://storage.googleapis.com/coderabbit_public_assets/review-stack-in-coderabbit-ui.svg?v=3" alt="Review in Change Stack →" height="32"></picture></a> <!-- review_stack_entry_end --> <!-- recent_review_status_start --> No actionable comments were generated in the recent review. <!-- recent_review_status_end --> <!-- walkthrough_start --> <details> <summary><strong>Walkthrough</strong></summary> <dl> <dd> The patch records direct-call targets during function decoding and expands known function starts when memory protection is enabled. It accepts jump tables within a registered module image, while resolved targets must remain inside the function. During red-zone analysis, it detects certain RBP-relative accesses below RSP and marks the function for conservative tracking. **Suggested reviewers:** `nmzik`, `ew-sudo` </dd> </dl> </details> <!-- walkthrough_end --> <!-- change_assessment_start --> <!-- change_assessment_commit:"52235b388494324552472fd182ef23e7c603f753" --> <!-- change_assessment_priority:{"version":1,"urgency":"p2","manual":false} --> **Change:** Bug fix <!-- change_assessment_end --> <details> <summary><strong>Merge Risk:</strong> <strong><picture><img src="https://storage.googleapis.com/coderabbit_public_assets/review-comment-icons-v1/risk-unassessed-16.svg" width="16" height="16" align="absmiddle" alt=""></picture> Minimal</strong> · up to <code>52235</code></summary> <dl> <dd> <!-- final_review_risk_start --> <!-- final_review_risk_label **Merge Risk:** **⚪ Minimal** · up to `52235` --> <!-- final_review_risk_coverage:{"sourceCommitId":"52235b388494324552472fd182ef23e7c603f753","coveredCommitId":"52235b388494324552472fd182ef23e7c603f753","kind":"reviewed"} --> The change extends Windows red-zone protection to functions missing from .eh_frame and to RBP-relative accesses. No merge-blocking risk remains. A rare frame-pointer pattern with a later stack adjustment could stay unprotected, but this is a narrow edge case that can be handled as a follow-up. <!-- final_review_risk_end --> </dd> </dl> </details> <!-- pre_merge_checks_walkthrough_start --> <details> <summary><strong>Pre-merge checks: <sup><picture><img src="https://storage.googleapis.com/coderabbit_public_assets/pre-merge-checks-counter-passed-14-v2.svg" width="14" height="14" align="middle" alt="Passed" title="Passed"></picture></sup> 4 · <sup><picture><img src="https://storage.googleapis.com/coderabbit_public_assets/pre-merge-checks-counter-failed-14-v2.svg" width="14" height="14" align="middle" alt="Failed" title="Failed"></picture></sup> 1</strong></summary> <dl> <dd> <strong><sup><picture><img src="https://storage.googleapis.com/coderabbit_public_assets/pre-merge-checks-counter-failed-14-v2.svg" width="14" height="14" align="middle" alt="Failed" title="Failed"></picture></sup> Failed checks: 1 warning</strong> | Check name | Status | Explanation | Resolution | | :---------------- | :----------------------------------------------------------------------------------------------: | :-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | :--------------------------------------------------------------------------------- | | Docstring Coverage | ![Warning](https://storage.googleapis.com/coderabbit_public_assets/pre-merge-checks-warning.svg) | Docstring coverage is 40.00% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff with cyclomatic complexity of at least 2. Documenta… | Write docstrings for the functions missing them to satisfy the coverage threshold. | <details> <summary><strong><sup><picture><img src="https://storage.googleapis.com/coderabbit_public_assets/pre-merge-checks-counter-passed-14-v2.svg" width="14" height="14" align="middle" alt="Passed" title="Passed"></picture></sup> Passed checks: 4 passed</strong></summary> <dl> <dd> | Check name | Status | Explanation | | :------------------------ | :--------------------------------------------------------------------------------------------: | :-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | | Title check | ![Passed](https://storage.googleapis.com/coderabbit_public_assets/pre-merge-checks-passed.svg) | The title names the main change: extending Windows red-zone protection to functions missing from .eh_frame, with the game (Saros) named. The changeset adds call-target function discovery, RBP-relative… | | Description check | ![Passed](https://storage.googleapis.com/coderabbit_public_assets/pre-merge-checks-passed.svg) | The description covers the game affected, the crash symptoms, the root causes (missing .eh_frame functions, RBP-relative red-zone operands, rejected out-of-segment jump tables), the specific changes i… | | Linked Issues check | ![Passed](https://storage.googleapis.com/coderabbit_public_assets/pre-merge-checks-passed.svg) | Check skipped because no linked issues were found for this pull request. | | Out of Scope Changes check | ![Passed](https://storage.googleapis.com/coderabbit_public_assets/pre-merge-checks-passed.svg) | Check skipped because no linked issues were found for this pull request. | <details> <summary><strong>Full details: Title check</strong></summary> <dl> <dd> **Explanation** The title names the main change: extending Windows red-zone protection to functions missing from .eh_frame, with the game (Saros) named. The changeset adds call-target function discovery, RBP-relative red-zone detection, and jump-table bound changes in guestInstructionPatcher.cpp. The title covers the function-discovery part, which is the first of three changes, so it is a partial match. It is specific enough to identify the primary change. The title is 86 characters, slightly above the short-title guideline, but it is a single sentence. </dd> </dl> </details> <details> <summary><strong>Full details: Description check</strong></summary> <dl> <dd> **Explanation** The description covers the game affected, the crash symptoms, the root causes (missing .eh_frame functions, RBP-relative red-zone operands, rejected out-of-segment jump tables), the specific changes in guestInstructionPatcher.cpp, testing done, and AI usage. It is directly related to the changeset. </dd> </dl> </details> </dd> </dl> </details> <details> <summary><strong>Full details: Docstring Coverage</strong></summary> <dl> <dd> **Explanation** Docstring coverage is 40.00% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff with cyclomatic complexity of at least 2. Documentation-only edits and deletions within surviving functions are included; unchanged diff context and fully deleted functions are excluded. Analyzed 5 functions across 1 files. **Resolution** Write docstrings for the functions missing them to satisfy the coverage threshold. </dd> </dl> </details> </dd> </dl> </details> <!-- pre_merge_checks_walkthrough_end --> <!-- announcements_start --> > [!TIP] > <details> > <summary><strong>🎯 Customize your risk assessments</strong></summary> > <dl> > <dd> > > Guide how CodeRabbit assesses merge risk and security architecture risk. Highlight sensitive changes and explain your security boundaries using **Merge Risk Instructions** and **Security Risk Instructions** in your review settings. Security architecture risk assessments require the **Advanced plan or above**. > > </dd> > </dl> > </details> > > <details> > <summary><strong>✅ Automatic approvals, with your rules</strong></summary> > <dl> > <dd> > > Let CodeRabbit approve pull requests once required threads are resolved, pre-merge checks pass, and your risk limits are met. Enable **Automatic approvals** in review settings, with overrides for teams or file paths. > > </dd> > </dl> > </details> <!-- announcements_end --> <!-- tips_start --> Thanks for using [CodeRabbit](https://coderabbit.ai?utm_source=oss&utm_medium=github&utm_campaign=KytyPS5/KytyPS5&utm_content=2099)! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. <details> <summary><strong>Share</strong></summary> <dl> <dd> - [X](https://twitter.com/intent/tweet?text=I%20just%20used%20%40coderabbitai%20for%20my%20code%20review%2C%20and%20it%27s%20fantastic%21%20It%27s%20free%20for%20OSS%20and%20offers%20a%20free%20trial%20for%20the%20proprietary%20code.%20Check%20it%20out%3A&url=https%3A//coderabbit.ai) - [Mastodon](https://mastodon.social/share?text=I%20just%20used%20%40coderabbitai%20for%20my%20code%20review%2C%20and%20it%27s%20fantastic%21%20It%27s%20free%20for%20OSS%20and%20offers%20a%20free%20trial%20for%20the%20proprietary%20code.%20Check%20it%20out%3A%20https%3A%2F%2Fcoderabbit.ai) - [Reddit](https://www.reddit.com/submit?title=Great%20tool%20for%20code%20review%20-%20CodeRabbit&text=I%20just%20used%20CodeRabbit%20for%20my%20code%20review%2C%20and%20it%27s%20fantastic%21%20It%27s%20free%20for%20OSS%20and%20offers%20a%20free%20trial%20for%20proprietary%20code.%20Check%20it%20out%3A%20https%3A//coderabbit.ai) - [LinkedIn](https://www.linkedin.com/sharing/share-offsite/?url=https%3A%2F%2Fcoderabbit.ai&mini=true&title=Great%20tool%20for%20code%20review%20-%20CodeRabbit&summary=I%20just%20used%20CodeRabbit%20for%20my%20code%20review%2C%20and%20it%27s%20fantastic%21%20It%27s%20free%20for%20OSS%20and%20offers%20a%20free%20trial%20for%20proprietary%20code) </dd> </dl> </details> <!-- tips_end --> <!-- recent_review_start --> <h6> <details> <summary><strong><picture><img src="https://storage.googleapis.com/coderabbit_public_assets/review-comment-icons-v1/info.svg" width="12" height="12" align="absmiddle" alt=""></picture> Recent review info</strong></summary> <dl> <dd> <!-- review_info_content --> <details> <summary><strong><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</strong></summary> <dl> <dd> - **Source mode**: Auto - **Repository**: [KytyPS5/KytyPS5](<https://github.com/KytyPS5/KytyPS5>) - **Branch**: [main](<https://github.com/KytyPS5/KytyPS5/tree/main>) - **File**: [.coderabbit.yaml](<https://github.com/KytyPS5/KytyPS5/blob/eecd982193ba5f3df3ddec500847ea3d99b87a0c/.coderabbit.yaml>) - **Commit**: [eecd982](<https://github.com/KytyPS5/KytyPS5/commit/eecd982193ba5f3df3ddec500847ea3d99b87a0c>) - **Layers**: Repository YAML - **Review profile**: CHILL - **Plan**: Advanced - **Run ID**: `b0d3b03a-b23d-4014-b3de-0f381e359330` </dd> </dl> </details> <details> <summary><strong><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</strong></summary> <dl> <dd> Reviewing files that changed from the base of the PR and between ea342ad7a1673f1cf20523fcad78b086ddc65ffc and 52235b388494324552472fd182ef23e7c603f753. </dd> </dl> </details> <details> <summary><strong><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: 1</strong></summary> <dl> <dd> * `src/loader/guestInstructionPatcher.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> <!-- recent_review_end -->
Author
Owner

@⁠coderabbitai[bot] on GitHub · 2026-11-04 04:24 UTC · original · edited 2026-11-04 04:48 UTC
On src/loader/guestInstructionPatcher.cpp line 559 (outdated):

@@ -538,7 +548,15 @@ DecodedFunction DecodeFunction(uintptr_t function_start, uintptr_t function_end,
 
 				const uintptr_t next_address  = address + decoded.instruction.length;
 				const uintptr_t branch_target = GetRelativeTarget(decoded);
-				if (branch_target >= function_start && branch_target < function_end) {
+				if (decoded.instruction.meta.category == ZYDIS_CATEGORY_CALL) {
+					// A direct call always enters a function, even one that lies inside the
+					// caller's range because .eh_frame did not list it. Tail jumps are left
+					// out: they can target the middle of a function, and splitting it there
+					// would truncate the red-zone liveness of its first half.
+					if (branch_target >= segment_start && branch_target < segment_end) {
+						function.callees.insert(branch_target);
+					}
+				} else if (branch_target >= function_start && branch_target < function_end) {

Minor · Functional Correctness · Quick win

Keep in-range direct-call targets in branch_targets.

Before this change, a direct call whose target was inside [function_start, function_end) went into branch_targets. The new if/else if sends every call to callees only. This is safe when AddDecodedCallees runs, because it splits the function at that target. It does not run when protect_memory is false, which covers --amd-cpu without --redzone and the TSC-only path.

In those paths, an unlisted function that lies inside its predecessor's range is no longer marked as a branch target. collect_forward_span, collect_backward_span, and find_host check branch_targets before they relocate instructions into a trampoline. A relocated span can now cover the entry of that function. A later direct call into the middle of the patched span then lands on nop padding or a partial jump.

Record the in-range target as a branch target in addition to recording the callee:

Proposed fix
--- "a/src/loader/guestInstructionPatcher.cpp"
+++ "b/src/loader/guestInstructionPatcher.cpp"
@@ -548,17 +548,18 @@
 
 				const uintptr_t next_address  = address + decoded.instruction.length;
 				const uintptr_t branch_target = GetRelativeTarget(decoded);
 				if (decoded.instruction.meta.category == ZYDIS_CATEGORY_CALL) {
 					// A direct call always enters a function, even one that lies inside the
 					// caller's range because .eh_frame did not list it. Tail jumps are left
 					// out: they can target the middle of a function, and splitting it there
 					// would truncate the red-zone liveness of its first half.
 					if (branch_target >= segment_start && branch_target < segment_end) {
 						function.callees.insert(branch_target);
 					}
-				} else if (branch_target >= function_start && branch_target < function_end) {
+				}
+				if (branch_target >= function_start && branch_target < function_end) {
 					function.branch_targets.insert(branch_target);
 				}
 				function.instructions.emplace(address, decoded);
 
 				if (decoded.instruction.meta.category == ZYDIS_CATEGORY_COND_BR) {

After AddDecodedCallees splits the function, the callee lies outside the range. The extra entry then has no effect on the --redzone path.

Committable suggestion

IMPORTANT
Review the replacement and indentation, then test before committing.

				if (decoded.instruction.meta.category == ZYDIS_CATEGORY_CALL) {
					// A direct call always enters a function, even one that lies inside the
					// caller's range because .eh_frame did not list it. Tail jumps are left
					// out: they can target the middle of a function, and splitting it there
					// would truncate the red-zone liveness of its first half.
					if (branch_target >= segment_start && branch_target < segment_end) {
						function.callees.insert(branch_target);
					}
				}
				if (branch_target >= function_start && branch_target < function_end) {

✅ Addressed in commit 52235b3

**@⁠coderabbitai[bot]** on GitHub · 2026-11-04 04:24 UTC · [original](https://github.com/KytyPS5/KytyPS5/pull/2099#discussion_r4463557984) · edited 2026-11-04 04:48 UTC On `src/loader/guestInstructionPatcher.cpp` line 559 (outdated): ```diff @@ -538,7 +548,15 @@ DecodedFunction DecodeFunction(uintptr_t function_start, uintptr_t function_end, const uintptr_t next_address = address + decoded.instruction.length; const uintptr_t branch_target = GetRelativeTarget(decoded); - if (branch_target >= function_start && branch_target < function_end) { + if (decoded.instruction.meta.category == ZYDIS_CATEGORY_CALL) { + // A direct call always enters a function, even one that lies inside the + // caller's range because .eh_frame did not list it. Tail jumps are left + // out: they can target the middle of a function, and splitting it there + // would truncate the red-zone liveness of its first half. + if (branch_target >= segment_start && branch_target < segment_end) { + function.callees.insert(branch_target); + } + } else if (branch_target >= function_start && branch_target < function_end) { ``` <!-- coderabbit-ai-generated:v1 --> **<picture><img src="https://storage.googleapis.com/coderabbit_public_assets/review-comment-icons-v1/amber-16.svg" width="16" height="16" align="absmiddle" alt=""></picture> Minor** · **Functional Correctness** · **Quick win** **Keep in-range direct-call targets in `branch_targets`.** Before this change, a direct call whose target was inside `[function_start, function_end)` went into `branch_targets`. The new `if/else if` sends every call to `callees` only. This is safe when `AddDecodedCallees` runs, because it splits the function at that target. It does not run when `protect_memory` is false, which covers `--amd-cpu` without `--redzone` and the TSC-only path. In those paths, an unlisted function that lies inside its predecessor's range is no longer marked as a branch target. `collect_forward_span`, `collect_backward_span`, and `find_host` check `branch_targets` before they relocate instructions into a trampoline. A relocated span can now cover the entry of that function. A later direct call into the middle of the patched span then lands on `nop` padding or a partial jump. Record the in-range target as a branch target in addition to recording the callee: <details> <summary>Proposed fix</summary> <dl> <dd> ```diff --- "a/src/loader/guestInstructionPatcher.cpp" +++ "b/src/loader/guestInstructionPatcher.cpp" @@ -548,17 +548,18 @@ const uintptr_t next_address = address + decoded.instruction.length; const uintptr_t branch_target = GetRelativeTarget(decoded); if (decoded.instruction.meta.category == ZYDIS_CATEGORY_CALL) { // A direct call always enters a function, even one that lies inside the // caller's range because .eh_frame did not list it. Tail jumps are left // out: they can target the middle of a function, and splitting it there // would truncate the red-zone liveness of its first half. if (branch_target >= segment_start && branch_target < segment_end) { function.callees.insert(branch_target); } - } else if (branch_target >= function_start && branch_target < function_end) { + } + if (branch_target >= function_start && branch_target < function_end) { function.branch_targets.insert(branch_target); } function.instructions.emplace(address, decoded); if (decoded.instruction.meta.category == ZYDIS_CATEGORY_COND_BR) { ``` </dd> </dl> </details> After `AddDecodedCallees` splits the function, the callee lies outside the range. The extra entry then has no effect on the `--redzone` path. <!-- suggestion_start --> <details> <summary>Committable suggestion</summary> <dl> <dd> > <picture><img src="https://storage.googleapis.com/coderabbit_public_assets/pre-merge-checks-warning.svg" width="20" height="20" align="absmiddle" alt=""></picture> **IMPORTANT** > Review the replacement and indentation, then test before committing. ```suggestion if (decoded.instruction.meta.category == ZYDIS_CATEGORY_CALL) { // A direct call always enters a function, even one that lies inside the // caller's range because .eh_frame did not list it. Tail jumps are left // out: they can target the middle of a function, and splitting it there // would truncate the red-zone liveness of its first half. if (branch_target >= segment_start && branch_target < segment_end) { function.callees.insert(branch_target); } } if (branch_target >= function_start && branch_target < function_end) { ``` </dd> </dl> </details> <!-- suggestion_end --> <!-- fingerprinting:phantom:medusa:beignet55 --> <!-- cr-indicator-types:potential_issue --> <!-- cr-comment:v1:061ce9b8dc66ee4a20797bf4 --> <!-- This is an auto-generated reply by CodeRabbit --> ✅ Addressed in commit 52235b3
Author
Owner

@⁠coderabbitai[bot] on GitHub · 2026-11-04 04:24 UTC · original · reviewed

Actionable comments posted: 1

Review info
Run configuration
Commits

Reviewing files that changed from the base of the PR and between eecd982193 and ea342ad7a1.

Files selected for processing: 1
  • src/loader/guestInstructionPatcher.cpp

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

**@⁠coderabbitai[bot]** on GitHub · 2026-11-04 04:24 UTC · [original](https://github.com/KytyPS5/KytyPS5/pull/2099#pullrequestreview-5763198658) · **reviewed** <!-- coderabbit-ai-generated:v1 --> **Actionable comments posted: 1** <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> <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> - **Source mode**: Auto - **Repository**: [KytyPS5/KytyPS5](<https://github.com/KytyPS5/KytyPS5>) - **Branch**: [main](<https://github.com/KytyPS5/KytyPS5/tree/main>) - **File**: [.coderabbit.yaml](<https://github.com/KytyPS5/KytyPS5/blob/eecd982193ba5f3df3ddec500847ea3d99b87a0c/.coderabbit.yaml>) - **Commit**: [eecd982](<https://github.com/KytyPS5/KytyPS5/commit/eecd982193ba5f3df3ddec500847ea3d99b87a0c>) - **Layers**: Repository YAML - **Review profile**: CHILL - **Plan**: Advanced - **Run ID**: `ef1e8452-40a9-46ab-960e-7bd0b9cb3947` </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> Reviewing files that changed from the base of the PR and between eecd982193ba5f3df3ddec500847ea3d99b87a0c and ea342ad7a1673f1cf20523fcad78b086ddc65ffc. </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: 1</summary> <dl> <dd> * `src/loader/guestInstructionPatcher.cpp` </dd> </dl> </details> **Included review availability:** This review used your included allowance. Your plan provides up to 10 included reviews per hour; 9 remain after this review. </dd> </dl> </details> </h6> <!-- This is an auto-generated comment by CodeRabbit for review status --> <!-- coderabbit-review-publication v1 publication=7149bb8c-32a5-4fab-9b88-8f97490880a6 attempt=5bd15a1f-620c-485f-8e0b-8c399210bd45 batch=1/1 -->
Author
Owner

@⁠joaocaparroz on GitHub · 2026-11-04 04:41 UTC · original
On src/loader/guestInstructionPatcher.cpp line 559 (outdated):

@@ -538,7 +548,15 @@ DecodedFunction DecodeFunction(uintptr_t function_start, uintptr_t function_end,
 
 				const uintptr_t next_address  = address + decoded.instruction.length;
 				const uintptr_t branch_target = GetRelativeTarget(decoded);
-				if (branch_target >= function_start && branch_target < function_end) {
+				if (decoded.instruction.meta.category == ZYDIS_CATEGORY_CALL) {
+					// A direct call always enters a function, even one that lies inside the
+					// caller's range because .eh_frame did not list it. Tail jumps are left
+					// out: they can target the middle of a function, and splitting it there
+					// would truncate the red-zone liveness of its first half.
+					if (branch_target >= segment_start && branch_target < segment_end) {
+						function.callees.insert(branch_target);
+					}
+				} else if (branch_target >= function_start && branch_target < function_end) {

Good catch, applied in 52235b38. In-range call targets are now kept as branch targets too, so the non---redzone paths behave as before. Retested Saros with --redzone and the CI test set.

**@⁠joaocaparroz** on GitHub · 2026-11-04 04:41 UTC · [original](https://github.com/KytyPS5/KytyPS5/pull/2099#discussion_r4463662732) On `src/loader/guestInstructionPatcher.cpp` line 559 (outdated): ```diff @@ -538,7 +548,15 @@ DecodedFunction DecodeFunction(uintptr_t function_start, uintptr_t function_end, const uintptr_t next_address = address + decoded.instruction.length; const uintptr_t branch_target = GetRelativeTarget(decoded); - if (branch_target >= function_start && branch_target < function_end) { + if (decoded.instruction.meta.category == ZYDIS_CATEGORY_CALL) { + // A direct call always enters a function, even one that lies inside the + // caller's range because .eh_frame did not list it. Tail jumps are left + // out: they can target the middle of a function, and splitting it there + // would truncate the red-zone liveness of its first half. + if (branch_target >= segment_start && branch_target < segment_end) { + function.callees.insert(branch_target); + } + } else if (branch_target >= function_start && branch_target < function_end) { ``` Good catch, applied in 52235b38. In-range call targets are now kept as branch targets too, so the non-`--redzone` paths behave as before. Retested Saros with `--redzone` and the CI test set.
Author
Owner

@⁠coderabbitai[bot] on GitHub · 2026-11-04 04:48 UTC · original
On src/loader/guestInstructionPatcher.cpp line 559 (outdated):

@@ -538,7 +548,15 @@ DecodedFunction DecodeFunction(uintptr_t function_start, uintptr_t function_end,
 
 				const uintptr_t next_address  = address + decoded.instruction.length;
 				const uintptr_t branch_target = GetRelativeTarget(decoded);
-				if (branch_target >= function_start && branch_target < function_end) {
+				if (decoded.instruction.meta.category == ZYDIS_CATEGORY_CALL) {
+					// A direct call always enters a function, even one that lies inside the
+					// caller's range because .eh_frame did not list it. Tail jumps are left
+					// out: they can target the middle of a function, and splitting it there
+					// would truncate the red-zone liveness of its first half.
+					if (branch_target >= segment_start && branch_target < segment_end) {
+						function.callees.insert(branch_target);
+					}
+				} else if (branch_target >= function_start && branch_target < function_end) {

@⁠joaocaparroz Thanks for the fix and retesting. The supplied code now records in-range direct-call targets in branch_targets independently of callees. This preserves relocation boundaries when AddDecodedCallees does not run.


📝 Learnings were not changed: only organization members or users with write access to this repository can add or remove learnings.


If you found this review helpful, would you consider giving us a shout-out on X?

Thank you for using CodeRabbit!

Review thread resolved.

You are interacting with an AI system.

**@⁠coderabbitai[bot]** on GitHub · 2026-11-04 04:48 UTC · [original](https://github.com/KytyPS5/KytyPS5/pull/2099#discussion_r4463705582) On `src/loader/guestInstructionPatcher.cpp` line 559 (outdated): ```diff @@ -538,7 +548,15 @@ DecodedFunction DecodeFunction(uintptr_t function_start, uintptr_t function_end, const uintptr_t next_address = address + decoded.instruction.length; const uintptr_t branch_target = GetRelativeTarget(decoded); - if (branch_target >= function_start && branch_target < function_end) { + if (decoded.instruction.meta.category == ZYDIS_CATEGORY_CALL) { + // A direct call always enters a function, even one that lies inside the + // caller's range because .eh_frame did not list it. Tail jumps are left + // out: they can target the middle of a function, and splitting it there + // would truncate the red-zone liveness of its first half. + if (branch_target >= segment_start && branch_target < segment_end) { + function.callees.insert(branch_target); + } + } else if (branch_target >= function_start && branch_target < function_end) { ``` <!-- coderabbit-ai-generated:v1 --> @⁠joaocaparroz Thanks for the fix and retesting. The supplied code now records in-range direct-call targets in `branch_targets` independently of `callees`. This preserves relocation boundaries when `AddDecodedCallees` does not run. --- > 📝 Learnings were not changed: only organization members or users with write access to this repository can add or remove learnings. --- <!-- This is an auto-generated comment: tweet message by coderabbit.ai --> If you found this review helpful, would you consider giving us a shout-out on [X](https://twitter.com/intent/tweet?related=CodeRabbitAI&text=In-range%20calls%20must%20stay%20in%20%60branch_targets%60%20too%3A%20otherwise%20trampoline%20relocation%20can%20overwrite%20function%20entries%20in%20non-%60--redzone%60%20paths.%20%40coderabbitai%20caught%20it%E2%80%94try%20it%20on%20your%20next%20review!%20%F0%9F%90%87&url=https%3A%2F%2Fgithub.com%2FKytyPS5%2FKytyPS5%2Fpull%2F2099%23discussion_r4463557984)? Thank you for using CodeRabbit! <!-- end of auto-generated comment: tweet message by coderabbit.ai --> Review thread resolved. <sub><i>You are interacting with an AI system.</i></sub> <!-- This is an auto-generated reply by CodeRabbit -->
This pull request can be merged automatically.
This branch is out-of-date with the base branch
You are not authorized to merge this pull request.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin '+refs/pull/2099/head:github-bot/gh-2099'
git switch 'github-bot/gh-2099'

Merge

Merge the changes and update on Forgejo.
git switch 'main'
git merge --no-ff 'github-bot/gh-2099'
git switch 'github-bot/gh-2099'
git rebase 'main'
git switch 'main'
git merge --ff-only 'github-bot/gh-2099'
git switch 'github-bot/gh-2099'
git rebase 'main'
git switch 'main'
git merge --no-ff 'github-bot/gh-2099'
git switch 'main'
git merge --squash 'github-bot/gh-2099'
git switch 'main'
git merge --ff-only 'github-bot/gh-2099'
git switch 'main'
git merge 'github-bot/gh-2099'
git push origin 'main'
This discussion has been locked. Commenting is limited to contributors.
No description provided.