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

Merged
grahamster2 merged 1 commit from upstream-pr/413 into main 2026-11-22 00:55:36 +00:00
grahamster2 commented 2026-11-22 00:49:57 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by grahamster2 on 2026-11-22T00:49:57Z.


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.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:issue:6536506982 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/413). Originally posted by [grahamster2](https://github.com/grahamster2) on 2026-11-22T00:49:57Z. --- 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.
upstream-sync closed this pull request 2026-11-24 06:42:55 +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!407
No description provided.