cpu: accept narrow register writes and return widths in the jump-table translator #281
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/285"
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 bosseurdinternet on 2026-11-21T20:42:16Z.
Change
Fixes #276.
tools/cpu_switch_aot.pyaccepted only 32- and 64-bit register writes and required RAX to be fully defined at everyRET. It refused functions that write no RAX byte beforeRET, functions that write only AL (mov al, 1; retandcmp; setcc al; ret),ret 0(C2 00 00) and every other 8- or 16-bit register write.CMOVccstays rejected.RETandret 0no longer read RAX; any otherret Nstays rejected.Function.return_widthis the number of consecutive low RAX bytes definitely defined at every reachableRETorret 0: 0, 1, 2 or 8, the lower bound over joins and over returns. It counts defined bytes and does not establish a source-language type. The generated ABI staysword; callers need no width declaration and may use only those bytes. The upper bytes the generator initializes carry no guest result.RETis reachable,return_widthisNone, and the CLI and the generated source sayno reachable normal return. This describes the reachable control flow and makes no termination claim.RETwhose RAX may hold a guest address is rejected.SETccinto a byte register is such a narrow write and no longer needs the rest of the register to be defined. Aftersetl alonly AL is defined, and a later wider read is still rejected.IMULmultiplies instd::uint32_t: twouint16_tfactors are promoted toint, where0xffff * 0xffffoverflows.Generated C++ for functions that already translate is unchanged, with one exception: a
SETccinto a register casts its 0/1 condition tostd::uint8_tinstead ofword.Verification
Local runs used the same compiler build as CI (MSVC 14.52.37025:
cl,link,ml64) under Wine 11.0 on Linux, atb252f57on maind311159, which includes #282. I reran every item below on this head.ctest --no-tests=error -R '^(cpu_aot_narrow_returns|cpu_aot_jump_tables)$' -V: both ran and passed, and so didcpu_aot_entry_orderfrom #282 (both fixture registrations are kept).cpu_aot_narrow_returns. Twelve called exports, 3,819 inputs, each original and translated export compared with a Python reference through the register probe on the defined RAX bytes, the guarded trace, RBX and RSI. Widths covered: 0 (two exports, one ending inret 0), 1 (AL on every path; AL afterSETcc; EAX on one exit and AL on the other), 2 (AX) and 8. For widths below 8 it prints that the original left the caller's RAX in the remaining bytes (249 of 249 calls; 146 of 249 for the export with two different exits).Inspected, never called:
self_loop, a loop that branches to itself, reportsreturn_width None, noreturnstatement andno reachable normal return. The storing export with a reachableRETreports width 0, onereturnstatement and0 low byte(s) are defined at every reachable RET.Seven rejection controls, printed by the test:
mov ah, dl; a wide read after a narrow write (twice, once afterSETcc);ret 8; a join of a 32-bit and an 8-bit definition followed by a 32-bit read; a 16-bitCMOVcc; a returned guest address.Other
cpu_*tests: same results as main. Under Wine,cpu_aot_import_call,cpu_aot_ordinal_import,cpu_native_reloc_freeandcpu_native_tlsfail on main too because Wine's loader differs from Windows, andcpu_native_exe_starttimes out. The Windows job is the reference for those.Corpus. 1,119 functions compiled with MSVC
/O2from this repository's C++ sources plus one plain C++ file, and the/O1and/Odbuilds of the same sources, translated with main and with this branch:/O2/O1/OdNo function is lost. Of 1,004 functions translated by both, 991 have identical C++ and 13 differ only in the
SETcccast above. Of the 132 gained at/O2, 68 have two instructions or fewer (52 are an empty function ending inret 0), and 64 are longer.Differential execution (each function compiled with
cland called beside the original on 64 inputs, comparing the defined RAX bytes and a seeded memory arena): the 132 functions gained at/O2and the 153 gained at/O1match on every input, and so do all 639 functions this branch translates at/O2that could be run. This is evidence on a few inputs per function, not a proof of equivalence.Manual mutants, not in CI. Each of these fails
cpu_aot_narrow_returns: no merge in the narrow write; a narrow write counted as defining the whole register; return width forced to 8; guest state dropped;SETccreading the full register;SETccdefining the whole register;Nonereplaced by 8 for a function without a reachableRET; width 0 reported asNone.Not caught by any committed test: removing the 16-bit
IMULcast. MSVC compiles the overflowing multiply as a wrapping one, so the result is the same. A host build of the generated source with UBSan reportssigned integer overflow: 65535 * 65533without the cast and nothing with it.Not run locally: the D3D12 tests and anything that needs Windows loader behaviour.
Evidence and follow-up
tests/cpu/narrow.asmis original MASM assembled in the build. No binaries were added./O2corpus, refused on main too), and a function whose RAX held a jump-table base atRET.CMOVccwith its own fixture; classifynarrowintools/form_coverage.py.AI-assisted (Claude Code).
🤖 Generated with Claude Code
Imported from R0Y00/Xbox2Windows. Originally posted by R0Y00 on 2026-11-21T20:57:29Z.
At
841f09d0Git reports conflicts inCMakeLists.txtanddocs/experiments.mdafter #282. The translator merges automatically. Please keep both fixture registrations and verifycpu_aotcoverage for the lower-address entry and narrow returns on the combined head.Imported from R0Y00/Xbox2Windows. Originally posted by R0Y00 on 2026-11-21T20:58:37Z.
The inferred return-width contract is approved for #276. Keep the generated
wordABI.Function.return_widthrecords how many consecutive low bytes of RAX are definitely defined at every reachable normal return (RETorRET 0). Take the conservative lower bound at control-flow joins and across returns. The supported widths are 0, 1, 2 and 8. Callers need no width declaration and may consume only those bytes. Preserve rejection of guest-address returns and wider reads unless a supported operation has defined the extra bytes. Generator-initialized upper bits provide no guest result. Please describe widths as defined-byte counts: neither 0 nor 1 establishes a source-language type.At
841f09d0,min(widths, default=8)reports a full-width result when there is no reachable normal return. UseNonefor that case and emitno reachable normal returnin both CLI output and generated source. This describes the reachable control flow and makes no termination claim. Please add a reachable self-loop fixture, translate and inspect it without calling it, and distinguish itsNonefrom a reachableRETwith width 0. Keep the AL/AX/EAX and multiple-exit controls.After resolving the #282 conflicts noted above, please run
ctest --preset dev --no-tests=error -R '^(cpu_aot_narrow_returns|cpu_aot_jump_tables)$' -Von the final Windows build. We’ll verify both named tests ran on the submitted head before merging.Imported from R0Y00/Xbox2Windows. Originally posted by bosseurdinternet on 2026-11-21T21:09:09Z.
Thanks for the review and for settling the contract. All points are addressed at
b252f57, rebased ond311159.Conflicts after #282. Both fixture registrations are kept in
CMakeLists.txt(cpu_aot_entry_order, thencpu_aot_narrow_returns) and both paragraphs indocs/experiments.md. On the combined head,cpu_aot_entry_order,cpu_aot_narrow_returnsandcpu_aot_jump_tablesall pass.No reachable normal return.
return_widthis nowNonewhen noRETis reachable, and both the CLI and the generated source sayno reachable normal return. New exportself_loop(spin: inc rcx; jmp spin) is translated in the build and inspected by the test, never called:Wording. Widths are described as counts of defined low RAX bytes in the docstring, the generated comment, the docs and the test output. The exports that implied a type are renamed:
bool_less→al_less,void_store→store_only,void_ret0→store_ret0.AL/AX/EAX and multiple exits. Kept, and the AX case was missing, so I added it:
ax_copy(mov ax, dx; ret, width 2). Widths 0, 1, 2 and 8 are now each covered by a called export;mixed_widthstill has one EAX exit and one AL exit and reports 1.The requested command.
ctest --no-tests=error -R '^(cpu_aot_narrow_returns|cpu_aot_jump_tables)$' -Vran both tests and both passed, here with the CI compiler build (MSVC 14.52.37025) under Wine on Linux: 3,819 inputs and 7 rejection controls for the first; 13 rejection controls, 4 flag-consumer controls and the SETcc/CMOVcc checks for the second. I cannot run the Windows preset on this machine, so the Windows job on this head is the Windows result.Manual mutants, not in CI: replacing
Noneby 8 for a function without a reachableRETfails atself_loop, and reporting width 0 asNonefails atstore_only.Corpus and differential-execution numbers are unchanged by these edits and are updated in the description for this head.
AI-assisted (Claude Code).