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

Manually merged
github merged 1 commit from gh-pr/282/head into gh-pr/282/base 2026-11-26 21:39:52 +00:00
Owner

@⁠bosseurdinternet on GitHub · 2026-11-21 20:32 UTC · original
bosseurdinternet:cpu/entry-order → main · +109 −1 · 6 files · 1 commits · merged 2026-11-21 20:55 UTC

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

**@⁠bosseurdinternet** on GitHub · 2026-11-21 20:32 UTC · [original](https://github.com/R0Y00/Xbox2Windows/pull/282) `bosseurdinternet:cpu/entry-order` → `main` · +109 −1 · 6 files · 1 commits · merged 2026-11-21 20:55 UTC ## 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)
The jump-table translator emits C++ in address order. 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
its lowest instruction and never ran the entry. Emit a jump to the entry
first in that case, and add a source-built fixture with two such exports and
a control whose entry is its lowest instruction.

Co-Authored-By: Claude Mythos 5.2 <[email protected]>
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 21:39:50 +00:00
github manually merged commit c3842fd7cc into gh-pr/282/base 2026-11-26 21:39:52 +00:00
This discussion has been locked. Commenting is limited to contributors.
No description provided.