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

Manually merged
github merged 2 commits from gh-pr/2590/head into gh-pr/2590/base 2026-11-27 01:05:10 +00:00
Owner

@⁠thepatrickworld on GitHub · 2026-11-25 22:13 UTC · original
thepatrickworld:panam/2572-teardown → main · +426 −0 · 8 files · 2 commits · merged 2026-11-25 22:24 UTC

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.

**@⁠thepatrickworld** on GitHub · 2026-11-25 22:13 UTC · [original](https://github.com/R0Y00/Xbox2Windows/pull/2590) `thepatrickworld:panam/2572-teardown` → `main` · +426 −0 · 8 files · 2 commits · merged 2026-11-25 22:24 UTC ## 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._
The indexed-validation pipeline and the rectangle-list converter own a
device, root signatures and pipeline states for as long as the shim is
loaded. They are function-local statics, so they are destroyed during the
module's CRT teardown, which runs at process exit as well as on FreeLibrary.
At process exit the display driver is already detached, and releasing a
pipeline state from the destructor reaches a stopped driver (amdxc64,
0x35f2a19, through D3D12Core!CPipelineState::FinalRelease), which ends the
process with __fastfail(7). DllMain now records which of the two detaches is
running, and both destructors detach their references on the terminating
path and release them otherwise. Device keying is unchanged.

tests/startup_teardown_unload.cpp loads the built d3d12_xs.dll by path,
warms both caches through a probe, unloads it and checks that the objects
come back, then leaves a second load to process termination.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-27 01:05:08 +00:00
github manually merged commit 1e903e8cb3 into gh-pr/2590/base 2026-11-27 01:05:10 +00:00
Author
Owner

@⁠ChazY11 on GitHub · 2026-11-25 22:52 UTC · original

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.

**@⁠ChazY11** on GitHub · 2026-11-25 22:52 UTC · [original](https://github.com/R0Y00/Xbox2Windows/pull/2590#issuecomment-7176885643) ## 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.
This discussion has been locked. Commenting is limited to contributors.
No description provided.