cpu: decisions needed before the jump-table translator handles calls, RIP-relative data and stack frames #317
Labels
No labels
accessibility
awaiting author
bug
documentation
duplicate
enhancement
good first issue
help wanted
invalid
priority: P0
question
wontfix
No milestone
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set
Reference
github/R0Y00--Xbox2Windows#317
Loading…
Reference in a new issue
No description provided.
Delete branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
@bosseurdinternet on GitHub · 2026-11-21 21:54 UTC · original
Problem
After #282, #285 and #309 (open),
tools/cpu_switch_aot.pytranslates 651 of 1,119 functions of an MSVC 14.52/O2build of this repository's own C++ sources (plus one plain C++ file). What refuses the other 468 is mostly not an instruction: it is a transfer to other code, or an address.call rel32jmp qword ptr [rip+slot], a tail jump through the import tablecall qword ptr [rip+slot]Limits of these numbers: 831 of the 1,119 functions are MSVC STL instantiations; only the first refusal of each function is counted, so a function refused for a call usually also needs a stack frame; and this is desktop code, not a title.
Each of these needs a contract that the machine code of one function does not supply. I would rather ask than pick one and have several PRs built on it. Four questions follow, each with the option I would take.
Proposal
1. Imports (call or tail jump through an import slot). Translate only through an explicit binding, with
cpu_call_aot.resolve_importand its--bind PROVIDER SYMBOLconvention. The callee's code is not in the image, so the number of low RAX bytes it defines is unknown to the translator.return_widthas aRETsite contributes its own; after a call, RAX has that many defined bytes.I am preparing the tail-jump case this way now. It is small and I will change it to match your answer.
2. Direct calls and direct jumps to other functions in the same image. Translate the callee by RVA into the same generated source, starting from the requested exports and following calls, with internal functions named by RVA. The caller is refused if any callee is refused. After a call, RAX has the callee's
return_widthdefined bytes, and volatile registers are undefined.return_widthas a fixed point over the call graph starting from the lower bound.3. RIP-relative data. Today a RIP-relative
LEAresult is a guest address and may only feed a jump table. Three models:pe_map), and a RIP-relative operand becomesimage_base + RVA. A RIP-relativeLEAthen yields a real address, so it can be stored, compared and returned. The translation then depends on the original image being mapped, which the existing differential tests already do.ffx_version_query.lower()does with its globals.Proposed: (a). It needs no per-function input and keeps one copy of the data, shared with the native run it is compared against.
4. Stack frames. A non-leaf function saves nonvolatile registers, allocates with
sub rsp, Nand addresses[rsp+disp].[rsp+disp]an offset into it, so a slot's address is a real address that can be passed to a callee. Refused at first: a function whose RSP is not a constant offset from entry at every instruction,alloca, a callee that reads stack arguments (fifth and later), and any read of a slot that no path wrote.ffx_version_query.lower().SSE/AVX is not part of this question; #314 covers a first case.
Alternatives
Keep the translator to leaf functions and count the rest as out of scope. That is a valid answer too: it would stop this line of PRs at integer mnemonics.
Verification
Each step would come as its own PR with a source-built MASM or C++ fixture, a differential check through the register probe, rejection controls printed by the test, and before and after counts on the corpus above with a check that no translated function is lost and that the generated C++ of functions already translated is unchanged. For calls, the corpus functions can also be executed beside their originals on generated inputs, as described in #282 and #285.
Checklist
AI-assisted (Claude Code).
@R0Y00 on GitHub · 2026-11-21 21:56 UTC · original
Start with the IAT tail jump. Bind the module and slot explicitly by symbol name or ordinal, with the input ABI and the number of guaranteed low RAX bytes (0, 1, 2 or 8). Zero denotes a normal return with no guaranteed result bits. Take the conservative minimum across tail jumps and other reachable normal returns. For ordinary calls apply volatile-register and flag clobbers.
For internal edges, validate function-entry RVAs and emit each target once. Start with acyclic graphs and diagnose self- and mutual recursion. For RIP-relative data, accept an explicitly supplied live mapping matching the input image and a checked RVA. Preserve aliases and shared writes; compare writable data under identical initial state using separate mappings or restoration, and compare cross-mapping addresses by RVA. Keep the existing ABI available and limit pointer validity to the binding lifetime.
For frames, use a bounded byte array per invocation with checked size and alignment. Track stack offsets, save/restore and bytewise definition before reads on each path. Allow only known bounded pointer uses across calls; reject escapes, dynamic frames, unsupported stack arguments and unproved effects. Separate source-built fixtures are appropriate for each step. Report corpus figures for the measured desktop inputs.
@grahamster2 on GitHub · 2026-11-21 23:37 UTC · original
I'd like to take step 4 (stack frames) in
tools/cpu_switch_aot.py, following the decision above. @bosseurdinternet, if you already have frames underway, say so and I'll verify or review yours instead.First PR: call-free functions with a fixed frame. Calls stay refused until step 2 lands, so no frame pointer crosses a call yet.
sub rsp, regand any other RSP write.push/popof a register,sub rsp, immandadd rsp, imm, and[rsp+disp]loads and stores with no index. Every RET must see offset 0.alignas(16)byte array per invocation, sized at translation time to the deepest offset plus the caller's 32-byte home area.[rsp+disp]becomes a checked offset into it. The return-address slot and anything above the home area (stack arguments) are refused.pushof RBX, RBP, RSI, RDI or R12–R15 before any write saves the caller's value, and every RET must see that register restored by the matchingpop. A nonvolatile write without such a save, or a read of a save slot other than its restore, is refused.lea r, [rsp+d]or any other way a frame address reaches a register, RSP as an index, andenter/leave.Verification: a source-built MASM fixture with push/pop of nonvolatiles,
sub rspspills and reloads, home-area spills of RCX and RDX, and a frame value that crosses a join. Differential checks throughcpu_register_probe(return, trace, RBX, RSI) against the original, and printed rejection controls for an unbalanced pop, a read before write, mismatched offsets at a join, a return-address read, a stack-argument read, an RSP write from a register and a frame-address escape. I'll report before/after counts on the MSVC/O2corpus at the top of this issue. @bosseurdinternet, could you share how you built and walked it, so the figures are comparable?I'll base on current
mainand rebase after #343, #321 and #330, which also editcpu_switch_aot.py. Environment: Windows 11 build 26200, MSVC 19.50, Python 3.12.@bosseurdinternet on GitHub · 2026-11-21 23:51 UTC · original
Hey, thanks for the detailed plan! I've already implemented most of step 4 and am wrapping up the remaining tests and verification. I'm planning to open a PR within the next couple of hours.
It would probably make sense to review and build on that rather than duplicate the work. I'll share the PR here as soon as it's ready, along with the testing details and corpus results.
@grahamster2 on GitHub · 2026-11-21 23:53 UTC · original
Thanks, @bosseurdinternet. I'll review and verify yours rather than open a second PR.
I had already built an independent version while waiting, so it can serve as a cross-check:
cpu/switch-stack-frameson my fork (be4784f, not a PR). The fixture and controls don't depend on my translator, so once your PR is up I'll run them against your branch on Windows 11 build 26200 (MSVC 19.50) and report what each one does:tests/cpu/frames.asm: five exports covering PUSH/POP of RBX and RSI used as scratch, qword/dword/word/byte stores combined by a wider reload, home-area spills of RCX and RDX with no allocation, the same slots written differently on two paths before a join, and a frame-resident counter across a loop back edge. Throughcpu_register_probe, 545 inputs per export against a Python reference.[rsp],[rsp+28h]and[rsp-8]at entry, LEA and MOV copies of RSP, an unsaved write to RDI,mov rsp, raxandsub rsp, rcx.Corpus method, in case it helps make figures comparable: a Release (
/O2) build of the repo at9bdbb20, every.pdataentry of the 81 non-_liftedimages, deduplicated by function bytes, each translated by RVA with four arguments. That gave 1,406 unique functions. Two observations from it: three CRT-free fixtures (cpu_startup_a,cpu_startup_a_alt,cpu_life_sink) fail to link at/O2with an unresolvedmemcpy, and once frames are accepted the most common new first refusal is a RIP-relative operand (28 functions), followed by XMM/YMM registers (8).@grahamster2 on GitHub · 2026-11-22 01:08 UTC · original
@bosseurdinternet, I see #343 (step 1) and #416 (step 3), with frames coming from you as well. Is step 2, direct calls and direct jumps to other functions in the same image, also on your list?
If not, I'd like to take it, following the decision above: validate function-entry RVAs, emit each callee once, start with acyclic call graphs, diagnose self- and mutual recursion, and apply volatile-register and flag clobbers after a call. Every non-leaf callee needs a frame, so I'd build on your frames PR once it's up and prototype against my frames branch until then. If you already have it underway, say so and I'll review and verify it instead, as with frames.
@grahamster2 on GitHub · 2026-11-22 01:35 UTC · original
I've opened step 2 (direct calls and tail jumps to other functions in the same image) as draft #425. It is stacked on #420 and I'll rebase once #420 lands. It follows the decision above: function-entry RVAs are validated, each callee is emitted once, self- and mutual recursion are refused, and volatile-register and flag clobbers apply after a call. The PR lists the points the decision left open for review, the main one being callees that have no
.pdataentry. @bosseurdinternet, if you already have step 2 underway, say so and I'll close mine and review yours.@grahamster2 on GitHub · 2026-11-22 02:50 UTC · original
A design question for the next step after #425, before any code is written for it: frame addresses passed to callees.
With frames (#420) and direct calls (#425), the most common first refusal at
/Odin @bosseurdinternet's corpus is a frame address that escapes or is passed to a callee (about 190 functions). In mine (/O2) it's 41. The typical shape islea rcx, [rsp+20h]; call fwherefwrites or reads through RCX, i.e. an out-parameter or a small struct passed by pointer. The decision above says: "Allow only known bounded pointer uses across calls; reject escapes, dynamic frames, unsupported stack arguments and unproved effects."Proposed reading, for you to accept, narrow or reject:
lea reg, [rsp+d]immediately feeding the call, never stored to memory, returned, or kept in a register after the call.[0, n)from the pointer: constant offsets only, and no further escape (not stored, not passed on, not returned).nand the bytes the callee reads before writing and writes are recorded as the callee's effect summary.[d, d+n)must lie inside its frame and not overlap the callee's home space or the return address. Bytes the callee reads must be defined in the caller. After the call, bytes the callee may write become written (defined if written on every callee path, otherwise undefined). Nothing else in the frame changes.load/storereach it directly. Pointer validity is limited to the call, matching "pointer validity to the binding lifetime".[0, n), a pointer to the caller's home space or save slots, frame addresses passed to imports (unknown effects), and recursion through such calls.Is that the boundary you want, or would you prefer something narrower first, e.g. read-only or write-only callee windows, or a single pointer argument per call? I'd do it as one PR stacked on #425 with fixtures for each rule and the usual controls.
AI-assisted (Claude Code).
@tedin7 on GitHub · 2026-11-24 02:23 UTC · original
Claiming one narrow addition to
tools/cpu_switch_aot.py: the inverted unsigned bound check,cmp reg, limit; jb/jbe dispatch_block; jmp default. JB and JBE enter the table, withlimitandlimit + 1entries. It touchesdispatch()andprove()only, not the ABI, frames, calls or data binding, so it does not overlap steps 1 to 4 or any open PR I could find.Planned files:
tools/cpu_switch_aot.py, a new source-built fixturetests/cpu/switch_inverted.asmwith its check,CMakeLists.txtanddocs/experiments.md. The check runs native, translated and exchanged-table builds against a Python reference and adds byte-patched rejection controls.Environment: Linux, LLVM 19 (
llvm-mc,lld-link), mingw-w64 g++ with Wine 10 for the native-versus-translated run. ML64 and MSVC are not available to me, so the MASM source and the/O2build are only exercised by the Windows CI jobs.AI-assisted (Claude Code).
@dicnunz on GitHub · 2026-11-25 05:11 UTC · original
Claiming two stacked follow-ups to step 2 on
tools/cpu_switch_aot.py, after the acyclic-call work already on main.Recursion through direct
CALLs: allow a call to close a call cycle and settlereturn_widthand argument demands by iterating to a fixed point, the method proposed in this issue. A tail jump that closes a cycle stays refused. Branch: https://github.com/dicnunz/Xbox2Windows/tree/cpu/switch-recursion (9e8d909)Stack arguments (5th to 12th) of internal callees: each caller must have written the bytes the callee reads. A tail jump to such a callee, and stack arguments of exported functions, stay refused. This branch contains the recursion commits, so it should land second: https://github.com/dicnunz/Xbox2Windows/tree/cpu/switch-stack-arguments (
172c07f)Both use new source-built MASM fixtures with differential checks and refusal controls. They were developed in Claude Code (Opus 5.5). On the fork's full Windows CI (
windows-2025, Debug and /O2),cpu_aot_recursionandcpu_aot_stack_argumentspassed. The one failing test,call_evidence_check, is unchanged by these branches and fails the same way on upstream main.These do not overlap the remaining questions on this issue (frame addresses passed to callees, RIP-relative data, imports) or the open
cpu:PRs I could find. They editcpu_switch_aot.py,CMakeLists.txtanddocs/experiments.md.AI-assisted (Claude Code, Opus 5.5).