cpu: translate direct calls and tail jumps between functions in the jump-table translator #419

Merged
grahamster2 merged 8 commits from upstream-pr/425 into main 2026-11-22 03:31:35 +00:00
grahamster2 commented 2026-11-22 01:35:12 +00:00 (Migrated from github.com)

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 commit 7fb6a9a); 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 that 7fb6a9a now 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.py follows a direct CALL rel32, and a direct JMP that 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, as static 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

  • A call or tail-jump target that is a function entry of this image, following the boundary in the review:
    • (a) the first byte of a verified primary runtime function: an unchained .pdata record whose unwind header is version 1 or 2, with a non-empty interval in executable code; or
    • (b) a non-forwarded code export: outside the export directory, in an executable section, and outside every runtime-function interval (an export at a primary start is already covered by (a)).
  • Argument registers: each callee records how many low bytes (1, 2 or 8) of each of RCX, RDX, R8 and R9 it may read while the register may still hold its entry value. The caller must have those bytes defined at the call.
  • After a call:
    • RAX has the callee's return_width defined bytes.
    • RCX, RDX, R8 to R11, the flags and the callee's 32-byte home space at RSP are undefined. The return address and the callee's frame lie below RSP, where every frame byte is already undefined: no access is below RSP, and since #420's 7fb6a9a the bytes that ADD RSP and POP leave below RSP become unwritten. So they stay undefined if RSP later falls back over them. (b9935bc cleared every byte below RSP + 32 at a call for this; with 7fb6a9a that clearing was redundant, and 843154f removes it, see below.)
    • Nonvolatile registers keep their state, because each callee is itself checked to restore every one.
    • The other frame bytes are kept. This includes MOV saves into the caller's own home space above entry RSP (the MSVC /O2 shape from #420), which the callee's home space cannot reach.
  • A tail jump is checked like a RET (RSP at its entry value, every nonvolatile register restored) and contributes the callee's return width.
  • The callee's frame is its own array from #420's model, so no frame address crosses a call.

Refused

  • A call to the caller's own entry (self-recursion) and any longer cycle (mutual recursion: call cycle A -> B -> A).
  • A caller is refused when any callee is, as callee at RVA X refused: <reason>, nested for deeper callees.
  • Call targets:
    • not a function entry. The message names the distinction:
      • the start of a chained secondary range (the start of a chained range of the function at X);
      • an interior label, exported or not (an exported label inside the function at X, an address inside the function at X);
      • a record start whose unwind header is not version 1 or 2;
      • a forwarded export, or a data export in a non-executable section;
      • no runtime-function entry or export;
    • outside the image;
    • in a non-executable section.
  • At a call site: RSP not 8 mod 16 from entry, or the callee's home space overlapping the return address.
  • Flags read after a call, and a callee with no reachable normal return.
  • As before: stack arguments, frame escapes (including a frame address passed to a callee), indirect calls and recursion.

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.asm is 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 with call_leaf;
    • narrow_call: a callee that defines only AL, widened by MOVZX;
    • 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 by MOV into its own home space and RSI by MOV above 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):

    • a call to its own entry;
    • mid_step calling back into chain3;
    • calls to an instruction interior and to a non-entry instruction boundary;
    • a target outside the image, and one in .rdata;
    • reads of RCX, R8 and the full RAX after a call;
    • a second call to the leaf right after the first, whose RCX and RDX the first call left undefined (callee argument rcx is undefined);
    • SETE after a call;
    • a callee reading a stack argument;
    • an unsupported instruction two callees down;
    • a spill, and a MOV save of RSI, moved into the callee's home space and reloaded after the call;
    • calls at entry RSP-0x30 and entry RSP-0x8;
    • a tail jump with a frame still allocated;
    • a tail jump with RBX not restored;
    • 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 with undefined stack byte at entry RSP-0x40. 165a66f accepted it and returned x, a soundness bug found by differential fuzzing of the call model. b9935bc fixed it in the call step; on this head it is refused by #420's 7fb6a9a, which forgets the slot when ADD RSP releases it.
      The fuzzer generates random MASM callers and callees with frames, home-space saves, ADD/SUB RSP around 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 against 843154f over 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 at b9935bc (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 plain PROC without .pdata that 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.asm writes its .pdata and .xdata explicitly. It has four primary functions, one continued by a chained secondary range, an exported interior label, a .data variable exported as DATA, and a forwarder to kernel32.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:

      Control Refusal
      call retargeted to the exported interior label an exported label inside the function at 0x1000
      call retargeted to the chained range's start the start of a chained range of the function at 0x1009
      call retargeted to the data export a data export in a non-executable section
      call retargeted to the forwarder a forwarded export
      tail jump retargeted to the interior label jump target 0x1004 ... an exported label inside ...
      tail jump retargeted to the chained start jump target 0x100e ... the start of a chained range ...
      leaf's unwind header patched from version 1 to 3, called a runtime function whose unwind data is not version 1 or 2 ...
      the same, tail-jumped the same
    • 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:

    Planted bug Result
    unwind version check removed entry_caller control accepted: ... not version 1 or 2
    export at an unverified record start accepted entry_caller control accepted: ... not version 1 or 2
    interior code exports counted as entries entries ['0x1000', '0x1004', ...] != the primary functions
    chained records counted as primary primary runtime functions ['0x1000', '0x1009', '0x100e', ...]
    forwarders classified by section export kinds {... 'entry_forward': 'data'}
    data exports classified as code 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 086e446 run above shows.

  • Mutation checks for the calls, each planted in the translator and restored afterwards:

    Planted bug Result
    volatile registers not clobbered after a call call_leaf control accepted: undefined register rcx
    shared callee not cached callee leaf at RVA 0x1000 emitted 5 times; the C++ fails with C2084
    RAX fully defined after a call narrow_call control accepted: undefined register rax
    callee argument demands dropped call_leaf control accepted: callee argument rcx is undefined
    #420's release of the bytes left below RSP disabled (ADD RSP/POP), with this call step below_call accepted
    callee home space kept across a call chain3 control accepted: ... undefined stack byte at entry RSP-0x10
    wrong callee named at the call site translation chain3(0x0, 0x1): ... != ...
  • The generated C++ for cpu_switch_original, cpu_switch_swapped, cpu_switch_addr32, cpu_entry_order, cpu_narrow, cpu_fourth_arg and #420's cpu_frame (now with realloc_slot) is byte-identical to #420's translator at 5ec180f. cpu_calls and cpu_call_entries are 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 at 843154f: 153 passed, 3 capability skips (bvh_wave64_hardware, bvh_wave64_warp, wave_shuffle_warp). This includes #420's cpu_aot_stack_frames (1,992 calls per side, 32 controls including realloc_slot, stale_save and stale_pop). I did not run the three intake tests. docs_links, tests/bidi_controls_check.py and tests/form_coverage_check.py passed.

  • Corpus (measured at b9935bc, before the merge of 7fb6a9a; not rerun on this head): a Release (/O2) build of this branch, covering every .pdata entry of 96 readable non-generated images, with each unique function body translated with four arguments. A 13-byte placeholder d3d10warp.dll was skipped.

    Translator Translated of 2,343
    #420 78
    this PR before the entry rule (086e446) 97
    this PR 97
    • None lost against #420. The entry rule changes no corpus result: the two heads agree on every function.
    • Of the 19 gained over #420, 8 are in calls.asm, 2 in call_entries.asm, 8 are callers in the other MASM call fixtures (call_pair, repeat_call, direct), and 1 is MSVC output: a tail jump in cpu_life_a.dll, translated but not executed.
    • New top first refusals:
      • a call target with no unwind entry or export: 806, mostly MSVC leaves emitted without .pdata, and import thunks;
      • a refused callee: 299. Its innermost reason is an indirect transfer in 186 and again a target with no unwind entry in 102;
      • a frame address passed to a callee: 32.
    • This is desktop code, not a title.

Evidence and follow-up

tests/cpu/calls.asm, calls.def, calls_check.py, call_entries.asm, call_entries.def and call_entries_check.py are original sources written for this PR (MIT); the DLLs are assembled in the build and no binaries are added. calls and call_entries are classified in tools/form_coverage.py. Docs: the jump-table section of docs/experiments.md and the README CPU row.

Decisions not specified in #317, flagged for review

  1. Targets with no .pdata entry 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.
  2. Internal functions always take all four argument registers. The caller is checked against the callee's recorded byte demands, so a three-argument export cannot call a callee that reads R9.
  3. An export that is also called internally is emitted twice, as lifted_<name> and as fn_<rva>. This keeps the existing export output unchanged.
  4. At a call, RSP must be 8 mod 16 from entry, and the callee's home space must lie below the return address. Otherwise the call is refused.
  5. The callee's 32-byte home space is undefined in the caller after the call, including any save placed there.
  6. A callee with no reachable normal return is refused, for both calls and tail jumps.
  7. Refusals are cached per callee. A cycle is a property of the call graph, so this is sound. Translation is capped at 256 callees.
  8. A frame address passed to a callee stays refused, following "allow only known bounded pointer uses across calls; reject escapes". Bounded pointer arguments would be a separate step.

Not reproduced: exception unwinding through the original frames, as in #420.

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.

AI-assisted (Claude Code).

<!-- greyfinch-sync:R0Y00/Xbox2Windows:issue:6536883824 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/425). Originally posted by [grahamster2](https://github.com/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 commit 7fb6a9a); 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 that 7fb6a9a now 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.py` follows a direct `CALL rel32`, and a direct `JMP` that 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, as `static 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 - A call or tail-jump target that is a function entry of this image, following the boundary in the review: - (a) the first byte of a verified primary runtime function: an unchained `.pdata` record whose unwind header is version 1 or 2, with a non-empty interval in executable code; or - (b) a non-forwarded code export: outside the export directory, in an executable section, and outside every runtime-function interval (an export at a primary start is already covered by (a)). - Argument registers: each callee records how many low bytes (1, 2 or 8) of each of RCX, RDX, R8 and R9 it may read while the register may still hold its entry value. The caller must have those bytes defined at the call. - After a call: - RAX has the callee's `return_width` defined bytes. - RCX, RDX, R8 to R11, the flags and the callee's 32-byte home space at RSP are undefined. The return address and the callee's frame lie below RSP, where every frame byte is already undefined: no access is below RSP, and since #420's 7fb6a9a the bytes that `ADD RSP` and `POP` leave below RSP become unwritten. So they stay undefined if RSP later falls back over them. (b9935bc cleared every byte below RSP + 32 at a call for this; with 7fb6a9a that clearing was redundant, and 843154f removes it, see below.) - Nonvolatile registers keep their state, because each callee is itself checked to restore every one. - The other frame bytes are kept. This includes `MOV` saves into the caller's own home space above entry RSP (the MSVC `/O2` shape from #420), which the callee's home space cannot reach. - A tail jump is checked like a `RET` (RSP at its entry value, every nonvolatile register restored) and contributes the callee's return width. - The callee's frame is its own array from #420's model, so no frame address crosses a call. ### Refused - A call to the caller's own entry (`self-recursion`) and any longer cycle (`mutual recursion: call cycle A -> B -> A`). - A caller is refused when any callee is, as `callee at RVA X refused: <reason>`, nested for deeper callees. - Call targets: - not a function entry. The message names the distinction: - the start of a chained secondary range (`the start of a chained range of the function at X`); - an interior label, exported or not (`an exported label inside the function at X`, `an address inside the function at X`); - a record start whose unwind header is not version 1 or 2; - a forwarded export, or a data export in a non-executable section; - `no runtime-function entry or export`; - outside the image; - in a non-executable section. - At a call site: RSP not 8 mod 16 from entry, or the callee's home space overlapping the return address. - Flags read after a call, and a callee with no reachable normal return. - As before: stack arguments, frame escapes (including a frame address passed to a callee), indirect calls and recursion. ## 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.asm` is 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 with `call_leaf`; - `narrow_call`: a callee that defines only AL, widened by `MOVZX`; - `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 by `MOV` into its own home space and RSI by `MOV` above 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): - a call to its own entry; - `mid_step` calling back into `chain3`; - calls to an instruction interior and to a non-entry instruction boundary; - a target outside the image, and one in `.rdata`; - reads of RCX, R8 and the full RAX after a call; - a second call to the leaf right after the first, whose RCX and RDX the first call left undefined (`callee argument rcx is undefined`); - `SETE` after a call; - a callee reading a stack argument; - an unsupported instruction two callees down; - a spill, and a `MOV` save of RSI, moved into the callee's home space and reloaded after the call; - calls at entry RSP-0x30 and entry RSP-0x8; - a tail jump with a frame still allocated; - a tail jump with RBX not restored; - `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 with `undefined stack byte at entry RSP-0x40`. 165a66f accepted it and returned x, a soundness bug found by differential fuzzing of the call model. b9935bc fixed it in the call step; on this head it is refused by #420's 7fb6a9a, which forgets the slot when `ADD RSP` releases it. The fuzzer generates random MASM callers and callees with frames, home-space saves, `ADD`/`SUB RSP` around 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 against 843154f over 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 at b9935bc (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 plain `PROC` without `.pdata` that 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.asm` writes its `.pdata` and `.xdata` explicitly. It has four primary functions, one continued by a chained secondary range, an exported interior label, a `.data` variable exported as `DATA`, and a forwarder to `kernel32.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: | Control | Refusal | |---|---| | call retargeted to the exported interior label | `an exported label inside the function at 0x1000` | | call retargeted to the chained range's start | `the start of a chained range of the function at 0x1009` | | call retargeted to the data export | `a data export in a non-executable section` | | call retargeted to the forwarder | `a forwarded export` | | tail jump retargeted to the interior label | `jump target 0x1004 ... an exported label inside ...` | | tail jump retargeted to the chained start | `jump target 0x100e ... the start of a chained range ...` | | leaf's unwind header patched from version 1 to 3, called | `a runtime function whose unwind data is not version 1 or 2 ...` | | the same, tail-jumped | the same | - **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: | Planted bug | Result | |---|---| | unwind version check removed | `entry_caller control accepted: ... not version 1 or 2` | | export at an unverified record start accepted | `entry_caller control accepted: ... not version 1 or 2` | | interior code exports counted as entries | `entries ['0x1000', '0x1004', ...] != the primary functions` | | chained records counted as primary | `primary runtime functions ['0x1000', '0x1009', '0x100e', ...]` | | forwarders classified by section | `export kinds {... 'entry_forward': 'data'}` | | data exports classified as code | `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 086e446 run above shows. - Mutation checks for the calls, each planted in the translator and restored afterwards: | Planted bug | Result | |---|---| | volatile registers not clobbered after a call | `call_leaf control accepted: undefined register rcx` | | shared callee not cached | `callee leaf at RVA 0x1000 emitted 5 times`; the C++ fails with C2084 | | RAX fully defined after a call | `narrow_call control accepted: undefined register rax` | | callee argument demands dropped | `call_leaf control accepted: callee argument rcx is undefined` | | #420's release of the bytes left below RSP disabled (`ADD RSP`/`POP`), with this call step | `below_call` accepted | | callee home space kept across a call | `chain3 control accepted: ... undefined stack byte at entry RSP-0x10` | | wrong callee named at the call site | `translation chain3(0x0, 0x1): ... != ...` | - The generated C++ for `cpu_switch_original`, `cpu_switch_swapped`, `cpu_switch_addr32`, `cpu_entry_order`, `cpu_narrow`, `cpu_fourth_arg` and #420's `cpu_frame` (now with `realloc_slot`) is byte-identical to #420's translator at 5ec180f. `cpu_calls` and `cpu_call_entries` are 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 at 843154f: 153 passed, 3 capability skips (`bvh_wave64_hardware`, `bvh_wave64_warp`, `wave_shuffle_warp`). This includes #420's `cpu_aot_stack_frames` (1,992 calls per side, 32 controls including `realloc_slot`, `stale_save` and `stale_pop`). I did not run the three intake tests. `docs_links`, `tests/bidi_controls_check.py` and `tests/form_coverage_check.py` passed. - Corpus (measured at b9935bc, before the merge of 7fb6a9a; not rerun on this head): a Release (`/O2`) build of this branch, covering every `.pdata` entry of 96 readable non-generated images, with each unique function body translated with four arguments. A 13-byte placeholder `d3d10warp.dll` was skipped. | Translator | Translated of 2,343 | |---|---| | #420 | 78 | | this PR before the entry rule (086e446) | 97 | | this PR | 97 | - None lost against #420. The entry rule changes no corpus result: the two heads agree on every function. - Of the 19 gained over #420, 8 are in `calls.asm`, 2 in `call_entries.asm`, 8 are callers in the other MASM call fixtures (`call_pair`, `repeat_call`, `direct`), and 1 is MSVC output: a tail jump in `cpu_life_a.dll`, translated but not executed. - New top first refusals: - a call target with no unwind entry or export: 806, mostly MSVC leaves emitted without `.pdata`, and import thunks; - a refused callee: 299. Its innermost reason is an indirect transfer in 186 and again a target with no unwind entry in 102; - a frame address passed to a callee: 32. - This is desktop code, not a title. ## Evidence and follow-up `tests/cpu/calls.asm`, `calls.def`, `calls_check.py`, `call_entries.asm`, `call_entries.def` and `call_entries_check.py` are original sources written for this PR (MIT); the DLLs are assembled in the build and no binaries are added. `calls` and `call_entries` are classified in `tools/form_coverage.py`. Docs: the jump-table section of `docs/experiments.md` and the README CPU row. ### Decisions not specified in #317, flagged for review 1. **Targets with no `.pdata` entry 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. 2. **Internal functions always take all four argument registers.** The caller is checked against the callee's recorded byte demands, so a three-argument export cannot call a callee that reads R9. 3. **An export that is also called internally is emitted twice**, as `lifted_<name>` and as `fn_<rva>`. This keeps the existing export output unchanged. 4. **At a call, RSP must be 8 mod 16 from entry**, and the callee's home space must lie below the return address. Otherwise the call is refused. 5. **The callee's 32-byte home space is undefined in the caller after the call**, including any save placed there. 6. **A callee with no reachable normal return is refused**, for both calls and tail jumps. 7. **Refusals are cached per callee.** A cycle is a property of the call graph, so this is sound. Translation is capped at 256 callees. 8. **A frame address passed to a callee stays refused**, following "allow only known bounded pointer uses across calls; reject escapes". Bounded pointer arguments would be a separate step. Not reproduced: exception unwinding through the original frames, as in #420. ## Checklist - [x] This pull request covers one reviewable behavior. - [x] I included the exact command and result for every check I ran. - [x] I considered a negative control or failure case for a new fixture. - [x] I updated README/docs when a capability or build requirement changed. - [x] I did not include game files, dumps, distribution links or secrets. _AI-assisted (Claude Code)._
upstream-sync closed this pull request 2026-11-24 06:42:59 +00:00
R0Y00 commented 2026-11-24 06:48:17 +00:00 (Migrated from github.com)

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.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:comment:7072567123 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/425#issuecomment-7072567123). Originally posted by [R0Y00](https://github.com/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.
bosseurdinternet commented 2026-11-24 06:48:17 +00:00 (Migrated from github.com)

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.

Level Functions #420 #425 (086e446) #430 (ec499fc)
/O2 1,119 647 647 649
/O1 1,116 643 643 644
/Od 1,084 379 385 539
  • No function is lost by either branch, and all 1,669 translations that #420 produces keep identical C++ and the same shared header.
  • Not done yet: execution. My differential tool excludes any function that contains a call, so none of the newly translated functions has been run against its original here. I am extending it to compile callees and will post the result on this PR.
  • What remains at /O2 after #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) and jmp qword ptr [rip+slot] (about 80, which #343 covers). At /Od the 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).

<!-- greyfinch-sync:R0Y00/Xbox2Windows:comment:7072599591 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/425#issuecomment-7072599591). Originally posted by [bosseurdinternet](https://github.com/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`. | Level | Functions | #420 | #425 (`086e446`) | #430 (`ec499fc`) | |---|---|---|---|---| | `/O2` | 1,119 | 647 | 647 | 649 | | `/O1` | 1,116 | 643 | 643 | 644 | | `/Od` | 1,084 | 379 | 385 | 539 | - No function is lost by either branch, and all 1,669 translations that #420 produces keep identical C++ and the same shared header. - **Not done yet: execution.** My differential tool excludes any function that contains a call, so none of the newly translated functions has been run against its original here. I am extending it to compile callees and will post the result on this PR. - **What remains at `/O2` after #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) and `jmp qword ptr [rip+slot]` (about 80, which #343 covers). At `/Od` the 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)._
grahamster2 commented 2026-11-24 06:48:17 +00:00 (Migrated from github.com)

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:

  • (a) the first byte of a verified primary runtime function: an unchained record with a version 1 or 2 unwind header and a non-empty interval in executable code;
  • (b) a non-forwarded code export: outside the export directory, in an executable section, and outside every runtime-function interval.

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_entries uses a fixture with explicit .pdata/.xdata:

  • it checks that the entry table is exactly its four primary functions;
  • a call and a tail jump to a primary leaf match native (249 inputs each);
  • 8 controls are refused with those reasons: call and tail jump to the exported interior label and to the chained start, call to the data export and to the forwarder, and the leaf's unwind version patched to 3 for both.

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.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:comment:7072679524 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/425#issuecomment-7072679524). Originally posted by [grahamster2](https://github.com/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: - **(a)** the first byte of a verified primary runtime function: an unchained record with a version 1 or 2 unwind header and a non-empty interval in executable code; - **(b)** a non-forwarded code export: outside the export directory, in an executable section, and outside every runtime-function interval. 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_entries` uses a fixture with explicit `.pdata`/`.xdata`: - it checks that the entry table is exactly its four primary functions; - a call and a tail jump to a primary leaf match native (249 inputs each); - 8 controls are refused with those reasons: call and tail jump to the exported interior label and to the chained start, call to the data export and to the forwarder, and the leaf's unwind version patched to 3 for both. 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.
grahamster2 commented 2026-11-24 06:48:18 +00:00 (Migrated from github.com)

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.asm gains the reproducer (below_call): the test shows the native result is the return address, and the translation is refused with undefined 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).

<!-- greyfinch-sync:R0Y00/Xbox2Windows:comment:7072944155 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/425#issuecomment-7072944155). Originally posted by [grahamster2](https://github.com/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.asm` gains the reproducer (`below_call`): the test shows the native result is the return address, and the translation is refused with `undefined 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).
grahamster2 commented 2026-11-24 06:48:19 +00:00 (Migrated from github.com)

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 b9935bc that covered about 3,100 accepted exports, roughly 1,770 of them with calls or tail jumps, with no mismatch. On 165a66f it found the below-RSP clobber fixed in b9935bc. 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).

<!-- greyfinch-sync:R0Y00/Xbox2Windows:comment:7073091032 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/425#issuecomment-7073091032). Originally posted by [grahamster2](https://github.com/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 `b9935bc` that covered about 3,100 accepted exports, roughly 1,770 of them with calls or tail jumps, with no mismatch. On `165a66f` it found the below-RSP clobber fixed in `b9935bc`. 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)._
bosseurdinternet commented 2026-11-24 06:48:19 +00:00 (Migrated from github.com)

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.

Selection Functions No mismatch Mismatch Not executed
#425 (086e446), newly translated, /Od 6 6 0 0
#430 (ec499fc), newly translated, /Od 160 150 9 1
#430, newly translated, /O2 2 2 0 0
#430, newly translated, /O1 1 1 0 0
  • No wrong value. On every input where the original returns, the translation returns the same value and leaves the same arena bytes. The 160 /Od functions reach 151 distinct callees.
  • The 9 mismatches have one cause, and it is not in your call logic. The original faults on a bad pointer and the translation returns. Example: mov r8,[rax+10h]; mov rdx,[rax+8]; mov rcx,[rax]; call callee where the callee only spills its arguments to its home space. Once cl /O2 inlines the translated callee, the three loaded values are dead and the loads are removed. The same happens on main without any call (mov rax,[rcx]; mov rax,rdx; ret), so this predates #425. Calls make it common in unoptimised MSVC output.
  • Fix sent as #437 (every guest read through the existing load_always). With that one-line change applied to a copy of your #430 tree, all 9 agree: 159 of 160 with no mismatch.
  • The one not executed is a loop whose trip count is its argument; the original timed out on the first inputs, which were pointers. A subagent of mine checked it separately on 2,061 small counts with no difference; I did not rerun that check myself.
  • What this does not show: no measured function contains a tail jump, so that path was not executed; a few functions completed only 1 to 5 inputs on both sides; the callee's writes to stack memory are not compared; Wine only.
  • Self-test of the tool, run by the subagent and not rerun by me: with argument registers swapped at call sites, or the callee's result ignored, 115 and 85 of the 160 functions mismatch.

AI-assisted (Claude Code).

<!-- greyfinch-sync:R0Y00/Xbox2Windows:comment:7073181735 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/425#issuecomment-7073181735). Originally posted by [bosseurdinternet](https://github.com/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. | Selection | Functions | No mismatch | Mismatch | Not executed | |---|---|---|---|---| | #425 (`086e446`), newly translated, `/Od` | 6 | 6 | 0 | 0 | | #430 (`ec499fc`), newly translated, `/Od` | 160 | 150 | 9 | 1 | | #430, newly translated, `/O2` | 2 | 2 | 0 | 0 | | #430, newly translated, `/O1` | 1 | 1 | 0 | 0 | - **No wrong value.** On every input where the original returns, the translation returns the same value and leaves the same arena bytes. The 160 `/Od` functions reach 151 distinct callees. - **The 9 mismatches have one cause, and it is not in your call logic.** The original faults on a bad pointer and the translation returns. Example: `mov r8,[rax+10h]; mov rdx,[rax+8]; mov rcx,[rax]; call callee` where the callee only spills its arguments to its home space. Once `cl /O2` inlines the translated callee, the three loaded values are dead and the loads are removed. The same happens on `main` without any call (`mov rax,[rcx]; mov rax,rdx; ret`), so this predates #425. Calls make it common in unoptimised MSVC output. - **Fix sent as #437** (every guest read through the existing `load_always`). With that one-line change applied to a copy of your #430 tree, all 9 agree: 159 of 160 with no mismatch. - **The one not executed** is a loop whose trip count is its argument; the original timed out on the first inputs, which were pointers. A subagent of mine checked it separately on 2,061 small counts with no difference; I did not rerun that check myself. - **What this does not show:** no measured function contains a tail jump, so that path was not executed; a few functions completed only 1 to 5 inputs on both sides; the callee's writes to stack memory are not compared; Wine only. - **Self-test of the tool,** run by the subagent and not rerun by me: with argument registers swapped at call sites, or the callee's result ignored, 115 and 85 of the 160 functions mismatch. _AI-assisted (Claude Code)._
grahamster2 commented 2026-11-24 06:48:20 +00:00 (Migrated from github.com)

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 165a66f and b9935bc, 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: ec499fc was 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 /Od functions are still useful as candidates: with a manifest from source labels they're what the binding path would cover.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:comment:7073206450 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/425#issuecomment-7073206450). Originally posted by [grahamster2](https://github.com/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 `165a66f` and `b9935bc`, 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: `ec499fc` was 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 `/Od` functions are still useful as candidates: with a manifest from source labels they're what the binding path would cover.
grahamster2 commented 2026-11-24 06:48:21 +00:00 (Migrated from github.com)

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.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:comment:7073447480 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/425#issuecomment-7073447480). Originally posted by [grahamster2](https://github.com/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.
R0Y00 commented 2026-11-24 06:48:22 +00:00 (Migrated from github.com)

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 d470872 passed 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.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:comment:7073539393 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/425#issuecomment-7073539393). Originally posted by [R0Y00](https://github.com/R0Y00) on 2026-11-22T03:26:42Z. --- @grahamster2 I integrated current main while preserving both sides of the documentation conflicts. Locally d470872 passed 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.
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!419
No description provided.