cpu: accept narrow register writes and return widths in the jump-table translator #281

Merged
bosseurdinternet merged 2 commits from upstream-pr/285 into main 2026-11-21 21:25:37 +00:00
bosseurdinternet commented 2026-11-21 20:42:16 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by bosseurdinternet on 2026-11-21T20:42:16Z.


Change

Fixes #276. tools/cpu_switch_aot.py accepted only 32- and 64-bit register writes and required RAX to be fully defined at every RET. It refused functions that write no RAX byte before RET, functions that write only AL (mov al, 1; ret and cmp; setcc al; ret), ret 0 (C2 00 00) and every other 8- or 16-bit register write.

  • Definedness by byte. The must-defined pass records, per register, how many consecutive low bytes every path defines (0, 1, 2 or 8) and whether the register may hold a guest address. A read needs as many defined bytes as it uses. Joins take the lower bound.
  • Narrow writes. An 8- or 16-bit write merges into the 64-bit variable and keeps the other bytes. It raises the defined count to its own width at most, so it never makes a wider read legal. AH, BH, CH and DH stay rejected. A 16-bit CMOVcc stays rejected.
  • Return width. RET and ret 0 no longer read RAX; any other ret N stays rejected. Function.return_width is the number of consecutive low RAX bytes definitely defined at every reachable RET or ret 0: 0, 1, 2 or 8, the lower bound over joins and over returns. It counts defined bytes and does not establish a source-language type. The generated ABI stays word; callers need no width declaration and may use only those bytes. The upper bytes the generator initializes carry no guest result.
  • No reachable normal return. When no RET is reachable, return_width is None, and the CLI and the generated source say no reachable normal return. This describes the reachable control flow and makes no termination claim.
  • Guest addresses. A RET whose RAX may hold a guest address is rejected.
  • SETcc (from #173). A SETcc into a byte register is such a narrow write and no longer needs the rest of the register to be defined. After setl al only AL is defined, and a later wider read is still rejected.
  • 16-bit IMUL multiplies in std::uint32_t: two uint16_t factors are promoted to int, where 0xffff * 0xffff overflows.

Generated C++ for functions that already translate is unchanged, with one exception: a SETcc into a register casts its 0/1 condition to std::uint8_t instead of word.

Verification

Local runs used the same compiler build as CI (MSVC 14.52.37025: cl, link, ml64) under Wine 11.0 on Linux, at b252f57 on main d311159, which includes #282. I reran every item below on this head.

  • ctest --no-tests=error -R '^(cpu_aot_narrow_returns|cpu_aot_jump_tables)$' -V: both ran and passed, and so did cpu_aot_entry_order from #282 (both fixture registrations are kept).

  • cpu_aot_narrow_returns. Twelve called exports, 3,819 inputs, each original and translated export compared with a Python reference through the register probe on the defined RAX bytes, the guarded trace, RBX and RSI. Widths covered: 0 (two exports, one ending in ret 0), 1 (AL on every path; AL after SETcc; EAX on one exit and AL on the other), 2 (AX) and 8. For widths below 8 it prints that the original left the caller's RAX in the remaining bytes (249 of 249 calls; 146 of 249 for the export with two different exits).

  • Inspected, never called: self_loop, a loop that branches to itself, reports return_width None, no return statement and no reachable normal return. The storing export with a reachable RET reports width 0, one return statement and 0 low byte(s) are defined at every reachable RET.

  • Seven rejection controls, printed by the test: mov ah, dl; a wide read after a narrow write (twice, once after SETcc); ret 8; a join of a 32-bit and an 8-bit definition followed by a 32-bit read; a 16-bit CMOVcc; a returned guest address.

  • 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. The Windows job is the reference for those.

  • Corpus. 1,119 functions compiled with MSVC /O2 from this repository's C++ sources plus one plain C++ file, and the /O1 and /Od builds of the same sources, translated with main and with this branch:

    Level main this branch non-STL
    /O2 510 642 73 → 110
    /O1 486 639 68 → 111
    /Od 8 15 5 → 5

    No function is lost. Of 1,004 functions translated by both, 991 have identical C++ and 13 differ only in the SETcc cast above. Of the 132 gained at /O2, 68 have two instructions or fewer (52 are an empty function ending in ret 0), and 64 are longer.

  • Differential execution (each function compiled with cl and called beside the original on 64 inputs, comparing the defined RAX bytes and a seeded memory arena): the 132 functions gained at /O2 and the 153 gained at /O1 match on every input, and so do all 639 functions this branch translates at /O2 that could be run. This is evidence on a few inputs per function, not a proof of equivalence.

  • Manual mutants, not in CI. Each of these fails cpu_aot_narrow_returns: no merge in the narrow write; a narrow write counted as defining the whole register; return width forced to 8; guest state dropped; SETcc reading the full register; SETcc defining the whole register; None replaced by 8 for a function without a reachable RET; width 0 reported as None.

Not caught by any committed test: removing the 16-bit IMUL cast. MSVC compiles the overflowing multiply as a wrapping one, so the result is the same. A host build of the generated source with UBSan reports signed integer overflow: 65535 * 65533 without the cast and nothing with it.

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

Evidence and follow-up

  • tests/cpu/narrow.asm is original MASM assembled in the build. No binaries were added.
  • 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.
  • Conservative refusals kept: a function that returns a pointer to a global (8 in the /O2 corpus, refused on main too), and a function whose RAX held a jump-table base at RET.
  • Follow-ups: 16-bit CMOVcc with its own fixture; classify narrow in tools/form_coverage.py.
  • Jump-table translator notes

AI-assisted (Claude Code).

🤖 Generated with Claude Code

<!-- greyfinch-sync:R0Y00/Xbox2Windows:issue:6534140034 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/285). Originally posted by [bosseurdinternet](https://github.com/bosseurdinternet) on 2026-11-21T20:42:16Z. --- ## Change Fixes #276. `tools/cpu_switch_aot.py` accepted only 32- and 64-bit register writes and required RAX to be fully defined at every `RET`. It refused functions that write no RAX byte before `RET`, functions that write only AL (`mov al, 1; ret` and `cmp; setcc al; ret`), `ret 0` (`C2 00 00`) and every other 8- or 16-bit register write. - **Definedness by byte.** The must-defined pass records, per register, how many consecutive low bytes every path defines (0, 1, 2 or 8) and whether the register may hold a guest address. A read needs as many defined bytes as it uses. Joins take the lower bound. - **Narrow writes.** An 8- or 16-bit write merges into the 64-bit variable and keeps the other bytes. It raises the defined count to its own width at most, so it never makes a wider read legal. AH, BH, CH and DH stay rejected. A 16-bit `CMOVcc` stays rejected. - **Return width.** `RET` and `ret 0` no longer read RAX; any other `ret N` stays rejected. `Function.return_width` is the number of consecutive low RAX bytes definitely defined at every reachable `RET` or `ret 0`: 0, 1, 2 or 8, the lower bound over joins and over returns. It counts defined bytes and does not establish a source-language type. The generated ABI stays `word`; callers need no width declaration and may use only those bytes. The upper bytes the generator initializes carry no guest result. - **No reachable normal return.** When no `RET` is reachable, `return_width` is `None`, and the CLI and the generated source say `no reachable normal return`. This describes the reachable control flow and makes no termination claim. - **Guest addresses.** A `RET` whose RAX may hold a guest address is rejected. - **SETcc (from #173).** A `SETcc` into a byte register is such a narrow write and no longer needs the rest of the register to be defined. After `setl al` only AL is defined, and a later wider read is still rejected. - **16-bit `IMUL`** multiplies in `std::uint32_t`: two `uint16_t` factors are promoted to `int`, where `0xffff * 0xffff` overflows. Generated C++ for functions that already translate is unchanged, with one exception: a `SETcc` into a register casts its 0/1 condition to `std::uint8_t` instead of `word`. ## Verification Local runs used the same compiler build as CI (MSVC 14.52.37025: `cl`, `link`, `ml64`) under Wine 11.0 on Linux, at `b252f57` on main `d311159`, which includes #282. I reran every item below on this head. - **`ctest --no-tests=error -R '^(cpu_aot_narrow_returns|cpu_aot_jump_tables)$' -V`: both ran and passed**, and so did `cpu_aot_entry_order` from #282 (both fixture registrations are kept). - **`cpu_aot_narrow_returns`.** Twelve called exports, 3,819 inputs, each original and translated export compared with a Python reference through the register probe on the defined RAX bytes, the guarded trace, RBX and RSI. Widths covered: 0 (two exports, one ending in `ret 0`), 1 (AL on every path; AL after `SETcc`; EAX on one exit and AL on the other), 2 (AX) and 8. For widths below 8 it prints that the original left the caller's RAX in the remaining bytes (249 of 249 calls; 146 of 249 for the export with two different exits). - **Inspected, never called:** `self_loop`, a loop that branches to itself, reports `return_width None`, no `return` statement and `no reachable normal return`. The storing export with a reachable `RET` reports width 0, one `return` statement and `0 low byte(s) are defined at every reachable RET`. - **Seven rejection controls, printed by the test:** `mov ah, dl`; a wide read after a narrow write (twice, once after `SETcc`); `ret 8`; a join of a 32-bit and an 8-bit definition followed by a 32-bit read; a 16-bit `CMOVcc`; a returned guest address. - **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. The Windows job is the reference for those. - **Corpus.** 1,119 functions compiled with MSVC `/O2` from this repository's C++ sources plus one plain C++ file, and the `/O1` and `/Od` builds of the same sources, translated with main and with this branch: | Level | main | this branch | non-STL | |---|---|---|---| | `/O2` | 510 | 642 | 73 → 110 | | `/O1` | 486 | 639 | 68 → 111 | | `/Od` | 8 | 15 | 5 → 5 | No function is lost. Of 1,004 functions translated by both, 991 have identical C++ and 13 differ only in the `SETcc` cast above. Of the 132 gained at `/O2`, 68 have two instructions or fewer (52 are an empty function ending in `ret 0`), and 64 are longer. - **Differential execution** (each function compiled with `cl` and called beside the original on 64 inputs, comparing the defined RAX bytes and a seeded memory arena): the 132 functions gained at `/O2` and the 153 gained at `/O1` match on every input, and so do all 639 functions this branch translates at `/O2` that could be run. This is evidence on a few inputs per function, not a proof of equivalence. - **Manual mutants, not in CI.** Each of these fails `cpu_aot_narrow_returns`: no merge in the narrow write; a narrow write counted as defining the whole register; return width forced to 8; guest state dropped; `SETcc` reading the full register; `SETcc` defining the whole register; `None` replaced by 8 for a function without a reachable `RET`; width 0 reported as `None`. Not caught by any committed test: removing the 16-bit `IMUL` cast. MSVC compiles the overflowing multiply as a wrapping one, so the result is the same. A host build of the generated source with UBSan reports `signed integer overflow: 65535 * 65533` without the cast and nothing with it. Not run locally: the D3D12 tests and anything that needs Windows loader behaviour. ## Evidence and follow-up - `tests/cpu/narrow.asm` is original MASM assembled in the build. No binaries were added. - 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. - Conservative refusals kept: a function that returns a pointer to a global (8 in the `/O2` corpus, refused on main too), and a function whose RAX held a jump-table base at `RET`. - Follow-ups: 16-bit `CMOVcc` with its own fixture; classify `narrow` in `tools/form_coverage.py`. - [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:14 +00:00
R0Y00 commented 2026-11-24 06:48:01 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by R0Y00 on 2026-11-21T20:57:29Z.


At 841f09d0 Git reports conflicts in CMakeLists.txt and docs/experiments.md after #282. The translator merges automatically. Please keep both fixture registrations and verify cpu_aot coverage for the lower-address entry and narrow returns on the combined head.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:comment:7069130629 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/285#issuecomment-7069130629). Originally posted by [R0Y00](https://github.com/R0Y00) on 2026-11-21T20:57:29Z. --- At `841f09d0` Git reports conflicts in `CMakeLists.txt` and `docs/experiments.md` after #282. The translator merges automatically. Please keep both fixture registrations and verify `cpu_aot` coverage for the lower-address entry and narrow returns on the combined head.
R0Y00 commented 2026-11-24 06:48:01 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by R0Y00 on 2026-11-21T20:58:37Z.


The inferred return-width contract is approved for #276. Keep the generated word ABI. Function.return_width records how many consecutive low bytes of RAX are definitely defined at every reachable normal return (RET or RET 0). Take the conservative lower bound at control-flow joins and across returns. The supported widths are 0, 1, 2 and 8. Callers need no width declaration and may consume only those bytes. Preserve rejection of guest-address returns and wider reads unless a supported operation has defined the extra bytes. Generator-initialized upper bits provide no guest result. Please describe widths as defined-byte counts: neither 0 nor 1 establishes a source-language type.

At 841f09d0, min(widths, default=8) reports a full-width result when there is no reachable normal return. Use None for that case and emit no reachable normal return in both CLI output and generated source. This describes the reachable control flow and makes no termination claim. Please add a reachable self-loop fixture, translate and inspect it without calling it, and distinguish its None from a reachable RET with width 0. Keep the AL/AX/EAX and multiple-exit controls.

After resolving the #282 conflicts noted above, please run ctest --preset dev --no-tests=error -R '^(cpu_aot_narrow_returns|cpu_aot_jump_tables)$' -V on the final Windows build. We’ll verify both named tests ran on the submitted head before merging.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:comment:7069144382 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/285#issuecomment-7069144382). Originally posted by [R0Y00](https://github.com/R0Y00) on 2026-11-21T20:58:37Z. --- The inferred return-width contract is approved for #276. Keep the generated `word` ABI. `Function.return_width` records how many consecutive low bytes of RAX are definitely defined at every reachable normal return (`RET` or `RET 0`). Take the conservative lower bound at control-flow joins and across returns. The supported widths are 0, 1, 2 and 8. Callers need no width declaration and may consume only those bytes. Preserve rejection of guest-address returns and wider reads unless a supported operation has defined the extra bytes. Generator-initialized upper bits provide no guest result. Please describe widths as defined-byte counts: neither 0 nor 1 establishes a source-language type. At `841f09d0`, `min(widths, default=8)` reports a full-width result when there is no reachable normal return. Use `None` for that case and emit `no reachable normal return` in both CLI output and generated source. This describes the reachable control flow and makes no termination claim. Please add a reachable self-loop fixture, translate and inspect it without calling it, and distinguish its `None` from a reachable `RET` with width 0. Keep the AL/AX/EAX and multiple-exit controls. After resolving the #282 conflicts noted above, please run `ctest --preset dev --no-tests=error -R '^(cpu_aot_narrow_returns|cpu_aot_jump_tables)$' -V` on the final Windows build. We’ll verify both named tests ran on the submitted head before merging.
bosseurdinternet commented 2026-11-24 06:48:02 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by bosseurdinternet on 2026-11-21T21:09:09Z.


Thanks for the review and for settling the contract. All points are addressed at b252f57, rebased on d311159.

Conflicts after #282. Both fixture registrations are kept in CMakeLists.txt (cpu_aot_entry_order, then cpu_aot_narrow_returns) and both paragraphs in docs/experiments.md. On the combined head, cpu_aot_entry_order, cpu_aot_narrow_returns and cpu_aot_jump_tables all pass.

No reachable normal return. return_width is now None when no RET is reachable, and both the CLI and the generated source say no reachable normal return. New export self_loop (spin: inc rcx; jmp spin) is translated in the build and inspected by the test, never called:

inspected self_loop: return_width None, 0 return statement(s), source says: no reachable normal return
inspected store_only: return_width 0, 1 return statement(s), source says: 0 low byte(s) are defined at every reachable RET

Wording. Widths are described as counts of defined low RAX bytes in the docstring, the generated comment, the docs and the test output. The exports that implied a type are renamed: bool_less → al_less, void_store → store_only, void_ret0 → store_ret0.

AL/AX/EAX and multiple exits. Kept, and the AX case was missing, so I added it: ax_copy (mov ax, dx; ret, width 2). Widths 0, 1, 2 and 8 are now each covered by a called export; mixed_width still has one EAX exit and one AL exit and reports 1.

The requested command. ctest --no-tests=error -R '^(cpu_aot_narrow_returns|cpu_aot_jump_tables)$' -V ran both tests and both passed, here with the CI compiler build (MSVC 14.52.37025) under Wine on Linux: 3,819 inputs and 7 rejection controls for the first; 13 rejection controls, 4 flag-consumer controls and the SETcc/CMOVcc checks for the second. I cannot run the Windows preset on this machine, so the Windows job on this head is the Windows result.

Manual mutants, not in CI: replacing None by 8 for a function without a reachable RET fails at self_loop, and reporting width 0 as None fails at store_only.

Corpus and differential-execution numbers are unchanged by these edits and are updated in the description for this head.

AI-assisted (Claude Code).

<!-- greyfinch-sync:R0Y00/Xbox2Windows:comment:7069271484 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/285#issuecomment-7069271484). Originally posted by [bosseurdinternet](https://github.com/bosseurdinternet) on 2026-11-21T21:09:09Z. --- Thanks for the review and for settling the contract. All points are addressed at `b252f57`, rebased on `d311159`. **Conflicts after #282.** Both fixture registrations are kept in `CMakeLists.txt` (`cpu_aot_entry_order`, then `cpu_aot_narrow_returns`) and both paragraphs in `docs/experiments.md`. On the combined head, `cpu_aot_entry_order`, `cpu_aot_narrow_returns` and `cpu_aot_jump_tables` all pass. **No reachable normal return.** `return_width` is now `None` when no `RET` is reachable, and both the CLI and the generated source say `no reachable normal return`. New export `self_loop` (`spin: inc rcx; jmp spin`) is translated in the build and inspected by the test, never called: ```text inspected self_loop: return_width None, 0 return statement(s), source says: no reachable normal return inspected store_only: return_width 0, 1 return statement(s), source says: 0 low byte(s) are defined at every reachable RET ``` **Wording.** Widths are described as counts of defined low RAX bytes in the docstring, the generated comment, the docs and the test output. The exports that implied a type are renamed: `bool_less` → `al_less`, `void_store` → `store_only`, `void_ret0` → `store_ret0`. **AL/AX/EAX and multiple exits.** Kept, and the AX case was missing, so I added it: `ax_copy` (`mov ax, dx; ret`, width 2). Widths 0, 1, 2 and 8 are now each covered by a called export; `mixed_width` still has one EAX exit and one AL exit and reports 1. **The requested command.** `ctest --no-tests=error -R '^(cpu_aot_narrow_returns|cpu_aot_jump_tables)$' -V` ran both tests and both passed, here with the CI compiler build (MSVC 14.52.37025) under Wine on Linux: 3,819 inputs and 7 rejection controls for the first; 13 rejection controls, 4 flag-consumer controls and the SETcc/CMOVcc checks for the second. I cannot run the Windows preset on this machine, so the Windows job on this head is the Windows result. Manual mutants, not in CI: replacing `None` by 8 for a function without a reachable `RET` fails at `self_loop`, and reporting width 0 as `None` fails at `store_only`. Corpus and differential-execution numbers are unchanged by these edits and are updated in the description for this head. _AI-assisted (Claude Code)._
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!281
No description provided.