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

Open
fuzeofficlal wants to merge 1 commit from upstream-pr/3255 into main
fuzeofficlal commented 2026-11-26 14:44:14 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by fuzeofficlal on 2026-11-26T14:44:14Z.


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.
<!-- greyfinch-sync:Marvisak/VIctory:issue:5512088611 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/3255). Originally posted by [fuzeofficlal](https://github.com/fuzeofficlal) on 2026-11-26T14:44:14Z. --- ### 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.
Dukestar69 commented 2026-11-26 14:47:36 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by Dukestar69 on 2026-11-26T14:47:36Z.

Review: APPROVED


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).

<!-- greyfinch-sync:Marvisak/VIctory:review:6076131563 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/3255#pullrequestreview-6076131563). Originally posted by [Dukestar69](https://github.com/Dukestar69) on 2026-11-26T14:47:36Z. **Review:** APPROVED --- 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).
Jackster commented 2026-11-26 22:48:38 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by Jackster on 2026-11-26T22:48:38Z.


Windows 11, 63bcca4 + this PR, Radeon RX 7900 XTX:

  • cargo test -p victory-gpu --lib: 233 passed / 0 failed / 1 ignored (unpatched 63bcca4 here: 231).
  • cargo clippy -p victory-gpu --all-targets -- -D warnings: clean.

Both new tests — released_queue_holding_work_stays_alive_and_does_not_alias_new_queues and held_queue_pinning_handles_multiple_items_signals_waits_and_concurrent_accounting — are in that count and pass, so the pinning/aliasing accounting is exercised against a real device. Synthetic only. AI-assisted.

<!-- greyfinch-sync:Marvisak/VIctory:comment:7205004184 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/3255#issuecomment-7205004184). Originally posted by [Jackster](https://github.com/Jackster) on 2026-11-26T22:48:38Z. --- Windows 11, `63bcca4` + this PR, Radeon RX 7900 XTX: - `cargo test -p victory-gpu --lib`: 233 passed / 0 failed / 1 ignored (unpatched `63bcca4` here: 231). - `cargo clippy -p victory-gpu --all-targets -- -D warnings`: clean. Both new tests — `released_queue_holding_work_stays_alive_and_does_not_alias_new_queues` and `held_queue_pinning_handles_multiple_items_signals_waits_and_concurrent_accounting` — are in that count and pass, so the pinning/aliasing accounting is exercised against a real device. Synthetic only. AI-assisted.
reallyvibe commented 2026-11-29 01:47:19 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by reallyvibe on 2026-11-29T01:47:19Z.


Heads-up: this conflicts with #3227 in queue.rs; a resolution that keeps both fixes, tested on WARP

This head a4690408 and #3227 8ef3f17d both start from main 63bcca43 and both redefine queue::Holding. Here it's a struct that pins queues; in #3227 it's Held<_, KeptFence>. git merge-tree gives 5 conflict hunks in queue.rs: Holding, signal, wait, and the two test blocks. So whichever lands second needs a rebase. I found no mention of the pairing on either PR, and neither is in #3409's tested set.

Smallest resolution I tested. Keep #3227's KeptFence unchanged, keep this PR's Holding with #3227's token type, and keep all three tests. Production hunks:

// Holding: this PR's struct, holding #3227's fence tokens
#[cfg(windows)]
struct Holding {
    held: crate::hold::Held<std::sync::Arc<crate::host::HostList>, KeptFence>,
    pinned: Vec<usize>,
}
// ...and in `impl Holding`:
fn push(&mut self, queue: This, item: Item<std::sync::Arc<crate::host::HostList>, KeptFence>) {

// signal: no separate fence::keep (KeptFence::new keeps it)
held.push(this, Item::Signal(KeptFence::new(fence), value));

// wait: #3227's already-satisfied check, this PR's by-value pump
if crate::fence::value(fence) < value {
    held.push(this, Item::FenceWait(KeptFence::new(fence), value));
    pump(held, Why::Wait);
}

pump auto-merges correctly: #3227's KeptFence closures are used through guard.held, and take_drained and release run after the lock is dropped.

On that tree, cargo build -p victory-gpu --lib and cargo clippy -p victory-gpu --all-targets -- -D warnings (default and --all-features) are clean. WARP queue:: + hold:: + fence:: pass 21/21, also with each test in its own process.

Do the existing tests still cover both fixes after the merge? I changed one thing at a time and ran each test in its own process.

Detected:

change fails
no object::add_ref in Holding::push (this PR's pin removed) both of this PR's tests; #3227's cross-queue test crashes
KeptFence holds no reference (#3227's keep removed) #3227's cross_queue_fence_wait_keeps_fence_alive_until_satisfied ("queued Signal holds one reference": 1, want 2)

Not detected:

change result
wait pushes unconditionally (this PR's side of that hunk) all 21 pass

Within the tested cases, this change makes no observable difference in behaviour: an already-satisfied wait is popped and its fence reference dropped at the next pump. That doesn't establish the change is harmless in general. No test pins that choice, so the resolution above keeps #3227's check.

Lock order, within the code I read. KeptFence can release a fence's last reference while HELD is held, and release_boxed then takes PRIVATE. This PR moves queue releases out of HELD for that reason. In the merged queue.rs, fence.rs and object.rs, main's pump already releases fences under HELD (fence::let_go in its signal closure), and none of the private_data() callers there call into the queue code. So I found no new lock cycle in those files. I didn't audit other modules for code that takes PRIVATE and then HELD.

The full resolved queue.rs diff is available on request.

Windows 11, Ryzen 7 7445HS, WARP, synthetic data only.

AI-assisted (Claude Code).

<!-- greyfinch-sync:Marvisak/VIctory:comment:7249012940 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/3255#issuecomment-7249012940). Originally posted by [reallyvibe](https://github.com/reallyvibe) on 2026-11-29T01:47:19Z. --- **Heads-up: this conflicts with #3227 in `queue.rs`; a resolution that keeps both fixes, tested on WARP** This head `a4690408` and #3227 `8ef3f17d` both start from main `63bcca43` and both redefine `queue::Holding`. Here it's a struct that pins queues; in #3227 it's `Held<_, KeptFence>`. `git merge-tree` gives 5 conflict hunks in `queue.rs`: `Holding`, `signal`, `wait`, and the two test blocks. So whichever lands second needs a rebase. I found no mention of the pairing on either PR, and neither is in #3409's tested set. **Smallest resolution I tested.** Keep #3227's `KeptFence` unchanged, keep this PR's `Holding` with #3227's token type, and keep all three tests. Production hunks: ```rust // Holding: this PR's struct, holding #3227's fence tokens #[cfg(windows)] struct Holding { held: crate::hold::Held<std::sync::Arc<crate::host::HostList>, KeptFence>, pinned: Vec<usize>, } // ...and in `impl Holding`: fn push(&mut self, queue: This, item: Item<std::sync::Arc<crate::host::HostList>, KeptFence>) { // signal: no separate fence::keep (KeptFence::new keeps it) held.push(this, Item::Signal(KeptFence::new(fence), value)); // wait: #3227's already-satisfied check, this PR's by-value pump if crate::fence::value(fence) < value { held.push(this, Item::FenceWait(KeptFence::new(fence), value)); pump(held, Why::Wait); } ``` `pump` auto-merges correctly: #3227's `KeptFence` closures are used through `guard.held`, and `take_drained` and `release` run after the lock is dropped. On that tree, `cargo build -p victory-gpu --lib` and `cargo clippy -p victory-gpu --all-targets -- -D warnings` (default and `--all-features`) are clean. WARP `queue::` + `hold::` + `fence::` pass 21/21, also with each test in its own process. **Do the existing tests still cover both fixes after the merge?** I changed one thing at a time and ran each test in its own process. Detected: | change | fails | |---|---| | no `object::add_ref` in `Holding::push` (this PR's pin removed) | both of this PR's tests; #3227's cross-queue test crashes | | `KeptFence` holds no reference (#3227's keep removed) | #3227's `cross_queue_fence_wait_keeps_fence_alive_until_satisfied` ("queued Signal holds one reference": 1, want 2) | Not detected: | change | result | |---|---| | `wait` pushes unconditionally (this PR's side of that hunk) | all 21 pass | Within the tested cases, this change makes no observable difference in behaviour: an already-satisfied wait is popped and its fence reference dropped at the next pump. That doesn't establish the change is harmless in general. No test pins that choice, so the resolution above keeps #3227's check. **Lock order, within the code I read.** `KeptFence` can release a fence's last reference while `HELD` is held, and `release_boxed` then takes `PRIVATE`. This PR moves queue releases out of `HELD` for that reason. In the merged `queue.rs`, `fence.rs` and `object.rs`, main's `pump` already releases fences under `HELD` (`fence::let_go` in its signal closure), and none of the `private_data()` callers there call into the queue code. So I found no new lock cycle in those files. I didn't audit other modules for code that takes `PRIVATE` and then `HELD`. The full resolved `queue.rs` diff is available on request. Windows 11, Ryzen 7 7445HS, WARP, synthetic data only. AI-assisted (Claude Code).
This pull request cannot be merged automatically due to conflicts.
Merge manually to resolve the conflicts.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin 'upstream-pr/3255:upstream-pr/3255'
git switch 'upstream-pr/3255'

Merge

Merge the changes and update on Forgejo.

Warning: The "Autodetect manual merge" setting is not enabled for this repository, you will have to mark this pull request as manually merged afterwards.

git switch 'main'
git merge --no-ff 'upstream-pr/3255'
git switch 'upstream-pr/3255'
git rebase 'main'
git switch 'main'
git merge --ff-only 'upstream-pr/3255'
git switch 'upstream-pr/3255'
git rebase 'main'
git switch 'main'
git merge --no-ff 'upstream-pr/3255'
git switch 'main'
git merge --squash 'upstream-pr/3255'
git switch 'main'
git merge --ff-only 'upstream-pr/3255'
git switch 'main'
git merge 'upstream-pr/3255'
git push origin 'main'
Sign in to join this conversation.
No reviewers
No labels
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.

Reference
Marvisak/VIctory!3253
No description provided.