cpu: accept stack frames of call-free functions in the jump-table translator #414

Merged
bosseurdinternet merged 4 commits from upstream-pr/420 into main 2026-11-22 03:21:46 +00:00
bosseurdinternet commented 2026-11-22 01:12:36 +00:00 (Migrated from github.com)

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.py refuses 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.

  • Stack offsets. RSP is tracked at every instruction as entry RSP plus a constant. Only PUSH and POP of a 64-bit nonvolatile register and SUB or ADD RSP by an immediate multiple of 8 change it. Offsets must agree at joins and be 0 at every RET. Any other write of RSP (sub rsp, rcx, mov rsp, r, and rsp, imm, leave, enter) is rejected as a dynamic frame.
  • The frame. One zero-initialised alignas(16) std::uint8_t array 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 __chkstk before allocating a page or more and calls are rejected. An RSP-based operand becomes a load or store at a constant index. RSP is never a C++ value.
  • Rejected accesses. Below the current RSP; the return-address slot; at or above entry RSP + 40 (a stack argument).
  • Escapes. lea reg, [rsp+d], mov reg, rsp, push rsp and any other read of RSP as a value are rejected.
  • Bytewise definition. A separate pass follows every frame byte on every path. A read needs each of its bytes written on every path to it; home space starts undefined. Partial overlaps work byte by byte.
  • Save and restore. The same pass tracks which register and which frame bytes hold which nonvolatile register's entry value, through PUSH, POP and 64-bit MOV to and from the frame (MSVC /O2 saves 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.
  • Guest addresses. A register byte that may hold a guest address is never defined in the existing pass, and a frame store reads its source register, so a guest address is refused at the store. No frame taint state is added.
  • Functions that do not touch the stack translate to the same C++ as before.

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 memcpy and 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 main e5e0079. 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 /O2 home-space save shape, the cl /Od output 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_returns and cpu_aot_fourth_argument pass. Other cpu_* tests: same results as main. Under Wine, cpu_aot_import_call, cpu_aot_ordinal_import, cpu_native_reloc_free and cpu_native_tls fail 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:

    Level Functions Translated on main With this PR Not STL
    /O2 1,119 642 647 110 → 113
    /O1 1,116 639 643 111 → 112
    /Od 1,084 15 379 5 → 111

    No 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 /Od functions, the 5 new /O2 functions, 3 of the 4 new /O1 functions (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.py and tests/form_coverage_check.py passed.

Not run locally: Windows itself. The README sentence about Windows rests on this PR's Windows job.

Evidence and follow-up

  • tests/cpu/frame.asm is original MASM assembled in the build, with unwind directives. No binaries were added. frame is classified in tools/form_coverage.py.
  • Credit. @grahamster2 has an unpublished branch on the same subject (cpu/switch-stack-frames) and was waiting for this PR to compare. The home_spill and loop_counter exports 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 /O2 and /O1 that save a nonvolatile register into home space with MOV; I did not find a soundness difference. Review from that side is welcome.
  • Not reproduced: exception unwinding through the original frame layout. Four of the newly translated functions are catch funclets; they are translated and run as ordinary functions, and their behaviour as funclets is not tested.
  • Limits of the numbers. 831 of the 1,119 /O2 functions are MSVC STL instantiations, and this is desktop code, not a title. The gain is mostly at /Od; at /O2 the frame is a prerequisite for calls, which remain the first refusal of most functions (634 of the 705 still refused at /Od).
  • #321, #343 and #416 add lines next to this PR's in CMakeLists.txt, docs/experiments.md and tools/form_coverage.py. I will rebase as they land.
  • Jump-table translator notes

AI-assisted (Claude Code).

🤖 Generated with Claude Code

<!-- greyfinch-sync:R0Y00/Xbox2Windows:issue:6536698405 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/420). Originally posted by [bosseurdinternet](https://github.com/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.py` refuses 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. - **Stack offsets.** RSP is tracked at every instruction as entry RSP plus a constant. Only PUSH and POP of a 64-bit nonvolatile register and SUB or ADD RSP by an immediate multiple of 8 change it. Offsets must agree at joins and be 0 at every RET. Any other write of RSP (`sub rsp, rcx`, `mov rsp, r`, `and rsp, imm`, `leave`, `enter`) is rejected as a dynamic frame. - **The frame.** One zero-initialised `alignas(16) std::uint8_t` array 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 `__chkstk` before allocating a page or more and calls are rejected. An RSP-based operand becomes a `load` or `store` at a constant index. RSP is never a C++ value. - **Rejected accesses.** Below the current RSP; the return-address slot; at or above entry RSP + 40 (a stack argument). - **Escapes.** `lea reg, [rsp+d]`, `mov reg, rsp`, `push rsp` and any other read of RSP as a value are rejected. - **Bytewise definition.** A separate pass follows every frame byte on every path. A read needs each of its bytes written on every path to it; home space starts undefined. Partial overlaps work byte by byte. - **Save and restore.** The same pass tracks which register and which frame bytes hold which nonvolatile register's entry value, through PUSH, POP and 64-bit MOV to and from the frame (MSVC `/O2` saves 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. - **Guest addresses.** A register byte that may hold a guest address is never defined in the existing pass, and a frame store reads its source register, so a guest address is refused at the store. No frame taint state is added. - Functions that do not touch the stack translate to the same C++ as before. **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 `memcpy` and 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 main `e5e0079`. 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 `/O2` home-space save shape, the `cl /Od` output 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_returns` and `cpu_aot_fourth_argument` pass. Other `cpu_*` tests: same results as main. Under Wine, `cpu_aot_import_call`, `cpu_aot_ordinal_import`, `cpu_native_reloc_free` and `cpu_native_tls` fail 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: | Level | Functions | Translated on main | With this PR | Not STL | |---|---|---|---|---| | `/O2` | 1,119 | 642 | 647 | 110 → 113 | | `/O1` | 1,116 | 639 | 643 | 111 → 112 | | `/Od` | 1,084 | 15 | 379 | 5 → 111 | No 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 `/Od` functions, the 5 new `/O2` functions, 3 of the 4 new `/O1` functions (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.py` and `tests/form_coverage_check.py` passed. Not run locally: Windows itself. The README sentence about Windows rests on this PR's Windows job. ## Evidence and follow-up - `tests/cpu/frame.asm` is original MASM assembled in the build, with unwind directives. No binaries were added. `frame` is classified in `tools/form_coverage.py`. - **Credit.** @grahamster2 has an unpublished branch on the same subject (`cpu/switch-stack-frames`) and was waiting for this PR to compare. The `home_spill` and `loop_counter` exports 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 `/O2` and `/O1` that save a nonvolatile register into home space with MOV; I did not find a soundness difference. Review from that side is welcome. - **Not reproduced:** exception unwinding through the original frame layout. Four of the newly translated functions are `catch` funclets; they are translated and run as ordinary functions, and their behaviour as funclets is not tested. - **Limits of the numbers.** 831 of the 1,119 `/O2` functions are MSVC STL instantiations, and this is desktop code, not a title. The gain is mostly at `/Od`; at `/O2` the frame is a prerequisite for calls, which remain the first refusal of most functions (634 of the 705 still refused at `/Od`). - #321, #343 and #416 add lines next to this PR's in `CMakeLists.txt`, `docs/experiments.md` and `tools/form_coverage.py`. I will rebase as they land. - [Jump-table translator notes](https://github.com/R0Y00/Xbox2Windows/blob/main/docs/experiments.md#switch-dispatch-through-jump-tables) _AI-assisted (Claude Code)._ 🤖 Generated with [Claude Code](https://claude.com/claude-code)
upstream-sync closed this pull request 2026-11-24 06:42:57 +00:00
grahamster2 commented 2026-11-24 06:48:16 +00:00 (Migrated from github.com)

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, ad423ac on e5e0079.

  • cpu_aot_stack_frames passes: 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/intake not run here).
  • Independent fixture: my five frame exports from frames.asm all translate with this PR and match native over 545 inputs each (return, guarded trace, RBX, RSI).
  • Independent controls: all 15 of my same-length byte patches are refused (swapped and missing POPs, a read of the caller's RBX, missing stores including a one-path store, a save-slot read, [rsp], [rsp+28h] and [rsp-8], lea and mov copies of RSP, an unsaved RDI write, mov rsp, rax, sub rsp, rcx). One note: my one-path push rax is refused as an unsupported push before your join check runs. Your own different-depth control covers that rule.
  • Corpus: a Release (/O2) build of the repo, every .pdata function of its images, 2,321 unique bodies. main translates 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 /O2 MOV 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-operand IMUL is treated as reading its destination. One corpus function, offscreen_check.exe at RVA 0x1b6a0, gets past its MOV save with this PR and then stops at imul edi, ecx, 1010101h for 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.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:comment:7072168905 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/420#issuecomment-7072168905). Originally posted by [grahamster2](https://github.com/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, `ad423ac` on `e5e0079`. - `cpu_aot_stack_frames` passes: 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/intake` not run here). - **Independent fixture:** my five frame exports from [`frames.asm`](https://github.com/grahamster2/Xbox2Windows/blob/cpu/switch-stack-frames/tests/cpu/frames.asm) all translate with this PR and match native over 545 inputs each (return, guarded trace, RBX, RSI). - **Independent controls:** all 15 of my same-length byte patches are refused (swapped and missing POPs, a read of the caller's RBX, missing stores including a one-path store, a save-slot read, `[rsp]`, `[rsp+28h]` and `[rsp-8]`, `lea` and `mov` copies of RSP, an unsaved RDI write, `mov rsp, rax`, `sub rsp, rcx`). One note: my one-path `push rax` is refused as an unsupported push before your join check runs. Your own different-depth control covers that rule. - **Corpus:** a Release (`/O2`) build of the repo, every `.pdata` function of its images, 2,321 unique bodies. `main` translates 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 `/O2` MOV 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-operand `IMUL` is treated as reading its destination. One corpus function, `offscreen_check.exe` at RVA `0x1b6a0`, gets past its MOV save with this PR and then stops at `imul edi, ecx, 1010101h` for 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.
R0Y00 commented 2026-11-24 06:48:17 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by R0Y00 on 2026-11-22T01:57:28Z.


One frame-lifetime issue in prove_frame.step at ad423ac. The byte-state vector survives ADD RSP and POP, 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.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:comment:7072573130 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/420#issuecomment-7072573130). Originally posted by [R0Y00](https://github.com/R0Y00) on 2026-11-22T01:57:28Z. --- One frame-lifetime issue in `prove_frame.step` at `ad423ac`. The byte-state vector survives `ADD RSP` and `POP`, 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.
grahamster2 commented 2026-11-24 06:48:18 +00:00 (Migrated from github.com)

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, imm or a POP). 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 later sub rsp, imm brings 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.

sub rsp, 58h
mov [rsp+18h], rcx      ; entry RSP-0x40
add rsp, 20h            ; that slot is now below RSP
call leaf               ; natively overwrites it with the return address
sub rsp, 20h
mov rax, [rsp+18h]      ; accepted: translated RAX = x, native RAX = return address
add rsp, 58h
ret

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).

<!-- greyfinch-sync:R0Y00/Xbox2Windows:comment:7072929990 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/420#issuecomment-7072929990). Originally posted by [grahamster2](https://github.com/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, imm` or a `POP`). 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 later `sub rsp, imm` brings 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. ```asm sub rsp, 58h mov [rsp+18h], rcx ; entry RSP-0x40 add rsp, 20h ; that slot is now below RSP call leaf ; natively overwrites it with the return address sub rsp, 20h mov rax, [rsp+18h] ; accepted: translated RAX = x, native RAX = return address add rsp, 58h ret ``` 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)._
bosseurdinternet commented 2026-11-24 06:48:20 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by bosseurdinternet on 2026-11-22T03:01:16Z.


Fixed in 7fb6a9a, and main merged in 5ec180f (the branch conflicted in README.md and tools/form_coverage.py). I merged instead of rebasing so that ad423ac stays an ancestor for #425 and #430.

The change. When an instruction raises RSP (ADD RSP or POP), the bytes it leaves below RSP become unwritten in the proof, after a POP has 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:

  • Passing: realloc_slot stores to a slot, releases it with ADD 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.
  • Refused: realloc_slot with the store into its second allocation replaced by a NOP of the same length (undefined stack byte at entry RSP-0x18).
  • Refused: stale_save, a saved RBX whose slot is released by ADD RSP, 8 and allocated again before POP RBX (undefined stack byte at entry RSP-0x8). This is the provenance case.
  • Refused: stale_pop, a slot that POP RSI read 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_frames passes: 1,992 calls per side, 32 rejection controls. The other cpu_* tests have the same results as main.
  • Mutant: with the invalidation removed, all three refused controls are accepted, and realloc_slot is accepted either way.
  • Corpus, 7fb6a9a against ad423ac: the same 647, 643 and 379 functions translate at /O2, /O1 and /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).

<!-- greyfinch-sync:R0Y00/Xbox2Windows:comment:7073275738 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/420#issuecomment-7073275738). Originally posted by [bosseurdinternet](https://github.com/bosseurdinternet) on 2026-11-22T03:01:16Z. --- Fixed in `7fb6a9a`, and `main` merged in `5ec180f` (the branch conflicted in `README.md` and `tools/form_coverage.py`). I merged instead of rebasing so that `ad423ac` stays an ancestor for #425 and #430. **The change.** When an instruction raises RSP (`ADD RSP` or `POP`), the bytes it leaves below RSP become unwritten in the proof, after a `POP` has 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`: - Passing: `realloc_slot` stores to a slot, releases it with `ADD 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. - Refused: `realloc_slot` with the store into its second allocation replaced by a NOP of the same length (`undefined stack byte at entry RSP-0x18`). - Refused: `stale_save`, a saved RBX whose slot is released by `ADD RSP, 8` and allocated again before `POP RBX` (`undefined stack byte at entry RSP-0x8`). This is the provenance case. - Refused: `stale_pop`, a slot that `POP RSI` read 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_frames` passes: 1,992 calls per side, 32 rejection controls. The other `cpu_*` tests have the same results as `main`. - Mutant: with the invalidation removed, all three refused controls are accepted, and `realloc_slot` is accepted either way. - Corpus, `7fb6a9a` against `ad423ac`: the same 647, 643 and 379 functions translate at `/O2`, `/O1` and `/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)._
R0Y00 commented 2026-11-24 06:48:21 +00:00 (Migrated from github.com)

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.py branches (#321, #343 and #437) until we review and land this one. This also gives #425 a stable frame implementation to build on.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:comment:7073349004 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/420#issuecomment-7073349004). Originally posted by [R0Y00](https://github.com/R0Y00) on 2026-11-22T03:08:15Z. --- Please prioritize the frame-lifetime fix on #420. Hold rebases of your other `cpu_switch_aot.py` branches (#321, #343 and #437) until we review and land this one. This also gives #425 a stable frame implementation to build on.
bosseurdinternet commented 2026-11-24 06:48:21 +00:00 (Migrated from github.com)

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.

main moved again with #330 and this branch conflicted in README.md and docs/experiments.md, so I merged main once 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_frames passes (1,992 calls per side, 32 rejection controls), as does cpu_aot_jump_tables; the other cpu_* tests have the same results as main.
  • Corpus against main at fb4fb85: 642 → 647, 639 → 643 and 15 → 380 functions at /O2, /O1 and /Od, none lost, and the 1,296 translations that main produces keep identical C++. The one extra /Od function compared with my earlier figure uses a condition that #330 added.
  • Differential execution of the newly translated functions: 365 at /Od and 5 at /O2 with no mismatch.

AI-assisted (Claude Code).

<!-- greyfinch-sync:R0Y00/Xbox2Windows:comment:7073437392 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/420#issuecomment-7073437392). Originally posted by [bosseurdinternet](https://github.com/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. `main` moved again with #330 and this branch conflicted in `README.md` and `docs/experiments.md`, so I merged `main` once 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_frames` passes (1,992 calls per side, 32 rejection controls), as does `cpu_aot_jump_tables`; the other `cpu_*` tests have the same results as `main`. - Corpus against `main` at `fb4fb85`: 642 → 647, 639 → 643 and 15 → 380 functions at `/O2`, `/O1` and `/Od`, none lost, and the 1,296 translations that `main` produces keep identical C++. The one extra `/Od` function compared with my earlier figure uses a condition that #330 added. - Differential execution of the newly translated functions: 365 at `/Od` and 5 at `/O2` with no mismatch. _AI-assisted (Claude Code)._
grahamster2 commented 2026-11-24 06:48:21 +00:00 (Migrated from github.com)

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_slot without its store: undefined stack byte at entry RSP-0x18
    • stale_save: undefined stack byte at entry RSP-0x8
    • stale_pop: undefined stack byte at entry RSP-0x10
  • The generated C++ for cpu_frame (with realloc_slot) and for the jump-table, addr32, entry-order, narrow and fourth-argument fixtures is byte-identical between 5ec180f and #425 merged onto it.
  • Full Debug suite, run serially: ctest --preset dev --no-tests=error -E '^(conditional_import_paths|static_exe_intake|bundled_export_forwarders)$'. At 5ec180f: 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.
  • Differential fuzzing (random MASM frames with PUSH saves, MOV saves 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 translator alone, frames only, 20 seeds: 636 exports, 625 translated, 11 refused (all expected), 0 mismatches;
    • frames with #425's calls and tail jumps on top, 60 seeds: 1,915 exports, 1,577 translated (943 with calls or tail jumps), 338 refused (all expected), 0 mismatches.

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). Its below_call control is still refused, now by your release of the bytes left below RSP by ADD RSP.

#425 and #430 are merged onto 5ec180f.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:comment:7073440279 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/420#issuecomment-7073440279). Originally posted by [grahamster2](https://github.com/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_slot` without its store: `undefined stack byte at entry RSP-0x18` - `stale_save`: `undefined stack byte at entry RSP-0x8` - `stale_pop`: `undefined stack byte at entry RSP-0x10` - The generated C++ for `cpu_frame` (with `realloc_slot`) and for the jump-table, `addr32`, entry-order, narrow and fourth-argument fixtures is byte-identical between 5ec180f and #425 merged onto it. - Full Debug suite, run serially: `ctest --preset dev --no-tests=error -E '^(conditional_import_paths|static_exe_intake|bundled_export_forwarders)$'`. At 5ec180f: 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. - Differential fuzzing (random MASM frames with `PUSH` saves, `MOV` saves 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 translator alone, frames only, 20 seeds: 636 exports, 625 translated, 11 refused (all expected), 0 mismatches; - frames with #425's calls and tail jumps on top, 60 seeds: 1,915 exports, 1,577 translated (943 with calls or tail jumps), 338 refused (all expected), 0 mismatches. 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). Its `below_call` control is still refused, now by your release of the bytes left below RSP by `ADD RSP`. #425 and #430 are merged onto 5ec180f.
R0Y00 commented 2026-11-24 06:48:22 +00:00 (Migrated from github.com)

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.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:comment:7073469145 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/420#issuecomment-7073469145). Originally posted by [R0Y00](https://github.com/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.
R0Y00 commented 2026-11-24 06:48:22 +00:00 (Migrated from github.com)

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.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:comment:7073538904 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/420#issuecomment-7073538904). Originally posted by [R0Y00](https://github.com/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.
Sign in to join this conversation.
No reviewers
No labels
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Reference
R0Y00/Xbox2Windows!414
No description provided.