feat(relinker): --frame-red-zone gives red-zone leaf functions a frame on Windows #5052
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/5052"
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 parl0v on 2026-10-13T09:42:57Z.
What
Windows has no red zone: when a guest thread takes a hardware exception that AnyPS5 resumes (write tracking of shared views, retargets, SSE4a), the kernel writes the exception frame directly below RSP before any handler runs. System V leaf functions keep live values there (#361), so a resumed fault can zero a pointer a leaf is about to use.
--frame-red-zone(Windows only, opt-in) gives those leaves a frame at relink time, so the values sit above RSP and a fault's frame lands below them:push rbp; mov rbp, rsp; push ..., it reads or writes below its pushes through RBP, and it has no other use of RSP and no indirect jump.lea -0x80(%rsp), %rsp.pop ...; pop rbp; retexit runslea 0x80(%rsp), %rsp, then the pops and theret.--to-intelstub are skipped. The relinker prints what it framed and why it skipped the rest.DecodedInstructionInfogainsModRmOffsetso the framer can read memory operands.What it doesn't cover (frameless leaves addressing below RSP directly, short exits) is recorded in the relinker's technical debt.
Tested
red_zone_framertest (a framed leaf's exact stub bytes; leaves without red-zone use and functions that call are left alone; a site over another stub is skipped). Rebased on main after #4140; the usage line lists both flags. 119 of 120 relinker tests pass:windows_import_modulesfails the same way on plainmainon this machine (its Windows DLL stdout probe prints nothing).--to-intel --frame-red-zone: 604 leaves keep values below RSP, 137 are framed (139 exits), including0x1437f74c0, the function of its boot crash. The same counts come out of this branch and of the SotS working-group base.mainto reach its menu), the boot crash atapp.exe+0x37f77b3happened in 6 of 23 boots without the flag and 0 of 42 with it, at the same 40,960 resumed write faults per boot. Main menu speed is unchanged: 8.09 FPS framed vs 7.91 / 7.97. The menu renders correctly.Checklist
main; 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)Imported from boykopovar/AnyPS5. Originally posted by fdfawef324 on 2026-10-13T19:04:55Z.
Tested PR #5052 on ASTRO BOT (PPSA21567), the title from my earlier data point on #361. The
--frame-red-zoneflag changes no output bytes for this title, so its audio-thread crash remains.The test used Windows Server 2025 build 26100, Ryzen 9 5900X, and NVIDIA GeForce RTX 3060. I applied PR commit 9b8fe8444 to a local integration branch based on main
fc1f063f5. The older relinker layout needed the framing call moved into its existing conversion entry point and the build sources adapted. The framing implementation itself was unchanged. All 86 relinker tests passed, includingred_zone_framer.The relinker examined 311,729 functions and found 455 red-zone leaf functions. It framed zero: 454 were outside the supported frame-pointer prologue shape, and one had another unsupported stack-pointer use. The executable and both guest modules were byte-identical with and without the flag. The crashing limiter falls within the PR's documented limitation for frameless leaves whose red-zone slots use the stack pointer directly.
Three runs used the same libraries and scripted inputs:
--frame-red-zone--frame-red-zoneAll three failed in the same limiter and at the same two crash sites previously reported in #361. The flag neither fixes nor regresses this title, because it frames no function and produces identical binaries. ASTRO BOT needs support for the documented frameless shape before this approach can address its crash.
This evidence is for the tested implementation at 9b8fe8444, with the integration adaptations described above; it does not establish a runtime result for the newer PR head.
GPUs tested: NVIDIA GeForce RTX 3060 12 GB, driver 591.86 (32.0.15.9186), Vulkan 1.4.325, Windows Server 2025 10.0.26100.
AI-assisted: yes.
Imported from boykopovar/AnyPS5. Originally posted by parl0v on 2026-10-13T19:05:53Z.
Thanks @fdfawef324, that's a useful data point. It matches the limitation recorded in
docs/dev/debt/relinker.md: this PR only frames leaves with thepush rbp; mov rbp, rspprologue shape. A title whose red-zone leaves are frameless gets byte-identical output, so nothing changes, as you saw on ASTRO BOT (0 of 455 framed, same crash sites with and without the flag). For PPSA25344 the boot race is in framed-shape leaves (137 framed; 0 crashes in 27 boots against 6 in 23 without the flag). Frameless leaves would need a different approach and belong in a separate PR, not this one.Imported from boykopovar/AnyPS5. Originally posted by tibzejoker on 2026-10-13T21:47:45Z.
@parl0v a data point from GTA V (PPSA04264), the title behind #361's inflate crash. I relinked its eboot with this PR on
main665ad7da7(Windows 11, i9-13900K,--windows --to-intel --frame-red-zone):So none of GTA's red-zone leaves has the RBP prologue this PR frames. The leaf we traced in #361 is zlib's
inflate_fast, which spillsstrm/statestraight to[rsp-0x20],[rsp-0x18]and[rsp-0x78]. On a current-mainbuild without #637, GTA still crashes with write watch on: an access violation in the RAGE main thread right after the save loads (eboot 0x28aacee). That fits the same red-zone damage, though I have not traced this one. This is the frameless case your debt entry lists. For this title it is most of the cases, not an edge case.AI-assisted (Claude Code).
Imported from boykopovar/AnyPS5. Originally posted by oneandonlydean on 2026-10-14T00:10:09Z.
Function starts here are direct call targets plus 16-byte-aligned code after int3 padding (
RedZoneFramer.cpp:143). A function reached only through a pointer and not preceded by int3 ends up inside the previous region, and itspop rbp; retexit gets thelea 0x80(%rsp)without the matching prologue. Please take function bounds from the.eh_frameFDEs and throw when they can't be read. Also, once #637 is in, a PPSA25344 boot count without the flag would show whether the framing is still needed then.Imported from boykopovar/AnyPS5. Originally posted by parl0v on 2026-10-14T03:52:27Z.
@oneandonlydean thanks, you're right. A function reached only through a pointer and not preceded by
int3padding gets merged into the previous region, so itspop rbp; retexit could get the epilogueleawithout a matching prologue. I'm moving the function bounds to the.eh_frameFDEs (the relinker's existingEhFrameReader). Framing will throw when the executable's.eh_framecan't be read, and a candidate leaf will need an FDE that starts exactly at its prologue. A test will cover a pointer-only function with noint3before it.I'll also take the boot count you suggested: PPSA25344 on
main+ #5596 relinked without--frame-red-zone, against the 6 crashes in 23 boots on the old base. If #5596 removes the race on its own, I'll say so here and close this PR.Imported from boykopovar/AnyPS5. Originally posted by parl0v on 2026-10-14T04:50:15Z.
@oneandonlydean here's the count you asked for. Closing this one: #5596 fixed it at the source.
PPSA25344 on
mainat00f4b3351(includes #5596), 60 s boots, alternating, same machine (i5-14400F, RTX 4060):--frame-red-zone, bounds from.eh_frameBefore #5596, the same title crashed at
app.exe+0x37f77b3in 6 of 23 boots without the flag. Both builds reach the title at about 70 presents per 60 s.I also made the fix you described: function bounds from
.eh_frameFDEs viaReadExceptionFunctions, with a test for two adjacent pointer-only leaves. It wouldn't have helped this title. SotS has 14,483 FDEs, covering 7.8 % of its text. One 70.7 MiB stretch has none, and the crash function0x37f74c0sits inside it. With FDE bounds the framer frames 19 functions instead of 137, and that function isn't one of them.So on SotS, the safe version of the flag can't reach the code that crashed, and the unsafe version isn't worth carrying now that the race is gone. If another title still shows the red-zone clobber on current main, the
.eh_frameversion is on my fork as a starting point: parl0v/AnyPS5@3ff74b9. Thanks for the hint toward #5596.Pull request closed