startup: release the per-device D3D12 caches only when the module is unloaded #2582

Merged
thepatrickworld merged 2 commits from upstream-pr/2590 into main 2026-11-25 22:24:33 +00:00
thepatrickworld commented 2026-11-25 22:13:12 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by thepatrickworld on 2026-11-25T22:13:12Z.


What / Why

Refs #2572. Two caches keep desktop D3D12 objects for a device for as long as the module that holds them is loaded: the indexed-validation pipeline (startup_stubs/d3d12_draw.inl) and the rectangle-list argument converter (startup_stubs/rect_indirect_args.h). Both are function-local statics, so they are destroyed during the module's CRT teardown — and that teardown runs at two different moments.

At process exit Windows has already detached the display driver, so releasing a pipeline state from those destructors reaches a stopped driver (amdxc64, offset 0x35f2a19, through D3D12Core!CPipelineState::FinalRelease) and the loader's __fastfail(7) turns it into 0xC0000409. That is what killed the five startup_indirect_*_hardware runs. FreeLibrary unloads the module while the process and the driver are alive, and there every owned object must be released.

Change

  • src/d3d12_xs_shim.cpp: DllMain records which detach is running — lpReserved is non-null only when the process is terminating, and the user entry runs before this module's atexit table, so the destructors see the flag.
  • src/startup_stubs/d3d12_teardown.h: the flag, with the boundary written down, and why an inline variable is the right scope (each module that includes the caches keeps releasing normally).
  • startup_stubs/d3d12_draw.inl, startup_stubs/rect_indirect_args.h: explicit destructors that detach their references on the terminating path and release them otherwise. Device keying is unchanged, as asked.
  • src/startup_stubs/d3d12_xs_device.cpp: x2w_d3d12_cached_objects_probe, which warms both caches and reports how many objects they own for a device (5 when both are warm).
  • tests/startup_teardown_unload.cpp + two CMake entries (warp, hardware): loads the built d3d12_xs.dll by path, warms both caches through the probe, FreeLibrary, and checks that the objects came back and the module is gone; a second load is left loaded so process termination exercises the other path.
  • docs/experiments.md: one section, and the reproduction commands.

Test

Built and run at fb5f5e62 on main 8f045aee, tree clean:

cmake --preset dev -DINTAKE_PYTHON=C:/Users/walde/Downloads/x2w-pr641/out/pe-env/Scripts/python.exe
cmake --build --preset dev --target d3d12_xs_shim startup_indirect_draw_indexed startup_teardown_unload
ctest --preset dev --no-tests=error -R "^startup_teardown_unload_" -V
ctest --preset dev --no-tests=error -R "^startup_indirect_(translated_indexed|rectlist)" -V

Windows 11 25H2 26200.9974, MSVC 19.44.35230, AMD Radeon RX 9070 XT (1002:7550, driver 32.0.32029.2010), d3d12SDKLayers.dll absent so no debug layer.

  • startup_teardown_unload: passes on WARP and on the card.
  • The five startup_indirect_*_hardware runs now reach their last assertion and exit 0 (the executables' own status, run directly with their add_test arguments); all five died with 0xC0000409 before.
  • Per-site controls, each rebuilt from the same tree with one destructor removed: without the validation-pipeline destructor the four translated_indexed* runs read -1073740791 and rect reads 0; without the converter destructor rect reads -1073740791 and the four read 0.

Not covered

An explicit unload while a title still references the device, any adapter other than the one above, and the third static the issue names (ComPtr<ID3D12Resource> pages in d3d12_xs_device.cpp:4103, cross-adapter placed-buffer path), which this patch does not touch. Logs and dumps stay local.

AI-assisted.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:issue:6614822275 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/2590). Originally posted by [thepatrickworld](https://github.com/thepatrickworld) on 2026-11-25T22:13:12Z. --- ## What / Why Refs #2572. Two caches keep desktop D3D12 objects for a device for as long as the module that holds them is loaded: the indexed-validation pipeline (`startup_stubs/d3d12_draw.inl`) and the rectangle-list argument converter (`startup_stubs/rect_indirect_args.h`). Both are function-local statics, so they are destroyed during the module's CRT teardown — and that teardown runs at two different moments. At process exit Windows has already detached the display driver, so releasing a pipeline state from those destructors reaches a stopped driver (`amdxc64`, offset `0x35f2a19`, through `D3D12Core!CPipelineState::FinalRelease`) and the loader's `__fastfail(7)` turns it into `0xC0000409`. That is what killed the five `startup_indirect_*_hardware` runs. `FreeLibrary` unloads the module while the process and the driver are alive, and there every owned object must be released. ## Change - `src/d3d12_xs_shim.cpp`: `DllMain` records which detach is running — `lpReserved` is non-null only when the process is terminating, and the user entry runs before this module's `atexit` table, so the destructors see the flag. - `src/startup_stubs/d3d12_teardown.h`: the flag, with the boundary written down, and why an inline variable is the right scope (each module that includes the caches keeps releasing normally). - `startup_stubs/d3d12_draw.inl`, `startup_stubs/rect_indirect_args.h`: explicit destructors that detach their references on the terminating path and release them otherwise. Device keying is unchanged, as asked. - `src/startup_stubs/d3d12_xs_device.cpp`: `x2w_d3d12_cached_objects_probe`, which warms both caches and reports how many objects they own for a device (5 when both are warm). - `tests/startup_teardown_unload.cpp` + two CMake entries (`warp`, `hardware`): loads the built `d3d12_xs.dll` by path, warms both caches through the probe, `FreeLibrary`, and checks that the objects came back and the module is gone; a second load is left loaded so process termination exercises the other path. - `docs/experiments.md`: one section, and the reproduction commands. ## Test Built and run at `fb5f5e62` on `main` `8f045aee`, tree clean: ```powershell cmake --preset dev -DINTAKE_PYTHON=C:/Users/walde/Downloads/x2w-pr641/out/pe-env/Scripts/python.exe cmake --build --preset dev --target d3d12_xs_shim startup_indirect_draw_indexed startup_teardown_unload ctest --preset dev --no-tests=error -R "^startup_teardown_unload_" -V ctest --preset dev --no-tests=error -R "^startup_indirect_(translated_indexed|rectlist)" -V ``` Windows 11 25H2 26200.9974, MSVC 19.44.35230, AMD Radeon RX 9070 XT (`1002:7550`, driver `32.0.32029.2010`), `d3d12SDKLayers.dll` absent so no debug layer. - `startup_teardown_unload`: passes on WARP and on the card. - The five `startup_indirect_*_hardware` runs now reach their last assertion and exit `0` (the executables' own status, run directly with their `add_test` arguments); all five died with `0xC0000409` before. - Per-site controls, each rebuilt from the same tree with one destructor removed: without the validation-pipeline destructor the four `translated_indexed*` runs read `-1073740791` and `rect` reads `0`; without the converter destructor `rect` reads `-1073740791` and the four read `0`. ## Not covered An explicit unload while a title still references the device, any adapter other than the one above, and the third static the issue names (`ComPtr<ID3D12Resource>` pages in `d3d12_xs_device.cpp:4103`, cross-adapter placed-buffer path), which this patch does not touch. Logs and dumps stay local. _AI-assisted._
upstream-sync closed this pull request 2026-11-25 23:01:57 +00:00
ChazY11 commented 2026-11-25 23:02:08 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by ChazY11 on 2026-11-25T22:52:26Z.


Built and run here: 2/2 pass, and four mutations of the teardown boundary are all caught — including the one that only changes when objects are released

ctest --preset dev -R 'teardown_unload'
    1/2 startup_teardown_unload_warp .......  Passed   0.48 sec
    2/2 startup_teardown_unload_hardware ...  Passed   6.28 sec
    100% tests passed, 0 tests failed out of 2

The distinction this PR is about

d3d12_teardown.h separates two detaches that look identical to a destructor:

  • process exit — Windows has already stopped the display driver, so releasing a D3D12 object reaches a stopped driver
    (D3D12Core!CPipelineState::FinalRelease) and the loader's __fastfail(7) turns it into 0xC0000409;
  • FreeLibrary — the process and driver are alive, so every owned object must be released or it leaks for the rest
    of the process.

DllMain's lpReserved is the discriminator, and src/d3d12_xs_shim.cpp:291 sets the flag from it. That is a
one-bool design, which means the failure mode of getting it backwards is a crash at exit or a silent leak — neither of
which an ordinary assertion catches. So I mutated the flag and both consumers.

mutation caught
d3d12_process_terminating initialised true (never release, in either path) caught
DllMain stops setting it (if (false) …) caught
the draw-cache guard inverted (if (!terminating) return; → if (terminating) return;) caught
the rectangle-list guard inverted caught

All four are caught by the single fixture, on both adapters. The two guard inversions matter most: inverting one of them
leaves the other correct, so the fixture must be reaching both caches — the draw-side pipeline cache and
rect_indirect_args.h — which is what the header claims and what a "just check the process doesn't crash" test would
miss.

One thing the design gets right that is easy to miss

inline bool d3d12_process_terminating is defined inline so each including module has its own copy. That means the
test executables, which include rect_indirect_args.h without owning a DllMain, keep releasing normally — only the DLL
that owns the DllMain can silence its own teardown. The comment says as much and the fixture depends on it: if the flag
were a single extern, silencing it in the DLL would also silence the test's own caches and the FreeLibrary half would
stop testing anything.

Method note

My mutation run was interrupted mid-way (the third of four). I checked the tree before continuing rather than assuming:
git status clean, and all four sites — the flag's initialiser, the DllMain assignment, and both guards — still in
their original form, with the draw guard still spelled if (!…) return;. Only then did I re-run the fourth mutation. The
result above is from a clean tree before and after.

AI-assisted; Windows 11 build 26300, WARP plus the installed physical GPU (Intel Arc 140T, driver 32.0.101.8132). Each
mutation rebuilt the target, ran the suite, reverted and rebuilt; final state 2/2 green and a clean working tree.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:comment:7176885643 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/2590#issuecomment-7176885643). Originally posted by [ChazY11](https://github.com/ChazY11) on 2026-11-25T22:52:26Z. --- ## Built and run here: 2/2 pass, and four mutations of the teardown boundary are all caught — including the one that only changes *when* objects are released ```text ctest --preset dev -R 'teardown_unload' 1/2 startup_teardown_unload_warp ....... Passed 0.48 sec 2/2 startup_teardown_unload_hardware ... Passed 6.28 sec 100% tests passed, 0 tests failed out of 2 ``` ### The distinction this PR is about `d3d12_teardown.h` separates two detaches that look identical to a destructor: - **process exit** — Windows has already stopped the display driver, so releasing a D3D12 object reaches a stopped driver (`D3D12Core!CPipelineState::FinalRelease`) and the loader's `__fastfail(7)` turns it into `0xC0000409`; - **`FreeLibrary`** — the process and driver are alive, so every owned object *must* be released or it leaks for the rest of the process. `DllMain`'s `lpReserved` is the discriminator, and `src/d3d12_xs_shim.cpp:291` sets the flag from it. That is a one-bool design, which means the failure mode of getting it backwards is a crash at exit or a silent leak — neither of which an ordinary assertion catches. So I mutated the flag and both consumers. | mutation | caught | |---|---| | `d3d12_process_terminating` initialised `true` (never release, in either path) | **caught** | | `DllMain` stops setting it (`if (false) …`) | **caught** | | the draw-cache guard inverted (`if (!terminating) return;` → `if (terminating) return;`) | **caught** | | the rectangle-list guard inverted | **caught** | All four are caught by the single fixture, on both adapters. The two guard inversions matter most: inverting one of them leaves the other correct, so the fixture must be reaching **both** caches — the draw-side pipeline cache *and* `rect_indirect_args.h` — which is what the header claims and what a "just check the process doesn't crash" test would miss. ### One thing the design gets right that is easy to miss `inline bool d3d12_process_terminating` is **defined inline** so each including module has its own copy. That means the test executables, which include `rect_indirect_args.h` without owning a `DllMain`, keep releasing normally — only the DLL that owns the `DllMain` can silence its own teardown. The comment says as much and the fixture depends on it: if the flag were a single extern, silencing it in the DLL would also silence the test's own caches and the `FreeLibrary` half would stop testing anything. ### Method note My mutation run was interrupted mid-way (the third of four). I checked the tree before continuing rather than assuming: `git status` clean, and all four sites — the flag's initialiser, the `DllMain` assignment, and both guards — still in their original form, with the draw guard still spelled `if (!…) return;`. Only then did I re-run the fourth mutation. The result above is from a clean tree before and after. AI-assisted; Windows 11 build 26300, WARP plus the installed physical GPU (Intel Arc 140T, driver 32.0.101.8132). Each mutation rebuilt the target, ran the suite, reverted and rebuilt; final state 2/2 green and a clean working tree.
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!2582
No description provided.