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

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

@⁠fuzeofficlal on GitHub · 2026-11-26 13:45 UTC · original
fuzeofficlal:fix/gpu-fence-wait-lifetime → main · +340 −30 · 2 files · 1 commits · open

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.
**@⁠fuzeofficlal** on GitHub · 2026-11-26 13:45 UTC · [original](https://github.com/Marvisak/VIctory/pull/3227) `fuzeofficlal:fix/gpu-fence-wait-lifetime` → `main` · +340 −30 · 2 files · 1 commits · open ## 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.
ID3D12CommandQueue::Wait keeps an internal reference to the fence until the
wait is satisfied, so a title may legally release its own fence reference right
after calling Wait (including after pairing it with a queued Signal on another
queue, or when satisfying the fence later via a GPU bottom-of-pipe write to its
GPU virtual address).

Previously, queue::wait pushed Item::FenceWait(fence as usize, value) without
calling fence::keep, and Held::pump popped Item::FenceWait without releasing
any reference. Once the title released the fence (and any queued Signal dropped
its own reference), the very next FenceWait check in Held::pump or the held
work timer dereferenced freed Fence memory (use-after-free).

Parameterize hold::Item, hold::Held, and hold::World over a fence token type
(defaulting to usize) and store an RAII KeptFence guard in queue::Holding that
calls fence::keep on creation and fence::let_go on Drop. Both Item::Signal and
Item::FenceWait now retain and release their fence references automatically on
every completion or drop path without leaks or double-frees, and queue::wait
evaluates the fence value under the held lock before enqueuing.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 13:47:24 +00:00
Author
Owner

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

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

**@⁠Dukestar69** on GitHub · 2026-11-26 13:48 UTC · [original](https://github.com/Marvisak/VIctory/pull/3227#pullrequestreview-6075238079) · **approved these changes** 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).
Author
Owner

@⁠oni-swr on GitHub · 2026-11-26 14:04 UTC · original

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

**@⁠oni-swr** on GitHub · 2026-11-26 14:04 UTC · [original](https://github.com/Marvisak/VIctory/pull/3227#issuecomment-7194251766) **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 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/3227/head:github-bot/gh-3227'
git switch 'github-bot/gh-3227'

Merge

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