cpu: convert a console EXE with one command or report the first refusal #865

Merged
rm-NoobInCoding merged 2 commits from upstream-pr/872 into main 2026-11-23 15:52:05 +00:00
rm-NoobInCoding commented 2026-11-23 15:11:51 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by rm-NoobInCoding on 2026-11-23T15:11:51Z.


What this adds

tools/exe_aot.py converts one PE32+ x64 EXE into a recompiled Windows EXE with one command. If it can't, it stops and reports the stage that refused. The stages run in order:

Stage Check
header AMD64 PE32+ executable image (not a DLL, not a non-PE file)
imports Picks the profile that leaves the fewest imports unbound and lists every unbound import
translate Runs that profile's translator and records the first refused RVA from the lifter message
build Compiles the generated C++ with cl /O2 /MT (/NOIMPLIB /NOEXP, so the provenance export leaves no .lib/.exp)

Each run writes report.json with the input SHA-256, header facts, the import list, the profile and either the generated paths or the refusal. Stale <stem>_aot.* outputs are removed first, so a refusal never leaves an EXE from an earlier run.

The only profile is console-stdio, the existing tools/cpu_stdio_exe_aot.py with its getchar/putchar/fflush bindings. New profiles are one entry in PROFILES. The only change to the existing translator is that its startup-wrapper exception (cpu_exe_io.dll!finish_exe) becomes a named STARTUP_ONLY constant, so the driver applies the same rule. Behavior is unchanged.

This does not widen what the lifter can translate. A passing conversion only means the program fits console-stdio.

Test

cpu_exe_aot_convert (tests/cpu/exe_aot_check.py) runs the tool as a subprocess at test time on cpu_crc_line and cpu_crc_castagnoli:

  • the generated C++ equals cpu_stdio_exe_aot.translate(..., entry_export="native_entry") byte for byte
  • the converted EXE embeds the input SHA-256 and does not import cpu_exe_io.dll
  • stdout and exit status match the native EXE for a line, a final line without a newline, an empty line and EOF (CBF43926\r\n and E3069283\r\n respectively, 00000000\r\n, and no output with exit 1)

Negative controls must each exit 1, name the expected stage and leave no _aot EXE:

Input Expected stage Observed
cpu_exe_original (fixture trace imports) imports cpu_exe_io.dll!emit_trace, cpu_exe_io.dll!read_selector, cpu_import_provider.dll!mix_args
cpu_crc_line from the PE entry (no --entry-export) imports cpu_exe_io.dll!finish_exe
cpu_exe_io.dll header not an executable image
text file named .exe header not a PE image
cpu_crc_line with xor esi, esi patched to cpuid translate unsupported instruction at RVA 0x103b: cpuid; the earlier successful EXE in the same directory is removed
nonexistent --cxx build C++ kept, no EXE

The stale-output check found a real issue during development: without /NOIMPLIB /NOEXP the linker left .lib/.exp behind after a later refusal.

Results

Commit bdbb574, Windows 11 Pro build 26300, MSVC 19.51 (Visual Studio 18 Community), Python 3.14.4, dev preset.

ctest --preset dev --no-tests=error -R '^cpu_exe_aot_convert$' -V

Passed.

ctest --preset dev --no-tests=error -R '^cpu_' -E '_hardware$'

148/148 passed, including cpu_native_crc_line and the archive tests that use cpu_stdio_exe_aot.py. Hardware tests were not run.

tests/docs_links_check.py and tests/bidi_controls_check.py pass. Docs: new section in docs/experiments.md#converting-an-exe-with-one-command and a README experiment row.

Refs #870

<!-- greyfinch-sync:R0Y00/Xbox2Windows:issue:6562374848 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/872). Originally posted by [rm-NoobInCoding](https://github.com/rm-NoobInCoding) on 2026-11-23T15:11:51Z. --- ## What this adds `tools/exe_aot.py` converts one PE32+ x64 EXE into a recompiled Windows EXE with one command. If it can't, it stops and reports the stage that refused. The stages run in order: | Stage | Check | |---|---| | header | AMD64 PE32+ executable image (not a DLL, not a non-PE file) | | imports | Picks the profile that leaves the fewest imports unbound and lists every unbound import | | translate | Runs that profile's translator and records the first refused RVA from the lifter message | | build | Compiles the generated C++ with `cl /O2 /MT` (`/NOIMPLIB /NOEXP`, so the provenance export leaves no .lib/.exp) | Each run writes `report.json` with the input SHA-256, header facts, the import list, the profile and either the generated paths or the refusal. Stale `<stem>_aot.*` outputs are removed first, so a refusal never leaves an EXE from an earlier run. The only profile is `console-stdio`, the existing `tools/cpu_stdio_exe_aot.py` with its `getchar`/`putchar`/`fflush` bindings. New profiles are one entry in `PROFILES`. The only change to the existing translator is that its startup-wrapper exception (`cpu_exe_io.dll!finish_exe`) becomes a named `STARTUP_ONLY` constant, so the driver applies the same rule. Behavior is unchanged. This does not widen what the lifter can translate. A passing conversion only means the program fits `console-stdio`. ## Test `cpu_exe_aot_convert` (`tests/cpu/exe_aot_check.py`) runs the tool as a subprocess at test time on `cpu_crc_line` and `cpu_crc_castagnoli`: - the generated C++ equals `cpu_stdio_exe_aot.translate(..., entry_export="native_entry")` byte for byte - the converted EXE embeds the input SHA-256 and does not import `cpu_exe_io.dll` - stdout and exit status match the native EXE for a line, a final line without a newline, an empty line and EOF (`CBF43926\r\n` and `E3069283\r\n` respectively, `00000000\r\n`, and no output with exit 1) Negative controls must each exit 1, name the expected stage and leave no `_aot` EXE: | Input | Expected stage | Observed | |---|---|---| | `cpu_exe_original` (fixture trace imports) | imports | `cpu_exe_io.dll!emit_trace`, `cpu_exe_io.dll!read_selector`, `cpu_import_provider.dll!mix_args` | | `cpu_crc_line` from the PE entry (no `--entry-export`) | imports | `cpu_exe_io.dll!finish_exe` | | `cpu_exe_io.dll` | header | not an executable image | | text file named `.exe` | header | not a PE image | | `cpu_crc_line` with `xor esi, esi` patched to `cpuid` | translate | `unsupported instruction at RVA 0x103b: cpuid`; the earlier successful EXE in the same directory is removed | | nonexistent `--cxx` | build | C++ kept, no EXE | The stale-output check found a real issue during development: without `/NOIMPLIB /NOEXP` the linker left `.lib`/`.exp` behind after a later refusal. ## Results Commit `bdbb574`, Windows 11 Pro build 26300, MSVC 19.51 (Visual Studio 18 Community), Python 3.14.4, `dev` preset. ```powershell ctest --preset dev --no-tests=error -R '^cpu_exe_aot_convert$' -V ``` Passed. ```powershell ctest --preset dev --no-tests=error -R '^cpu_' -E '_hardware$' ``` 148/148 passed, including `cpu_native_crc_line` and the archive tests that use `cpu_stdio_exe_aot.py`. Hardware tests were not run. `tests/docs_links_check.py` and `tests/bidi_controls_check.py` pass. Docs: new section in `docs/experiments.md#converting-an-exe-with-one-command` and a README experiment row. Refs #870
upstream-sync closed this pull request 2026-11-24 06:45:11 +00:00
R0Y00 commented 2026-11-24 06:49:14 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by R0Y00 on 2026-11-23T15:38:07Z.


Review fix at 9fa011c: on the previous head, giving the converter a source-built text fixture named report.json inside the output directory returned a header refusal but overwrote the input with its report. The converter now checks all output paths for input aliases before changing files. The focused fixture checks direct and hard-linked collisions and requires that the input bytes stay unchanged. It still verifies that an ordinary translation refusal clears the earlier generated EXE.

ctest --preset release --no-tests=error -R '^cpu_exe_aot_convert$' passed with the original CRC runs and the new controls. Windows CI is rerunning.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:comment:7105872248 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/872#issuecomment-7105872248). Originally posted by [R0Y00](https://github.com/R0Y00) on 2026-11-23T15:38:07Z. --- Review fix at `9fa011c`: on the previous head, giving the converter a source-built text fixture named `report.json` inside the output directory returned a header refusal but overwrote the input with its report. The converter now checks all output paths for input aliases before changing files. The focused fixture checks direct and hard-linked collisions and requires that the input bytes stay unchanged. It still verifies that an ordinary translation refusal clears the earlier generated EXE. `ctest --preset release --no-tests=error -R '^cpu_exe_aot_convert$'` passed with the original CRC runs and the new controls. Windows CI is rerunning.
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!865
No description provided.