GPU: keep command queues alive while HELD tracks pending work #3255

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

@⁠fuzeofficlal on GitHub · 2026-11-26 14:44 UTC · original
fuzeofficlal:fix/gpu-held-queue-release-uaf → main · +342 −23 · 1 files · 1 commits · open

Summary

queue::execute, queue::signal, and queue::wait previously stored raw *const Queue addresses (this as usize) in HELD without retaining a COM reference to the queue object. Under D3D12, a title may legally release an ID3D12CommandQueue while submitted command lists, signals, or fence waits are still pending. Doing so freed Box<Queue> while HELD still tracked items under that address, causing two bugs:

  1. Heap use-after-free: pump() (for safety-valve warnings and released-work accounting) and account_frame() called kind(queue) on the freed Queue pointer. account_frame() also dropped the HELD mutex before reading kind(*queue), racing with pump_held draining and freeing the queue concurrently.
  2. Cross-queue ABA deadlock: if a newly created ID3D12CommandQueue reused the freed Box<Queue> address, its independent work was queued behind the released queue's blocked wait in HELD, stalling unrelated submissions (including the work meant to unblock the wait).

Changes

  • Wrapped HELD in Holding { held, pinned } in crates/victory-gpu/src/queue.rs:
    • Holding::push calls object::add_ref(queue) once per distinct queue when its HELD FIFO becomes non-empty and records its address in pinned.
    • pump takes the HELD lock guard by value, finishes all kind(queue) reads while drained queues are still pinned, extracts drained queues via Holding::take_drained(), drops the HELD lock, and then calls release(queue) outside the lock (preserving lock ordering without nesting HELD -> PRIVATE).
    • account_frame resolves kind(queue) into u32 queue kinds while still holding HELD.
  • Added two deterministic regression tests in crates/victory-gpu/src/queue.rs:
    • released_queue_holding_work_stays_alive_and_does_not_alias_new_queues (reproduces the UAF + ABA address-reuse deadlock before the fix and verifies clean drain + zero D3D12 debug layer messages after).
    • held_queue_pinning_handles_multiple_items_signals_waits_and_concurrent_accounting (covers single-ref deduplication across multiple lists and held Signal, Item::FenceWait pinning/unpinning, and concurrent account_frame vs. pump_held drain).

Verification

  • cargo test -p victory-gpu --locked targeted queue/hold/fence tests: 20 passed (RTX 5090 default, RTX 5090 + VICTORY_D3D12_DEBUG=1 --features diagnostics --test-threads=1, and WARP + VICTORY_D3D12_DEBUG=1 --features diagnostics --test-threads=1).
  • Full victory-gpu suite on WARP (VICTORY_D3D12_WARP=1 --features diagnostics --locked): 248 passed, 0 failed, 1 ignored.
  • cargo clippy --workspace --all-targets --locked -- -D warnings and --all-features: clean.
  • cargo test -p xtask --locked policy::tests::repository_passes_policy: passed.
**@⁠fuzeofficlal** on GitHub · 2026-11-26 14:44 UTC · [original](https://github.com/Marvisak/VIctory/pull/3255) `fuzeofficlal:fix/gpu-held-queue-release-uaf` → `main` · +342 −23 · 1 files · 1 commits · open ### Summary `queue::execute`, `queue::signal`, and `queue::wait` previously stored raw `*const Queue` addresses (`this as usize`) in `HELD` without retaining a COM reference to the queue object. Under D3D12, a title may legally release an `ID3D12CommandQueue` while submitted command lists, signals, or fence waits are still pending. Doing so freed `Box<Queue>` while `HELD` still tracked items under that address, causing two bugs: 1. **Heap use-after-free**: `pump()` (for safety-valve warnings and released-work accounting) and `account_frame()` called `kind(queue)` on the freed `Queue` pointer. `account_frame()` also dropped the `HELD` mutex before reading `kind(*queue)`, racing with `pump_held` draining and freeing the queue concurrently. 2. **Cross-queue ABA deadlock**: if a newly created `ID3D12CommandQueue` reused the freed `Box<Queue>` address, its independent work was queued behind the released queue's blocked wait in `HELD`, stalling unrelated submissions (including the work meant to unblock the wait). ### Changes - Wrapped `HELD` in `Holding { held, pinned }` in `crates/victory-gpu/src/queue.rs`: - `Holding::push` calls `object::add_ref(queue)` once per distinct queue when its `HELD` FIFO becomes non-empty and records its address in `pinned`. - `pump` takes the `HELD` lock guard by value, finishes all `kind(queue)` reads while drained queues are still pinned, extracts drained queues via `Holding::take_drained()`, drops the `HELD` lock, and then calls `release(queue)` outside the lock (preserving lock ordering without nesting `HELD -> PRIVATE`). - `account_frame` resolves `kind(queue)` into `u32` queue kinds while still holding `HELD`. - Added two deterministic regression tests in `crates/victory-gpu/src/queue.rs`: - `released_queue_holding_work_stays_alive_and_does_not_alias_new_queues` (reproduces the UAF + ABA address-reuse deadlock before the fix and verifies clean drain + zero D3D12 debug layer messages after). - `held_queue_pinning_handles_multiple_items_signals_waits_and_concurrent_accounting` (covers single-ref deduplication across multiple lists and held `Signal`, `Item::FenceWait` pinning/unpinning, and concurrent `account_frame` vs. `pump_held` drain). ### Verification - `cargo test -p victory-gpu --locked` targeted queue/hold/fence tests: 20 passed (RTX 5090 default, RTX 5090 + `VICTORY_D3D12_DEBUG=1 --features diagnostics --test-threads=1`, and WARP + `VICTORY_D3D12_DEBUG=1 --features diagnostics --test-threads=1`). - Full `victory-gpu` suite on WARP (`VICTORY_D3D12_WARP=1 --features diagnostics --locked`): 248 passed, 0 failed, 1 ignored. - `cargo clippy --workspace --all-targets --locked -- -D warnings` and `--all-features`: clean. - `cargo test -p xtask --locked policy::tests::repository_passes_policy`: passed.
queue::execute, queue::signal, and queue::wait previously inserted raw *const Queue pointers (this as usize) into HELD without retaining a COM reference. Under D3D12 a title may legally release an ID3D12CommandQueue while submitted lists or waits are still pending; doing so freed Box<Queue> immediately, causing (1) heap use-after-free when pump() or account_frame() later called kind(queue), and (2) cross-queue ABA deadlock when a newly created queue reused the freed address and had its independent work queued behind the released queue's blocked wait.

Pin each queue with object::add_ref while its HELD FIFO is non-empty, read kind(queue) inside account_frame while still holding the HELD lock, and release drained queue references after dropping HELD.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 14:47:58 +00:00
Author
Owner

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

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

  • Pinning each command queue with object::add_ref while HELD tracks pending items and releasing drained queues after dropping the HELD mutex guard cleanly prevents UAF and pointer aliasing on concurrent queue Release().
  • cargo test -p victory-gpu --lib queue:: -- --test-threads=1 (RTX 5090): 7 passed, 0 failed (including held_queue_pinning_handles_multiple_items_signals_waits_and_concurrent_accounting and
    eleased_queue_holding_work_stays_alive_and_does_not_alias_new_queues).
  • VICTORY_FORCE_WARP=1 cargo test -p victory-gpu --lib queue:: -- --test-threads=1 (WARP): 7 passed, 0 failed.
  • cargo clippy -p victory-gpu --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:47 UTC · [original](https://github.com/Marvisak/VIctory/pull/3255#pullrequestreview-6076131563) · **approved these changes** Reviewed the diff and verified locally on Windows 11 x64 (RTX 5090 + WARP): - Pinning each command queue with object::add_ref while HELD tracks pending items and releasing drained queues after dropping the HELD mutex guard cleanly prevents UAF and pointer aliasing on concurrent queue Release(). - cargo test -p victory-gpu --lib queue:: -- --test-threads=1 (RTX 5090): 7 passed, 0 failed (including held_queue_pinning_handles_multiple_items_signals_waits_and_concurrent_accounting and eleased_queue_holding_work_stays_alive_and_does_not_alias_new_queues). - VICTORY_FORCE_WARP=1 cargo test -p victory-gpu --lib queue:: -- --test-threads=1 (WARP): 7 passed, 0 failed. - cargo clippy -p victory-gpu --all-targets -- -D warnings: clean. - cargo xtask policy: 191 files ok. Synthetic tests only (no game files used). AI-assisted (Gemini 4 Neon).
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/3255/head:github-bot/gh-3255'
git switch 'github-bot/gh-3255'

Merge

Merge the changes and update on Forgejo.
git switch 'main'
git merge --no-ff 'github-bot/gh-3255'
git switch 'github-bot/gh-3255'
git rebase 'main'
git switch 'main'
git merge --ff-only 'github-bot/gh-3255'
git switch 'github-bot/gh-3255'
git rebase 'main'
git switch 'main'
git merge --no-ff 'github-bot/gh-3255'
git switch 'main'
git merge --squash 'github-bot/gh-3255'
git switch 'main'
git merge --ff-only 'github-bot/gh-3255'
git switch 'main'
git merge 'github-bot/gh-3255'
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!3255
No description provided.