GPU: guard bindless texture tracking and made_copy against concurrent Resource::drop #3213

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

@⁠fuzeofficlal on GitHub · 2026-11-26 13:13 UTC · original
fuzeofficlal:fix/gpu-bindless-resource-drop-uaf → main · +224 −25 · 2 files · 1 commits · open

Problem

In D3D12, a title may release a texture resource (Release()) without first overwriting old shader-visible descriptor heap slots that previously viewed it, while another thread runs heap mirroring (HostHeap::mirror), descriptor::sync_heaps, or post-submission HostHeap::stamp_read. Three coupled races between Resource::drop, bindless::Viewed::set, and resource::made_copy allow freed *const Resource pointers to survive and be dereferenced:

  1. Wrong teardown order in Resource::drop: crate::bindless::forget ran before removing the texture from TEXTURES_AT and before crate::descriptor::forget. Between those calls, a concurrent thread could still resolve View::Shader(r, _) and re-insert r into Viewed::inner.textures after bindless::forget had already returned.
  2. TOCTOU gap in bindless::Viewed::set: seen(view) resolved Seen::Texture(r) before acquiring Viewed::inner. If Resource::drop ran bindless::forget(r) and freed Box<Resource> in that window, Viewed::set inserted the freed pointer into inner.textures and inner.slots, causing subsequent bindless::uploads submissions to dereference freed memory via resource::made_copy.
  3. Unchecked dereference in resource::made_copy: Callers holding a cloned View::Shader(r, _) after releasing VIEWS (bindless::seen, HostHeap::stamp_read) passed raw r directly to resource(this).host.lock(), dereferencing freed memory if Resource::drop completed concurrently.

Fix

  • crates/victory-gpu/src/resource.rs:
    • Maintain an O(1) live texture pointer set inside TEXTURES_AT (LiveTextures) alongside by_memory (also avoiding a per-eviction temporary HashSet allocation in give_up).
    • Add resource::is_live_texture(usize) and check TEXTURES_AT under lock inside resource::made_copy before calling made(resource(this)).
    • Reorder Resource::drop to invalidate descriptor records (descriptor::forget) and remove the resource from TEXTURES_AT before draining bindless registries (bindless::forget).
  • crates/victory-gpu/src/bindless.rs:
    • Inside Viewed::set, after acquiring Viewed::inner and clearing any previous entry for index, verify crate::resource::is_live_texture(r) before inserting Seen::Texture(r).

Because Resource::drop removes the pointer from TEXTURES_AT before acquiring Viewed::inner in bindless::forget, and Viewed::set checks is_live_texture while holding Viewed::inner, any concurrent Viewed::set either finishes before bindless::forget removes it or observes the texture no longer live and skips insertion. The lock order remains strictly acyclic (SHADER_HEAPS -> HostHeap::made -> Viewed::inner -> MEMORY -> TEXTURES_AT -> Resource::host).

Verification

  • Added deterministic regression tests in crates/victory-gpu/src/bindless.rs:
    • concurrent_release_during_viewed_set_does_not_leave_dangling_texture (fails on main, passes with this fix)
    • release_before_viewed_set_and_made_copy_rejects_freed_texture
  • Verified on RTX 5090 (default features and --features diagnostics with VICTORY_D3D12_DEBUG=1), WARP with VICTORY_D3D12_DEBUG=1, full WARP victory-gpu suite (248 passed, 0 failed, 1 ignored), cargo clippy --workspace --all-targets --locked -- -D warnings (default and --all-features), rustfmt --edition 2024, and xtask policy checks.
**@⁠fuzeofficlal** on GitHub · 2026-11-26 13:13 UTC · [original](https://github.com/Marvisak/VIctory/pull/3213) `fuzeofficlal:fix/gpu-bindless-resource-drop-uaf` → `main` · +224 −25 · 2 files · 1 commits · open ## Problem In D3D12, a title may release a texture resource (`Release()`) without first overwriting old shader-visible descriptor heap slots that previously viewed it, while another thread runs heap mirroring (`HostHeap::mirror`), `descriptor::sync_heaps`, or post-submission `HostHeap::stamp_read`. Three coupled races between `Resource::drop`, `bindless::Viewed::set`, and `resource::made_copy` allow freed `*const Resource` pointers to survive and be dereferenced: 1. **Wrong teardown order in `Resource::drop`**: `crate::bindless::forget` ran before removing the texture from `TEXTURES_AT` and before `crate::descriptor::forget`. Between those calls, a concurrent thread could still resolve `View::Shader(r, _)` and re-insert `r` into `Viewed::inner.textures` after `bindless::forget` had already returned. 2. **TOCTOU gap in `bindless::Viewed::set`**: `seen(view)` resolved `Seen::Texture(r)` before acquiring `Viewed::inner`. If `Resource::drop` ran `bindless::forget(r)` and freed `Box<Resource>` in that window, `Viewed::set` inserted the freed pointer into `inner.textures` and `inner.slots`, causing subsequent `bindless::uploads` submissions to dereference freed memory via `resource::made_copy`. 3. **Unchecked dereference in `resource::made_copy`**: Callers holding a cloned `View::Shader(r, _)` after releasing `VIEWS` (`bindless::seen`, `HostHeap::stamp_read`) passed raw `r` directly to `resource(this).host.lock()`, dereferencing freed memory if `Resource::drop` completed concurrently. ## Fix - **`crates/victory-gpu/src/resource.rs`**: - Maintain an O(1) live texture pointer set inside `TEXTURES_AT` (`LiveTextures`) alongside `by_memory` (also avoiding a per-eviction temporary `HashSet` allocation in `give_up`). - Add `resource::is_live_texture(usize)` and check `TEXTURES_AT` under lock inside `resource::made_copy` before calling `made(resource(this))`. - Reorder `Resource::drop` to invalidate descriptor records (`descriptor::forget`) and remove the resource from `TEXTURES_AT` before draining bindless registries (`bindless::forget`). - **`crates/victory-gpu/src/bindless.rs`**: - Inside `Viewed::set`, after acquiring `Viewed::inner` and clearing any previous entry for `index`, verify `crate::resource::is_live_texture(r)` before inserting `Seen::Texture(r)`. Because `Resource::drop` removes the pointer from `TEXTURES_AT` before acquiring `Viewed::inner` in `bindless::forget`, and `Viewed::set` checks `is_live_texture` while holding `Viewed::inner`, any concurrent `Viewed::set` either finishes before `bindless::forget` removes it or observes the texture no longer live and skips insertion. The lock order remains strictly acyclic (`SHADER_HEAPS -> HostHeap::made -> Viewed::inner -> MEMORY -> TEXTURES_AT -> Resource::host`). ## Verification - Added deterministic regression tests in `crates/victory-gpu/src/bindless.rs`: - `concurrent_release_during_viewed_set_does_not_leave_dangling_texture` (fails on `main`, passes with this fix) - `release_before_viewed_set_and_made_copy_rejects_freed_texture` - Verified on RTX 5090 (default features and `--features diagnostics` with `VICTORY_D3D12_DEBUG=1`), WARP with `VICTORY_D3D12_DEBUG=1`, full WARP `victory-gpu` suite (`248 passed, 0 failed, 1 ignored`), `cargo clippy --workspace --all-targets --locked -- -D warnings` (default and `--all-features`), `rustfmt --edition 2024`, and `xtask` policy checks.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 13:19:07 +00:00
Author
Owner

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

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

  • cargo test -p victory-gpu --lib bindless:: -- --test-threads=1: passed.
  • cargo test -p victory-gpu --lib resource:: -- --test-threads=1: 18/18 passed; descriptor::host::: 14/14 passed.
  • cargo clippy -p victory-gpu --all-targets -- -D warnings: clean.
  • cargo xtask policy: 191 files ok.

Cleanly guards bindless texture tracking and made_copy against concurrent Resource::drop.

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

**@⁠Dukestar69** on GitHub · 2026-11-26 13:29 UTC · [original](https://github.com/Marvisak/VIctory/pull/3213#pullrequestreview-6074960443) · **approved these changes** Verified on RTX 5090 against `origin/main` (`63bcca4`): - `cargo test -p victory-gpu --lib bindless:: -- --test-threads=1`: passed. - `cargo test -p victory-gpu --lib resource:: -- --test-threads=1`: 18/18 passed; `descriptor::host::`: 14/14 passed. - `cargo clippy -p victory-gpu --all-targets -- -D warnings`: clean. - `cargo xtask policy`: 191 files ok. Cleanly guards bindless texture tracking and `made_copy` against concurrent `Resource::drop`. Synthetic tests only (no game files used). AI-assisted (Gemini 4 Neon).
Author
Owner

@⁠oni-swr on GitHub · 2026-11-26 13:49 UTC · original

WARP verification of head 92915c647fed37a42dbbf72561598c229be32ebe (on origin/main 63bcca4; no earlier verification on this PR)

Environment: Windows 11, no physical GPU used. VICTORY_WARP=1 and VICTORY_D3D12_WARP=1 set in every process, with --features diagnostics (the host only honours the WARP switch in that build). A local probe (not committed) printed and asserted the host device's adapter: Microsoft Basic Render Driver, DXGI flags 0x2 (software).

Commands (-j 4, --locked, --test-threads=1):

  • cargo test -p victory-gpu --features diagnostics --lib concurrent_release_during_viewed_set and release_before_viewed_set: 2 passed
  • cargo test -p victory-gpu --features diagnostics --lib: 248 passed, 0 failed, 1 ignored (main: 246 passed, 0 failed, 1 ignored)

Control (this head with only the two guards removed: the liveness re-check in Viewed::set and the TEXTURES_AT check in made_copy; test code and LiveTextures kept): concurrent_release_during_viewed_set_does_not_leave_dangling_texture fails (dangling texture remains in Viewed::textures); release_before_viewed_set_and_made_copy_rejects_freed_texture aborts with STATUS_HEAP_CORRUPTION (0xc0000374), i.e. the use-after-free the PR describes. Both tests catch the bug.

Diff review: lock order TEXTURES_AT then Resource::host in made_copy matches give_up, so no new inversion that I can see; Resource::drop now removes the texture from the live set before bindless::forget, which closes the window. A minor note only: liveness is by address, so a new Resource allocated at a freed one's address would pass is_live_texture; that is no worse than before and not something these tests cover.

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

**@⁠oni-swr** on GitHub · 2026-11-26 13:49 UTC · [original](https://github.com/Marvisak/VIctory/pull/3213#issuecomment-7193890154) **WARP verification of head `92915c647fed37a42dbbf72561598c229be32ebe`** (on origin/main `63bcca4`; no earlier verification on this PR) Environment: Windows 11, no physical GPU used. `VICTORY_WARP=1` and `VICTORY_D3D12_WARP=1` set in every process, with `--features diagnostics` (the host only honours the WARP switch in that build). A local probe (not committed) printed and asserted the host device's adapter: `Microsoft Basic Render Driver`, DXGI flags `0x2` (software). Commands (`-j 4`, `--locked`, `--test-threads=1`): - `cargo test -p victory-gpu --features diagnostics --lib concurrent_release_during_viewed_set` and `release_before_viewed_set`: 2 passed - `cargo test -p victory-gpu --features diagnostics --lib`: 248 passed, 0 failed, 1 ignored (main: 246 passed, 0 failed, 1 ignored) Control (this head with only the two guards removed: the liveness re-check in `Viewed::set` and the `TEXTURES_AT` check in `made_copy`; test code and `LiveTextures` kept): `concurrent_release_during_viewed_set_does_not_leave_dangling_texture` fails (dangling texture remains in `Viewed::textures`); `release_before_viewed_set_and_made_copy_rejects_freed_texture` aborts with `STATUS_HEAP_CORRUPTION` (0xc0000374), i.e. the use-after-free the PR describes. Both tests catch the bug. Diff review: lock order `TEXTURES_AT` then `Resource::host` in `made_copy` matches `give_up`, so no new inversion that I can see; `Resource::drop` now removes the texture from the live set before `bindless::forget`, which closes the window. A minor note only: liveness is by address, so a new `Resource` allocated at a freed one's address would pass `is_live_texture`; that is no worse than before and not something these tests cover. 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/3213/head:github-bot/gh-3213'
git switch 'github-bot/gh-3213'

Merge

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