cpu: compare the lifecycle of two DLLs with the C runtime under Windows and pe_map #292

Merged
yummancito merged 1 commit from upstream-pr/296 into main 2026-11-21 21:27:12 +00:00
yummancito commented 2026-11-21 21:09:46 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by yummancito on 2026-11-21T21:09:46Z.


Refs #165

Change

Adds a differential test for the lifecycle of two DLLs built with the static MSVC runtime (/MT), under LoadLibraryExW/FreeLibrary and under pe_map_image/pe_map_export/pe_map_release. Only tests and documentation; no change to tools/pe_map.cpp.

  • cpu_life_a.dll and cpu_life_b.dll keep their default entry point, so _DllMainCRTStartup runs a real C++ global constructor before DllMain(DLL_PROCESS_ATTACH) and a real global destructor after DllMain(DLL_PROCESS_DETACH). Nothing calls them by hand. The DLLs are linked without /INCREMENTAL, so the entry point is the runtime's function and not an incremental-link thunk.
  • B imports A. A's constructor sets its state to 7. B's constructor stores a_value() * 2 and its destructor reads A's state again. b_via_a() depends on A's state at the time of the call.
  • cpu_life_sink.dll is a small CRT-free DLL that the test host loads first and keeps for the whole child process. Every constructor, destructor and DllMain call logs a token into it, so it is still valid during the last destructor. DllMain only calls into the sink and takes no lock.
  • Each constructor records its callers with RtlCaptureContext, RtlLookupFunctionEntry and RtlVirtualUnwind. The test requires the function that _DllMainCRTStartup tail-jumps to on the constructor's stack, with the loader's (or pe_map's) frame directly above it.

What the test (cpu_native_crt_lifecycle) does

Child processes with a 30 s timeout. The scenario loads B, calls the exports, changes A's state with a_set(99), unloads, reloads, unloads, and runs two more load/unload cycles. Three fresh processes per loader must report identical stages, and the Windows and pe_map stages are compared event by event, value by value.

Responsibilities. For pe_map, the harness orders the images from the import tables, maps A before B, serves B's import of A with pe_map_export and releases B before A. pe_map does none of that: it keeps no dependency graph, no module table and no reference counts. Windows does all of it itself. This PR does not claim otherwise, and the test shows it (below).

Windows 11 Pro build 26300, MSVC 19.44.35228, CMake 3.31.6, base d311159:

cmake --preset dev --fresh
cmake --build --preset dev
ctest --preset dev --no-tests=error -R '^cpu_native_crt_lifecycle$' -V
load:          A:ctor, A:attach, B:ctor, B:attach          a_value 7, b_value 14, b_via_a 21
a_set(99):     (no events)                                 a_value 99, b_via_a 113
unload:        B:detach, B:dtor a=9, A:detach, A:dtor      A and B no longer mapped
reload:        A:ctor, A:attach, B:ctor, B:attach          a_value 7 (fresh state), b_via_a 21
unload again:  B:detach, B:dtor a=7, A:detach, A:dtor      A and B no longer mapped
cycles 3, 4:   the same load and unload lines

Identical under both loaders. B's destructor still sees A alive, with A's state at that moment. Residency is GetModuleHandleW for Windows and VirtualQuery on the mapped base right after the releases for pe_map.

  • Controls (identical under both loaders): without A's constructor (cpu_life_a_noctor) the load logs A:attach, B:ctor, B:attach and every value is 0; without B's destructor (cpu_life_b_nodtor) the unload logs B:detach, A:detach, A:dtor.
  • Differences the test pins: Windows counts references (a second LoadLibrary of B returns the same module and runs nothing; A stays loaded while B or the harness holds it). pe_map has none: mapping B twice gives a second image that runs its constructor again. Windows loaded B at the same address in all three processes (system ASLR is per boot); pe_map mapped it at a different address each time, never at the preferred base.
  • Mutation checks, not in CI (made on the previous base, c832696; finalize() is unchanged): making finalize() skip the process detach, or not free the image, each made the test fail.

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

Not covered

  • Releasing A before B, because it would touch released memory, and double release or foreign pointers, which cpu_native_lifetime already checks.
  • Worker-thread notifications for these DLLs, implicit TLS and C++ exceptions in mapped images.
  • A single machine; one MSVC version.

This shows two DLLs with the C runtime starting and stopping in the same order, with the same state, under both loaders when the harness supplies the order; it does not show that any title starts. Docs: docs/experiments.md, "Lifecycle of two DLLs with the static C runtime".

AI-assisted.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:issue:6534429439 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/296). Originally posted by [yummancito](https://github.com/yummancito) on 2026-11-21T21:09:46Z. --- Refs #165 ## Change Adds a differential test for the lifecycle of two DLLs built with the static MSVC runtime (`/MT`), under `LoadLibraryExW`/`FreeLibrary` and under `pe_map_image`/`pe_map_export`/`pe_map_release`. Only tests and documentation; no change to `tools/pe_map.cpp`. - `cpu_life_a.dll` and `cpu_life_b.dll` keep their default entry point, so `_DllMainCRTStartup` runs a real C++ global constructor before `DllMain(DLL_PROCESS_ATTACH)` and a real global destructor after `DllMain(DLL_PROCESS_DETACH)`. Nothing calls them by hand. The DLLs are linked without `/INCREMENTAL`, so the entry point is the runtime's function and not an incremental-link thunk. - B imports A. A's constructor sets its state to 7. B's constructor stores `a_value() * 2` and its destructor reads A's state again. `b_via_a()` depends on A's state at the time of the call. - `cpu_life_sink.dll` is a small CRT-free DLL that the test host loads first and keeps for the whole child process. Every constructor, destructor and `DllMain` call logs a token into it, so it is still valid during the last destructor. `DllMain` only calls into the sink and takes no lock. - Each constructor records its callers with `RtlCaptureContext`, `RtlLookupFunctionEntry` and `RtlVirtualUnwind`. The test requires the function that `_DllMainCRTStartup` tail-jumps to on the constructor's stack, with the loader's (or `pe_map`'s) frame directly above it. ## What the test (`cpu_native_crt_lifecycle`) does Child processes with a 30 s timeout. The scenario loads B, calls the exports, changes A's state with `a_set(99)`, unloads, reloads, unloads, and runs two more load/unload cycles. Three fresh processes per loader must report identical stages, and the Windows and `pe_map` stages are compared event by event, value by value. **Responsibilities.** For `pe_map`, the harness orders the images from the import tables, maps A before B, serves B's import of A with `pe_map_export` and releases B before A. `pe_map` does none of that: it keeps no dependency graph, no module table and no reference counts. Windows does all of it itself. This PR does not claim otherwise, and the test shows it (below). Windows 11 Pro build 26300, MSVC 19.44.35228, CMake 3.31.6, base `d311159`: ```powershell cmake --preset dev --fresh cmake --build --preset dev ctest --preset dev --no-tests=error -R '^cpu_native_crt_lifecycle$' -V ``` ```text load: A:ctor, A:attach, B:ctor, B:attach a_value 7, b_value 14, b_via_a 21 a_set(99): (no events) a_value 99, b_via_a 113 unload: B:detach, B:dtor a=9, A:detach, A:dtor A and B no longer mapped reload: A:ctor, A:attach, B:ctor, B:attach a_value 7 (fresh state), b_via_a 21 unload again: B:detach, B:dtor a=7, A:detach, A:dtor A and B no longer mapped cycles 3, 4: the same load and unload lines ``` Identical under both loaders. B's destructor still sees A alive, with A's state at that moment. Residency is `GetModuleHandleW` for Windows and `VirtualQuery` on the mapped base right after the releases for `pe_map`. - **Controls** (identical under both loaders): without A's constructor (`cpu_life_a_noctor`) the load logs `A:attach, B:ctor, B:attach` and every value is 0; without B's destructor (`cpu_life_b_nodtor`) the unload logs `B:detach, A:detach, A:dtor`. - **Differences the test pins:** Windows counts references (a second `LoadLibrary` of B returns the same module and runs nothing; A stays loaded while B or the harness holds it). `pe_map` has none: mapping B twice gives a second image that runs its constructor again. Windows loaded B at the same address in all three processes (system ASLR is per boot); `pe_map` mapped it at a different address each time, never at the preferred base. - **Mutation checks, not in CI** (made on the previous base, `c832696`; `finalize()` is unchanged): making `finalize()` skip the process detach, or not free the image, each made the test fail. Full suite (`ctest --preset dev --no-tests=error`): 137 tests, 0 failures, 3 capability skips reported separately (`bvh_wave64_hardware`, `bvh_wave64_warp`, `wave_shuffle_warp`). ## Not covered - Releasing A before B, because it would touch released memory, and double release or foreign pointers, which `cpu_native_lifetime` already checks. - Worker-thread notifications for these DLLs, implicit TLS and C++ exceptions in mapped images. - A single machine; one MSVC version. This shows two DLLs with the C runtime starting and stopping in the same order, with the same state, under both loaders when the harness supplies the order; it does not show that any title starts. Docs: `docs/experiments.md`, "Lifecycle of two DLLs with the static C runtime". _AI-assisted._
upstream-sync closed this pull request 2026-11-24 06:42:17 +00:00
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!292
No description provided.