cpu: translate import tail jumps through an explicit binding #338
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/343"
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 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.pyrefused it as unsupported control flow. It is now translated, and only through an explicit binding.--bind PROVIDER SYMBOL, whereSYMBOLis an export name or#ordinal(with--link-namefor 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 withcpu_call_aot.resolve_import.cpu_translation()forwards the same values.return import(...);with exactly the bound number of arguments, and the import is declared once with that manywordparameters. Every register it takes must be fully defined at the jump and hold no guest address. The export's own count (nameorname: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.Function.return_widthas aRETcontributes its own, and the width is the minimum over both. It isNoneonly 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 example2 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.FF 25and48 FF 25with 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.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, at3272929on mainb860dd5. 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_mixbound to a three-argument import, andtail_choose,tail_pass4:4bound 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 throughprobeandtail_pass4:4throughprobe4.RET(AX defined) and a tail jump.tail_choosereports 2, 1 and 0;tail_mix, whose only exit is the tail jump, reports the stated number.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.cpu_aot_jump_tables,cpu_aot_entry_order,cpu_aot_narrow_returnsandcpu_aot_fourth_argumentpass. Othercpu_*tests: same results as main. Under Wine,cpu_aot_import_call,cpu_aot_ordinal_import,cpu_native_reloc_freeandcpu_native_tlsfail on main too because Wine's loader differs from Windows, andcpu_native_exe_starttimes out intermittently (#281). The Windows job is the reference for those./O2,/O1and/Od, and all 1,296 have identical C++.tail_mixbound to the wrong symbol is accepted, which the wrong-symbol control rejects./O2and 32 at/O1. Only 9 and 7 of them could be built today, because the others jump tofree,memset,strlen,memcmp,memcpyor_aligned_free, whose redeclaration MSVC rejects (C2733) since the prelude's headers already declare them. The bound assumes a correct binding for every slot;memset,memcpy,memcmp,strlen, one notify function), each tail bound to a forwarder that reaches the original image's import: 21 of 45 at/O2and 22 of 32 at/O1ran, with no mismatch. This is evidence on a few inputs per function, not a proof of equivalence;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.pyandtests/form_coverage_check.pypassed.Not run locally: the D3D12 tests and anything that needs Windows loader behaviour.
Evidence and follow-up
tests/cpu/tail_import.asmandtests/cpu/tail_provider.cppare original sources built in the build. No binaries were added.tail_importis classified intools/form_coverage.py.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;--bindfor the whole list;call qword ptr [rip+slot]with volatile-register and flag clobbers, as #317 describes.AI-assisted (Claude Code).
🤖 Generated with Claude Code