Skip to content

cpu: pass stack arguments to internal callees in cpu_switch_aot - #2208

Merged
R0Y00 merged 8 commits into
R0Y00:mainfrom
dicnunz:cpu/switch-stack-arguments
Nov 25, 2026
Merged

R0Y00 merged 8 commits into
R0Y00:mainfrom
dicnunz:cpu/switch-stack-arguments

Conversation

@dicnunz

@dicnunz dicnunz commented Nov 25, 2026

Copy link
Copy Markdown
Contributor

Change

tools/cpu_switch_aot.py refused any callee that read a fifth or later argument (unsupported stack argument), and so were its callers. An internal callee may now take stack arguments, the fifth to the twelfth: the 8-byte slots from its entry RSP + 0x28, which the Windows x64 calling convention places above the caller's home space. Each access must lie inside one slot. The caller must have written the bytes the callee reads, at RSP + 0x20 + 8 * slot and below the caller's return address. A slot the callee only writes is passed as 0. After the call those slots are undefined, like the callee's home space. A tail jump to a callee with stack arguments stays refused, and so do stack arguments of exported functions.

This branch contains the commits of the recursion PR (cpu/switch-recursion). Please review and merge that PR first, then this one. A rebase will be needed after the first lands.

Refs #317.

Verification

Tested commit: 172c07f (tip of dicnunz:cpu/switch-stack-arguments), rebased onto 166d866 (upstream #2176, 10:12 PM ET on 24 Nov 2026), including a docs commit that counts all eight stack-argument exports. The three-dot compare against current R0Y00/Xbox2Windows main is 7 commits ahead and 26 behind (main tip ab9a020 at 11:54 PM ET on 24 Nov 2026). The compare is 12 files, +1530 / -52, limited to the translator, tools/fuzz_cpu.py (the planted stale_rsp fault now passes the new Frame argument), the recursion fixture, the new stack-argument fixture, existing call-test controls, CMake test wiring and docs/experiments.md. docs/experiments.md now says the fixture "translates eight exports" and "all eight exports were refused" before the change.

Full Windows results are from a temporary fork branch ci/stack-full (b38a69f) that differed from the previous tip 39e43fc only by raising the Windows job timeouts from 20 minutes to 150 in .github/workflows/windows.yml. That timeout change is not in this PR branch. Those jobs have not been re-run on this tip; there are no GitHub Actions runs on 172c07f yet. Fork Windows checks workflow, workflow_dispatch, windows-2025 runners, 6:16 to 6:49 PM ET: https://github.com/dicnunz/Xbox2Windows/actions/runs/45508514322

Verified from the job logs:

Job Result
Build and test on Windows 925 tests, 906 passed, 18 skipped, 1 failed (call_evidence_check)
Build and test on Windows at /O2 925 tests, 906 passed, 18 skipped, 1 failed (call_evidence_check)
Run platform-independent Python checks on Linux passed
Assemble and translate gfx1013 fixtures passed
Publish the coverage chart skipped

In both Windows jobs cpu_aot_recursion and cpu_aot_stack_arguments passed. The 18 skips are the same capability or host skips listed in the recursion PR.

The one failure is tests/call_evidence_check.py, test_main_writes_expected_json_for_three_images at line 312: AssertionError comparing C:/Users/runneradmin/AppData/Local/Temp/... with the 8.3 short path C:/Users/RUNNER~1/AppData/Local/Temp/.... This branch does not touch tests/call_evidence_check.py or tools/call_evidence.py. The same assertion failed on upstream main in Windows checks run 45533450301 (push of #2141, 9:30 PM ET), so it is pre-existing and unrelated.

Failure-before, verified from the logs of fork run https://github.com/dicnunz/Xbox2Windows/actions/runs/45508518358 (ci/before-main e14f2a8, workflow conclusion success, Debug and /O2). On main, building the lifted DLLs exited 1, and ctest of the two new tests exited nonzero. The native originals matched the Python reference. All 8 stack-argument exports were refused (unsupported stack argument, except call_rec, which main refuses first as self-recursion). check_structure and check_refusals failed. No unexpected pass. The same run also shows the 4 recursion exports refused, as described in the recursion PR.

cmake --build --preset dev --target cpu_stack_args_lifted cpu_register_probe
ctest --preset dev --no-tests=error -R '^cpu_aot_stack_arguments$' -V

Claude also reported a local Linux run (llvm-ml 18, lld-link, clang++ at -O0 and -O2) where the translation matched the original on 7,184 calls, and that generated C++ was unchanged for the other cpu_switch_aot fixtures (including recursion.asm) and for the fuzz areas llvm-ml can assemble. I did not re-run those checks. From the fixture source, 8 exports over 449 input pairs is 3,592 translation-vs-original comparisons plus 3,592 original-vs-Python checks, which is the 7,184 figure.

Evidence and follow-up

tests/cpu/stack_args.asm is original source written for this test (MIT), marked in the file as a source-owned differential fixture, with tests/cpu/stack_args.def and tests/cpu/stack_args_check.py. I found no game files, dumps or other proprietary material in the compare.

Open this after the recursion PR, or rebase this branch onto that PR once it lands. This branch was rebased onto 166d866, which already includes tools/cpu_switch_aot.py (47fa55b, register-indexed memory bit tests, 6:26 PM ET on 24 Nov 2026). The BIT_TESTS lowering on this tip is identical to that commit and to current main. Current main (ab9a020) is 26 commits ahead of the rebase base. Those commits do not touch tools/cpu_switch_aot.py. They do edit CMakeLists.txt and docs/experiments.md, which auto-merge cleanly onto current main (local merge-tree, no conflicts). A further rebase is optional.

Not verified here: upstream PR CI has not been run. The pr CTest preset is a small smoke set and does not include cpu_aot_stack_arguments, cpu_aot_recursion or call_evidence_check. A full-suite baseline on main (ci/main-full, run 45515371589) was still queued with no jobs as of 10:11 PM ET and has produced no results. Floating-point arguments, frame addresses passed to callees and indirect calls stay refused. Recursion depth depends on the host stack. Nothing here shows that a game starts.

Checklist

  • This pull request covers one reviewable behavior.
  • I included the exact command and result for every check I ran.
  • I considered a negative control or failure case for a new fixture.
  • I updated README/docs when a capability or build requirement changed.
  • I did not include game files, dumps, distribution links or secrets.

This change was written by Claude (Anthropic's Claude Code, Opus 5.5) and submitted by dicnunz after review.

🤖 Generated with Claude Code
Depends on #2207, which should merge first; the first three commits here are that PR's.

A CALL may now close a call cycle, an export's call to its own entry
included. A call into a function whose translation is in progress uses
an assumed return width and argument demands, at first 8 bytes and none.
The cycle is translated again from the meet of the assumed and computed
summaries until no computed width is lower and no demand is higher, the
fixed-point return_width proposed in R0Y00#317. The functions of a cycle are
declared before their definitions and kept once its first function is
done. A tail jump that closes a cycle stays refused, because the
original runs such a cycle in constant stack.

cpu_aot_recursion translates four exports that reach self-recursion,
two- and three-function cycles, a cycle inside a cycle and an export that
calls its own entry, and compares them with the original and a Python
reference over 484 inputs each. Four exports must be refused, and two of
them only because the cycle is translated again. The direct-call
controls for self- and mutual recursion now translate, and a call that
closes a cycle with a tail jump replaces them as a refusal.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_014uAfgrqe3XyeqvkJrdnUK6
When a function of a call cycle goes on hold, the functions held during
its translation still recorded its stack depth as their dependence. A
later translation at the same depth that reused one of them was then
kept on its own, with the summary first assumed for the cycle, and a
tail jump to it was not seen to close the cycle. The held functions now
take on the dependence of the function that goes on hold.

stale_e in recursion.asm reaches that order and must be refused: its
callee reads all of RAX after a function that returns only AL through
the cycle. Before this change it was accepted with 8 bytes.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_014uAfgrqe3XyeqvkJrdnUK6
The call-cycle change stored (entry, internal) pairs in Program.active.
The planted call-retarget fault of cpu_aot_fuzz_calls compares
Program.active[-1] with an export's RVA, so it no longer planted, and the
test would report the fault as not detected. Program.active holds the
entries again, and a parallel list says which ones are internal.

The clean and planted translations of every fuzz area that llvm-ml
assembles, at the CMake default seeds, are byte-identical to main.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_014uAfgrqe3XyeqvkJrdnUK6
An internal callee may now read and write up to eight stack arguments,
the 8-byte slots from entry RSP + 40, each access inside one slot. Its
frame array extends over them, copied at entry from one C++ parameter
per slot after R9, and it records how many low bytes of each slot some
read reaches. Each caller's frame proof must show those bytes written at
RSP + 32 + 8 * slot when the CALL executes, below its own return address,
and the slots are undefined after the call because the callee may write
them. Slot demands join the call-cycle iteration. A tail jump to a
function with stack arguments stays refused, and exports still take
none.

cpu_aot_stack_arguments compares five exports that reach callees with
five to eight arguments, 1-, 2-, 4- and 8-byte slot reads, forwarded and
modified slots and recursion with a stack argument, with the original
and a Python reference over 449 inputs each, and checks eight refusal
controls. Three of them are accepted when the frame proof ignores the
slots of calls. The direct-call control whose callee reads its fifth
argument is now refused because that slot holds the caller's saved RSI.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_014uAfgrqe3XyeqvkJrdnUK6
…sp fault

A callee that only writes a stack-argument slot demands none of its
bytes, so its caller may have no frame array, but the call still loaded
the slot from one and the C++ did not compile. Such a slot is passed as
0. The planted stale_rsp fault of cpu_aot_fuzz_rsp replaced
Frame.__init__ with the old signature and failed to translate; it now
passes the internal flag on.

stack_args.asm adds a caller with no stack byte of its own for a
write-only slot, two calls whose last slot ends exactly at the return
address, an accepted twelfth argument, and a self-call that leaves the
fifth argument it reads unwritten, which is refused only because the
cycle is translated again.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_014uAfgrqe3XyeqvkJrdnUK6
cpu_aot_stack_arguments compares call_fill, call_edge and call_twelfth,
but the CMake translation step still named only the first five exports,
so the lifted DLL did not export them.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_014uAfgrqe3XyeqvkJrdnUK6
cpu_aot_stack_arguments translates eight exports since call_fill,
call_edge and call_twelfth were added; the paragraph still said five.
All eight are refused before this change, and call_rec is refused for
self-recursion first when the recursion change is absent.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_014uAfgrqe3XyeqvkJrdnUK6

@tedin7 tedin7 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review of head 172c07fae2e22e4806b8b2724a78d12db79875c8 (external PR, comment only).

Summary: no P1, no P2 found. The stack-argument handling in tools/cpu_switch_aot.py is consistent end to end: slot-aligned access check in Frame, per-slot demand width, entry state of the byte proof (caller-written bytes), kill of the callee-writable slots after a call, and rejection of tail jumps and of calls whose slots would reach the return address.

Commands executed locally (Linux), all clean:

  • git diff --stat origin/main...HEAD: 12 files, +1530/-52 (also carries the 3 recursion commits, as the body says; that PR must land first, then a rebase).
  • python3 tests/docs_links_check.py: self-test passed, 18 Markdown files, 406 links.
  • python3 tests/bidi_controls_check.py: no bidirectional controls in 1471 files.
  • git diff --check origin/main...HEAD: clean.
  • CI at review time: Linux Python checks pass, gfx1013 fixtures pass, Windows build pending, /O2 skipped.

Windows (Server 2025, MSVC, dev preset), same head:

  • ctest --preset dev -R 'cpu_aot_(stack_arguments|recursion|direct_calls)' after building cpu_stack_args(_lifted), cpu_recursion(_lifted), cpu_calls(_lifted), cpu_register_probe: 3/3 passed.
  • Mutation: in prove_frame, the check that every stack argument byte a callee reads was written by the caller was disabled (if passed != b"\1" * needed: replaced by if False:). cpu_aot_stack_arguments failed with unset_slot accepted: stack argument 6 of the call ... is undefined, so the refusal tests do kill it. The mutation was reverted.

Notes (nit, not blocking):

  • nit: the first check of lift_function that rejects stack arguments reaching the return address (frame.offsets[ip] + HOME_SIZE + 8 * len(stack) > 0) is exercised only through high_slots; fine, but a comment with the arithmetic would help the next reader.
  • nit: the PR is stacked on the recursion PR, so the diff is large for one review. Land the recursion PR first and rebase, as the description already says.

AI-assisted; commands listed were executed locally.

@tedin7 tedin7 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Re-review of head ba24d5fda8aff96a553a5548fa00daa26551fc46 (external PR, comment only). Previous review was on 172c07fa.

Summary: no P1, no P2. The previous review had no P2 either, so there was nothing to resolve. Its two nits are still open and are not blocking. The new head is a merge of main. The recursion PR (#2207) has landed, so the diff is now 8 files, +775/-30, and only the stack-argument increment remains.

Commands executed locally (Linux), all clean:

  • git diff --stat origin/main...HEAD: 8 files (CMakeLists.txt, docs/experiments.md, tests/cpu/calls_check.py, stack_args.asm/.def/_check.py, tools/cpu_switch_aot.py, tools/fuzz_cpu.py).
  • git diff --check origin/main...HEAD: clean.
  • python3 tests/docs_links_check.py: self-test passed, 18 Markdown files, 430 links.
  • python3 tests/bidi_controls_check.py: no bidirectional controls in 1546 files.
  • python3 -I -m py_compile tools/cpu_switch_aot.py tests/cpu/stack_args_check.py: ok.
  • CI at review time: Linux Python checks pass, gfx1013 fixtures pass, Windows build pending, /O2 skipped. PR is mergeable.

Windows (Server 2025, MSVC, dev preset), same head:

  • python3 x2w_pipe.py win2025 2208 --skip-release --targets cpu_stack_args,cpu_stack_args_lifted,cpu_recursion,cpu_recursion_lifted,cpu_calls,cpu_calls_lifted,cpu_register_probe --regex 'cpu_aot_(stack_arguments|recursion|direct_calls)'
  • ctest --preset dev -R 'cpu_aot_(stack_arguments|recursion|direct_calls)': 3/3 passed (cpu_aot_recursion, cpu_aot_direct_calls, cpu_aot_stack_arguments).

Mutation: not repeated on this head. The merge only brought in main's recursion changes. The stack-argument logic is unchanged since the earlier review, where disabling the caller-written-bytes check in prove_frame made cpu_aot_stack_arguments fail.

Notes (nit, not blocking), carried over:

  • nit: the check in lift_function that rejects stack arguments reaching the return address is exercised only through high_slots. A comment with the arithmetic would help the next reader.

AI-assisted; commands listed were executed locally.

@R0Y00
R0Y00 merged commit 27af557 into R0Y00:main Nov 25, 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.

4 participants