cpu: translate direct calls and tail jumps between functions in the jump-table translator #419
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/425"
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?
Imported from R0Y00/Xbox2Windows. Originally posted by grahamster2 on 2026-11-22T01:35:12Z.
Refs #317 (step 2). Stacked on #420: this branch is merged onto #420's head
5ec180f(with its frame-lifetime commit7fb6a9a); review only the commits after #420 (086e446, the calls;165a66f, the entry rule;b9935bc, a frame fix found by differential fuzzing;843154f, which drops the part of that fix that7fb6a9anow covers) until #420 lands, and I'll rebase then.Opened as a draft after asking in #317; @bosseurdinternet, if you already have step 2 underway, say so and I'll close this and review yours.
Change
tools/cpu_switch_aot.pyfollows a directCALL rel32, and a directJMPthat leaves the function's owned ranges (a tail jump), into the same generated source. This follows the decision in #317: validate function-entry RVAs, emit each target once, start with acyclic graphs and diagnose self- and mutual recursion, and apply volatile-register and flag clobbers after ordinary calls. Each callee is translated once, asstatic word fn_<rva>(word rcx, word rdx, word r8, word r9), and defined before its first caller. Functions that make no call or tail jump translate to the same C++ as before.Accepted
.pdatarecord whose unwind header is version 1 or 2, with a non-empty interval in executable code; orreturn_widthdefined bytes.7fb6a9athe bytes thatADD RSPandPOPleave below RSP become unwritten. So they stay undefined if RSP later falls back over them. (b9935bccleared every byte below RSP + 32 at a call for this; with7fb6a9athat clearing was redundant, and843154fremoves it, see below.)MOVsaves into the caller's own home space above entry RSP (the MSVC/O2shape from #420), which the callee's home space cannot reach.RET(RSP at its entry value, every nonvolatile register restored) and contributes the callee's return width.Refused
self-recursion) and any longer cycle (mutual recursion: call cycle A -> B -> A).callee at RVA X refused: <reason>, nested for deeper callees.the start of a chained range of the function at X);an exported label inside the function at X,an address inside the function at X);no runtime-function entry or export;Verification
Environment: Windows 11 Pro build 26200, VS Build Tools 2026 18.3 / MSVC 19.50.35725, ML64, Windows SDK 10.0.26100, Python 3.12.
ctest --preset dev --no-tests=error -R '^cpu_aot_direct_calls$' -V: passed.tests/cpu/calls.asmis original MASM with unwind directives. It has seven exports and three unexported callees placed below their callers:call_leaf: frame plus call, with the trace pointer kept in RBX;chain3 -> mid_step -> leaf_mix: three levels, with the leaf shared withcall_leaf;narrow_call: a callee that defines only AL, widened byMOVZX;narrow_tail: tail jump to that callee, width 1;tail_call: tail jump to the leaf;tail_after_frame: pops its frame, then tail-jumps;home_save_call: RBX saved byMOVinto its own home space and RSI byMOVabove the callee's home space, both across a call.Through
cpu_register_probe, 7 exports x 545 inputs = 3,815 calls per side. The original matches a Python reference, and the translation matches the original on the defined return bytes, guarded trace, RBX and RSI.Each callee is defined exactly once in the generated source. The reported return widths are 8, except 1 for
narrow_tail.20 controls, each refused with its stated reason (19 same-length patches and one export as built):
mid_stepcalling back intochain3;.rdata;callee argument rcx is undefined);SETEafter a call;MOVsave of RSI, moved into the callee's home space and reloaded after the call;below_call, as built. It stores x at entry RSP-0x40, raises RSP to entry RSP-0x38 for a call whose return address overwrites that slot, then lowers RSP and reloads it. Natively it returns the return address for every input; the test checks this. It is refused withundefined stack byte at entry RSP-0x40.165a66faccepted it and returned x, a soundness bug found by differential fuzzing of the call model.b9935bcfixed it in the call step; on this head it is refused by #420's7fb6a9a, which forgets the slot whenADD RSPreleases it.The fuzzer generates random MASM callers and callees with frames, home-space saves,
ADD/SUB RSParound calls, chains and tail jumps, and compares native and translated RAX (masked to the return width) and a 256-byte buffer. It found this case at seed 208. Rerun against843154fover seeds 100 to 139 and 200 to 219, 30 callers each, 200 inputs per export: 1,915 exports, 1,577 translated (943 with calls or tail jumps), 338 refused, none unexpectedly, 0 mismatches. Seeds 200 to 219 give the same counts as atb9935bc(636 exports, 545 translated, 332 with calls or tail jumps, 91 refused). In seeds 114, 127, 130 and 139 the fuzzer expected 13 exports to be refused that are accepted, as at the previous head: each reaches a plainPROCwithout.pdatathat a.pdata-less export already owns, so the jump is translated as a branch inside that export rather than as a call; all 13 match natively.ctest --preset dev --no-tests=error -R '^cpu_aot_call_entries$' -V: passed.tests/cpu/call_entries.asmwrites its.pdataand.xdataexplicitly. It has four primary functions, one continued by a chained secondary range, an exported interior label, a.datavariable exported asDATA, and a forwarder tokernel32.GetTickCount.The test reads the records from the image itself and checks that the translator's entries are exactly the four primary functions.
A framed call and a tail jump to the primary leaf match the original and a Python reference over 249 inputs each.
8 controls, each refused with the reason that names its distinction:
an exported label inside the function at 0x1000the start of a chained range of the function at 0x1009a data export in a non-executable sectiona forwarded exportjump target 0x1004 ... an exported label inside ...jump target 0x100e ... the start of a chained range ...a runtime function whose unwind data is not version 1 or 2 ...What the previous head (
086e446) did with the same copies. It refused the first six, but later and for unrelated reasons (no unique function interval,not in an executable section,edge outside owned ranges). It accepted the last two: an unverified record start counted as an entry, and so did an export at that address. Both gaps are closed.The primary-function case is the positive half of the same test.
Mutation checks for the entry rule, each planted and restored:
entry_caller control accepted: ... not version 1 or 2entry_caller control accepted: ... not version 1 or 2entries ['0x1000', '0x1004', ...] != the primary functionsprimary runtime functions ['0x1000', '0x1009', '0x100e', ...]export kinds {... 'entry_forward': 'data'}export kinds {... 'entry_data': 'code'}The last four are caught by the entry-table check. Behind it, the translator still refuses those inputs by later checks, as the
086e446run above shows.Mutation checks for the calls, each planted in the translator and restored afterwards:
call_leaf control accepted: undefined register rcxcallee leaf at RVA 0x1000 emitted 5 times; the C++ fails with C2084narrow_call control accepted: undefined register raxcall_leaf control accepted: callee argument rcx is undefinedADD RSP/POP), with this call stepbelow_callacceptedchain3 control accepted: ... undefined stack byte at entry RSP-0x10translation chain3(0x0, 0x1): ... != ...The generated C++ for
cpu_switch_original,cpu_switch_swapped,cpu_switch_addr32,cpu_entry_order,cpu_narrow,cpu_fourth_argand #420'scpu_frame(now withrealloc_slot) is byte-identical to #420's translator at5ec180f.cpu_callsandcpu_call_entriesare byte-identical to the previous head (5817408).Full Debug suite, run serially:
ctest --preset dev --no-tests=error -E '^(conditional_import_paths|static_exe_intake|bundled_export_forwarders)$'. Result at843154f: 153 passed, 3 capability skips (bvh_wave64_hardware,bvh_wave64_warp,wave_shuffle_warp). This includes #420'scpu_aot_stack_frames(1,992 calls per side, 32 controls includingrealloc_slot,stale_saveandstale_pop). I did not run the three intake tests.docs_links,tests/bidi_controls_check.pyandtests/form_coverage_check.pypassed.Corpus (measured at
b9935bc, before the merge of7fb6a9a; not rerun on this head): a Release (/O2) build of this branch, covering every.pdataentry of 96 readable non-generated images, with each unique function body translated with four arguments. A 13-byte placeholderd3d10warp.dllwas skipped.086e446)calls.asm, 2 incall_entries.asm, 8 are callers in the other MASM call fixtures (call_pair,repeat_call,direct), and 1 is MSVC output: a tail jump incpu_life_a.dll, translated but not executed..pdata, and import thunks;Evidence and follow-up
tests/cpu/calls.asm,calls.def,calls_check.py,call_entries.asm,call_entries.defandcall_entries_check.pyare original sources written for this PR (MIT); the DLLs are assembled in the build and no binaries are added.callsandcall_entriesare classified intools/form_coverage.py. Docs: the jump-table section ofdocs/experiments.mdand the README CPU row.Decisions not specified in #317, flagged for review
.pdataentry and no export are refused. This is now the largest single refusal. Following your review, a binding for them is opt-in in #430, and unbound hits are counted there as candidates only.lifted_<name>and asfn_<rva>. This keeps the existing export output unchanged.Not reproduced: exception unwinding through the original frames, as in #420.
Checklist
AI-assisted (Claude Code).
Imported from R0Y00/Xbox2Windows. Originally posted by R0Y00 on 2026-11-22T01:56:57Z.
For #317, automatic discovery can use the verified primary runtime-function start in executable code, or a non-forwarded code export from this image known to be used as a function. Reject chained secondary ranges, interior labels and data exports as independent entries. Please check those distinctions when rebasing onto #420. This PR can proceed on that boundary independently of #430.
Imported from R0Y00/Xbox2Windows. Originally posted by bosseurdinternet on 2026-11-22T01:59:42Z.
@grahamster2 Step 2 is not underway on my side, so please keep this one and #430. Thanks for the Windows verification of #420.
I ran both branches on my measurement corpus (the repository's desktop C++ sources plus one extra file, built with MSVC 14.52.37025 at three optimisation levels, one translation per function with three arguments, under Wine). Base is #420 at
ad423ac.086e446)ec499fc)/O2/O1/Od/O2after #430 on this corpus: imports are the first refusal of about 240 of the 470 functions still refused: a call or tail jump to an import thunk (about 110, some inside a callee),call qword ptr [rip+slot](about 55) andjmp qword ptr [rip+slot](about 80, which #343 covers). At/Odthe first refusal is a frame address that escapes or is passed to a callee in about 190.To avoid overlap: I will take ordinary calls of a bound import next,
call qword ptr [rip+slot]and calls or tail jumps to an import thunk, with the clobber model of this PR and the explicit binding of #343, stacked on #430 and #343. If you have already started that, say so and I will review yours instead.AI-assisted (Claude Code).
Imported from R0Y00/Xbox2Windows. Originally posted by grahamster2 on 2026-11-22T02:06:52Z.
Thanks. I checked those distinctions on the #420 base and added
165a66f.An entry is now one of:
Everything else is refused, and the message says which case applies: a chained secondary start, an interior label (exported or not), a record start with another unwind version, a forwarded export, a data export.
cpu_aot_call_entriesuses a fixture with explicit.pdata/.xdata:The previous head accepted the version-3 cases. It refused the other six only later, for unrelated reasons. Six mutations of the rule fail the test.
Unchanged: the C++ for the existing fixtures (byte-identical against #420) and the corpus result (97 of 2,343 against #420's 78, none lost, and identical to the previous head). The serial Debug suite passes 147/147, with 3 capability skips.
Imported from R0Y00/Xbox2Windows. Originally posted by grahamster2 on 2026-11-22T02:29:59Z.
Added
b9935bc, a soundness fix found by differential fuzzing of the call model. After a call, only the callee's 32-byte home space was marked undefined. The return address and the callee's frame below RSP were kept, so a function that raises RSP for a call and lowers it again could reload a slot the CALL had overwritten. Natively that reload returns the return address. Every frame byte below RSP + 32 at the call is now undefined.calls.asmgains the reproducer (below_call): the test shows the native result is the return address, and the translation is refused withundefined stack byte at entry RSP-0x40. The previous range accepted it. Unchanged: the generated C++ for existing fixtures, the corpus (97 of 2,346, none lost) and the serial suite (147/147).Imported from R0Y00/Xbox2Windows. Originally posted by grahamster2 on 2026-11-22T02:44:17Z.
@bosseurdinternet Thanks for the corpus run. Imports are yours; I haven't started them and won't.
On execution: functions that call have already run against their originals on Windows. A differential fuzzer generated random callers and callees (frames with PUSH and MOV saves, chains, shared callees, tail jumps, calls inside loops, narrow returns), assembled them with ML64, and compared native and translated runs on RAX and a 256-byte buffer over 200 inputs each. On
b9935bcthat covered about 3,100 accepted exports, roughly 1,770 of them with calls or tail jumps, with no mismatch. On165a66fit found the below-RSP clobber fixed inb9935bc. I'm happy to share the generator if it helps your tool.Marking this ready for review. It stays stacked on #420, and once the frame-lifetime change R0Y00 asked for in #420 lands, I'll rebase and align: this PR's extra clearing at a call should then be covered by the general rule.
AI-assisted (Claude Code).
Imported from R0Y00/Xbox2Windows. Originally posted by bosseurdinternet on 2026-11-22T02:52:16Z.
@grahamster2 The execution results I promised. The tool now compiles each entry together with its translated callees (each once) and runs it against the original entry, which calls the original callees in the loaded DLL. Same corpus and setup as my previous comment; up to 64 inputs per function; return value masked to the reported width, plus a memory arena. Evidence on a few inputs, not a proof.
086e446), newly translated,/Odec499fc), newly translated,/Od/O2/O1/Odfunctions reach 151 distinct callees.mov r8,[rax+10h]; mov rdx,[rax+8]; mov rcx,[rax]; call calleewhere the callee only spills its arguments to its home space. Oncecl /O2inlines the translated callee, the three loaded values are dead and the loads are removed. The same happens onmainwithout any call (mov rax,[rcx]; mov rax,rdx; ret), so this predates #425. Calls make it common in unoptimised MSVC output.load_always). With that one-line change applied to a copy of your #430 tree, all 9 agree: 159 of 160 with no mismatch.AI-assisted (Claude Code).
Imported from R0Y00/Xbox2Windows. Originally posted by grahamster2 on 2026-11-22T02:54:26Z.
@bosseurdinternet Thanks, that's exactly the execution evidence this needed, and #437 is a good catch: the dead guest read removed after inlining isn't something my fuzzers could see, because they only pass valid pointers.
On the gap you list, tail jumps: the differential fuzzer covered them on Windows. It ran about 900 tail jumps across
165a66fandb9935bc, about 700 of them after popping a frame, and some with narrow callee widths, each against the original on 200 inputs (RAX masked to the reported width, plus a 256-byte buffer), with no mismatch. Callee writes to the caller's stack aren't compared in those runs either, since frame addresses still don't cross a call.One note on the #430 rows:
ec499fcwas the heuristic version, which R0Y00 asked me to replace. #430 now refuses those targets by default and accepts them only through an opt-in[start, end)range manifest bound to the image hash, so with no manifest its corpus result equals #425's. Your 160/Odfunctions are still useful as candidates: with a manifest from source labels they're what the binding path would cover.Imported from R0Y00/Xbox2Windows. Originally posted by grahamster2 on 2026-11-22T03:17:49Z.
Synced onto #420's
5ec180f(frame-lifetime rule) shortly before the hold request on #420. With that rule, the call step clears only the callee's home space again (843154f); the below-RSP bytes are already invalidated when RSP rises. No further pushes to this branch or #430 until #420 lands.Imported from R0Y00/Xbox2Windows. Originally posted by R0Y00 on 2026-11-22T03:26:42Z.
@grahamster2 I integrated current main while preserving both sides of the documentation conflicts. Locally
d470872passed cpu_aot_stack_frames, cpu_aot_direct_calls (7 exports x 545 inputs, 20 rejection controls) and cpu_aot_call_entries (2 x 249, 8 rejection controls). #430 also passed locally on top (3 x 545, 16 binding rejection controls). #441 reuses call_bare in the EXE itself. I am handling CI and sequential merges.