cpu: accept LEAVE on a proven RBP anchor in cpu_switch_aot #933

Manually merged
github merged 2 commits from gh-pr/933/head into gh-pr/933/base 2026-11-26 22:29:23 +00:00
Owner

@⁠legacy-code-archaeologist on GitHub · 2026-11-23 17:47 UTC · original
legacy-code-archaeologist:cpu/switch-leave → main · +367 −9 · 6 files · 2 commits · merged 2026-11-23 18:23 UTC

Change

As agreed in #907, tools/cpu_switch_aot.py accepts LEAVE as the epilogue of a fixed frame with a proven RBP anchor (the anchor proof from #889).

Frame proof (Frame.step). LEAVE is accepted only when:

  • the anchor is proven on every path;
  • the anchor is not below RSP;
  • anchor + 8 is at or below entry RSP.

It then sets RSP to the anchor and pops RBP from it, an 8-byte read at the anchor. The existing released bookkeeping frees every byte from the old RSP through anchor + 8, the pop slot included. prove_frame reads the slot before it zeroes the interval.

Restore proof. slot_register(LEAVE) is RBP, so the existing saved-register proof applies. RBP counts as restored only when the slot still holds the saved RBP bytes; eight ordinary defined bytes are not enough.

Flags. LEAVE is in FLAG_NEUTRAL, since it writes no flags. A consumer after it uses the producer before it.

Lowering. RBP is reloaded from the anchor slot. RSP stays a constant offset, as for ADD RSP.

Still rejected. ENTER and a separate MOV RSP, RBP stay rejected as dynamic frames. The module docstring, the Frame and prove_frame docstrings, and docs/experiments.md say so.

Tests

cpu_aot_leave (leave.asm) compares three translated exports with native execution over direct_check.input_pairs(), on return value, guarded trace, RBX and RSI:

  • leave_frame: MOV RBP, RSP, with CMP before LEAVE and SETB after it;
  • leave_lea: LEA RBP, [RSP+20h] after SUB RSP, 20h;
  • leave_join: both paths keep the anchor and write the same local.

It also checks in the generated C++ that LEAVE reloads RBP from exactly the slot PUSH RBP wrote. The register probe samples only RBX and RSI, and the translated RBP is a local of the generated function, so the restore is shown by the frame proof and this slot check, not by execution.

Nine controls must each be refused with a named reason:

Control Reason
MOV RBP, RSP patched to a NOP LEAVE without a proven frame anchor
leave_split: anchors differ at a join frame pointer differs at join
leave_unsaved: anchor slot never written undefined stack byte at entry RSP-0x8
leave_overwritten: saved RBP overwritten with ordinary data (mov [rbp], rcx) nonvolatile register rbp is not restored
leave_released: a local read after LEAVE released it undefined stack byte at entry RSP-0x10
leave_lowers: anchor below RSP LEAVE with the frame anchor below RSP
leave_above: anchor in the caller's home space LEAVE would raise RSP above entry RSP
mov_rsp_frame: a separate MOV RSP, RBP dynamic frame
enter_frame: ENTER dynamic frame

Verification

At 8e3d647 (on main af8fcba) on Windows 11 Pro (build 26100), Intel Core i7-6820HQ, MSVC 19.52, Python 3.12:

  • ctest --preset dev --no-tests=error -R '^cpu_aot_leave$' -V: passes. 747 calls per side match. All nine controls are rejected with their reasons.
  • ctest --preset dev --no-tests=error -R '^cpu_' -j 4: 173 of 174 pass. The capability skips are cpu_aot_fences_hints_clwb, cpu_aot_crypto_sha and cpu_shim_chain_hardware.
    • The stack-output, stack-frame and direct-call tests that #895 touches (cpu_aot_stack_output, cpu_aot_stack_frames, cpu_aot_direct_calls) pass.
    • The one failure is cpu_shim_shadow_hardware in its native run (D3D12XboxCreateDevice failed (HRESULT 0x887a0004)), independent of this change and reported in #799.
  • tools/form_coverage.py on a JUnit run of the ^cpu_ suite no longer lists leave.
  • tests/docs_links_check.py, tests/bidi_controls_check.py and tests/form_coverage_check.py pass.

Refs #907

  • I did not include game files, dumps, distribution links or secrets.

AI-assisted: written with Claude Code; design and diff reviewed by fresh agents; checked and submitted by the account owner.

🤖 Generated with Claude Code

**@⁠legacy-code-archaeologist** on GitHub · 2026-11-23 17:47 UTC · [original](https://github.com/R0Y00/Xbox2Windows/pull/933) `legacy-code-archaeologist:cpu/switch-leave` → `main` · +367 −9 · 6 files · 2 commits · merged 2026-11-23 18:23 UTC ## Change As agreed in #907, `tools/cpu_switch_aot.py` accepts `LEAVE` as the epilogue of a fixed frame with a proven RBP anchor (the anchor proof from #889). **Frame proof (`Frame.step`).** `LEAVE` is accepted only when: - the anchor is proven on every path; - the anchor is not below RSP; - `anchor + 8` is at or below entry RSP. It then sets RSP to the anchor and pops RBP from it, an 8-byte read at the anchor. The existing `released` bookkeeping frees every byte from the old RSP through `anchor + 8`, the pop slot included. `prove_frame` reads the slot before it zeroes the interval. **Restore proof.** `slot_register(LEAVE)` is RBP, so the existing saved-register proof applies. RBP counts as restored only when the slot still holds the saved RBP bytes; eight ordinary defined bytes are not enough. **Flags.** `LEAVE` is in `FLAG_NEUTRAL`, since it writes no flags. A consumer after it uses the producer before it. **Lowering.** RBP is reloaded from the anchor slot. RSP stays a constant offset, as for `ADD RSP`. **Still rejected.** `ENTER` and a separate `MOV RSP, RBP` stay rejected as dynamic frames. The module docstring, the `Frame` and `prove_frame` docstrings, and `docs/experiments.md` say so. ## Tests `cpu_aot_leave` ([`leave.asm`](tests/cpu/leave.asm)) compares three translated exports with native execution over `direct_check.input_pairs()`, on return value, guarded trace, RBX and RSI: - `leave_frame`: `MOV RBP, RSP`, with `CMP` before `LEAVE` and `SETB` after it; - `leave_lea`: `LEA RBP, [RSP+20h]` after `SUB RSP, 20h`; - `leave_join`: both paths keep the anchor and write the same local. It also checks in the generated C++ that `LEAVE` reloads RBP from exactly the slot `PUSH RBP` wrote. The register probe samples only RBX and RSI, and the translated RBP is a local of the generated function, so the restore is shown by the frame proof and this slot check, not by execution. **Nine controls** must each be refused with a named reason: | Control | Reason | |---|---| | `MOV RBP, RSP` patched to a NOP | `LEAVE without a proven frame anchor` | | `leave_split`: anchors differ at a join | `frame pointer differs at join` | | `leave_unsaved`: anchor slot never written | `undefined stack byte at entry RSP-0x8` | | `leave_overwritten`: saved RBP overwritten with ordinary data (`mov [rbp], rcx`) | `nonvolatile register rbp is not restored` | | `leave_released`: a local read after `LEAVE` released it | `undefined stack byte at entry RSP-0x10` | | `leave_lowers`: anchor below RSP | `LEAVE with the frame anchor below RSP` | | `leave_above`: anchor in the caller's home space | `LEAVE would raise RSP above entry RSP` | | `mov_rsp_frame`: a separate `MOV RSP, RBP` | `dynamic frame` | | `enter_frame`: `ENTER` | `dynamic frame` | ## Verification At `8e3d647` (on `main` `af8fcba`) on Windows 11 Pro (build 26100), Intel Core i7-6820HQ, MSVC 19.52, Python 3.12: - `ctest --preset dev --no-tests=error -R '^cpu_aot_leave$' -V`: passes. 747 calls per side match. All nine controls are rejected with their reasons. - `ctest --preset dev --no-tests=error -R '^cpu_' -j 4`: 173 of 174 pass. The capability skips are `cpu_aot_fences_hints_clwb`, `cpu_aot_crypto_sha` and `cpu_shim_chain_hardware`. - The stack-output, stack-frame and direct-call tests that #895 touches (`cpu_aot_stack_output`, `cpu_aot_stack_frames`, `cpu_aot_direct_calls`) pass. - The one failure is `cpu_shim_shadow_hardware` in its native run (`D3D12XboxCreateDevice failed (HRESULT 0x887a0004)`), independent of this change and reported in #799. - `tools/form_coverage.py` on a JUnit run of the `^cpu_` suite no longer lists `leave`. - `tests/docs_links_check.py`, `tests/bidi_controls_check.py` and `tests/form_coverage_check.py` pass. Refs #907 - [x] I did not include game files, dumps, distribution links or secrets. AI-assisted: written with Claude Code; design and diff reviewed by fresh agents; checked and submitted by the account owner. 🤖 Generated with [Claude Code](https://claude.com/claude-code)
As agreed in #907, the jump-table translator accepts LEAVE as the
epilogue of a fixed frame. It requires the RBP anchor to be proven on
every path. The anchor must not lie below RSP, and RSP must not rise
above entry RSP. LEAVE then sets RSP to the anchor and pops RBP from it,
reusing the saved-register proof, so RBP counts as restored only when the
slot still holds the saved RBP. The frame proof releases every byte from
the old RSP through anchor + 8, the pop slot included. LEAVE writes no
flags. ENTER and a separate MOV RSP, RBP stay rejected as dynamic frames.

cpu_aot_leave compares three anchored frames with native execution: MOV
RBP, RSP with flags that cross LEAVE, LEA RBP, [RSP+20h], and a join. It
checks that the translation reloads RBP from the slot PUSH RBP wrote.
Nine controls must each be refused with a named reason, among them a
saved RBP overwritten with ordinary data.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 22:29:20 +00:00
github manually merged commit 82a1eedb0e into gh-pr/933/base 2026-11-26 22:29:23 +00:00
Author
Owner

@⁠R0Y00 on GitHub · 2026-11-23 18:02 UTC · original

I found that the decoder also classifies 66 c9 as LEAVE, with a 16-bit operand. The frame proof implements an eight-byte pop, so I restricted it to the qword encoding and added a static-only rejection control. The native/lifted LEAVE, stack frame and direct call suites passed 3/3 in Windows x64 Release. Fresh CI is running.

**@⁠R0Y00** on GitHub · 2026-11-23 18:02 UTC · [original](https://github.com/R0Y00/Xbox2Windows/pull/933#issuecomment-7109571924) I found that the decoder also classifies 66 c9 as LEAVE, with a 16-bit operand. The frame proof implements an eight-byte pop, so I restricted it to the qword encoding and added a static-only rejection control. The native/lifted LEAVE, stack frame and direct call suites passed 3/3 in Windows x64 Release. Fresh CI is running.
Author
Owner

@⁠legacy-code-archaeologist on GitHub · 2026-11-23 18:13 UTC · original

Thanks for catching the 66 c9 form. With 82a1eed on the Skylake host (Windows 11 26100, MSVC 19.52, dev preset), ctest --preset dev --no-tests=error -R "^cpu_aot_(leave|stack_frames|direct_calls|stack_output)$" passes 4/4:

  • cpu_aot_leave: 747 calls per side match, and the ten rejection controls pass, including leave_word (unsupported LEAVE operand size).
  • cpu_aot_stack_frames: 2241 calls per side and 36 rejection controls pass.
  • cpu_aot_direct_calls and cpu_aot_stack_output also pass.

AI-assisted (Claude Code).

**@⁠legacy-code-archaeologist** on GitHub · 2026-11-23 18:13 UTC · [original](https://github.com/R0Y00/Xbox2Windows/pull/933#issuecomment-7109848995) Thanks for catching the `66 c9` form. With `82a1eed` on the Skylake host (Windows 11 26100, MSVC 19.52, `dev` preset), `ctest --preset dev --no-tests=error -R "^cpu_aot_(leave|stack_frames|direct_calls|stack_output)$"` passes 4/4: - `cpu_aot_leave`: 747 calls per side match, and the ten rejection controls pass, including `leave_word` (`unsupported LEAVE operand size`). - `cpu_aot_stack_frames`: 2241 calls per side and 36 rejection controls pass. - `cpu_aot_direct_calls` and `cpu_aot_stack_output` also pass. AI-assisted (Claude Code).
This discussion has been locked. Commenting is limited to contributors.
No description provided.