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

Merged
bosseurdinternet merged 3 commits from upstream-pr/343 into main 2026-11-22 08:25:38 +00:00
bosseurdinternet commented 2026-11-21 22:46:28 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by bosseurdinternet on 2026-11-21T22:46:28Z.


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

<!-- greyfinch-sync:R0Y00/Xbox2Windows:issue:6535393553 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/343). Originally posted by [bosseurdinternet](https://github.com/bosseurdinternet) on 2026-11-21T22:46:28Z. --- ## 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)
upstream-sync closed this pull request 2026-11-24 06:42:32 +00:00
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!338
No description provided.