cpu: translate import tail jumps through an explicit binding #343

Manually merged
github merged 3 commits from gh-pr/343/head into gh-pr/343/base 2026-11-26 21:44:31 +00:00
Owner

@⁠bosseurdinternet on GitHub · 2026-11-21 22:46 UTC · original
bosseurdinternet:cpu/import-tail-jump → main · +483 −15 · 9 files · 3 commits · merged 2026-11-22 08:25 UTC

Change

Refs #317 (first step: the IAT tail jump). Builds on #309 and #285 (both merged).

MSVC ends a function whose last action is a call to an imported function with jmp qword ptr [rip+slot]. tools/cpu_switch_aot.py refused it as unsupported control flow. It is now translated, and only through an explicit binding.

  • Binding. --bind PROVIDER SYMBOL, where SYMBOL is an export name or #ordinal (with --link-name for an ordinal), plus two numbers the binding must state: --import-arguments 3|4, the integer arguments the import takes, and --return-bytes 0|1|2|8, the guaranteed low RAX bytes of its return (0 is a normal return with no guaranteed result bits). The slot is checked with cpu_call_aot.resolve_import. cpu_translation() forwards the same values.
  • Translation. The jump becomes return import(...); with exactly the bound number of arguments, and the import is declared once with that many word parameters. Every register it takes must be fully defined at the jump and hold no guest address. The export's own count (name or name:4) and the import's count are independent: a three-argument export can pass a fourth argument only after writing R9 itself. The path ends at the jump, so no register or flag clobber after the call is modelled.
  • Return width. A tail jump contributes the binding's guaranteed bytes to Function.return_width as a RET contributes its own, and the width is the minimum over both. It is None only when no normal return of either kind is reachable. For a function with a tail jump the generated comment and the CLI state both parts, for example 2 low byte(s) at every reachable normal return: 2 defined at every reachable RET, 8 guaranteed after every tail jump to mix4 as the binding states.
  • What the numbers are. Both are choices of whoever invokes the translator. Neither is derived from the machine code or read from the provider; the generated declaration says so.
  • Accepted forms. FF 25 and 48 FF 25 with a RIP base, no index and no segment prefix. The far form (FF 2D), the EIP-relative form (67) and a segment-prefixed form stay rejected.
  • Not covered. Ordinary calls through the import table, direct jumps to other functions, more than one bound slot per run, stack and floating-point arguments.

Without a binding nothing changes: the same functions translate and their generated C++ is identical to main's.

Verification

Local runs used the same compiler build as CI (MSVC 14.52.37025: cl, link, ml64) under Wine 11.0 on Linux, at 3272929 on main b860dd5. I reran the items below on this head unless a line says otherwise.

  • ctest --no-tests=error -R '^cpu_aot_import_tail_jump$' -V: passed. Three callers (named imports, an altered provider, ordinal-only imports), each translated twice: tail_mix bound to a three-argument import, and tail_choose,tail_pass4:4 bound to a four-argument import. 5,976 calls per side match a Python reference on return value, guarded trace, RBX and RSI, 5,589 of them through a tail jump. Three-argument entries are called through probe and tail_pass4:4 through probe4.
  • Combinations covered: three-argument entry with a three-argument binding; four-argument entry passing on its incoming R9 to a four-argument binding; three-argument entry that writes R9 itself, with a four-argument binding; a function with both a RET (AX defined) and a tail jump.
  • Return width, inspected: with 8, 1 and 0 stated bytes, tail_choose reports 2, 1 and 0; tail_mix, whose only exit is the tail jump, reports the stated number.
  • Altered provider: its translations differ from the first only in the return value, on 249 of 249, 120 of 249 and 1,494 of 1,494 calls; trace, RBX and RSI are identical.
  • Thirteen rejection controls, printed one line each: no binding; wrong symbol; wrong provider file; a named binding for an ordinal-only import; the displacement patched to the import table's terminator; FF 2D; 67 FF 25; 64 FF 25; a guest address in RCX; a four-argument binding with R9 not written; R9 only partly written; a second slot; a binding that no tail jump uses. All run inside the translator; none asserts a Windows loader error code.
  • Existing tests: cpu_aot_jump_tables, cpu_aot_entry_order, cpu_aot_narrow_returns and cpu_aot_fourth_argument pass. Other cpu_* tests: same results as main. Under Wine, cpu_aot_import_call, cpu_aot_ordinal_import, cpu_native_reloc_free and cpu_native_tls fail on main too because Wine's loader differs from Windows, and cpu_native_exe_start times out intermittently (#281). The Windows job is the reference for those.
  • Corpus without a binding, against main, with no allowed difference: the same 642, 639 and 15 functions translate at /O2, /O1 and /Od, and all 1,296 have identical C++.
  • Manual mutant, not in CI: with the slot check removed, tail_mix bound to the wrong symbol is accepted, which the wrong-symbol control rejects.
  • Run during development, not rerun on this head:
    • an upper bound from a scratch script that binds every corpus tail slot to the import it names, with 8 return bytes: 45 more functions translate at /O2 and 32 at /O1. Only 9 and 7 of them could be built today, because the others jump to free, memset, strlen, memcmp, memcpy or _aligned_free, whose redeclaration MSVC rejects (C2733) since the prelude's headers already declare them. The bound assumes a correct binding for every slot;
    • differential execution of the functions whose import has no effect outside the test arena (memset, memcpy, memcmp, strlen, one notify function), each tail bound to a forwarder that reaches the original image's import: 21 of 45 at /O2 and 22 of 32 at /O1 ran, with no mismatch. This is evidence on a few inputs per function, not a proof of equivalence;
    • 16 further mutants (the call replaced by return rax;, R9 not forwarded, return bytes ignored, the forwarded registers not required to be defined, each rejected form accepted, and others), all failing the test or the build. One survives: removing the check for a second slot across the exports of one run, reachable only if two import slots name the same import.
  • pyflakes, tests/docs_links_check.py and tests/form_coverage_check.py passed.

Not run locally: the D3D12 tests and anything that needs Windows loader behaviour.

Evidence and follow-up

  • tests/cpu/tail_import.asm and tests/cpu/tail_provider.cpp are original sources built in the build. No binaries were added. tail_import is classified in tools/form_coverage.py.
  • The fixture's providers are ours, so their argument counts and return bytes are known from source. The test does not show how to establish those numbers for a provider without source.
  • Points where I had to choose, for your review:
    • an import with one or two arguments (free, strlen) can only be bound by stating three, because a binding accepts 3 or 4. Passing extra register arguments is harmless under the x64 convention, but the stated number is then larger than the real one. Allowing 1 and 2 is a small change if you prefer it;
    • one binding covers all exports of one run, since the CLI takes one --bind for the whole list;
    • for a function with a tail jump the width comment is always emitted, also at 8, because the number depends on the binding;
    • the bound import is called from the translated function, so it sees that function's frame and return address, where the original jump leaves no frame. An import that inspects its caller, and unwinding through that frame, are untested.
  • Limits of the corpus numbers: 831 of the 1,119 functions are MSVC STL instantiations, and this is desktop code built from this repository, not a title.
  • Follow-ups: C runtime names in the prelude; call qword ptr [rip+slot] with volatile-register and flag clobbers, as #317 describes.
  • Jump-table translator notes

AI-assisted (Claude Code).

🤖 Generated with Claude Code

**@⁠bosseurdinternet** on GitHub · 2026-11-21 22:46 UTC · [original](https://github.com/R0Y00/Xbox2Windows/pull/343) `bosseurdinternet:cpu/import-tail-jump` → `main` · +483 −15 · 9 files · 3 commits · merged 2026-11-22 08:25 UTC ## Change Refs #317 (first step: the IAT tail jump). Builds on #309 and #285 (both merged). MSVC ends a function whose last action is a call to an imported function with `jmp qword ptr [rip+slot]`. `tools/cpu_switch_aot.py` refused it as unsupported control flow. It is now translated, and only through an explicit binding. - **Binding.** `--bind PROVIDER SYMBOL`, where `SYMBOL` is an export name or `#ordinal` (with `--link-name` for an ordinal), plus two numbers the binding must state: `--import-arguments 3|4`, the integer arguments the import takes, and `--return-bytes 0|1|2|8`, the guaranteed low RAX bytes of its return (0 is a normal return with no guaranteed result bits). The slot is checked with `cpu_call_aot.resolve_import`. `cpu_translation()` forwards the same values. - **Translation.** The jump becomes `return import(...);` with exactly the bound number of arguments, and the import is declared once with that many `word` parameters. Every register it takes must be fully defined at the jump and hold no guest address. The export's own count (`name` or `name:4`) and the import's count are independent: a three-argument export can pass a fourth argument only after writing R9 itself. The path ends at the jump, so no register or flag clobber after the call is modelled. - **Return width.** A tail jump contributes the binding's guaranteed bytes to `Function.return_width` as a `RET` contributes its own, and the width is the minimum over both. It is `None` only when no normal return of either kind is reachable. For a function with a tail jump the generated comment and the CLI state both parts, for example `2 low byte(s) at every reachable normal return: 2 defined at every reachable RET, 8 guaranteed after every tail jump to mix4 as the binding states`. - **What the numbers are.** Both are choices of whoever invokes the translator. Neither is derived from the machine code or read from the provider; the generated declaration says so. - **Accepted forms.** `FF 25` and `48 FF 25` with a RIP base, no index and no segment prefix. The far form (`FF 2D`), the EIP-relative form (`67`) and a segment-prefixed form stay rejected. - **Not covered.** Ordinary calls through the import table, direct jumps to other functions, more than one bound slot per run, stack and floating-point arguments. Without a binding nothing changes: the same functions translate and their generated C++ is identical to main's. ## Verification Local runs used the same compiler build as CI (MSVC 14.52.37025: `cl`, `link`, `ml64`) under Wine 11.0 on Linux, at `3272929` on main `b860dd5`. I reran the items below on this head unless a line says otherwise. - **`ctest --no-tests=error -R '^cpu_aot_import_tail_jump$' -V`: passed.** Three callers (named imports, an altered provider, ordinal-only imports), each translated twice: `tail_mix` bound to a three-argument import, and `tail_choose,tail_pass4:4` bound to a four-argument import. 5,976 calls per side match a Python reference on return value, guarded trace, RBX and RSI, 5,589 of them through a tail jump. Three-argument entries are called through `probe` and `tail_pass4:4` through `probe4`. - **Combinations covered:** three-argument entry with a three-argument binding; four-argument entry passing on its incoming R9 to a four-argument binding; three-argument entry that writes R9 itself, with a four-argument binding; a function with both a `RET` (AX defined) and a tail jump. - **Return width, inspected:** with 8, 1 and 0 stated bytes, `tail_choose` reports 2, 1 and 0; `tail_mix`, whose only exit is the tail jump, reports the stated number. - **Altered provider:** its translations differ from the first only in the return value, on 249 of 249, 120 of 249 and 1,494 of 1,494 calls; trace, RBX and RSI are identical. - **Thirteen rejection controls, printed one line each:** no binding; wrong symbol; wrong provider file; a named binding for an ordinal-only import; the displacement patched to the import table's terminator; `FF 2D`; `67 FF 25`; `64 FF 25`; a guest address in RCX; a four-argument binding with R9 not written; R9 only partly written; a second slot; a binding that no tail jump uses. All run inside the translator; none asserts a Windows loader error code. - **Existing tests:** `cpu_aot_jump_tables`, `cpu_aot_entry_order`, `cpu_aot_narrow_returns` and `cpu_aot_fourth_argument` pass. Other `cpu_*` tests: same results as main. Under Wine, `cpu_aot_import_call`, `cpu_aot_ordinal_import`, `cpu_native_reloc_free` and `cpu_native_tls` fail on main too because Wine's loader differs from Windows, and `cpu_native_exe_start` times out intermittently (#281). The Windows job is the reference for those. - **Corpus without a binding, against main, with no allowed difference:** the same 642, 639 and 15 functions translate at `/O2`, `/O1` and `/Od`, and all 1,296 have identical C++. - **Manual mutant, not in CI:** with the slot check removed, `tail_mix` bound to the wrong symbol is accepted, which the wrong-symbol control rejects. - **Run during development, not rerun on this head:** - an upper bound from a scratch script that binds every corpus tail slot to the import it names, with 8 return bytes: 45 more functions translate at `/O2` and 32 at `/O1`. Only 9 and 7 of them could be built today, because the others jump to `free`, `memset`, `strlen`, `memcmp`, `memcpy` or `_aligned_free`, whose redeclaration MSVC rejects (C2733) since the prelude's headers already declare them. The bound assumes a correct binding for every slot; - differential execution of the functions whose import has no effect outside the test arena (`memset`, `memcpy`, `memcmp`, `strlen`, one notify function), each tail bound to a forwarder that reaches the original image's import: 21 of 45 at `/O2` and 22 of 32 at `/O1` ran, with no mismatch. This is evidence on a few inputs per function, not a proof of equivalence; - 16 further mutants (the call replaced by `return rax;`, R9 not forwarded, return bytes ignored, the forwarded registers not required to be defined, each rejected form accepted, and others), all failing the test or the build. One survives: removing the check for a second slot across the exports of one run, reachable only if two import slots name the same import. - `pyflakes`, `tests/docs_links_check.py` and `tests/form_coverage_check.py` passed. Not run locally: the D3D12 tests and anything that needs Windows loader behaviour. ## Evidence and follow-up - `tests/cpu/tail_import.asm` and `tests/cpu/tail_provider.cpp` are original sources built in the build. No binaries were added. `tail_import` is classified in `tools/form_coverage.py`. - The fixture's providers are ours, so their argument counts and return bytes are known from source. The test does not show how to establish those numbers for a provider without source. - **Points where I had to choose, for your review:** - an import with one or two arguments (`free`, `strlen`) can only be bound by stating three, because a binding accepts 3 or 4. Passing extra register arguments is harmless under the x64 convention, but the stated number is then larger than the real one. Allowing 1 and 2 is a small change if you prefer it; - one binding covers all exports of one run, since the CLI takes one `--bind` for the whole list; - for a function with a tail jump the width comment is always emitted, also at 8, because the number depends on the binding; - the bound import is called from the translated function, so it sees that function's frame and return address, where the original jump leaves no frame. An import that inspects its caller, and unwinding through that frame, are untested. - Limits of the corpus numbers: 831 of the 1,119 functions are MSVC STL instantiations, and this is desktop code built from this repository, not a title. - Follow-ups: C runtime names in the prelude; `call qword ptr [rip+slot]` with volatile-register and flag clobbers, as #317 describes. - [Jump-table translator notes](https://github.com/R0Y00/Xbox2Windows/blob/main/docs/experiments.md#switch-dispatch-through-jump-tables) _AI-assisted (Claude Code)._ 🤖 Generated with [Claude Code](https://claude.com/claude-code)
MSVC ends a function whose last action is a call to an imported function
with jmp qword ptr [rip+slot], a tail jump through the import address
table. tools/cpu_switch_aot.py refused it as unsupported control flow.

The decoder now accepts an indirect JMP through a RIP-relative 64-bit
memory operand without a segment prefix (FF 25, or 48 FF 25 with REX.W),
and the jump ends the path; the 67h (EIP-relative) form, a segment prefix
and a far jump stay rejected. The jump is translated only through an
explicit binding in the form tools/cpu_call_aot.py uses, checked with its
resolve_import: --bind PROVIDER_DLL SYMBOL names the provider and the
slot's import by name or by #ordinal (with --link-name), --import-arguments
N the integer arguments the import takes (3 or 4), and --return-bytes N the
number of guaranteed low RAX bytes of its return (0, 1, 2 or 8; 0 is a
normal return with no guaranteed result bits). Like the entry's own count
(name:4), these are chosen by whoever invokes the translator and are not
derived from the machine code or the provider; the entry's count and the
import's count are separate. The jump becomes "return import(...);"
passing RCX, RDX, R8 and, for four, R9, after a dllimport declaration with
as many word parameters. Each passed register must be fully defined at the
jump and hold no guest address, so a three-argument entry can call a
four-argument import only after writing R9 itself. A tail jump without a
binding, a second IAT slot, a binding that no tail jump uses and a slot
that is not the bound import are rejected. The path ends at the jump, so
no clobber after the call is modelled.

A tail jump is a normal return of the function: it contributes the stated
number to the return width, which stays the conservative minimum over the
reachable normal returns and is None only when neither a RET nor a tail
jump is reachable. For a function with a tail jump the generated comment
and the CLI always state the width with both of its sources, and the
import's declaration is preceded by a comment saying that both numbers are
stated by the binding. Output for functions without a tail jump is
unchanged.

cpu_translation forwards IMPORT_ARGUMENTS and RETURN_BYTES, depends on
cpu_call_aot.py, and takes an optional NAME so that one image can be
translated with two bindings. cpu_aot_import_tail_jump builds
tail_import.asm against a named, an altered-return and an ordinal-only
provider of mix3 (three arguments) and mix4 (four). tail_mix (three
arguments) is bound to mix3. tail_choose (three arguments, writes R9 before
its jump, and has a RET that defines only AX) and tail_pass4:4 (passes on
its incoming R9 through a REX.W jump) are bound to mix4. Native and
translated calls match a Python reference over the direct-call input pairs,
through probe for the three-argument entries and through probe4 with six
fourth arguments for tail_pass4, on the return value (whole after a tail
jump, two bytes after the RET), the guarded trace, RBX and RSI. The
translations bound to the altered provider differ from the named ones only
in the return. The test checks the encoding and slot of every tail jump,
that the built sources are the translations of the listed entries, the
argument counts of each declaration, call and signature, and the widths
with 8, 2, 1 and 0 stated bytes, and rejects thirteen bindings or patched
copies inside the translator.

Refs #317

Co-Authored-By: Claude Opus 5.5 <[email protected]>
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 21:44:30 +00:00
github manually merged commit ce75d1d710 into gh-pr/343/base 2026-11-26 21:44:31 +00:00
This discussion has been locked. Commenting is limited to contributors.
No description provided.