Skip to content

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

Merged
R0Y00 merged 2 commits into
R0Y00:mainfrom
legacy-code-archaeologist:cpu/switch-leave
Nov 23, 2026
Merged

R0Y00 merged 2 commits into
R0Y00:mainfrom
legacy-code-archaeologist:cpu/switch-leave

Conversation

@legacy-code-archaeologist

Copy link
Copy Markdown
Contributor

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

As agreed in R0Y00#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 <noreply@anthropic.com>
@R0Y00

R0Y00 commented Nov 23, 2026

Copy link
Copy Markdown
Owner

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

Copy link
Copy Markdown
Contributor Author

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

@R0Y00
R0Y00 merged commit 71273b4 into R0Y00:main Nov 23, 2026
5 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants