feat(relinker): frame red-zone leaves without a frame pointer by moving their rsp operands #5640
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/5640"
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?
Imported from boykopovar/AnyPS5. Originally posted by EagleBearerYS on 2026-10-14T02:36:50Z.
What
Why this is back. This closed on 2026-10-14, after #5596 hid Resident Evil Village's (PPSA01556) crash. Grand Theft Auto V (PPSA04264) now hits the same red-zone clobber in zlib's
inflate_fast(#9136, the #361 mechanism).MEM_MAPPEDview faults and is resumed. Windows writes the exception frame below RSP, over the leaf's red zone.inflate_fast, on the same thread.inflate_fast: 540 were skipped for "rsp operand that cannot move with rsp".inflate_faston the crashing thread. Framed with this PR and their backward extension (now commit 8 here), 0 of 4 crashed, with resumed writes in two of them. That points to framing as the fix for this crash. It's a small sample, and the framed runs were their build, not this head.What it does. With
--frame-red-zone, on Windows targets, each red-zone leaf the relinker can frame gets a prologue that lowers RSP past its red zone, and exit stubs that raise it back. Its RSP operands move with RSP, so the leaf's own slots stay where it addresses them, and an exception frame Windows writes below RSP lands under them.fab7d203fb), which frames leaves that set up a frame pointer. #5052 is closed. Its commit is carried here unchanged under his authorship.743ce530c1,a8ed810d07): leaves without a frame pointer, whose prologue is a chain of pushes, with their RSP operands moved.New since the close:
1a262894ad). RSP is lowered only as far as the leaf reaches below it, not a fixed 128 bytes. So more 8-bit displacements keep their encoding.subafter their pushes (132597c6e1), the shape ofinflate_fast.e0ba27fc60),red_zone_framer_execution, on Linux x86-64.[rsp-0x80, rsp)filled with0xa5before every instruction inside it.259bb69d65).0x7f,(%rsp), or(%rsp,%idx,s).retor a RIP-relative instruction falls inside it, or for EVEX forms (scaled disp8). The leaf is then skipped, as before.10fedd32e3). A game patch over a framed site or a moved displacement is refused with its reason, throughGamePatch::CheckOverlaps. Patches elsewhere in a framed leaf are applied before framing, as before..eh_frameFDEs. This is @parl0v's commit,606fa2ef91: his parl0v/AnyPS5@3ff74b9, a follow-up to #5052, carried under his authorship with his agreement (#5052 comment)..eh_frameand framing is asked for.a376e37b88).a8ed810d07) is dropped. With FDE bounds it isn't needed, and it left a cold block after aretinside the same FDE undecoded: its operands weren't moved and its exit wasn't stubbed.jmp, is now framed from its own FDE.e410c38b42, tested incec1f82d76). This is @tibzejoker's idea and diff (comment), and the commit is co-authored by them.addthat frees the frame, the stub could stay shorter than a jump and the leaf was skipped.inflate_fastframes this way: its one operand left, a 4-bytecmp %rdi,(%rsp), sits between anaddand ajne.red_zone_framer's leaf with its RSP operand before a RIP-relative load now frames. The one with its operand right before an exit stays refused, because its forward extension reaches the exit's pops.The debt entries and
USAGE.mddescribe the new shapes (a665ef2dfb,542cb30f70). Inside a moved frame, the CFA rules of the guest's unwind tables are off by the shift; the debt entry records it.Still skipped: exits shorter than a jump, indirect jumps, other uses of RSP (copying it into another register), branches out of the function, an operand with no room for a stub, and EVEX operands.
Tested
GTA V (PPSA04264) on this head, by @tibzejoker (comment): RTX 4090, Windows 11,
main3bbd2321dwith #9730, #9181, #9159 and #9445, and this PR's commits up tocec1f82d76cherry-picked; relinked with--frame-red-zone, the per-ripfault log andAPS5_MIMALLOC=1, four open-world runs of up to 420 s.inflate_fastamong them, and 1 "rsp operand that cannot move and has no room for a stub" is left. In the relinked imageinflate_fast's spills sit above RSP, and itscmp %rdi,(%rsp)at 0x28aac88 jumps to its stub.inflate_fastacross the four runs (at least 44 in run 1 alone, at six differentrips), and no HDD Streamer crash. Their log keeps only the 1st, 2nd, 3rd, 10th and 100th hit of eachrip, so the counts are lower bounds.State.cpp(dithered alpha-to-coverage requires eight matched raster/DB_EQAA samples and one pixel iteration), whichmainhits too; it isn't a red-zone symptom. Runs 1, 3 and 4 ran the full 420 s.GTA V (PPSA04264), by @tibzejoker (comment), Windows 11, i9-13900K, RTX 4090, with a376e37b88's commits cherry-picked onto their tree:
inflate_fastis one of the 4 left with "rsp operand that cannot move and has no room for a stub".inflate_fastamong them. In 4 open-world runs of 7 minutes withAPS5_MIMALLOC=1there were 0 crashes, and two runs still took a resumed write insideinflate_fast, which the game carried on through.The backward extension, on GitHub's runners (run, whose head has the same trees as
cec1f82d76; red with e410c38b42's change reverted):cmp %rdi,(%rsp)sits before ajne, after anadd(inflate_fast's shape) and after a load below RSP that heads the loop and moves inside the stub. Acmpwhose loop branches back to it stays refused, since the widened stub would cover the branch target. The execution test runs both framed leaves under the writes below RSP.red_zone_framerandred_zone_framer_executionfail on Linux, andred_zone_frameron Windows.relinker_conversion,relinker_executablesandrelinker_game_patch) pass 10 runs in a row; on Windows, 4 of 4 (the execution test is registered on Linux x86-64 only); none skipped or failed. The libc and libkernel suites pass on both, run withAPS5_SKIP_TIMING_TESTS=1, which relaxesprecise_wait's median bounds on GitHub's hosted runners.Linux x86-64, CI's configure flags (on a cloud worker):
check_test_registration.pyreports nothing new.red_zone_framer: 23 cases, parl0v's two included.red_zone_framer_execution: 20 of 20 runs, together withred_zone_framerandrelinker_game_patch.The execution test's leaves (each run unframed and framed, with the writes below RSP):
leaof a slot 0x20 below RSPinflate_fast's shape: six pushes, thensub $0x40sub $0x70with(%rsp)and0x60(%rsp)in stubs0x8(%rsp)Red/green per commit:
0x1049accepted.jmp. All three are red on the framer before parl0v's commit.ret: red on parl0v's commit alone, "the exit of a cold block after the ret inside the FDE was not framed".Real code under the same writes, as a scratch check on our own clang 18
-O2builds of open-source libraries, framed in memory the way the Windows trampolines are:compress2level 9, and the inflate of its stream (inflate_fast,inflate_table). Framed, the output is unchanged. Unframed, compress ends in a SIGSEGV, and inflate returnsZ_DATA_ERRORafter 111 steps with the writes.inflate_fastandinflate_tableamong them;Windows:
-fsyntax-only -Wall -Wextraof the touched relinker files: 0 errors and no new warnings.a376e37b88):red_zone_framerandrelinker_game_patchpass 3 of 3 runs, along with the libc and libkernel suites.red_zone_framer_executionis registered on Linux x86-64 only.Not tested again: Resident Evil Village. Its crash didn't reproduce on
mainwith #5596: 0 of 17 boots, measured on 2026-10-14 (comment). It hasn't been checked again since.Checklist
main3875f8c8ff(merged in), and merges cleanly into currentmain; no other open PR implements the same functionsNotImplemented_nid_no_patch); silent stubs are listed under Silent stubs in their library's technical debt file.mdfiles or images added to the repository (attach them to this PR)main, in the same files or more)inflate_fast(tibzejoker's runs above)Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-14T12:39:42Z.
Review: CHANGES_REQUESTED
Thanks for the detailed write-up. Two blockers before this can be reviewed:
Depends on: #5052was closed unmerged on 2026-10-14, so this branch still carries its wholeRedZoneFramer(the diff againstmainis +659) plus Josip Parlov's commit. Either coordinate with them to reopen/land it, or resubmit as a self-contained PR with the authorship preserved.mainwith #5596. Please re-measure the Village boot crash (2 of 5 without framing) on currentmainwith #5596 included, with and without--frame-red-zone. If it no longer reproduces, there is nothing for this opt-in pass to fix.If it still reproduces, also note that #5052's review asked for function bounds from the
.eh_frameFDEs (EhFrameReader) rather than heuristics; the "end of body" rule added ina8ed810d0(aligned code afterret/ud2,jmpto padding) is a heuristic that leaves a documented gap, so it should use the FDEs here too.Imported from boykopovar/AnyPS5. Originally posted by EagleBearerYS on 2026-10-14T13:46:36Z.
Re-measured on current
mainwith #5596: Resident Evil Village (PPSA01556, 01.210.000), Windows 11 Home 10.0.26300, RTX 3070 Laptop (driver 617.42), unframed (relinked withmain's relinker).app.exe+0x69cb959, in 2 of 5 boots.mainwith #5596 it doesn't reproduce, 0 of 17 boots:main23be5a02b+ #5688 + #5703 + #5815. None faulted, and several had reached the start-up autosave notice by then.mainat0b6d033b8,eac1e26e2and23be5a02b, six of them with #5688 and #5703.APS5_RE8_LAZY_PREPARE=1, withAPS5_NO_GPL=1). Most also have local fixes for later stops.So there's nothing left for this pass to fix in Village, and I'm closing it. Thanks for the review. parl0v's
.eh_frameversion (parl0v/AnyPS5@3ff74b9) is the starting point if a title needs framing again.