runtime: make RewrittenBlob COM refcount atomic in d3d12_xs_rootsig #2024

Open
github-bot wants to merge 1 commit from github-bot/gh-2024 into main AGit
Owner

@⁠IlyassRS3 on GitHub · 2026-11-25 18:24 UTC · original
IlyassRS3:fix/rt-xs-rootsig-atomic-blob-refcount → main · +84 −3 · 2 files · 1 commits · open

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.
**@⁠IlyassRS3** on GitHub · 2026-11-25 18:24 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/2024) `IlyassRS3:fix/rt-xs-rootsig-atomic-blob-refcount` → `main` · +84 −3 · 2 files · 1 commits · open 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.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 10:15:06 +00:00
Author
Owner

@⁠copilot-pull-request-reviewer[bot] on GitHub · 2026-11-25 18:24 UTC · original · reviewed

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.

**@⁠copilot-pull-request-reviewer[bot]** on GitHub · 2026-11-25 18:24 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/2024#pullrequestreview-6062834276) · **reviewed** 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.
This pull request can be merged automatically.
This branch is out-of-date with the base branch
You are not authorized to merge this pull request.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin '+refs/pull/2024/head:github-bot/gh-2024'
git switch 'github-bot/gh-2024'

Merge

Merge the changes and update on Forgejo.
git switch 'main'
git merge --no-ff 'github-bot/gh-2024'
git switch 'github-bot/gh-2024'
git rebase 'main'
git switch 'main'
git merge --ff-only 'github-bot/gh-2024'
git switch 'github-bot/gh-2024'
git rebase 'main'
git switch 'main'
git merge --no-ff 'github-bot/gh-2024'
git switch 'main'
git merge --squash 'github-bot/gh-2024'
git switch 'main'
git merge --ff-only 'github-bot/gh-2024'
git switch 'main'
git merge 'github-bot/gh-2024'
git push origin 'main'
This discussion has been locked. Commenting is limited to contributors.
No description provided.