cpu: start a translated function at its entry when code below it is reached #278

Merged
bosseurdinternet merged 1 commit from upstream-pr/282 into main 2026-11-21 20:55:50 +00:00
bosseurdinternet commented 2026-11-21 20:32:36 +00:00 (Migrated from github.com)

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


Change

Fixes a wrong translation in tools/cpu_switch_aot.py. lift() emits C++ in address order and relied on the entry being the lowest instruction. A leaf without unwind data may use any unowned bytes of its section, so a jump from the entry can continue in code assembled below it. The generated function then began at that lower code, returned, and left the entry unreachable.

Example from MSVC /O2: u32 tail_call(u32 x) { return helper_square(x + 1); } compiles to inc ecx; jmp helper_square, with helper_square at a lower address. On main the translation starts with the multiply and returns x * x.

The fix is two edits near the end of lift(): when the lowest instruction is not the entry, the entry gets a label and the body starts with goto loc_<entry>;. Output is unchanged for every function whose entry is its lowest instruction.

Verification

Local runs used the same compiler build as CI (MSVC 14.52.37025: cl, link, ml64), under Wine 11.0 on Linux, at c3842fd rebased on c832696.

  • New test cpu_aot_entry_order: passed. inc_square increments RCX and jumps to square_tail below it; pick_tail branches there on a comparison. Native and translated exports match a Python reference on return, guarded trace, RBX and RSI over 249 input pairs each, and pick_tail takes both paths. The test prints one control line per export: entry RVA, lowest instruction RVA, and whether the translation starts with a jump to the entry. square_tail, whose entry is its lowest instruction, must start at the entry.
  • Without the fix (translator from main, same fixture): the test fails. The translated inc_square(0, 0) returns 0 where the native export returns 1.
  • Other cpu_* tests: cpu_aot_jump_tables and the rest pass as on 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. None of them uses this translator path; the Windows job is the reference for those.
  • Unchanged output elsewhere. I translated 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, with main and with this branch. No function is lost. Of 1,004 functions lowered by both across the three levels, 1,002 have identical C++. The two that differ are tail_call at /O2 and /O1, each gaining the label and the jump.
  • Differential execution of the /O2 corpus (each lowered function compiled with cl and called beside the original on 64 inputs, comparing RAX and a seeded memory arena): main has 506 functions with every completed input matching and 1 mismatch, tail_call, wrong on all 64 inputs. This branch has 507 and 0. This is evidence on a few inputs per function, not a proof of equivalence.

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

Evidence and follow-up

  • The fixture is assembled from source in the build. No binaries were added.
  • How the bug was found: by running translated functions beside their originals, not by reading the code. Among the functions main lowers in that corpus, tail_call was the only one affected.
  • Not covered: a function with unwind data whose chained range lies below its entry takes the same path. I have no fixture for it; docs/experiments.md says so.
  • Unrelated to #276 (narrow register writes and return widths), which I am still preparing. That change edits other lines of lift().
  • Jump-table translator notes

AI-assisted (Claude Code).

🤖 Generated with Claude Code

<!-- greyfinch-sync:R0Y00/Xbox2Windows:issue:6534042862 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/282). Originally posted by [bosseurdinternet](https://github.com/bosseurdinternet) on 2026-11-21T20:32:36Z. --- ## Change Fixes a wrong translation in `tools/cpu_switch_aot.py`. `lift()` emits C++ in address order and relied on the entry being the lowest instruction. A leaf without unwind data may use any unowned bytes of its section, so a jump from the entry can continue in code assembled below it. The generated function then began at that lower code, returned, and left the entry unreachable. Example from MSVC `/O2`: `u32 tail_call(u32 x) { return helper_square(x + 1); }` compiles to `inc ecx; jmp helper_square`, with `helper_square` at a lower address. On main the translation starts with the multiply and returns `x * x`. The fix is two edits near the end of `lift()`: when the lowest instruction is not the entry, the entry gets a label and the body starts with `goto loc_<entry>;`. Output is unchanged for every function whose entry is its lowest instruction. ## Verification Local runs used the same compiler build as CI (MSVC 14.52.37025: `cl`, `link`, `ml64`), under Wine 11.0 on Linux, at `c3842fd` rebased on `c832696`. - **New test `cpu_aot_entry_order`: passed.** `inc_square` increments RCX and jumps to `square_tail` below it; `pick_tail` branches there on a comparison. Native and translated exports match a Python reference on return, guarded trace, RBX and RSI over 249 input pairs each, and `pick_tail` takes both paths. The test prints one control line per export: entry RVA, lowest instruction RVA, and whether the translation starts with a jump to the entry. `square_tail`, whose entry is its lowest instruction, must start at the entry. - **Without the fix** (translator from main, same fixture): the test fails. The translated `inc_square(0, 0)` returns 0 where the native export returns 1. - **Other `cpu_*` tests:** `cpu_aot_jump_tables` and the rest pass as on 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. None of them uses this translator path; the Windows job is the reference for those. - **Unchanged output elsewhere.** I translated 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, with main and with this branch. No function is lost. Of 1,004 functions lowered by both across the three levels, 1,002 have identical C++. The two that differ are `tail_call` at `/O2` and `/O1`, each gaining the label and the jump. - **Differential execution of the `/O2` corpus** (each lowered function compiled with `cl` and called beside the original on 64 inputs, comparing RAX and a seeded memory arena): main has 506 functions with every completed input matching and 1 mismatch, `tail_call`, wrong on all 64 inputs. This branch has 507 and 0. This is evidence on a few inputs per function, not a proof of equivalence. Not run locally: the D3D12 tests and anything that needs Windows loader behaviour. ## Evidence and follow-up - The fixture is assembled from source in the build. No binaries were added. - How the bug was found: by running translated functions beside their originals, not by reading the code. Among the functions main lowers in that corpus, `tail_call` was the only one affected. - Not covered: a function with unwind data whose chained range lies below its entry takes the same path. I have no fixture for it; `docs/experiments.md` says so. - Unrelated to #276 (narrow register writes and return widths), which I am still preparing. That change edits other lines of `lift()`. - [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:13 +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!278
No description provided.