GPU: pin descriptor view resources against concurrent Resource::Release #3234

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

@⁠fuzeofficlal on GitHub · 2026-11-26 14:06 UTC · original
fuzeofficlal:fix/gpu-descriptor-view-drop-uaf → main · +403 −25 · 5 files · 1 commits · open

Problem

descriptor::VIEWS stores View::Shader(r, ...) and View::Unordered(r, counter, ...) as raw *const Resource pointers without holding COM references. Both descriptor::view_at and descriptor::view drop the VIEWS lock immediately after cloning View, leaving callers to dereference those raw resource pointers without synchronization against a concurrent ID3D12Resource::Release() on another title thread.

Under legal multi-threaded D3D12 usage — for example, when descriptor::sync_heaps() scans a dirty 64 KiB page of a placed shader-visible heap containing an abandoned slot for a resource being concurrently released on a title thread, when HostHeap::mirror() drops self.made to run prepare(&view), or when descriptor::make_read() creates placed textures in parallel across crate::texture::in_parallel — Resource::drop can run descriptor::forget and deallocate Box<Resource> while the mirroring, read-back, or drawing thread still holds the cloned View and subsequently dereferences it in prepare, note_address (buffer_bytes / texture_desc), make_marked (host_copy / buffer_bytes / title_format), null_for, viewed.set, view_addresses, evict, stamp_read, placed_texture, or draw::State.

Fix

  • Add object::try_add_ref and object::release_boxed_with in crates/victory-gpu/src/object.rs.
  • In resource::resource_release, invoke descriptor::forget(r as usize) via release_boxed_with before Box::from_raw constructs &mut Resource, so any try_add_ref under the VIEWS lock either observes refs == 0 (and treats the view as stale) or increments a live reference count without racing Box::from_raw or deallocation.
  • Introduce descriptor::PinnedView (a Send + Sync RAII guard storing resources: [usize; 2], implementing Deref<Target = View> and Debug, and releasing its pinned Resource COM references on Drop) returned by descriptor::view_at, descriptor::pinned_view, HostHeap::mirror, and HostHeap::unmade_placed.
    • Duplicate resource/counter pointers (r == counter) are pinned once.
    • On partial-pin failure (e.g. live resource with a dying counter), VIEWS is dropped before releasing the earlier pin so resource_release never re-enters VIEWS.
  • Route HostHeap (mirror, view_addresses, evict, unmade_placed, stamp_read, describe_slot), descriptor::make_read (holding Vec<PinnedView> across in_parallel and dropping before re-locking SHADER_HEAPS), and draw.rs view lookups through PinnedView.

Verification

Added two regression tests in crates/victory-gpu/src/descriptor/host.rs:

  • descriptor::host::tests::mirrored_view_pins_its_resource_until_dropped: verifies that a mirrored texture SRV, a dual-resource (buffer, counter) UAV, and a self-counter UAV keep their resources alive across a concurrent Release() (refs == 1) and complete final destruction (invalidating the view with zero leaked references) when the PinnedView drops. Fails deterministically on main (left: 0, right: 1).
  • descriptor::host::tests::concurrent_mirror_and_sync_survive_resource_release: 64-round Barrier(2) multi-threaded race between sync_slots / mirror / stamp_read / pinned_view / view_addresses and concurrent Release() in alternating release orders (covering dual-resource partial-pin rollback), asserting zero crashes, zero deadlocks, zero leaked references, and zero D3D12 debug errors.

Verified across all gates: rustfmt --edition 2024, hardware D3D12 and WARP (including VICTORY_D3D12_DEBUG=1 --features diagnostics), full victory-gpu WARP suite (248 passed, 0 failed, 1 ignored), workspace clippy --all-targets (default and --all-features) with -D warnings, and xtask repository policy.

**@⁠fuzeofficlal** on GitHub · 2026-11-26 14:06 UTC · [original](https://github.com/Marvisak/VIctory/pull/3234) `fuzeofficlal:fix/gpu-descriptor-view-drop-uaf` → `main` · +403 −25 · 5 files · 1 commits · open ### Problem `descriptor::VIEWS` stores `View::Shader(r, ...)` and `View::Unordered(r, counter, ...)` as raw `*const Resource` pointers without holding COM references. Both `descriptor::view_at` and `descriptor::view` drop the `VIEWS` lock immediately after cloning `View`, leaving callers to dereference those raw resource pointers without synchronization against a concurrent `ID3D12Resource::Release()` on another title thread. Under legal multi-threaded D3D12 usage — for example, when `descriptor::sync_heaps()` scans a dirty 64 KiB page of a placed shader-visible heap containing an abandoned slot for a resource being concurrently released on a title thread, when `HostHeap::mirror()` drops `self.made` to run `prepare(&view)`, or when `descriptor::make_read()` creates placed textures in parallel across `crate::texture::in_parallel` — `Resource::drop` can run `descriptor::forget` and deallocate `Box<Resource>` while the mirroring, read-back, or drawing thread still holds the cloned `View` and subsequently dereferences it in `prepare`, `note_address` (`buffer_bytes` / `texture_desc`), `make_marked` (`host_copy` / `buffer_bytes` / `title_format`), `null_for`, `viewed.set`, `view_addresses`, `evict`, `stamp_read`, `placed_texture`, or `draw::State`. ### Fix - Add `object::try_add_ref` and `object::release_boxed_with` in `crates/victory-gpu/src/object.rs`. - In `resource::resource_release`, invoke `descriptor::forget(r as usize)` via `release_boxed_with` *before* `Box::from_raw` constructs `&mut Resource`, so any `try_add_ref` under the `VIEWS` lock either observes `refs == 0` (and treats the view as stale) or increments a live reference count without racing `Box::from_raw` or deallocation. - Introduce `descriptor::PinnedView` (a `Send + Sync` RAII guard storing `resources: [usize; 2]`, implementing `Deref<Target = View>` and `Debug`, and releasing its pinned `Resource` COM references on `Drop`) returned by `descriptor::view_at`, `descriptor::pinned_view`, `HostHeap::mirror`, and `HostHeap::unmade_placed`. - Duplicate resource/counter pointers (`r == counter`) are pinned once. - On partial-pin failure (e.g. live resource with a dying counter), `VIEWS` is dropped before releasing the earlier pin so `resource_release` never re-enters `VIEWS`. - Route `HostHeap` (`mirror`, `view_addresses`, `evict`, `unmade_placed`, `stamp_read`, `describe_slot`), `descriptor::make_read` (holding `Vec<PinnedView>` across `in_parallel` and dropping before re-locking `SHADER_HEAPS`), and `draw.rs` view lookups through `PinnedView`. ### Verification Added two regression tests in `crates/victory-gpu/src/descriptor/host.rs`: - `descriptor::host::tests::mirrored_view_pins_its_resource_until_dropped`: verifies that a mirrored texture SRV, a dual-resource `(buffer, counter)` UAV, and a self-counter UAV keep their resources alive across a concurrent `Release()` (`refs == 1`) and complete final destruction (invalidating the view with zero leaked references) when the `PinnedView` drops. Fails deterministically on `main` (`left: 0, right: 1`). - `descriptor::host::tests::concurrent_mirror_and_sync_survive_resource_release`: 64-round `Barrier(2)` multi-threaded race between `sync_slots` / `mirror` / `stamp_read` / `pinned_view` / `view_addresses` and concurrent `Release()` in alternating release orders (covering dual-resource partial-pin rollback), asserting zero crashes, zero deadlocks, zero leaked references, and zero D3D12 debug errors. Verified across all gates: `rustfmt --edition 2024`, hardware D3D12 and WARP (including `VICTORY_D3D12_DEBUG=1 --features diagnostics`), full `victory-gpu` WARP suite (`248 passed, 0 failed, 1 ignored`), workspace `clippy --all-targets` (default and `--all-features`) with `-D warnings`, and `xtask` repository policy.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 14:12:06 +00:00
Author
Owner

@⁠Dukestar69 on GitHub · 2026-11-26 14:53 UTC · original · approved these changes

Reviewed the diff and verified locally on Windows 11 x64 (RTX 5090 + WARP):

  • Adding object::try_add_ref / release_boxed_with (running descriptor::forget after refcount reaches 0 and before Box::from_raw) and returning RAII PinnedView from descriptor lookups (dropping the VIEWS lock before releasing partial pins) cleanly closes the concurrent Resource::Release UAF race across descriptor mirroring, sync, and draw paths.
  • cargo test -p victory-gpu --lib descriptor:: -- --test-threads=1 --skip dynamic_slots_are_held_until_released (RTX 5090): 23 passed, 0 failed (including concurrent_mirror_and_sync_survive_resource_release and mirrored_view_pins_its_resource_until_dropped).
  • cargo test -p victory-gpu --lib draw:: -- --test-threads=1 (RTX 5090): 20 passed, 0 failed.
  • VICTORY_FORCE_WARP=1 cargo test -p victory-gpu --lib descriptor::host::tests:: -- --test-threads=1: 16 passed (only pre-existing #1744 dynamic_slots_are_held_until_released failed).
  • cargo clippy -p victory-gpu --all-targets -- -D warnings: clean.
  • cargo clippy -p victory-gpu --features diagnostics --all-targets -- -D warnings: clean.
  • cargo xtask policy: 191 files ok.

Synthetic tests only (no game files used). AI-assisted (Gemini 4 Neon).

**@⁠Dukestar69** on GitHub · 2026-11-26 14:53 UTC · [original](https://github.com/Marvisak/VIctory/pull/3234#pullrequestreview-6076224753) · **approved these changes** Reviewed the diff and verified locally on Windows 11 x64 (RTX 5090 + WARP): - Adding `object::try_add_ref` / `release_boxed_with` (running `descriptor::forget` after refcount reaches 0 and before `Box::from_raw`) and returning RAII `PinnedView` from descriptor lookups (dropping the `VIEWS` lock before releasing partial pins) cleanly closes the concurrent `Resource::Release` UAF race across descriptor mirroring, sync, and draw paths. - `cargo test -p victory-gpu --lib descriptor:: -- --test-threads=1 --skip dynamic_slots_are_held_until_released` (RTX 5090): 23 passed, 0 failed (including `concurrent_mirror_and_sync_survive_resource_release` and `mirrored_view_pins_its_resource_until_dropped`). - `cargo test -p victory-gpu --lib draw:: -- --test-threads=1` (RTX 5090): 20 passed, 0 failed. - `VICTORY_FORCE_WARP=1 cargo test -p victory-gpu --lib descriptor::host::tests:: -- --test-threads=1`: 16 passed (only pre-existing #1744 `dynamic_slots_are_held_until_released` failed). - `cargo clippy -p victory-gpu --all-targets -- -D warnings`: clean. - `cargo clippy -p victory-gpu --features diagnostics --all-targets -- -D warnings`: clean. - `cargo xtask policy`: 191 files ok. Synthetic tests only (no game files used). AI-assisted (Gemini 4 Neon).
Author
Owner

@⁠oni-swr on GitHub · 2026-11-26 15:03 UTC · original

WARP verification at head 1b08f76c (main 63bcca43 as control).

  • New tests mirrored_view_pins_its_resource_until_dropped and concurrent_mirror_and_sync_survive_resource_release: both pass on the head.
  • Full victory-gpu lib suite on the head: 248 passed, 1 ignored, in 2 of 2 runs.
  • Control: the two new tests applied to main's code. mirrored_view_pins_its_resource_until_dropped fails (mirrored SRV must pin its texture against concurrent Release, left 0, right 1). concurrent_mirror_and_sync_survive_resource_release can't build on main as written because descriptor::pinned_view doesn't exist; with pinned_view swapped for view it passes on main, so on WARP that race test does not catch the bug without the fix. Only the pin test discriminates.

Software adapter (WARP, Microsoft Basic Render Driver), not a physical GPU. Synthetic tests only, no game files. Run with VICTORY_WARP=1 VICTORY_D3D12_WARP=1 cargo test -p victory-gpu --lib --features diagnostics -j 4 and dxil.dll available. Test report only, no code changes.

AI-assisted: verification by Claude Code (Claude Sonnet 5.5).

**@⁠oni-swr** on GitHub · 2026-11-26 15:03 UTC · [original](https://github.com/Marvisak/VIctory/pull/3234#issuecomment-7195728681) WARP verification at head `1b08f76c` (main `63bcca43` as control). - New tests `mirrored_view_pins_its_resource_until_dropped` and `concurrent_mirror_and_sync_survive_resource_release`: both pass on the head. - Full victory-gpu lib suite on the head: 248 passed, 1 ignored, in 2 of 2 runs. - Control: the two new tests applied to main's code. `mirrored_view_pins_its_resource_until_dropped` fails (`mirrored SRV must pin its texture against concurrent Release`, left 0, right 1). `concurrent_mirror_and_sync_survive_resource_release` can't build on main as written because `descriptor::pinned_view` doesn't exist; with `pinned_view` swapped for `view` it **passes** on main, so on WARP that race test does not catch the bug without the fix. Only the pin test discriminates. Software adapter (WARP, Microsoft Basic Render Driver), not a physical GPU. Synthetic tests only, no game files. Run with `VICTORY_WARP=1 VICTORY_D3D12_WARP=1 cargo test -p victory-gpu --lib --features diagnostics -j 4` and dxil.dll available. Test report only, no code changes. AI-assisted: verification by Claude Code (Claude Sonnet 5.5).
This pull request can be merged automatically.
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/3234/head:github-bot/gh-3234'
git switch 'github-bot/gh-3234'

Merge

Merge the changes and update on Forgejo.
git switch 'main'
git merge --no-ff 'github-bot/gh-3234'
git switch 'github-bot/gh-3234'
git rebase 'main'
git switch 'main'
git merge --ff-only 'github-bot/gh-3234'
git switch 'github-bot/gh-3234'
git rebase 'main'
git switch 'main'
git merge --no-ff 'github-bot/gh-3234'
git switch 'main'
git merge --squash 'github-bot/gh-3234'
git switch 'main'
git merge --ff-only 'github-bot/gh-3234'
git switch 'main'
git merge 'github-bot/gh-3234'
git push origin 'main'
This discussion has been locked. Commenting is limited to contributors.
No reviewers
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.

Dependencies

No dependencies set

Reference
github/Marvisak--VIctory!3234
No description provided.