fix(relinker): refuse red-zone leaves without a frame pointer that push or pop #11061
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/11061"
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 roboterspinne on 2026-10-25T16:16:25Z.
What
In a leaf without a frame pointer,
--frame-red-zonelowers RSP after the prologue and adds the shift to every RSP operand in the body (frameSegmentincore/relinker/codegen/src/x86/RedZoneFramer.cpp). That is right only while RSP stays where the prologue left it. The framer refuses explicit RSP arithmetic in the body (other use of rsp) but not opcode-encoded pushes and pops. Afterpush raxin the body,[rsp]addresses the pushed slot and needs no shift, yet the framer turned it into[rsp+0x10], a slot of the leaf's red zone. On untouched160f6592b5, the leafpush rbx; push r14; push r15; mov [rsp-8], rcx; push rdx; mov rax, [rsp]; pop rdx; pop r15; pop r14; pop rbx; ret, framed withFrameRedZoneFunctionsand run natively on Windows, returns RCX instead of RDX, andstc; pushfq; and qword [rsp], -2; popfq; sbb rax, raxreturns -1 instead of 0. CI did not catch it: nored_zone_framertest frames a body that pushes or pops (the ones #11063 added are all refused asother use of rsp), andred_zone_framer_execution, which runs framed leaves, is registered only on Linux x86-64 and has no push in a body either.push or pop inside the body.pushesOrPopscounts push and pop of a register (also with REX or66),push imm8/imm32,push r/m(FF /6),pop r/m(8F /0),pushf,popfand push or pop offsorgs. The pops of an exit, which the framer already matches against the prologue, still frame. The check runs after the body loop, so a reason found inside the loop, such asother use of rsp, is still the one reported. A leaf with neither pushes nor an allocation that pushes or pops is now reported under the new reason instead ofprologue shorter than a jump, which is checked after it. Tracking the push depth instead would need the depth at every branch and branch target; not done.mainand with this change. Compilers do not read a slot they have pushed over.skipsLeavesWithoutFramePointerThatPushOrPopinRedZoneFramerTests.cpp: seven leaves without a frame pointer that store below RSP and then push and pop in one of the forms above (three with an RSP operand between push and pop, one that pushes from and pops to RSP operands) must be refused with the new reason, and a leaf with a frame pointer that pushes and pops must still frame. Onmainbefore #11063, all eight leaves are framed. Since #11063, the leaf that pushes from and pops to RSP operands is refused asrsp operand that cannot move and has no room for a stub, and the other seven are still framed. Either way the test fails at its firstrequire.main, stoppedmemoryUsefrom reading8Fwithout REX as an XOP prefix, sopop qword [rsp+8](8f 44 24 08) in a leaf with a frame pointer is refused asother use of rsp, like its REX.W form. In a leaf without a frame pointer this PR refuses it aspush or pop inside the body, andpop rsp(8f c4) is still refused asother use of rspinside the loop. After mergingmain, both changes are inRedZoneFramer.cpp, andred_zone_framerruns #11063'stellsPopsWithoutRexFromXopnext to this PR's test.--frame-red-zoneentry indocs/dev/debt/relinker.mdlists these leaves among those that keep their red zone.pr_overlap.pyno longer reports the pair.pushesOrPops(code, insn)helper with the same opcodes at the same place, and refuses push or pop only in leaves without pushes or an allocation. This PR refuses them in every leaf without a frame pointer, and neither PR touches leaves with a frame pointer.RedZoneFramer.cpp: the helper, the body-loop line next to itspush or pop in a leaf without pushescheck, and the line after the loop next to its rewritten prologue checks. They also conflict in the same sentence ofrelinker.md.ExitSite, the new check comparesexit.Begin <= offset && offset < exit.End.f8a99b6b8a(this PR at 1d403f96fe): merging in either order gives the same tree after the same resolution of the three conflicts inRedZoneFramer.cpp(#10871'spushesOrPops(code, insn)helper kept, both body-loop checks kept, this PR's check usingExitSite) and the one in the--frame-red-zonedebt entry, now one bullet that states both rules;red_zone_framer(with the tests #10871 added in597f1392a8),amd64_only_windowsandamd64_only_converterpass on the combined tree on Windows, and the native Windows probe, extended with the five execution leaves from597f1392a8, gives 0 wrong results on the combined tree against 4 on #10871 alone (the three body-push leaves and apush rbxleaf whose prologue #10871 widens).mainf95acfc948(which includes #11063) merged into this branch, #10871 atf8a99b6b8astill conflicts only in the same three hunks ofRedZoneFramer.cppand the same debt sentence. #11063's changes toextensionBitsandmemoryUsemerge outside them, so the resolved hunks posted on #10871 still apply.RedZoneFramer.cppand its tests; it merges cleanly with this branch, and the mergedred_zone_framertests pass. Atde2b5de1b7it also merges cleanly withmainf95acfc948merged into this branch.pr_overlap.pycannot compare #5021, #10479 and #10861, which also changerelinker.md, because they do not merge withmain. Checked by hand: #5021 and #10479 each add one other entry, #10861 rewrites thePT_SCE_DYNLIBDATAentries and adds one on guest lifecycle arrays, and none touches the--frame-red-zonesentence.Tested
Windows 11, GCC 15.2.0 (WinLibs UCRT), relinker-only Release build (
-DANYPS5_RELINKER_ONLY=ON -DBUILD_TESTING=ON); synthetic leaves only, no title or homebrew.red_zone_framerwith the new test andmain'sRedZoneFramer.cpp(unchanged between3a2c51afd2and160f6592b5) fails witha leaf without a frame pointer that pushes or pops between its prologue and its exits was framed, so its rsp operands after a push were moved past the pushed slot. With the change it passes.relinker_coreplaces each leaf in executable memory fromVirtualAlloc, frames it withFrameRedZoneFunctions, applies the displacements and sites the wayRedZoneFramerExecutionTests.cppdoes, and calls it with RCX = 0x1111 and RDX = 0x2222.main, the push leaf above returns 0x1111 instead of 0x2222, both with[rsp]moved to a stub and with[rsp+0]moved in place, and thepushfqleaf returns -1 instead of 0. With the change, all three are refused and return their unframed results.push rbp; mov rbp, rsp; push r15; push rbx; mov [rbp-0x18], rcx; push rdx; pop rax; add rax, [rbp-0x18]; pop rbx; pop r15; pop rbp; retreturns 0x4444 unframed and 0x3333 framed, on both.main(f95acfc948, which includes #11063) into the branch without conflicts (fa941e9301). On fa941e9301,ctest -R 'red_zone|amd64_only|RedZone' -j 6: 3 tests passed:red_zone_framer(0.7 s, with this PR'sskipsLeavesWithoutFramePointerThatPushOrPopand #11063'stellsPopsWithoutRexFromXop),amd64_only_converterandamd64_only_windows.ctest --test-dir build-relinker -j 6: 310 tests, 307 passed,long_pathsskipped; failures known on this machine onmainas well:select_tests(Windows symlink fixture) andpatch_advisor_unit(path with spaces, which open #10667 fixes); both failed again alone.python tools/check_conventions.py --base upstream/mainwith this title and description passed on fa941e9301.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 MarcZX10 on 2026-10-25T16:43:24Z.
Review: DISMISSED
I manually checked the overlap with #10871.
git merge-treeof the current heads conflicts inRedZoneFramer.cppanddocs/dev/debt/relinker.md; both branches add thepushesOrPopshandling in the same area, and the debt description also conflicts. Your What section describes how to combine the helpers, but the checklist still saysDepends on: none, and I do not see an agreement on order from #10871’s author. Please coordinate with that author on which change lands first, then record the dependency/order and recheck the combined tree. The standalone relinker CI is green; this is specifically the repository’s shared-file ordering requirement.