GPU: keep fences alive while queues wait on them #3225

Open
fuzeofficlal wants to merge 1 commit from upstream-pr/3227 into main
fuzeofficlal commented 2026-11-26 13:45:39 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by fuzeofficlal on 2026-11-26T13:45:39Z.


Summary

Fix a use-after-free in ID3D12CommandQueue::Wait when a title releases its own fence reference while a command queue still has a pending Item::FenceWait for that fence.

Under D3D12, ID3D12CommandQueue::Wait holds an internal reference on the fence until the wait is satisfied, so titles can legally release the fence immediately after queuing Wait (for example after pairing queue_a->Signal(fence, v) with queue_b->Wait(fence, v), or when satisfying the fence later via a GPU bottom-of-pipe write to fence->GetGPUVirtualAddress()).

Previously, queue::signal retained the fence with fence::keep and released it when signaled, but queue::wait stored raw fence as usize inside Item::FenceWait without calling fence::keep, and Held::pump popped Item::FenceWait without releasing a reference. Once the title released the fence (and any queued Signal released its own reference), subsequent FenceWait polls in Held::pump or the held-work timer dereferenced freed Fence memory.

Changes

  • crates/victory-gpu/src/hold.rs: Parameterize Item<S, F = usize>, Held<S, F = usize>, and World<'a, S, F = usize> over the fence token type F so Held::pump passes &F to world.fence, moves F into world.signal, and drops F when popping a satisfied Item::FenceWait.
  • crates/victory-gpu/src/queue.rs:
    • Introduce a non-Copy RAII KeptFence guard (#[cfg(windows)]) that calls fence::keep on construction and fence::let_go on Drop, and use it as the fence token type in queue::Holding for both Item::Signal and Item::FenceWait. Every completion path and Held drop now balances keep/let_go automatically.
    • Validate null fence pointers (E_INVALIDARG) and check fence::value(fence) < value under the held() lock in queue::wait before retaining and enqueuing Item::FenceWait.
  • Tests:
    • hold::tests::fence_tokens_are_dropped_once_on_completion_and_on_held_drop: verifies fence tokens remain alive while blocked and are dropped exactly once on completion or when a non-empty Held is dropped.
    • queue::tests::cross_queue_fence_wait_keeps_fence_alive_until_satisfied: verifies cross-queue Signal + Wait with immediate title Release, observer refcount balance across pending polls / CPU Signal / already-satisfied Wait / null-fence E_INVALIDARG, and Wait holding the sole remaining reference until satisfied by a GPU bottom-of-pipe write to the fence GPU virtual address.

Verification

  • Confirmed deterministic RED failure before the production fix (queued FenceWait must hold its own reference: left 2, right 3).
  • cargo test --locked -p victory-gpu --lib passed on RTX 5090 (default: 231 passed), RTX 5090 + --features diagnostics + VICTORY_D3D12_DEBUG=1 (--test-threads=1: 245 passed), and WARP + --features diagnostics + VICTORY_D3D12_DEBUG=1 (--test-threads=1: 245 passed).
  • cargo clippy --workspace --all-targets --locked -- -D warnings and --all-features passed with 0 warnings.
  • git diff --check, rustfmt --edition 2024 --check on modified hunks, and cargo run --locked -p xtask -- policy (191 files ok) all passed.
<!-- greyfinch-sync:Marvisak/VIctory:issue:6627997895 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/3227). Originally posted by [fuzeofficlal](https://github.com/fuzeofficlal) on 2026-11-26T13:45:39Z. --- ## Summary Fix a use-after-free in `ID3D12CommandQueue::Wait` when a title releases its own fence reference while a command queue still has a pending `Item::FenceWait` for that fence. Under D3D12, `ID3D12CommandQueue::Wait` holds an internal reference on the fence until the wait is satisfied, so titles can legally release the fence immediately after queuing `Wait` (for example after pairing `queue_a->Signal(fence, v)` with `queue_b->Wait(fence, v)`, or when satisfying the fence later via a GPU bottom-of-pipe write to `fence->GetGPUVirtualAddress()`). Previously, `queue::signal` retained the fence with `fence::keep` and released it when signaled, but `queue::wait` stored raw `fence as usize` inside `Item::FenceWait` without calling `fence::keep`, and `Held::pump` popped `Item::FenceWait` without releasing a reference. Once the title released the fence (and any queued `Signal` released its own reference), subsequent `FenceWait` polls in `Held::pump` or the held-work timer dereferenced freed `Fence` memory. ## Changes - **`crates/victory-gpu/src/hold.rs`**: Parameterize `Item<S, F = usize>`, `Held<S, F = usize>`, and `World<'a, S, F = usize>` over the fence token type `F` so `Held::pump` passes `&F` to `world.fence`, moves `F` into `world.signal`, and drops `F` when popping a satisfied `Item::FenceWait`. - **`crates/victory-gpu/src/queue.rs`**: - Introduce a non-`Copy` RAII `KeptFence` guard (`#[cfg(windows)]`) that calls `fence::keep` on construction and `fence::let_go` on `Drop`, and use it as the fence token type in `queue::Holding` for both `Item::Signal` and `Item::FenceWait`. Every completion path and `Held` drop now balances `keep`/`let_go` automatically. - Validate null fence pointers (`E_INVALIDARG`) and check `fence::value(fence) < value` under the `held()` lock in `queue::wait` before retaining and enqueuing `Item::FenceWait`. - **Tests**: - `hold::tests::fence_tokens_are_dropped_once_on_completion_and_on_held_drop`: verifies fence tokens remain alive while blocked and are dropped exactly once on completion or when a non-empty `Held` is dropped. - `queue::tests::cross_queue_fence_wait_keeps_fence_alive_until_satisfied`: verifies cross-queue `Signal` + `Wait` with immediate title `Release`, observer refcount balance across pending polls / CPU `Signal` / already-satisfied `Wait` / null-fence `E_INVALIDARG`, and `Wait` holding the sole remaining reference until satisfied by a GPU bottom-of-pipe write to the fence GPU virtual address. ## Verification - Confirmed deterministic RED failure before the production fix (`queued FenceWait must hold its own reference: left 2, right 3`). - `cargo test --locked -p victory-gpu --lib` passed on RTX 5090 (default: 231 passed), RTX 5090 + `--features diagnostics` + `VICTORY_D3D12_DEBUG=1` (`--test-threads=1`: 245 passed), and WARP + `--features diagnostics` + `VICTORY_D3D12_DEBUG=1` (`--test-threads=1`: 245 passed). - `cargo clippy --workspace --all-targets --locked -- -D warnings` and `--all-features` passed with 0 warnings. - `git diff --check`, `rustfmt --edition 2024 --check` on modified hunks, and `cargo run --locked -p xtask -- policy` (`191 files ok`) all passed.
Dukestar69 commented 2026-11-26 14:01:44 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by Dukestar69 on 2026-11-26T13:48:43Z.

Review: APPROVED


Verified on RTX 5090 against origin/main (63bcca4):

  • cargo test -p victory-gpu --lib hold:: -- --test-threads=1: 11/11 passed (including fence_tokens_are_dropped_once_on_completion_and_on_held_drop).
  • cargo test -p victory-gpu --lib queue:: -- --test-threads=1: 6/6 passed (including cross_queue_fence_wait_keeps_fence_alive_until_satisfied); fence::: 2/2 passed.
  • cargo clippy -p victory-gpu --all-targets -- -D warnings: clean.
  • cargo xtask policy: 191 files ok.

Cleanly keeps fences alive while queues are waiting on them and drops fence tokens cleanly on completion or held-queue drop.

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

<!-- greyfinch-sync:Marvisak/VIctory:review:6075238079 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/3227#pullrequestreview-6075238079). Originally posted by [Dukestar69](https://github.com/Dukestar69) on 2026-11-26T13:48:43Z. **Review:** APPROVED --- Verified on RTX 5090 against `origin/main` (`63bcca4`): - `cargo test -p victory-gpu --lib hold:: -- --test-threads=1`: 11/11 passed (including `fence_tokens_are_dropped_once_on_completion_and_on_held_drop`). - `cargo test -p victory-gpu --lib queue:: -- --test-threads=1`: 6/6 passed (including `cross_queue_fence_wait_keeps_fence_alive_until_satisfied`); `fence::`: 2/2 passed. - `cargo clippy -p victory-gpu --all-targets -- -D warnings`: clean. - `cargo xtask policy`: 191 files ok. Cleanly keeps fences alive while queues are waiting on them and drops fence tokens cleanly on completion or held-queue drop. Synthetic tests only (no game files used). AI-assisted (Gemini 4 Neon).
oni-swr commented 2026-11-26 15:02:26 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by oni-swr on 2026-11-26T14:04:59Z.


WARP test report: head 8ef3f17d (parent and control base: main 63bcca43)

  • Adapter: Microsoft Basic Render Driver (WARP). A local, uncommitted probe asserted the host device's adapter name before each run; it passed and was reverted.
  • Environment: VICTORY_WARP=1, VICTORY_D3D12_WARP=1, cargo test -j 4 -p victory-gpu --features diagnostics --lib, rustc 1.98.1, Windows 11, dxil.dll from the Windows SDK 10.0.26100.0.
  • Head: hold::tests::fence_tokens_are_dropped_once_on_completion_and_on_held_drop and queue::tests::cross_queue_fence_wait_keeps_fence_alive_until_satisfied both pass. The whole victory-gpu lib suite passes: 249 passed (248 plus the probe), 0 failed, 1 ignored.
  • Control: main's queue.rs code with this PR's test module. cross_queue_fence_wait_keeps_fence_alive_until_satisfied fails: queued FenceWait must hold its own reference (left 2, right 3).
  • Not tested: the hold test has no control. It uses the PR's new Held<S, F> and Item<S, F> types, which main doesn't have, so it can't be built against main. Only the Wait/Signal reference counting in queue.rs was exercised, with synthetic fences, queues and buffers. No physical GPU and no title.

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

<!-- greyfinch-sync:Marvisak/VIctory:comment:7194251766 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/3227#issuecomment-7194251766). Originally posted by [oni-swr](https://github.com/oni-swr) on 2026-11-26T14:04:59Z. --- **WARP test report: head `8ef3f17d`** (parent and control base: main `63bcca43`) - Adapter: Microsoft Basic Render Driver (WARP). A local, uncommitted probe asserted the host device's adapter name before each run; it passed and was reverted. - Environment: `VICTORY_WARP=1`, `VICTORY_D3D12_WARP=1`, `cargo test -j 4 -p victory-gpu --features diagnostics --lib`, rustc 1.98.1, Windows 11, `dxil.dll` from the Windows SDK 10.0.26100.0. - Head: `hold::tests::fence_tokens_are_dropped_once_on_completion_and_on_held_drop` and `queue::tests::cross_queue_fence_wait_keeps_fence_alive_until_satisfied` both pass. The whole `victory-gpu` lib suite passes: 249 passed (248 plus the probe), 0 failed, 1 ignored. - Control: main's `queue.rs` code with this PR's test module. `cross_queue_fence_wait_keeps_fence_alive_until_satisfied` fails: `queued FenceWait must hold its own reference` (left 2, right 3). - Not tested: the `hold` test has no control. It uses the PR's new `Held<S, F>` and `Item<S, F>` types, which main doesn't have, so it can't be built against main. Only the Wait/Signal reference counting in `queue.rs` was exercised, with synthetic fences, queues and buffers. No physical GPU and no title. AI-assisted: verification by Claude Code (Claude Sonnet 5.5).
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/3227:upstream-pr/3227'
git switch 'upstream-pr/3227'

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/3227'
git switch 'upstream-pr/3227'
git rebase 'main'
git switch 'main'
git merge --ff-only 'upstream-pr/3227'
git switch 'upstream-pr/3227'
git rebase 'main'
git switch 'main'
git merge --no-ff 'upstream-pr/3227'
git switch 'main'
git merge --squash 'upstream-pr/3227'
git switch 'main'
git merge --ff-only 'upstream-pr/3227'
git switch 'main'
git merge 'upstream-pr/3227'
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!3225
No description provided.