GPU/queue: hold Queue::held behind Arc and Weak in WAITING to eliminate release/release_held UAF #620

Open
AscilCH wants to merge 1 commit from upstream-pr/620 into main
AscilCH commented 2026-11-25 03:25:53 +00:00 (Migrated from github.com)

Imported from fxceless/victory-fork. Originally posted by AscilCH on 2026-11-25T03:25:53Z.


Problem

In crates/victory-gpu/src/queue.rs, WAITING: Mutex<Vec<usize>> stored raw *const Queue pointers without holding any lifetime guarantee:

  1. In release(), object::release_boxed::<Queue>(this) dropped and freed Box<Queue> (and its embedded held: Mutex<Vec<Held>>) before removing this from WAITING.
  2. In release_held(), raw pointers were cloned out of WAITING and dereferenced via unsafe { &(*(q as *const Queue)).held } while locking the mutex across host.execute(&runs) with no synchronization against a concurrent ID3D12CommandQueue::Release().

If one thread released a waiting queue while another thread ran release_held(), release_held() could dereference freed memory, lock a destroyed Mutex, or drop a MutexGuard onto freed memory. In addition, removing by raw address after release_boxed risked removing a newer Queue allocated at the same address (ABA).

Fix

  • Change Queue::held to Arc<Mutex<Vec<Held>>> and change WAITING to store (usize, Weak<Mutex<Vec<Held>>>) entries.
  • Add register_waiting(this) to register (id, Arc::downgrade(&queue(this).held)) (replacing any stale entry for the same address).
  • In release(), downgrade queue(this).held before release_boxed::<Queue>(this), and remove only the matching Weak::ptr_eq entry when left == 0.
  • In release_held(), upgrade each Weak handle safely instead of dereferencing q as *const Queue, pruning dead entries via Weak::ptr_eq.
  • Add synthetic unit tests releasing_waiting_queue_prunes_waiting_and_never_uafs_on_release_held and concurrent_release_held_and_queue_release_do_not_uaf.

Verification

  • cargo test -p victory-gpu --locked queue::tests -- --test-threads=1: 6 passed, 0 failed
  • cargo xtask policy: 169 files ok
  • cargo clippy -p victory-gpu --all-targets --all-features --locked -- -D warnings: clean (default-features build on c96b4f2 has only the pre-existing dead_code warning on placed_filler_twins, tracked in #491)

(AI-assisted; synthetic unit tests only, no game data.)

<!-- greyfinch-sync:fxceless/victory-fork:issue:6596488753 --> Imported from [fxceless/victory-fork](https://github.com/fxceless/victory-fork/pull/620). Originally posted by [AscilCH](https://github.com/AscilCH) on 2026-11-25T03:25:53Z. --- ## Problem In `crates/victory-gpu/src/queue.rs`, `WAITING: Mutex<Vec<usize>>` stored raw `*const Queue` pointers without holding any lifetime guarantee: 1. In `release()`, `object::release_boxed::<Queue>(this)` dropped and freed `Box<Queue>` (and its embedded `held: Mutex<Vec<Held>>`) **before** removing `this` from `WAITING`. 2. In `release_held()`, raw pointers were cloned out of `WAITING` and dereferenced via `unsafe { &(*(q as *const Queue)).held }` while locking the mutex across `host.execute(&runs)` with no synchronization against a concurrent `ID3D12CommandQueue::Release()`. If one thread released a waiting queue while another thread ran `release_held()`, `release_held()` could dereference freed memory, lock a destroyed `Mutex`, or drop a `MutexGuard` onto freed memory. In addition, removing by raw address after `release_boxed` risked removing a newer `Queue` allocated at the same address (ABA). ## Fix - Change `Queue::held` to `Arc<Mutex<Vec<Held>>>` and change `WAITING` to store `(usize, Weak<Mutex<Vec<Held>>>)` entries. - Add `register_waiting(this)` to register `(id, Arc::downgrade(&queue(this).held))` (replacing any stale entry for the same address). - In `release()`, downgrade `queue(this).held` before `release_boxed::<Queue>(this)`, and remove only the matching `Weak::ptr_eq` entry when `left == 0`. - In `release_held()`, upgrade each `Weak` handle safely instead of dereferencing `q as *const Queue`, pruning dead entries via `Weak::ptr_eq`. - Add synthetic unit tests `releasing_waiting_queue_prunes_waiting_and_never_uafs_on_release_held` and `concurrent_release_held_and_queue_release_do_not_uaf`. ## Verification - `cargo test -p victory-gpu --locked queue::tests -- --test-threads=1`: 6 passed, 0 failed - `cargo xtask policy`: 169 files ok - `cargo clippy -p victory-gpu --all-targets --all-features --locked -- -D warnings`: clean (default-features build on `c96b4f2` has only the pre-existing `dead_code` warning on `placed_filler_twins`, tracked in #491) *(AI-assisted; synthetic unit tests only, no game data.)*
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/620:upstream-pr/620'
git switch 'upstream-pr/620'

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