cpu: compare the lifecycle of two DLLs with the C runtime under Windows and pe_map #292
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/296"
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 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), underLoadLibraryExW/FreeLibraryand underpe_map_image/pe_map_export/pe_map_release. Only tests and documentation; no change totools/pe_map.cpp.cpu_life_a.dllandcpu_life_b.dllkeep their default entry point, so_DllMainCRTStartupruns a real C++ global constructor beforeDllMain(DLL_PROCESS_ATTACH)and a real global destructor afterDllMain(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.a_value() * 2and its destructor reads A's state again.b_via_a()depends on A's state at the time of the call.cpu_life_sink.dllis a small CRT-free DLL that the test host loads first and keeps for the whole child process. Every constructor, destructor andDllMaincall logs a token into it, so it is still valid during the last destructor.DllMainonly calls into the sink and takes no lock.RtlCaptureContext,RtlLookupFunctionEntryandRtlVirtualUnwind. The test requires the function that_DllMainCRTStartuptail-jumps to on the constructor's stack, with the loader's (orpe_map's) frame directly above it.What the test (
cpu_native_crt_lifecycle) doesChild 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 andpe_mapstages 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 withpe_map_exportand releases B before A.pe_mapdoes 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:Identical under both loaders. B's destructor still sees A alive, with A's state at that moment. Residency is
GetModuleHandleWfor Windows andVirtualQueryon the mapped base right after the releases forpe_map.cpu_life_a_noctor) the load logsA:attach, B:ctor, B:attachand every value is 0; without B's destructor (cpu_life_b_nodtor) the unload logsB:detach, A:detach, A:dtor.LoadLibraryof B returns the same module and runs nothing; A stays loaded while B or the harness holds it).pe_maphas 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_mapmapped it at a different address each time, never at the preferred base.c832696;finalize()is unchanged): makingfinalize()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
cpu_native_lifetimealready checks.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.