runtime: make RewrittenBlob COM refcount atomic in d3d12_xs_rootsig #2024

Closed
leonida/mandy
quietfold wants to merge 0 commits from fix/rt-xs-rootsig-atomic-blob-refcount into main

Pull request closed

This pull request cannot be reopened because the branch was deleted.

quietfold commented 2026-11-25 18:24:13 +00:00

Coordination item: RT-XS-ROOTSIG-ATOMIC-BLOB-REFCOUNT (claimed on #1608).

Why

RewrittenBlob in runtime/shims/d3d12_xs_rootsig.cpp (returned by serialize_versioned_keeping_heap_flags and serialize_legacy_keeping_heap_flags when retrying directly-indexed root-signature flags) implements the COM ID3DBlob interface with a plain non-atomic ULONG refs_ = 1 incremented/decremented via ++refs_ / --refs_. COM objects handed back to the title can have AddRef/Release/QueryInterface called concurrently from multiple worker threads (e.g. pipeline/root-signature build threads), which causes a data race, lost increments/decrements, and premature delete this / use-after-free or leaks.

Changes

  • runtime/shims/d3d12_xs_rootsig.cpp: change RewrittenBlob::refs_ to std::atomic<ULONG> refs_{1} using fetch_add(1, std::memory_order_relaxed) + 1 in AddRef() and fetch_sub(1, std::memory_order_acq_rel) - 1 in Release().
  • runtime/test/root_sig_test.cpp: add test_rewritten_blob_concurrent_refcount() exercising serialize_versioned_keeping_heap_flags()'s retry path, verifying restored RTS0 flags, and stressing 4 threads × 20,000 concurrent AddRef/Release pairs on the returned RewrittenBlob.

Verification

  • AI-assisted: yes (Antigravity agent by Google DeepMind).
  • GPUs tested: none (CPU-only verification on macOS arm64 using minimal local d3d12.h COM/struct stubs; Windows CI / full root_sig_test covers the live host d3d12.dll paths).
  • Compiled runtime/shims/d3d12_xs_rootsig.cpp + concurrency test harness with clang++ -std=c++20 -Wall -Wextra -Werror:
    • ASan+UBSan (-O1 -g -fsanitize=address,undefined): 10/10 PASS.
    • TSan (-O1 -g -fsanitize=thread): 10/10 PASS, 0 data races (negative control against main's d3d12_xs_rootsig.cpp reliably tripped WARNING: ThreadSanitizer: data race in RewrittenBlob::AddRef).
    • Release (-O2): 10/10 PASS.
  • python3 -I scripts/check_no_game_data.py runtime/shims/d3d12_xs_rootsig.cpp runtime/test/root_sig_test.cpp: clean.
  • python3 -I tests/test_text_write_line_endings.py: 3/3 OK.
Coordination item: `RT-XS-ROOTSIG-ATOMIC-BLOB-REFCOUNT` (claimed on #1608). ### Why `RewrittenBlob` in `runtime/shims/d3d12_xs_rootsig.cpp` (returned by `serialize_versioned_keeping_heap_flags` and `serialize_legacy_keeping_heap_flags` when retrying directly-indexed root-signature flags) implements the COM `ID3DBlob` interface with a plain non-atomic `ULONG refs_ = 1` incremented/decremented via `++refs_` / `--refs_`. COM objects handed back to the title can have `AddRef`/`Release`/`QueryInterface` called concurrently from multiple worker threads (e.g. pipeline/root-signature build threads), which causes a data race, lost increments/decrements, and premature `delete this` / use-after-free or leaks. ### Changes - `runtime/shims/d3d12_xs_rootsig.cpp`: change `RewrittenBlob::refs_` to `std::atomic<ULONG> refs_{1}` using `fetch_add(1, std::memory_order_relaxed) + 1` in `AddRef()` and `fetch_sub(1, std::memory_order_acq_rel) - 1` in `Release()`. - `runtime/test/root_sig_test.cpp`: add `test_rewritten_blob_concurrent_refcount()` exercising `serialize_versioned_keeping_heap_flags()`'s retry path, verifying restored RTS0 flags, and stressing 4 threads × 20,000 concurrent `AddRef`/`Release` pairs on the returned `RewrittenBlob`. ### Verification - AI-assisted: yes (Antigravity agent by Google DeepMind). - GPUs tested: none (CPU-only verification on macOS arm64 using minimal local `d3d12.h` COM/struct stubs; Windows CI / full `root_sig_test` covers the live host `d3d12.dll` paths). - Compiled `runtime/shims/d3d12_xs_rootsig.cpp` + concurrency test harness with `clang++ -std=c++20 -Wall -Wextra -Werror`: - ASan+UBSan (`-O1 -g -fsanitize=address,undefined`): 10/10 PASS. - TSan (`-O1 -g -fsanitize=thread`): 10/10 PASS, 0 data races (negative control against `main`'s `d3d12_xs_rootsig.cpp` reliably tripped `WARNING: ThreadSanitizer: data race` in `RewrittenBlob::AddRef`). - Release (`-O2`): 10/10 PASS. - `python3 -I scripts/check_no_game_data.py runtime/shims/d3d12_xs_rootsig.cpp runtime/test/root_sig_test.cpp`: clean. - `python3 -I tests/test_text_write_line_endings.py`: 3/3 OK.
copilot-pull-request-reviewer[bot] reviewed 2026-11-25 18:24:21 +00:00
copilot-pull-request-reviewer[bot] left a comment
No description provided.
Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.
zmasi commented 2026-11-28 11:58:00 +00:00

#2024 | head f1d796cdaf14efd21828eb18af5a81cf46532969 (merged cleanly with main 4150b1a1cd9de753749e0417ff71a450b6ce92cc) | ctest --test-dir runtime/build -C RelWithDebInfo -R "^(root_sig|root_sig_public_flags)$" --output-on-failure (10 stress iterations) | red on main: root_sig crashes on attempt 1 with 0xc0000374 (STATUS_HEAP_CORRUPTION from concurrent non-atomic RewrittenBlob AddRef/Release double-free) | green on PR+main: 10/10 stress runs pass (root_sig + root_sig_public_flags, 20/20 CTest invocations, 0 failures); check_no_game_data.py 0 violations | Windows 11, MSVC 19.44, RTX 3090, Vulkan SDK 1.4.363.1 | AI-assisted: yes

#2024 | head f1d796cdaf14efd21828eb18af5a81cf46532969 (merged cleanly with main 4150b1a1cd9de753749e0417ff71a450b6ce92cc) | `ctest --test-dir runtime/build -C RelWithDebInfo -R "^(root_sig|root_sig_public_flags)$" --output-on-failure` (10 stress iterations) | red on main: `root_sig` crashes on attempt 1 with `0xc0000374` (`STATUS_HEAP_CORRUPTION` from concurrent non-atomic `RewrittenBlob` `AddRef`/`Release` double-free) | green on PR+main: 10/10 stress runs pass (`root_sig` + `root_sig_public_flags`, 20/20 CTest invocations, `0` failures); `check_no_game_data.py` `0` violations | Windows 11, MSVC 19.44, RTX 3090, Vulkan SDK 1.4.363.1 | AI-assisted: yes
OreoFR commented 2026-11-28 21:58:07 +00:00

Verified by OreoFR

  • Base: main c1ecf04ed + PR head f1d796cda; green run in a merge package of 16 PRs, no new ctest failure vs main
  • Test: ctest -R ^root_sig$
  • Red on main: OK
  • Green with the PR: OK
  • Verdict: NO RED (atomic refcount: the race is not reproduced on main)
  • Machine: RTX 4070 Laptop 8 GB, NVIDIA 621.40, Intel Core Ultra 7 155H, Windows 11

AI-assisted (Claude Code, Claude Opus 5.5).

**Verified by OreoFR** - Base: main `c1ecf04ed` + PR head `f1d796cda`; green run in a merge package of 16 PRs, no new ctest failure vs main - Test: `ctest -R ^root_sig$` - Red on main: OK - Green with the PR: OK - Verdict: NO RED (atomic refcount: the race is not reproduced on main) - Machine: RTX 4070 Laptop 8 GB, NVIDIA 621.40, Intel Core Ultra 7 155H, Windows 11 AI-assisted (Claude Code, Claude Opus 5.5).
aslan2142 commented 2026-12-01 16:41:19 +00:00

Closing this PR because the legacy C++ Vulkan runtime (and legacy Python shader compiler) is now considered deprecated in favor of the new Rust Vulkan runtime (in progress) and the Rust DX12 runtime (d3d12/) available now. Thank you for the contribution! If you believe this PR was closed by mistake (for example, if it still applies to active tools, d3d12/, or general docs), please feel free to reopen it.

Closing this PR because the legacy C++ Vulkan runtime (and legacy Python shader compiler) is now considered deprecated in favor of the new Rust Vulkan runtime (in progress) and the Rust DX12 runtime (`d3d12/`) available now. Thank you for the contribution! If you believe this PR was closed by mistake (for example, if it still applies to active tools, `d3d12/`, or general docs), please feel free to reopen it.
Sign in to join this conversation.
No description provided.