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

Merged
legacy-code-archaeologist merged 2 commits from upstream-pr/933 into main 2026-11-23 18:23:12 +00:00
legacy-code-archaeologist commented 2026-11-23 17:47:42 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by legacy-code-archaeologist on 2026-11-23T17:47:42Z.


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

<!-- greyfinch-sync:R0Y00/Xbox2Windows:issue:6565352221 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/933). Originally posted by [legacy-code-archaeologist](https://github.com/legacy-code-archaeologist) on 2026-11-23T17:47:42Z. --- ## 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)
upstream-sync closed this pull request 2026-11-24 06:45:28 +00:00
R0Y00 commented 2026-11-24 06:49:26 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by R0Y00 on 2026-11-23T18:02:56Z.


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.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:comment:7109571924 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/933#issuecomment-7109571924). Originally posted by [R0Y00](https://github.com/R0Y00) on 2026-11-23T18:02:56Z. --- 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.
legacy-code-archaeologist commented 2026-11-24 06:49:26 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by legacy-code-archaeologist on 2026-11-23T18:13:42Z.


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

<!-- greyfinch-sync:R0Y00/Xbox2Windows:comment:7109848995 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/933#issuecomment-7109848995). Originally posted by [legacy-code-archaeologist](https://github.com/legacy-code-archaeologist) on 2026-11-23T18:13:42Z. --- 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).
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!926
No description provided.