pe: start a mapped EXE with pe_map_run #235

Merged
yummancito merged 2 commits from upstream-pr/238 into main 2026-11-21 19:20:59 +00:00
yummancito commented 2026-11-21 18:56:28 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by yummancito on 2026-11-21T18:56:28Z.


Refs #165

Change

pe_map_image maps an EXE without notifying it, as LoadLibrary does. This adds an opt-in export, pe_map_run(mapped, &exit_code), that starts a mapped EXE on the calling thread: TLS callbacks in table order with DLL_PROCESS_ATTACH and a null reserved argument, then the entry point, whose return value is stored in exit_code. pe_map_image and pe_map_release are unchanged. Preconditions, documented at the function and in docs/experiments.md and not checked: mapped must be a live base returned by pe_map_image (the headers are read directly, as in pe_map_export), the image is trusted, because its callbacks and entry point are arbitrary code on the calling thread that can crash, hang or call ExitProcess in the host, and the call should happen once per image. pe_map_run returns 0 and runs nothing for an image with IMAGE_FILE_DLL set, for an image without an entry point and for an unreadable TLS callback list. The PEB argument Windows passes to an EXE's entry point is not forwarded, and no process-detach notification follows, because the host process keeps running. The API question from the issue: a new export, so the LoadLibrary-equivalent behavior of pe_map_image stays as measured.

Fixture

tests/cpu/exe_start.asm is a source-built EXE with no C runtime. A TLS callback and the entry point each append one base-4 digit to a log in .data; the entry point then calls the named import cpu_import_provider!mix_args(5, 3, out) (13) and returns (log << 8) | 13. cpu_exe_start_no_tls is the same source without a TLS directory.

Native comparison and controls (cpu_native_exe_start)

  • Native: the EXE is launched as a process; its exit code is the entry point's return value. Standard output and error of the launched EXE and of the pe_map child process are captured with subprocess.run(capture_output=True). They are empty by design, because the fixture writes nothing, and the test fails with the captured text if either stream is not empty; the child's only stdout is its own JSON result line.
  • Mapped: pe_map_image plus pe_map_run in a child process. Both give 0x60d: the callback ran (null reserved) before the entry point, and the import returned 13.
  • No TLS directory: both give 0x20d, so the log reflects the callback.
  • Altered provider (cpu_import_altered) bound under the imported name: 0x60e, so the import is live.
  • IMAGE_FILE_DLL set in a copy: pe_map_run returns 0 and leaves the exit code untouched.
  • Entry point moved into .data in a copy: the Windows launch faults with 0xc0000005; pe_map_image refuses it as "entry point outside executable section" and nothing runs.
  • One-off manual check, not in CI: with the callback loop removed from pe_map_run, the test failed with mapped original 0x20d != native 0x60d.

Verification

Windows 11 Pro build 26300, VS Build Tools 17.14 (MSVC 19.44), CMake 3.31.6, Python 3.13, base c04e65a:

cmake --preset dev --fresh
cmake --build --preset dev
ctest --preset dev --no-tests=error -R '^cpu_native_exe_start$' -V
native launch: original 0x60d, without TLS 0x20d
mapped: original 0x60d = native, without TLS 0x20d = native, altered provider 0x60e
pe_map_run refuses an image with IMAGE_FILE_DLL set and leaves the exit code untouched
entry point in .data: Windows faults (0xc0000005), pe_map refuses 'entry point outside executable section' and runs nothing
100% tests passed, 0 tests failed out of 116

Full suite (ctest --preset dev --no-tests=error): 116 tests, 0 failures, 3 capability skips reported separately (bvh_wave64_hardware, bvh_wave64_warp, wave_shuffle_warp: the adapters do not offer the forced wave widths). Checked earlier at e8895ad, the patch also applied and passed together with #228 (116 tests at that base, 0 failures); the two PRs share no fixture, target or test names, and edit different places in CMakeLists.txt and docs/experiments.md.

Not covered

CRT startup (it would call ExitProcess in the host), implicit TLS data, a second thread's TLS notifications, DLL_PROCESS_DETACH for an EXE, GetModuleHandle(NULL) and the PEB. This shows one EXE with an ordered start log and one resolved import; it does not show that any title starts. It touches the end of pe_map.cpp, like open #139 and #208, so a later rebase may need a small hand merge. Docs: docs/experiments.md, "Starting a mapped EXE".

AI-assisted.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:issue:6533048020 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/238). Originally posted by [yummancito](https://github.com/yummancito) on 2026-11-21T18:56:28Z. --- Refs #165 ## Change `pe_map_image` maps an EXE without notifying it, as `LoadLibrary` does. This adds an opt-in export, `pe_map_run(mapped, &exit_code)`, that starts a mapped EXE on the calling thread: TLS callbacks in table order with `DLL_PROCESS_ATTACH` and a null reserved argument, then the entry point, whose return value is stored in `exit_code`. `pe_map_image` and `pe_map_release` are unchanged. Preconditions, documented at the function and in `docs/experiments.md` and not checked: `mapped` must be a live base returned by `pe_map_image` (the headers are read directly, as in `pe_map_export`), the image is trusted, because its callbacks and entry point are arbitrary code on the calling thread that can crash, hang or call `ExitProcess` in the host, and the call should happen once per image. `pe_map_run` returns 0 and runs nothing for an image with `IMAGE_FILE_DLL` set, for an image without an entry point and for an unreadable TLS callback list. The PEB argument Windows passes to an EXE's entry point is not forwarded, and no process-detach notification follows, because the host process keeps running. The API question from the issue: a new export, so the `LoadLibrary`-equivalent behavior of `pe_map_image` stays as measured. ## Fixture `tests/cpu/exe_start.asm` is a source-built EXE with no C runtime. A TLS callback and the entry point each append one base-4 digit to a log in `.data`; the entry point then calls the named import `cpu_import_provider!mix_args(5, 3, out)` (13) and returns `(log << 8) | 13`. `cpu_exe_start_no_tls` is the same source without a TLS directory. ## Native comparison and controls (`cpu_native_exe_start`) - Native: the EXE is launched as a process; its exit code is the entry point's return value. Standard output and error of the launched EXE and of the `pe_map` child process are captured with `subprocess.run(capture_output=True)`. They are empty by design, because the fixture writes nothing, and the test fails with the captured text if either stream is not empty; the child's only stdout is its own JSON result line. - Mapped: `pe_map_image` plus `pe_map_run` in a child process. Both give `0x60d`: the callback ran (null reserved) before the entry point, and the import returned 13. - No TLS directory: both give `0x20d`, so the log reflects the callback. - Altered provider (`cpu_import_altered`) bound under the imported name: `0x60e`, so the import is live. - `IMAGE_FILE_DLL` set in a copy: `pe_map_run` returns 0 and leaves the exit code untouched. - Entry point moved into `.data` in a copy: the Windows launch faults with `0xc0000005`; `pe_map_image` refuses it as "entry point outside executable section" and nothing runs. - One-off manual check, not in CI: with the callback loop removed from `pe_map_run`, the test failed with `mapped original 0x20d != native 0x60d`. ## Verification Windows 11 Pro build 26300, VS Build Tools 17.14 (MSVC 19.44), CMake 3.31.6, Python 3.13, base `c04e65a`: ```powershell cmake --preset dev --fresh cmake --build --preset dev ctest --preset dev --no-tests=error -R '^cpu_native_exe_start$' -V ``` ```text native launch: original 0x60d, without TLS 0x20d mapped: original 0x60d = native, without TLS 0x20d = native, altered provider 0x60e pe_map_run refuses an image with IMAGE_FILE_DLL set and leaves the exit code untouched entry point in .data: Windows faults (0xc0000005), pe_map refuses 'entry point outside executable section' and runs nothing 100% tests passed, 0 tests failed out of 116 ``` Full suite (`ctest --preset dev --no-tests=error`): 116 tests, 0 failures, 3 capability skips reported separately (`bvh_wave64_hardware`, `bvh_wave64_warp`, `wave_shuffle_warp`: the adapters do not offer the forced wave widths). Checked earlier at `e8895ad`, the patch also applied and passed together with #228 (116 tests at that base, 0 failures); the two PRs share no fixture, target or test names, and edit different places in `CMakeLists.txt` and `docs/experiments.md`. ## Not covered CRT startup (it would call `ExitProcess` in the host), implicit TLS data, a second thread's TLS notifications, `DLL_PROCESS_DETACH` for an EXE, `GetModuleHandle(NULL)` and the PEB. This shows one EXE with an ordered start log and one resolved import; it does not show that any title starts. It touches the end of `pe_map.cpp`, like open #139 and #208, so a later rebase may need a small hand merge. Docs: `docs/experiments.md`, "Starting a mapped EXE". _AI-assisted._
upstream-sync closed this pull request 2026-11-24 06:41:57 +00:00
R0Y00 commented 2026-11-24 06:47:53 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by R0Y00 on 2026-11-21T19:13:10Z.


The explicit synchronous pe_map_run API is approved for this PR. In the function comment and docs/experiments.md, please specify that the caller keeps the EXE mapping alive for the whole call, starts it at most once, and prevents any thread, TLS callback or entry point from re-entering run or releasing that mapping during startup. Repeated calls are outside the contract; please remove the promise about what a second call does. This requires no registry or dependency on #139.

Keep the current-thread order (TLS process attach in table order with null reserved, then entry with a null argument), DWORD output on success, and the existing DLL/EXE distinction. No PEB setup or EXE process detach is required. The 0x60d, 0x20d, 0x60e controls and DLL refusal are in the current fixture. Please also verify refusal for a missing entry, a null output pointer and an unreadable callback table: return 0 before guest code runs and preserve any supplied output sentinel. Report cpu_native_exe_start on the updated Windows SHA. The two-DLL experiment remains separate.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:comment:7067854111 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/238#issuecomment-7067854111). Originally posted by [R0Y00](https://github.com/R0Y00) on 2026-11-21T19:13:10Z. --- The explicit synchronous `pe_map_run` API is approved for this PR. In the function comment and `docs/experiments.md`, please specify that the caller keeps the EXE mapping alive for the whole call, starts it at most once, and prevents any thread, TLS callback or entry point from re-entering `run` or releasing that mapping during startup. Repeated calls are outside the contract; please remove the promise about what a second call does. This requires no registry or dependency on #139. Keep the current-thread order (TLS process attach in table order with null reserved, then entry with a null argument), DWORD output on success, and the existing DLL/EXE distinction. No PEB setup or EXE process detach is required. The `0x60d`, `0x20d`, `0x60e` controls and DLL refusal are in the current fixture. Please also verify refusal for a missing entry, a null output pointer and an unreadable callback table: return 0 before guest code runs and preserve any supplied output sentinel. Report `cpu_native_exe_start` on the updated Windows SHA. The two-DLL experiment remains separate.
sof9816 commented 2026-11-24 06:47:53 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by sof9816 on 2026-11-21T19:14:00Z.


Independent Windows verification for e238a6537ae8c71d6173db549bee728497990e0e. This is the revision before the additional refusal checks requested in the review above.

Windows 11 build 26200.9711, x64 MSVC 19.44.35229 and Python 3.13.3. Build completed with cmake --build --preset dev --parallel 5.

ctest --preset dev --no-tests=error -R '^(cpu_native_exe_start|cpu_aot_exe_entry|static_exe_intake)$' -V
ctest --preset dev --no-tests=error
  • All 3 focused tests passed. Native and mapped startup both returned 0x60d. Both returned 0x20d without TLS, and substituting the bound provider changed the mapped result to 0x60e.
  • The mapped DLL control preserved the exit-code sentinel, and the mapper rejected the non-executable entry point before running it.
  • Full suite recorded 113 passes, zero failures and three capability skips on the RTX 3060 Ti/WARP system. The skips were bvh_wave64_hardware, bvh_wave64_warp and wave_shuffle_warp.

Built and tested entirely from the repository's fixtures. AI-assisted.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:comment:7067864876 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/238#issuecomment-7067864876). Originally posted by [sof9816](https://github.com/sof9816) on 2026-11-21T19:14:00Z. --- Independent Windows verification for `e238a6537ae8c71d6173db549bee728497990e0e`. This is the revision before the additional refusal checks requested in the review above. Windows 11 build 26200.9711, x64 MSVC 19.44.35229 and Python 3.13.3. Build completed with `cmake --build --preset dev --parallel 5`. ```powershell ctest --preset dev --no-tests=error -R '^(cpu_native_exe_start|cpu_aot_exe_entry|static_exe_intake)$' -V ctest --preset dev --no-tests=error ``` - All 3 focused tests passed. Native and mapped startup both returned `0x60d`. Both returned `0x20d` without TLS, and substituting the bound provider changed the mapped result to `0x60e`. - The mapped DLL control preserved the exit-code sentinel, and the mapper rejected the non-executable entry point before running it. - Full suite recorded 113 passes, zero failures and three capability skips on the RTX 3060 Ti/WARP system. The skips were `bvh_wave64_hardware`, `bvh_wave64_warp` and `wave_shuffle_warp`. Built and tested entirely from the repository's fixtures. AI-assisted.
R0Y00 commented 2026-11-24 06:47:54 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by R0Y00 on 2026-11-21T19:19:20Z.


@yummancito, your e238a653 fixture also runs the unchanged #228 chain successfully on my Windows machine (#249). To finish the requested startup checks I appended a maintainer edit, d9ac3b4, preserving your implementation. In an isolated Windows x64 build, cpu_native_exe_start passed the no-entry, null-output and unreadable-callback-table cases. Each refused before changing the fixture data and preserved the supplied sentinel. Original/no-TLS/changed-import results remained 0x60d/0x20d/0x60e. Thanks for the startup API.

ctest --test-dir out/start-contract/out/build/dev --no-tests=error -R '^cpu_native_exe_start$' -V

<!-- greyfinch-sync:R0Y00/Xbox2Windows:comment:7067935076 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/238#issuecomment-7067935076). Originally posted by [R0Y00](https://github.com/R0Y00) on 2026-11-21T19:19:20Z. --- @yummancito, your e238a653 fixture also runs the unchanged #228 chain successfully on my Windows machine (#249). To finish the requested startup checks I appended a maintainer edit, d9ac3b4, preserving your implementation. In an isolated Windows x64 build, cpu_native_exe_start passed the no-entry, null-output and unreadable-callback-table cases. Each refused before changing the fixture data and preserved the supplied sentinel. Original/no-TLS/changed-import results remained 0x60d/0x20d/0x60e. Thanks for the startup API. `ctest --test-dir out/start-contract/out/build/dev --no-tests=error -R '^cpu_native_exe_start$' -V`
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!235
No description provided.