cpu: label a conditional branch target that is its own fallthrough #413

Manually merged
github merged 1 commit from gh-pr/413/head into gh-pr/413/base 2026-11-26 21:49:57 +00:00
Owner

@⁠grahamster2 on GitHub · 2026-11-22 00:49 UTC · original
grahamster2:cpu/jcc-to-next-label → main · +29 −4 · 6 files · 1 commits · merged 2026-11-22 00:55 UTC

Fixes a wrong translation in tools/cpu_switch_aot.py: an accepted function whose generated C++ does not compile.

Problem

Function.explore() leaves a successor out of edges when it equals the instruction's next_ip, so ordinary fallthrough gets no label. A conditional branch whose target is the next instruction (jb with displacement 0) has both successors at that address, so its target was dropped as well. lift() still emits goto loc_<target>; for every conditional branch, so the translator accepted the function and the C++ failed to build:

cpu_entry_order_lifted.cpp(61): error C2094: use of undefined label 'loc_1026'

Optimizing compilers rarely emit such a branch, but it is valid machine code, and the translator accepts it.

Change

One line in explore(): every conditional branch target is added to edges. Joins and flag checks are unaffected, except that a flag consumer placed right after such a target now counts as being behind a join, which is the conservative direction.

Evidence

Windows 11 Pro build 26200, VS Build Tools 2026 18.3 (MSVC 19.50.35725), Windows SDK 10.0.26100, Python 3.12, on 6549244.

ctest --preset dev --no-tests=error -R '^cpu_aot_entry_order$' -V
  • entry_order.asm gains branch_next: cmp rcx, rdx; jb to the next instruction, then lea rax, [rcx+rdx]. The check asserts that the assembled branch targets its fallthrough and that the target is labelled. It then compares native and translated calls over the existing 249 input pairs (return, guarded trace, RBX, RSI).
  • Without the fix, cmake --build --preset dev fails on cpu_entry_order_lifted with the C2094 above. With it, the test passes.
  • The C++ generated for cpu_switch_original, cpu_switch_swapped, cpu_switch_addr32, cpu_entry_order (its original three exports), cpu_fourth_arg and cpu_narrow is byte-identical to main.
  • Full Debug suite: 143 of 143 pass (the three tests/intake cases were not run on this machine).

How it was found

A local differential fuzzer generates random leaf functions from the translator's accepted subset: ALU, shifts, IMUL, MOVZX/MOVSX/MOVSXD, LEA, loads and stores, compare→Jcc/SETcc/CMOVcc and forward branches at every operand width. It assembles them with ML64, translates them, builds the C++ with cl /O2, and compares RAX and a 256-byte buffer between native and translated calls on 200 inputs each. This was the only failure. With the fix, 1,840 generated functions all translate and match. I can turn the fuzzer into a checked-in tool in a separate PR if that's useful.

**@⁠grahamster2** on GitHub · 2026-11-22 00:49 UTC · [original](https://github.com/R0Y00/Xbox2Windows/pull/413) `grahamster2:cpu/jcc-to-next-label` → `main` · +29 −4 · 6 files · 1 commits · merged 2026-11-22 00:55 UTC Fixes a wrong translation in `tools/cpu_switch_aot.py`: an accepted function whose generated C++ does not compile. ## Problem `Function.explore()` leaves a successor out of `edges` when it equals the instruction's `next_ip`, so ordinary fallthrough gets no label. A conditional branch whose target is the next instruction (`jb` with displacement 0) has both successors at that address, so its target was dropped as well. `lift()` still emits `goto loc_<target>;` for every conditional branch, so the translator accepted the function and the C++ failed to build: cpu_entry_order_lifted.cpp(61): error C2094: use of undefined label 'loc_1026' Optimizing compilers rarely emit such a branch, but it is valid machine code, and the translator accepts it. ## Change One line in `explore()`: every conditional branch target is added to `edges`. Joins and flag checks are unaffected, except that a flag consumer placed right after such a target now counts as being behind a join, which is the conservative direction. ## Evidence Windows 11 Pro build 26200, VS Build Tools 2026 18.3 (MSVC 19.50.35725), Windows SDK 10.0.26100, Python 3.12, on `6549244`. ctest --preset dev --no-tests=error -R '^cpu_aot_entry_order$' -V - `entry_order.asm` gains `branch_next`: `cmp rcx, rdx; jb` to the next instruction, then `lea rax, [rcx+rdx]`. The check asserts that the assembled branch targets its fallthrough and that the target is labelled. It then compares native and translated calls over the existing 249 input pairs (return, guarded trace, RBX, RSI). - Without the fix, `cmake --build --preset dev` fails on `cpu_entry_order_lifted` with the C2094 above. With it, the test passes. - The C++ generated for `cpu_switch_original`, `cpu_switch_swapped`, `cpu_switch_addr32`, `cpu_entry_order` (its original three exports), `cpu_fourth_arg` and `cpu_narrow` is byte-identical to `main`. - Full Debug suite: 143 of 143 pass (the three `tests/intake` cases were not run on this machine). ## How it was found A local differential fuzzer generates random leaf functions from the translator's accepted subset: ALU, shifts, `IMUL`, `MOVZX`/`MOVSX`/`MOVSXD`, `LEA`, loads and stores, compare→`Jcc`/`SETcc`/`CMOVcc` and forward branches at every operand width. It assembles them with ML64, translates them, builds the C++ with `cl /O2`, and compares RAX and a 256-byte buffer between native and translated calls on 200 inputs each. This was the only failure. With the fix, 1,840 generated functions all translate and match. I can turn the fuzzer into a checked-in tool in a separate PR if that's useful.
Function.explore() leaves a successor out of the label set when it equals
the instruction's next address, so ordinary fallthrough gets no label. A
Jcc whose target is the next instruction (jb +0) has both successors at
that address, so its target was dropped too, but lift() still emits
"goto loc_<target>;". The function was accepted and its translation failed
to compile with C2094 (use of undefined label).

Always count a conditional branch's target as an edge. The C++ generated
for every existing jump-table translator fixture is unchanged.

entry_order gains branch_next, a CMP and a JB to the next instruction. The
test checks that the assembled branch targets its fallthrough, that the
target is labelled, and that native and translated calls match.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 21:49:55 +00:00
github manually merged commit 5636d6ae25 into gh-pr/413/base 2026-11-26 21:49:57 +00:00
This discussion has been locked. Commenting is limited to contributors.
No description provided.