cpu: compare an EXE and two dependent DLLs under Windows and pe_map #259

Merged
yummancito merged 2 commits from upstream-pr/262 into main 2026-11-21 20:25:52 +00:00
yummancito commented 2026-11-21 19:38:50 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by yummancito on 2026-11-21T19:38:50Z.


Refs #165

Change

Adds a source-built, CRT-free fixture of three images and a differential test, using pe_map_run (#238) and no new pe_map code:

  • cpu_startup_a.dll has no imports. It keeps an event log and a value set by its initializer (7).
  • cpu_startup_b.dll imports A. Its initializer publishes a_value() * 2, so it reads 0 if it runs before A's.
  • cpu_startup_exe.exe imports both. Its TLS callback and entry point log events, then it runs two worker threads in turn. Each thread keeps its own value in a TlsAlloc slot (1, 2, 3). It writes the log to stdout and returns b_value | worker1 << 8 | worker2 << 16 | main << 24.

What this does and does not exercise. The DLL "initializers" are code in each DLL's DllMain (DLL_PROCESS_ATTACH), called by the loader or by pe_map. They are not C++ static constructors: there is no C runtime, so no _DllMainCRTStartup and no dynamic initializers run. The per-thread values use explicit TLS through TlsAlloc/TlsSetValue/TlsGetValue, not implicit __declspec(thread) data, which pe_map still refuses. No thread is created inside DllMain: the DLLs create none, and the EXE creates its two workers from its entry point.

What the test (cpu_native_startup) does

  • Native: launches the EXE with the Windows loader next to the two DLLs.
  • Mapped: a child process reads the import tables, orders the images dependencies first, maps each DLL, serves their imports with pe_map_export on the mapped images (kernel32 from the host) and starts the EXE with pe_map_run. The harness does the dependency ordering; pe_map does not.
  • First asserts the dependencies from the PE import tables: A imports none of the fixture DLLs, B imports exactly a_log and a_value from A, the EXE imports from A and B.
  • Compares the event log, exit code, stdout and stderr. Every process runs with a 30 s timeout, the mapped side in its own child process.

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

cmake --preset dev --fresh
cmake --build --preset dev
ctest --preset dev --no-tests=error -R '^cpu_native_startup$' -V
native: LOG:abtew2w3m1 exit 0x103020e
mapped: LOG:abtew2w3m1 exit 0x103020e
control without B's initializer: LOG:atew2w3m1 exit 0x1030200 under both loaders
control with another A under the imported name: LOG:abtew2w3m1 exit 0x1030212 under both loaders
mapping the EXE before its DLLs: unresolved import cpu_startup_b.dll!b_value

Under both loaders a and b ran once and in dependency order, the TLS callback ran after the DLL initializers and before the entry point, and the entry point ran once. stderr was empty on both sides. One-off manual check, not in CI (made before rebasing; pe_map_run is unchanged): with the TLS callback loop removed from pe_map_run, the test failed on mapped launch differs from native.

Full suite (ctest --preset dev --no-tests=error): 122 tests, 0 failures, 3 capability skips reported separately (bvh_wave64_hardware, bvh_wave64_warp, wave_shuffle_warp).

Not covered

  • Worker-thread DLL_THREAD_ATTACH/DETACH for mapped images (Windows delivers them, pe_map does not; see #139). The fixture does not count them, so this PR neither depends on #139 nor hides the difference.
  • DLLs built with the C runtime (C++ constructors) and implicit TLS data, which pe_map refuses as "TLS data".
  • DLL_PROCESS_DETACH order at exit and GetModuleHandle(NULL).

This shows one EXE and two DLLs starting in the same order with the same log under both loaders; it does not show that any title starts. Docs: docs/experiments.md, "Starting an EXE with two DLLs".

AI-assisted.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:issue:6533502688 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/262). Originally posted by [yummancito](https://github.com/yummancito) on 2026-11-21T19:38:50Z. --- Refs #165 ## Change Adds a source-built, CRT-free fixture of three images and a differential test, using `pe_map_run` (#238) and no new `pe_map` code: - `cpu_startup_a.dll` has no imports. It keeps an event log and a value set by its initializer (7). - `cpu_startup_b.dll` imports A. Its initializer publishes `a_value() * 2`, so it reads 0 if it runs before A's. - `cpu_startup_exe.exe` imports both. Its TLS callback and entry point log events, then it runs two worker threads in turn. Each thread keeps its own value in a `TlsAlloc` slot (1, 2, 3). It writes the log to stdout and returns `b_value | worker1 << 8 | worker2 << 16 | main << 24`. **What this does and does not exercise.** The DLL "initializers" are code in each DLL's `DllMain` (`DLL_PROCESS_ATTACH`), called by the loader or by `pe_map`. They are **not C++ static constructors**: there is no C runtime, so no `_DllMainCRTStartup` and no dynamic initializers run. The per-thread values use explicit TLS through `TlsAlloc`/`TlsSetValue`/`TlsGetValue`, **not implicit `__declspec(thread)` data**, which `pe_map` still refuses. No thread is created inside `DllMain`: the DLLs create none, and the EXE creates its two workers from its entry point. ## What the test (`cpu_native_startup`) does - Native: launches the EXE with the Windows loader next to the two DLLs. - Mapped: a child process reads the import tables, orders the images dependencies first, maps each DLL, serves their imports with `pe_map_export` on the mapped images (kernel32 from the host) and starts the EXE with `pe_map_run`. The harness does the dependency ordering; `pe_map` does not. - First asserts the dependencies from the PE import tables: A imports none of the fixture DLLs, B imports exactly `a_log` and `a_value` from A, the EXE imports from A and B. - Compares the event log, exit code, stdout and stderr. Every process runs with a 30 s timeout, the mapped side in its own child process. Windows 11 Pro build 26300, VS Build Tools 17.14 (MSVC 19.44), CMake 3.31.6, Python 3.13, base `7233cec`: ```powershell cmake --preset dev --fresh cmake --build --preset dev ctest --preset dev --no-tests=error -R '^cpu_native_startup$' -V ``` ```text native: LOG:abtew2w3m1 exit 0x103020e mapped: LOG:abtew2w3m1 exit 0x103020e control without B's initializer: LOG:atew2w3m1 exit 0x1030200 under both loaders control with another A under the imported name: LOG:abtew2w3m1 exit 0x1030212 under both loaders mapping the EXE before its DLLs: unresolved import cpu_startup_b.dll!b_value ``` Under both loaders `a` and `b` ran once and in dependency order, the TLS callback ran after the DLL initializers and before the entry point, and the entry point ran once. stderr was empty on both sides. One-off manual check, not in CI (made before rebasing; `pe_map_run` is unchanged): with the TLS callback loop removed from `pe_map_run`, the test failed on `mapped launch differs from native`. Full suite (`ctest --preset dev --no-tests=error`): 122 tests, 0 failures, 3 capability skips reported separately (`bvh_wave64_hardware`, `bvh_wave64_warp`, `wave_shuffle_warp`). ## Not covered - Worker-thread `DLL_THREAD_ATTACH`/`DETACH` for mapped images (Windows delivers them, `pe_map` does not; see #139). The fixture does not count them, so this PR neither depends on #139 nor hides the difference. - DLLs built with the C runtime (C++ constructors) and implicit TLS data, which `pe_map` refuses as "TLS data". - `DLL_PROCESS_DETACH` order at exit and `GetModuleHandle(NULL)`. This shows one EXE and two DLLs starting in the same order with the same log under both loaders; it does not show that any title starts. Docs: `docs/experiments.md`, "Starting an EXE with two DLLs". _AI-assisted._
upstream-sync closed this pull request 2026-11-24 06:42:06 +00:00
R0Y00 commented 2026-11-24 06:47:57 +00:00 (Migrated from github.com)

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


In the new exe_entry fixture the return values of CreateThread and WaitForSingleObject are ignored. If a wait times out, the code closes the handle, reads g_worker_value, and starts the next iteration while the prior worker could still be updating the shared log. Please require WAIT_OBJECT_0 and a valid worker handle before consuming the result, and make worker setup or timeout fail the isolated child deterministically. Keep the normal and reversed-initializer comparisons.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:comment:7068426971 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/262#issuecomment-7068426971). Originally posted by [R0Y00](https://github.com/R0Y00) on 2026-11-21T19:59:46Z. --- In the new `exe_entry` fixture the return values of `CreateThread` and `WaitForSingleObject` are ignored. If a wait times out, the code closes the handle, reads `g_worker_value`, and starts the next iteration while the prior worker could still be updating the shared log. Please require `WAIT_OBJECT_0` and a valid worker handle before consuming the result, and make worker setup or timeout fail the isolated child deterministically. Keep the normal and reversed-initializer comparisons.
yummancito commented 2026-11-24 06:47:58 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by yummancito on 2026-11-21T20:12:48Z.


Thanks, good catch. Fixed in aa7edaf (same branch, no force push).

The flaw. In startup_exe.cpp, exe_entry ignored the results of CreateThread and WaitForSingleObject. I reproduced it with the previous source, a 1 ms wait and a worker that sleeps 300 ms: the EXE did not fail. It printed LOG:abtem1 (no worker event) and exited with 0, having read a worker that had not finished and started the next one.

The fix. The entry point now checks TlsAlloc and the thread handle, and requires WAIT_OBJECT_0 before it reads g_worker_value or starts the next worker. Otherwise it calls ExitProcess, which stops every other thread. CloseHandle would only release the handle and leave the thread running. Exit codes:

  • 0xE0000001 TlsAlloc failed
  • 0xE0000002 CreateThread failed
  • 0xE0000003 wait timed out
  • 0xE0000004 wait failed
  • 0xE0000005 unexpected wait result

The wait inside the EXE is now 10 s, below the harness's 30 s process limit, so the code is observed before the external timeout.

Tests. cpu_native_startup keeps the normal comparison, the reversed-order check and the two initializer controls, and adds two controls that must end both the native process and the pe_map child with the expected code and no output:

  • a worker that sleeps past a 1 ms wait: 0xe0000003 under both loaders (about 1 s of wall time);
  • a thread requested with a 2^62-byte stack: 0xe0000002 under both loaders. It fails with ERROR_INVALID_PARAMETER (87) because the size exceeds the address space, not because memory ran out. 100 of 100 runs gave the same code.

As a one-off manual check, not in CI, ignoring the timeout made the timeout control fail. Full suite on Windows 11 build 26300: 122 tests, 0 failures, 3 capability skips (bvh_wave64_hardware, bvh_wave64_warp, wave_shuffle_warp).

AI-assisted.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:comment:7068587277 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/262#issuecomment-7068587277). Originally posted by [yummancito](https://github.com/yummancito) on 2026-11-21T20:12:48Z. --- Thanks, good catch. Fixed in `aa7edaf` (same branch, no force push). **The flaw.** In `startup_exe.cpp`, `exe_entry` ignored the results of `CreateThread` and `WaitForSingleObject`. I reproduced it with the previous source, a 1 ms wait and a worker that sleeps 300 ms: the EXE did not fail. It printed `LOG:abtem1` (no worker event) and exited with 0, having read a worker that had not finished and started the next one. **The fix.** The entry point now checks `TlsAlloc` and the thread handle, and requires `WAIT_OBJECT_0` before it reads `g_worker_value` or starts the next worker. Otherwise it calls `ExitProcess`, which stops every other thread. `CloseHandle` would only release the handle and leave the thread running. Exit codes: - `0xE0000001` `TlsAlloc` failed - `0xE0000002` `CreateThread` failed - `0xE0000003` wait timed out - `0xE0000004` wait failed - `0xE0000005` unexpected wait result The wait inside the EXE is now 10 s, below the harness's 30 s process limit, so the code is observed before the external timeout. **Tests.** `cpu_native_startup` keeps the normal comparison, the reversed-order check and the two initializer controls, and adds two controls that must end both the native process and the `pe_map` child with the expected code and no output: - a worker that sleeps past a 1 ms wait: `0xe0000003` under both loaders (about 1 s of wall time); - a thread requested with a 2^62-byte stack: `0xe0000002` under both loaders. It fails with `ERROR_INVALID_PARAMETER` (87) because the size exceeds the address space, not because memory ran out. 100 of 100 runs gave the same code. As a one-off manual check, not in CI, ignoring the timeout made the timeout control fail. Full suite on Windows 11 build 26300: 122 tests, 0 failures, 3 capability skips (`bvh_wave64_hardware`, `bvh_wave64_warp`, `wave_shuffle_warp`). _AI-assisted._
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!259
No description provided.