cpu: accept stack frames of call-free functions in the jump-table translator #414
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/420"
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 R0Y00/Xbox2Windows. Originally posted by bosseurdinternet on 2026-11-22T01:12:36Z.
Change
Refs #317: step one of the frame decision, for functions that make no call. Builds on #285 and #309 (both merged), and is independent of #321, #343 and #416.
tools/cpu_switch_aot.pyrefuses every stack operation, every RSP-based operand and every nonvolatile register, so it translates only functions that never touch the stack. This accepts fixed frames in call-free functions. Calls of every kind stay rejected, so no frame address can cross one.sub rsp, rcx,mov rsp, r,and rsp, imm,leave,enter) is rejected as a dynamic frame.alignas(16) std::uint8_tarray per invocation, from the lowest RSP the function reaches to the end of the caller's home space at entry RSP + 40. Its size is computed at translation time and bounded at 4,096 bytes, because MSVC calls__chkstkbefore allocating a page or more and calls are rejected. An RSP-based operand becomes aloadorstoreat a constant index. RSP is never a C++ value.lea reg, [rsp+d],mov reg, rsp,push rspand any other read of RSP as a value are rejected./O2saves into home space with MOV). An entry value may only be saved; saved bytes may only be reloaded whole into the same register; every RET must find each nonvolatile register holding its entry value. A register is unreadable again once restored.Points where I interpreted "checked size and alignment", for your review. The size and every access index are constants checked at translation time. For alignment, the checks are that every RSP change is a multiple of 8 and that the array's start mirrors the real stack's residue modulo 16 given the ABI's entry alignment. In a model where values move by
memcpyand the address never escapes, alignment cannot change a result, so the residue is a property of the declaration and no test can observe it.Verification
Local runs used the same compiler build as CI (MSVC 14.52.37025:
cl,link,ml64) under Wine 11.0 on Linux, on maine5e0079. I reran the items below on the published translator unless a line says otherwise.ctest --no-tests=error -R '^cpu_aot_stack_frames$' -V: passed. Seven exports are called natively and translated: push and pop around a loop, spills at four widths with partial overlaps, the MSVC/O2home-space save shape, thecl /Odoutput of a small C function, two paths with different pushes, home-space spills, and a frame-resident loop counter. 1,743 calls per side match a Python reference on return value, guarded trace, RBX and RSI, and the test prints how many inputs took each path. An eighth export with a frame of exactly 4,096 bytes is translated and compiled but not called.What RBX and RSI show. The translation is C++ compiled by
cl, which preserves the host's nonvolatile registers itself. The probe shows the translation leaves them intact, not that it reproduces the original's save and restore.29 rejection controls, printed one line each, each with its expected message: an unbalanced RET; two different depths at a join; a loop that pushes each iteration; a register not restored on one path; a restore from the wrong slot by MOV and by POP; a saved slot read as data; reads of a byte not written on one path, of home space never written and of a loop counter written only on the back edge; a stack argument; the return-address slot read and written;
lea rax, [rsp+8];mov rax, rsp;sub rsp, rcx; adjustments that are not a multiple of 8 or raise RSP above entry; flags of an adjustment read; an access below RSP; a 4,112-byte frame; an entry value used in arithmetic;push rax;push 5; a guest address spilled; a reload straddling two saves; a partial save; an entry value reused after its restore.Existing tests:
cpu_aot_jump_tables,cpu_aot_entry_order,cpu_aot_narrow_returnsandcpu_aot_fourth_argumentpass. Othercpu_*tests: same results as main. Under Wine,cpu_aot_import_call,cpu_aot_ordinal_import,cpu_native_reloc_freeandcpu_native_tlsfail on main too because Wine's loader differs from Windows. The Windows job is the reference for those.Corpus (the repository's desktop C++ sources and one extra file, built with MSVC), against main with no allowed difference:
/O2/O1/OdNo function is lost, and all 1,296 translations that existed before have identical C++.
Differential execution of the original and the compiled translation on up to 64 inputs each, comparing return value and a memory arena: no mismatch on the 364 new
/Odfunctions, the 5 new/O2functions, 3 of the 4 new/O1functions (the fourth faulted on both sides for every input, so nothing was compared), and 644 of the 647 translated at/O2. A few functions completed fewer than 8 inputs. This is evidence on a few inputs, not a proof of equivalence.Mutant I ran: with the undefined-byte check disabled, the "home space never written" control is no longer rejected with its expected message, so the test fails.
Run during development, not rerun by me: 20 manual mutants (join check, restore proof, per-slot instead of per-byte definition, home space defined at entry, return-address slot writable, escape check, frame bound, and others). All fail the test; seven of them only because another check rejects the input with a different message than the one expected.
pyflakes,tests/docs_links_check.pyandtests/form_coverage_check.pypassed.Not run locally: Windows itself. The README sentence about Windows rests on this PR's Windows job.
Evidence and follow-up
tests/cpu/frame.asmis original MASM assembled in the build, with unwind directives. No binaries were added.frameis classified intools/form_coverage.py.cpu/switch-stack-frames) and was waiting for this PR to compare. Thehome_spillandloop_counterexports and two controls follow cases from that branch. The two designs agree on the frame array, offset rules, bytewise definition and escapes. On the corpus, I measured that this PR translates every function that branch does, plus four at/O2and/O1that save a nonvolatile register into home space with MOV; I did not find a soundness difference. Review from that side is welcome.catchfunclets; they are translated and run as ordinary functions, and their behaviour as funclets is not tested./O2functions are MSVC STL instantiations, and this is desktop code, not a title. The gain is mostly at/Od; at/O2the frame is a prerequisite for calls, which remain the first refusal of most functions (634 of the 705 still refused at/Od).CMakeLists.txt,docs/experiments.mdandtools/form_coverage.py. I will rebase as they land.AI-assisted (Claude Code).
🤖 Generated with Claude Code
Imported from R0Y00/Xbox2Windows. Originally posted by grahamster2 on 2026-11-22T01:23:00Z.
Verified on Windows, as promised in #317. No soundness problem found.
Environment: Windows 11 Pro build 26200, VS Build Tools 2026 18.3 (MSVC 19.50.35725, ML64), Windows SDK 10.0.26100, Python 3.12, Debug preset,
ad423acone5e0079.cpu_aot_stack_framespasses: 8 exports × 249 inputs match native, all paths taken, and 29 rejection controls pass. The full Debug suite passes 145/145 (three capability skips;tests/intakenot run here).frames.asmall translate with this PR and match native over 545 inputs each (return, guarded trace, RBX, RSI).[rsp],[rsp+28h]and[rsp-8],leaandmovcopies of RSP, an unsaved RDI write,mov rsp, rax,sub rsp, rcx). One note: my one-pathpush raxis refused as an unsupported push before your join check runs. Your own different-depth control covers that rule./O2) build of the repo, every.pdatafunction of its images, 2,321 unique bodies.maintranslates 47 and this PR 68, with none lost. That matches my independent implementation function for function: 0 functions translate under one and not the other.Differences from my version, all in this PR's favour or neutral: you accept the MSVC
/O2MOV save and restore through home space, which mine refused. Your 4,096-byte cap is stricter than mine but justified by__chkstk. You restrict PUSH/POP to nonvolatile registers, where mine allowed any defined 64-bit GPR; no fixture or corpus function depends on that.Outside this PR: on
main, three-operandIMULis treated as reading its destination. One corpus function,offscreen_check.exeat RVA0x1b6a0, gets past its MOV save with this PR and then stops atimul edi, ecx, 1010101hfor that reason. I'll send a separate fix.Happy to see this land. I'll build the direct-call work (step 2) on it if you're not already doing that.
Imported from R0Y00/Xbox2Windows. Originally posted by R0Y00 on 2026-11-22T01:57:28Z.
One frame-lifetime issue in
prove_frame.stepatad423ac. The byte-state vector survivesADD RSPandPOP, so a later allocation at the same offset inherits the earlier allocation’s defined bytes and saved-register tags. For example, a call-free function can allocate 8 bytes, store there, release them, allocate 8 again, and read the slot before writing it in the new allocation. During the gap that slot was below the current RSP, where the Windows x64 ABI permits its contents to change. The generated local array would silently reuse the old value.Please invalidate the released interval whenever RSP moves upward, after consuming any POP source, and preserve the slots still in the frame. Add a refusal control for release/reallocate/read and a passing control that writes the newly allocated slot before reading. Apply the same invalidation to saved-register provenance.
Imported from R0Y00/Xbox2Windows. Originally posted by grahamster2 on 2026-11-22T02:28:35Z.
One frame-model point from fuzzing #425 on top of this PR, for your consideration.
Bytes below RSP stay defined after RSP rises (
add rsp, immor aPOP). On its own that has no observable effect here, because reads below the current RSP are refused, and Windows x64 has no red zone, which is the same reasoning behind that refusal. But a latersub rsp, immbrings those bytes back into range with their old tag. With calls (#425) that is a real mismatch: natively, the CALL's return address and the callee's frame overwrite them.I'm fixing the call case in #425: everything below the callee's home space is undefined after a call. Marking the bytes below RSP undefined whenever RSP rises would close it at the source in this PR and keep the two models consistent. If you'd rather do that here, I'll align #425 with it.
The rest of the fuzzing is clean on this PR: about 4,000 generated functions with frames, PUSH and MOV saves, mid-body RSP changes and partial-overlap spills, 200 inputs each, with no mismatch and no over-refusal outside the case above.
AI-assisted (Claude Code).
Imported from R0Y00/Xbox2Windows. Originally posted by bosseurdinternet on 2026-11-22T03:01:16Z.
Fixed in
7fb6a9a, andmainmerged in5ec180f(the branch conflicted inREADME.mdandtools/form_coverage.py). I merged instead of rebasing so thatad423acstays an ancestor for #425 and #430.The change. When an instruction raises RSP (
ADD RSPorPOP), the bytes it leaves below RSP become unwritten in the proof, after aPOPhas read its source. Bytes still in the frame keep their state. Saved-register tags live in the same per-byte state, so the same step removes them: a register saved in a released slot can no longer be restored from it.Controls, each printed on one line by
ctest --no-tests=error -R '^cpu_aot_stack_frames$' -V:realloc_slotstores to a slot, releases it withADD RSP, 18h, allocates the same bytes again and writes the slot before reading it. It is called like the other exports: 249 input pairs match natively and translated.realloc_slotwith the store into its second allocation replaced by a NOP of the same length (undefined stack byte at entry RSP-0x18).stale_save, a saved RBX whose slot is released byADD RSP, 8and allocated again beforePOP RBX(undefined stack byte at entry RSP-0x8). This is the provenance case.stale_pop, a slot thatPOP RSIread and released, allocated again and read without a new store (undefined stack byte at entry RSP-0x10).Verification, MSVC 14.52.37025 under Wine, on the merged tree unless noted:
cpu_aot_stack_framespasses: 1,992 calls per side, 32 rejection controls. The othercpu_*tests have the same results asmain.realloc_slotis accepted either way.7fb6a9aagainstad423ac: the same 647, 643 and 379 functions translate at/O2,/O1and/Od, and all 1,669 translations have identical C++. So no measured MSVC function relied on a released slot.@grahamster2 this is the rule you proposed: bytes below RSP are undefined whenever RSP rises. Thanks for the fuzzing run.
AI-assisted (Claude Code).
Imported from R0Y00/Xbox2Windows. Originally posted by R0Y00 on 2026-11-22T03:08:15Z.
Please prioritize the frame-lifetime fix on #420. Hold rebases of your other
cpu_switch_aot.pybranches (#321, #343 and #437) until we review and land this one. This also gives #425 a stable frame implementation to build on.Imported from R0Y00/Xbox2Windows. Originally posted by bosseurdinternet on 2026-11-22T03:16:49Z.
Understood. The frame-lifetime fix is
7fb6a9a(details in my previous comment), and I am leaving #321, #343 and #437 as they are until this one lands. I had already rebased #321 and #343 before reading your message; nothing further will be pushed to them.mainmoved again with #330 and this branch conflicted inREADME.mdanddocs/experiments.md, so I mergedmainonce more (1ecd897); both conflicts were the same sentences edited on each side, and I kept #330's text with this PR's wording applied to it. On the merged tree, with MSVC 14.52.37025 under Wine:cpu_aot_stack_framespasses (1,992 calls per side, 32 rejection controls), as doescpu_aot_jump_tables; the othercpu_*tests have the same results asmain.mainatfb4fb85: 642 → 647, 639 → 643 and 15 → 380 functions at/O2,/O1and/Od, none lost, and the 1,296 translations thatmainproduces keep identical C++. The one extra/Odfunction compared with my earlier figure uses a condition that #330 added./Odand 5 at/O2with no mismatch.AI-assisted (Claude Code).
Imported from R0Y00/Xbox2Windows. Originally posted by grahamster2 on 2026-11-22T03:17:07Z.
Thanks for adopting the frame-lifetime rule in
7fb6a9a.Windows check of
5ec180f(Windows 11 Pro build 26200, VS Build Tools 2026 18.3 / MSVC 19.50.35725, ML64, Windows SDK 10.0.26100):ctest --preset dev --no-tests=error -R '^cpu_aot_stack_frames$' -V: passed. 1,992 calls per side match the reference, and all 32 controls are rejected, including the new ones:realloc_slotwithout its store:undefined stack byte at entry RSP-0x18stale_save:undefined stack byte at entry RSP-0x8stale_pop:undefined stack byte at entry RSP-0x10cpu_frame(withrealloc_slot) and for the jump-table,addr32, entry-order, narrow and fourth-argument fixtures is byte-identical between5ec180fand #425 merged onto it.ctest --preset dev --no-tests=error -E '^(conditional_import_paths|static_exe_intake|bundled_export_forwarders)$'. At5ec180f: 151 passed, 3 capability skips (bvh_wave64_hardware,bvh_wave64_warp,wave_shuffle_warp). With #425 merged onto it: 153 passed, the same 3 skips. The three intake tests were not run.PUSHsaves,MOVsaves into home space,SUB/ADD RSP, spills and partial reloads, loops; native against translated RAX and a 256-byte buffer, 200 inputs per export):Your rule also covers the call-side fix in #425 (
b9935bc): every byte below RSP is now already unwritten at a call, so #425 is back to clearing only the callee's home space (843154f). Itsbelow_callcontrol is still refused, now by your release of the bytes left below RSP byADD RSP.#425 and #430 are merged onto
5ec180f.Imported from R0Y00/Xbox2Windows. Originally posted by R0Y00 on 2026-11-22T03:19:58Z.
@bosseurdinternet @grahamster2 I am validating the current frame candidate on Windows, then will integrate #425 and #430 in order and check the source-built EXE. Please leave the three heads stable during integration; I will report any concrete mismatch here.
Imported from R0Y00/Xbox2Windows. Originally posted by R0Y00 on 2026-11-22T03:26:39Z.
Merged after Windows CI and a local Windows x64 run on
1ecd897(MSVC 19.51.36244). cpu_aot_stack_frames passed 1,992 calls per side and 32 rejection controls. In two_paths, both saved-register paths hit both RETs (68/78 and 52/51). Removing POP produced an unbalanced RET or a mismatched join, with the two entry-relative offsets in the diagnostic. Thanks for the fixture and differential review.