cpu: label a conditional branch target that is its own fallthrough #407
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/413"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
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 ofedgeswhen it equals the instruction'snext_ip, so ordinary fallthrough gets no label. A conditional branch whose target is the next instruction (jbwith displacement 0) has both successors at that address, so its target was dropped as well.lift()still emitsgoto loc_<target>;for every conditional branch, so the translator accepted the function and the C++ failed to build: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 toedges. 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.entry_order.asmgainsbranch_next:cmp rcx, rdx; jbto the next instruction, thenlea 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).cmake --build --preset devfails oncpu_entry_order_liftedwith the C2094 above. With it, the test passes.cpu_switch_original,cpu_switch_swapped,cpu_switch_addr32,cpu_entry_order(its original three exports),cpu_fourth_argandcpu_narrowis byte-identical tomain.tests/intakecases 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/CMOVccand forward branches at every operand width. It assembles them with ML64, translates them, builds the C++ withcl /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.