loader: protect frame pointer red-zone accesses in the guest patcher #1510
No reviewers
Labels
No labels
bug
documentation
duplicate
enhancement
GPU
GUI
help wanted
question
Shaders
Video
wontfix
No milestone
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set
Reference
github/KytyPS5--KytyPS5!1510
Loading…
Reference in a new issue
No description provided.
Delete branch "github-bot/gh-1510"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
@EmK530 on GitHub · 2026-10-14 15:22 UTC · original
EmK530:fix/sysv-patcher→main· +370 −49 · 2 files · 2 commits · openSummary
On Windows, the guest instruction patcher only recognized red-zone accesses addressed relative to the stack pointer (
[rsp - x]). Red-zone accesses relative to the frame pointer ([rbp - x]) were missed, so functions using them were left unprotected.Tetris Effect: Connected (PPSA07923) has such a function in
RenderThread 1that crashes right after the logos:Unhandled host exception: type=1 code=3221225477 pc=0x00000009041b57ee access=1 address=0x0000000000000002This PR makes the patcher also recognize red-zone accesses made relative to the frame pointer, so these functions get the same protection.
Changes
When a function sets the frame pointer from the stack pointer (
mov rbp, rsp), the patcher now tracks how farrbpis aboverspfollowing pushes, pops and stack adjustments to detect when a[rbp - x]access falls belowrsp, treating it as a red-zone access.Functions that restore
rspfromrbp(mov rsp, rbp,leave) are now tracked too. Previously the patcher would give up on them and patch every memory instruction in the function, not just the ones that need it.Testing
virtual_memory_allocationtests pass.WindowsGuestFramePointerRedZoneStaticPatcherAI usage
AI was involved in the process of debugging, identifying and writing a fix for the issue including the tests. I have reviewed the changes in post and wrote this PR myself.
@coderabbitai[bot] on GitHub · 2026-10-14 15:27 UTC · original
📝 Walkthrough
The patcher now tracks frame-pointer offsets through instruction successors and models supported RBP-relative red-zone accesses. It runs this analysis before red-zone liveness analysis. A Windows test checks patching for four stack-restoring epilogues and verifies that the value remains intact.
Suggested reviewers:
nmzikPriority: ➖ Normal
Change: Bug fix · Severity of issue fixed: Low
Merge Risk: 🔵 Low · up to
03475An uncommon frame-pointer sequence can leave a red-zone access unprotected and corrupt its value. Address that gap before relying on protection for those functions.
Pre-merge checks:
4 ·
1
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
Comment
@coderabbitai helpto get the list of available commands.@ -587,3 +813,4 @@std::optional<RedZoneMask> TranslateRedZoneMask(const RedZoneMask& mask, s64 stack_pointer_delta) {if (stack_pointer_delta == 0) {return mask;@coderabbitai[bot] on GitHub · 2026-10-14 15:27 UTC · original
🎯 Functional Correctness | 🟡 Minor | ⚡ Quick win
🔎 Supported by static analysis
🏁 Script executed:
Repository: KytyPS5/KytyPS5
Length of output: 15370
🏁 Script executed:
Repository: KytyPS5/KytyPS5
Length of output: 6841
🏁 Script executed:
Repository: KytyPS5/KytyPS5
Length of output: 8236
Treat unknown RBP-based operands as unmodeled red-zone accesses.
xchg rbp, regis detected as an RBP write, so the analysis changes the successor state toUnknown. The later[rbp - displacement]operand then bypasses theFrameState::Knownblock and is not recorded. If it is the function's only red-zone access,function.uses_red_zoneremains false, soAnalyzeRedZoneLivenessreturns without protecting the function. A reachable function with this sequence can therefore miss red-zone protection.Suggested fix
@coderabbitai[bot] on GitHub · 2026-10-14 15:27 UTC · original · reviewed
Actionable comments posted: 1
ℹ️ Review info
⚙️ Run configuration
b945798f-092b-4730-8817-0b9ce4001f6d📥 Commits
Reviewing files that changed from the base of the PR and between
9e41d8320aand034756baed.📒 Files selected for processing: 2
src/loader/guestInstructionPatcher.cpptests/VirtualMemoryAllocationTests.cppIncluded review availability: This review used your included allowance. Your plan provides up to 8 included reviews per hour; 7 remain after this review.
@Nawid3333 on GitHub · 2026-10-19 15:43 UTC · original · edited 2026-10-19 16:13 UTC
@EmK530 I measured coverage and cost with and without this PR (method in my #1421 comment):
libLLVM's disassembly has 126,181
mov %rsp,%rbp, 2,385,622 negative rbp displacements and 1 negative rsp displacement, so all its red-zone slots are rbp-relative and invisible to main's patcher.Run time (zstd level 3): the frame-pointer build with #1510 is 1.54x (19.33 -> 29.80 ms), the same as the build without frame pointers on main (1.54x), so the cost per relocated instruction is unchanged; #1510 just finds more of them. With #1510, about 4-5% of the red-zone memory instructions stay unrelocatable (128 of 3,088 and 524 of 10,499 above) and therefore unprotected.
Without #1510, code built with frame pointers got no protection in both binaries (and no run-time cost: 1.02x); the load-time decode is paid either way.
Written with Claude Code (AI): it wrote the probe and the benchmark harness and ran them; I checked the numbers before posting.
View command line instructions
Checkout
From your project repository, check out a new branch and test the changes.Merge
Merge the changes and update on Forgejo.