cpu: translate ordinary calls of a bound import #566

Merged
bosseurdinternet merged 2 commits from upstream-pr/572 into main 2026-11-22 14:29:40 +00:00
bosseurdinternet commented 2026-11-22 13:14:15 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by bosseurdinternet on 2026-11-22T13:14:15Z.


Change

Translate ordinary calls of the import bound with --bind in tools/cpu_switch_aot.py. Until now the explicit binding resolved only jmp qword ptr [rip+slot] (#343); calls of an import were translated only when a whole-program tool passed its own imports table.

Forms now resolved through the binding, each only when its slot is an IAT slot of the ordinary import directory and holds the bound import (resolve_import, as for tail jumps), one slot per run, callees included:

  1. call qword ptr [rip+slot];
  2. a direct CALL of the linker's import thunk (FF 25 disp32, as Function.thunk_slot recognises it, outside every unwind interval and bound range);
  3. a direct JMP to such a thunk, as the bound tail jump through the thunk's slot.

The call is a host call under #425's rules, with no new model: RSP 8 mod 16 with the home space below the return address; each register of the binding's argument count fully defined; afterwards RAX holds the binding's guaranteed bytes, RCX, RDX, R8 to R11, the flags and the 32 bytes at RSP are undefined. Nonvolatile registers and the rest of the frame are kept, which is a term of the binding: the translator cannot check a provider.

The slot read (#437's rule). The original reads the slot; the translation calls the named import and does not read it. The resolver therefore requires the slot to be 8 bytes of one section with IMAGE_SCN_MEM_READ, so the original's read cannot fault while the image's code runs. This check sits in the shared resolver, so it also applies to #343's tail jumps: that is the only behaviour change to the existing tail path. A slot changed at run time (a hook) is not followed; the module docstring says so.

Program.tail_resolver is renamed import_resolver. One control of cpu_aot_import_tail_jump expects the new text "no call or tail jump uses it"; it rejects the same case as before. Follows #343 (40fb465) and #425. Refs #317.

Verification

Commit 33c863a on main at d896867, MSVC 14.52 cl/link/ml64 under Wine. Not run locally: Windows itself, so the CI is the first native run.

  • New test cpu_aot_import_calls (fixture tests/cpu/import_calls.asm, linked against the named and the altered provider of cpu_aot_import_tail_jump): passes.
    • Through the register probe, 8 exports match a Python reference on 249 input pairs each, on the whole return value, the guarded trace, RBX and RSI: natively and translated, for both providers, and for a separate cl /O2 build of the named translations.
    • Through guarded_call, 9 fault cases give the same exception code, return value and trace for the original and both builds of the translation: a guest read that faults before the call, one that faults after the call has written the trace, and a fault inside the import.
    • 34 bindings or patched copies are rejected, one printed line each (no binding, wrong import in the slot, second slot, unused binding, unreadable IAT section, each volatile register and the flags read after the call, RAX beyond the guaranteed bytes, undefined arguments, a guest address as argument, a value spilled into the home space, misaligned RSP, three retargeted thunk calls, a range-bound callee that calls the import).
  • Mutants (not committed): 13 changes to the translator each make the new test fail; 8 are caught by a control that the mutant wrongly accepts (volatile registers kept, RAX fully defined regardless of the binding, home space kept, slot not checked, argument definedness not required, readable-slot check removed, alignment not checked, table constants kept across the call). The other 5 are still refused by another rule and are caught by the changed message only (flags kept; second-slot check removed; IAT-membership check removed in three places).
  • Other cpu_* tests: same results as main. Under Wine these fail on main too: cpu_aot_import_call, cpu_aot_ordinal_import, cpu_native_reloc_free, cpu_native_tls, cpu_aot_mul_div (Wine reports the divide overflow with another code), cpu_native_exe_start (intermittent timeout, #281). Not built locally for lack of tools: cpu_native_archive_reader, cpu_native_archive_blast, cpu_native_shim_startup, cpu_shim_compute_warp, cpu_shim_compute_hardware. tests/cpu/archive/translate_blast.py, which subclasses Lifter, was run by hand on the same input with both trees: identical output.
  • Corpus (the repository's desktop C++ sources and one extra file, MSVC /O2, /O1, /Od), no binding, against main: the same 655, 651 and 400 functions translate, no regression, 1,706 translations identical, identical prelude. Refusal messages change for 143, 150 and 98 functions, all to "no binding ...".

Evidence and follow-up

  • tests/cpu/import_calls.asm is original MASM assembled in the build; no binaries were added.
  • What a binding could reach. Binding every reached slot to the import listed there, without a provider check and allowing several slots, gives an upper bound of 77, 69 and 13 more corpus functions with three import arguments (almost all through one slot; mostly memset, free, memcpy, memcmp, strlen). As generated, only 11, 11 and 5 of them compile: a C runtime name declared with word parameters clashes with the prelude's headers (C2733). Declaring the import library's slot pointer instead (extern "C" word (*__imp_memset)(word, word, word);) compiled and linked all of them in a trial; that is not in this PR.
  • For your decision: the 66h prefix. main accepts 66 FF 25 as the bound tail jump, and this PR accepts 66 FF 15 the same way, for consistency. Intel processors ignore the prefix on a near branch in 64-bit mode; AMD processors use a 16-bit operand (iced decodes the patched bytes as call qword ptr with Intel rules and call word ptr with AMD rules, and the translator accepts them). If you want these refused, I would do it in a separate PR for all near-branch forms.
  • Follow-ups, not started: several bindings per run; the __imp_ declaration.
  • Jump-table translator notes

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).

🤖 Generated with Claude Code

<!-- greyfinch-sync:R0Y00/Xbox2Windows:issue:6542815441 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/572). Originally posted by [bosseurdinternet](https://github.com/bosseurdinternet) on 2026-11-22T13:14:15Z. --- ## Change Translate ordinary calls of the import bound with `--bind` in `tools/cpu_switch_aot.py`. Until now the explicit binding resolved only `jmp qword ptr [rip+slot]` (#343); calls of an import were translated only when a whole-program tool passed its own `imports` table. Forms now resolved through the binding, each only when its slot is an IAT slot of the ordinary import directory and holds the bound import (`resolve_import`, as for tail jumps), one slot per run, callees included: 1. `call qword ptr [rip+slot]`; 2. a direct `CALL` of the linker's import thunk (`FF 25 disp32`, as `Function.thunk_slot` recognises it, outside every unwind interval and bound range); 3. a direct `JMP` to such a thunk, as the bound tail jump through the thunk's slot. The call is a host call under #425's rules, with no new model: RSP 8 mod 16 with the home space below the return address; each register of the binding's argument count fully defined; afterwards RAX holds the binding's guaranteed bytes, RCX, RDX, R8 to R11, the flags and the 32 bytes at RSP are undefined. Nonvolatile registers and the rest of the frame are kept, which is a term of the binding: the translator cannot check a provider. **The slot read (#437's rule).** The original reads the slot; the translation calls the named import and does not read it. The resolver therefore requires the slot to be 8 bytes of one section with `IMAGE_SCN_MEM_READ`, so the original's read cannot fault while the image's code runs. This check sits in the shared resolver, so it also applies to #343's tail jumps: that is the only behaviour change to the existing tail path. A slot changed at run time (a hook) is not followed; the module docstring says so. `Program.tail_resolver` is renamed `import_resolver`. One control of `cpu_aot_import_tail_jump` expects the new text "no call or tail jump uses it"; it rejects the same case as before. Follows #343 (`40fb465`) and #425. Refs #317. ## Verification Commit `33c863a` on `main` at `d896867`, MSVC 14.52 `cl`/`link`/`ml64` under Wine. Not run locally: Windows itself, so the CI is the first native run. - **New test `cpu_aot_import_calls`** (fixture `tests/cpu/import_calls.asm`, linked against the named and the altered provider of `cpu_aot_import_tail_jump`): passes. - Through the register probe, 8 exports match a Python reference on 249 input pairs each, on the whole return value, the guarded trace, RBX and RSI: natively and translated, for both providers, and for a separate `cl /O2` build of the named translations. - Through `guarded_call`, 9 fault cases give the same exception code, return value and trace for the original and both builds of the translation: a guest read that faults before the call, one that faults after the call has written the trace, and a fault inside the import. - 34 bindings or patched copies are rejected, one printed line each (no binding, wrong import in the slot, second slot, unused binding, unreadable IAT section, each volatile register and the flags read after the call, RAX beyond the guaranteed bytes, undefined arguments, a guest address as argument, a value spilled into the home space, misaligned RSP, three retargeted thunk calls, a range-bound callee that calls the import). - **Mutants** (not committed): 13 changes to the translator each make the new test fail; 8 are caught by a control that the mutant wrongly accepts (volatile registers kept, RAX fully defined regardless of the binding, home space kept, slot not checked, argument definedness not required, readable-slot check removed, alignment not checked, table constants kept across the call). The other 5 are still refused by another rule and are caught by the changed message only (flags kept; second-slot check removed; IAT-membership check removed in three places). - **Other `cpu_*` tests**: same results as `main`. Under Wine these fail on `main` too: `cpu_aot_import_call`, `cpu_aot_ordinal_import`, `cpu_native_reloc_free`, `cpu_native_tls`, `cpu_aot_mul_div` (Wine reports the divide overflow with another code), `cpu_native_exe_start` (intermittent timeout, #281). Not built locally for lack of tools: `cpu_native_archive_reader`, `cpu_native_archive_blast`, `cpu_native_shim_startup`, `cpu_shim_compute_warp`, `cpu_shim_compute_hardware`. `tests/cpu/archive/translate_blast.py`, which subclasses `Lifter`, was run by hand on the same input with both trees: identical output. - **Corpus** (the repository's desktop C++ sources and one extra file, MSVC `/O2`, `/O1`, `/Od`), no binding, against `main`: the same 655, 651 and 400 functions translate, no regression, 1,706 translations identical, identical prelude. Refusal messages change for 143, 150 and 98 functions, all to "no binding ...". ## Evidence and follow-up - `tests/cpu/import_calls.asm` is original MASM assembled in the build; no binaries were added. - **What a binding could reach.** Binding every reached slot to the import listed there, without a provider check and allowing several slots, gives an upper bound of 77, 69 and 13 more corpus functions with three import arguments (almost all through one slot; mostly `memset`, `free`, `memcpy`, `memcmp`, `strlen`). As generated, only 11, 11 and 5 of them compile: a C runtime name declared with `word` parameters clashes with the prelude's headers (C2733). Declaring the import library's slot pointer instead (`extern "C" word (*__imp_memset)(word, word, word);`) compiled and linked all of them in a trial; that is not in this PR. - **For your decision: the 66h prefix.** `main` accepts `66 FF 25` as the bound tail jump, and this PR accepts `66 FF 15` the same way, for consistency. Intel processors ignore the prefix on a near branch in 64-bit mode; AMD processors use a 16-bit operand (iced decodes the patched bytes as `call qword ptr` with Intel rules and `call word ptr` with AMD rules, and the translator accepts them). If you want these refused, I would do it in a separate PR for all near-branch forms. - Follow-ups, not started: several bindings per run; the `__imp_` declaration. - [Jump-table translator notes](https://github.com/R0Y00/Xbox2Windows/blob/main/docs/experiments.md#switch-dispatch-through-jump-tables) ## 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)._ 🤖 Generated with [Claude Code](https://claude.com/claude-code)
upstream-sync closed this pull request 2026-11-24 06:43:45 +00:00
R0Y00 commented 2026-11-24 06:48:33 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by R0Y00 on 2026-11-22T14:25:43Z.


I updated the explicit binding documentation and CLI at 7da8d9b. Throughout execution the image and bound target must stay valid, and the original IAT slot must remain readable and hold the declared target. The generated call directly uses that binding. I also kept thunk probing off the old direct path when there is no import table or supplied import map. On Windows x64, ctest --preset dev --no-tests=error -R '^cpu_aot_(import_calls|import_tail_jump|direct_calls|import_call|direct_call|call_entries)$' passed 6/6, including the existing wrong-slot and missing-binding controls.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:comment:7080536636 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/572#issuecomment-7080536636). Originally posted by [R0Y00](https://github.com/R0Y00) on 2026-11-22T14:25:43Z. --- I updated the explicit binding documentation and CLI at `7da8d9b`. Throughout execution the image and bound target must stay valid, and the original IAT slot must remain readable and hold the declared target. The generated call directly uses that binding. I also kept thunk probing off the old direct path when there is no import table or supplied import map. On Windows x64, `ctest --preset dev --no-tests=error -R '^cpu_aot_(import_calls|import_tail_jump|direct_calls|import_call|direct_call|call_entries)$'` passed 6/6, including the existing wrong-slot and missing-binding controls.
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!566
No description provided.