GPU: return pinned HostTexture from alias_source to avoid source Release UAF #3245

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

@⁠fuzeofficlal on GitHub · 2026-11-26 14:24 UTC · original
fuzeofficlal:fix/gpu-alias-source-drop-uaf → main · +388 −33 · 5 files · 1 commits · open

Problem

  1. Use-after-free across resource::alias_source → texture::alias_copy:
    resource::alias_source(placed: This) locks TEXTURES_AT, finds a GPU-written aliased texture a at the same backing address whose made(a) differs from made(placed), drops the cloned Arc<HostTexture> and releases TEXTURES_AT, and returns the raw unowned pointer Some(a) (This = *const Object) without taking a COM reference.
    Under legal multi-threaded D3D12 usage (for example, releasing a transient placed render target / UAV / depth resource on another thread while a recording thread draws with an incompatible-format aliased texture at the same address), a->Release() removes a from TEXTURES_AT and frees Box<Resource> before texture::alias_copy runs. alias_copy then dereferences the freed Resource in both resource::host_resource(from) and list::ensure_state(list, from, COPY_SOURCE).

  2. Destination identity split under concurrent eviction in texture::alias_copy:
    alias_copy separately called resource::host_resource(to), texture::host_writes(list, to), and list::ensure_state(list, to, COPY_DEST). Because a title-written placed destination texture is evictable before host_writes runs at submission, a concurrent resource::evict_unused() between those calls could recreate a new HostTexture for host_writes and ensure_state while CopyTextureRegion wrote into the earlier, untransitioned and unpinned ID3D12Resource.

Fix

  • Change resource::alias_source(placed: This) to return Option<Arc<HostTexture>>, cloning the source Arc<HostTexture> while still holding TEXTURES_AT (which Resource::drop must acquire before dropping fields or freeing Box<Resource>).
  • Factor HostList::transition_copy(&self, copy: &Arc<HostTexture>, wanted: u32) (preserving depth-twin routing via twin::route) and list::ensure_copy_state so alias_copy transitions and retains pinned Arc<HostTexture> values directly without dereferencing from: This.
  • Update texture::alias_copy to take from: &Arc<HostTexture>, pin to_copy = resource::host_copy(to)? once at entry (skipping self-copy if Arc::ptr_eq(from, &to_copy)), and use the same pinned from and to_copy across first_use, GetDesc, host_writes_copy, ensure_copy_state, CopyTextureRegion, and log deduplication.
  • Update the call site in crates/victory-gpu/src/draw.rs (&a) and the null-view unit test in crates/victory-gpu/src/resource.rs.

Verification

  • Added deterministic GPU regression tests in crates/victory-gpu/src/texture.rs:
    • alias_copy_survives_source_resource_release_after_alias_source: reproduces the bug deterministically on unmodified main (BC1 block (0, 0) reads [0, 0] instead of [0x3f800000, 0x40000000] after Release(src) and slot reuse) and passes with exact readback and zero D3D12 debug errors after the fix.
    • alias_source_and_copy_handle_concurrent_release_and_destination_eviction: exercises a two-thread Barrier(2) race between alias_source + alias_copy and src->Release(), plus resource::give_up_now(dst) eviction between alias_source and alias_copy.
  • Verified on Windows with RTX 5090 (default features and VICTORY_D3D12_DEBUG=1 --features diagnostics), WARP (VICTORY_D3D12_DEBUG=1 --features diagnostics and full victory-gpu suite: 248 passed, 0 failed, 1 ignored), both workspace clippy -D warnings configurations, rustfmt --edition 2024, and xtask policy::tests::repository_passes_policy.
**@⁠fuzeofficlal** on GitHub · 2026-11-26 14:24 UTC · [original](https://github.com/Marvisak/VIctory/pull/3245) `fuzeofficlal:fix/gpu-alias-source-drop-uaf` → `main` · +388 −33 · 5 files · 1 commits · open ### Problem 1. **Use-after-free across `resource::alias_source` → `texture::alias_copy`**: `resource::alias_source(placed: This)` locks `TEXTURES_AT`, finds a GPU-written aliased texture `a` at the same backing address whose `made(a)` differs from `made(placed)`, drops the cloned `Arc<HostTexture>` and releases `TEXTURES_AT`, and returns the raw unowned pointer `Some(a)` (`This = *const Object`) without taking a COM reference. Under legal multi-threaded D3D12 usage (for example, releasing a transient placed render target / UAV / depth resource on another thread while a recording thread draws with an incompatible-format aliased texture at the same address), `a->Release()` removes `a` from `TEXTURES_AT` and frees `Box<Resource>` before `texture::alias_copy` runs. `alias_copy` then dereferences the freed `Resource` in both `resource::host_resource(from)` and `list::ensure_state(list, from, COPY_SOURCE)`. 2. **Destination identity split under concurrent eviction in `texture::alias_copy`**: `alias_copy` separately called `resource::host_resource(to)`, `texture::host_writes(list, to)`, and `list::ensure_state(list, to, COPY_DEST)`. Because a title-written placed destination texture is evictable before `host_writes` runs at submission, a concurrent `resource::evict_unused()` between those calls could recreate a new `HostTexture` for `host_writes` and `ensure_state` while `CopyTextureRegion` wrote into the earlier, untransitioned and unpinned `ID3D12Resource`. ### Fix - Change `resource::alias_source(placed: This)` to return `Option<Arc<HostTexture>>`, cloning the source `Arc<HostTexture>` while still holding `TEXTURES_AT` (which `Resource::drop` must acquire before dropping fields or freeing `Box<Resource>`). - Factor `HostList::transition_copy(&self, copy: &Arc<HostTexture>, wanted: u32)` (preserving depth-twin routing via `twin::route`) and `list::ensure_copy_state` so `alias_copy` transitions and retains pinned `Arc<HostTexture>` values directly without dereferencing `from: This`. - Update `texture::alias_copy` to take `from: &Arc<HostTexture>`, pin `to_copy = resource::host_copy(to)?` once at entry (skipping self-copy if `Arc::ptr_eq(from, &to_copy)`), and use the same pinned `from` and `to_copy` across `first_use`, `GetDesc`, `host_writes_copy`, `ensure_copy_state`, `CopyTextureRegion`, and log deduplication. - Update the call site in `crates/victory-gpu/src/draw.rs` (`&a`) and the null-view unit test in `crates/victory-gpu/src/resource.rs`. ### Verification - Added deterministic GPU regression tests in `crates/victory-gpu/src/texture.rs`: - `alias_copy_survives_source_resource_release_after_alias_source`: reproduces the bug deterministically on unmodified `main` (`BC1 block (0, 0)` reads `[0, 0]` instead of `[0x3f800000, 0x40000000]` after `Release(src)` and slot reuse) and passes with exact readback and zero D3D12 debug errors after the fix. - `alias_source_and_copy_handle_concurrent_release_and_destination_eviction`: exercises a two-thread `Barrier(2)` race between `alias_source` + `alias_copy` and `src->Release()`, plus `resource::give_up_now(dst)` eviction between `alias_source` and `alias_copy`. - Verified on Windows with RTX 5090 (default features and `VICTORY_D3D12_DEBUG=1 --features diagnostics`), WARP (`VICTORY_D3D12_DEBUG=1 --features diagnostics` and full `victory-gpu` suite: 248 passed, 0 failed, 1 ignored), both workspace `clippy -D warnings` configurations, `rustfmt --edition 2024`, and `xtask` `policy::tests::repository_passes_policy`.
resource::alias_source previously looked up a GPU-written aliased texture under TEXTURES_AT, dropped the Arc<HostTexture> and lock, and returned a raw *const Object (This) with no COM reference. If another thread legally called ID3D12Resource::Release() on the aliased source texture before texture::alias_copy ran, alias_copy dereferenced the freed Box<Resource> (or a recycled allocation at the same address) in resource::host_resource and list::ensure_state.

Return Option<Arc<HostTexture>> cloned while holding TEXTURES_AT, route pinned host-texture transitions (including depth twins) through HostList::transition_copy and list::ensure_copy_state, and pin the destination HostTexture once inside texture::alias_copy so concurrent eviction cannot split destination identity across GetDesc, host_writes, state tracking and CopyTextureRegion.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 14:27:19 +00:00
Author
Owner

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

Reviewed the diff and verified locally on Windows 11 x64 (RTX 5090 + WARP):

  • Cloning Arc inside lias_source under TEXTURES_AT and pinning o_copy once in lias_copy (plus ransition_copy / ensure_copy_state / host_writes_copy) cleanly closes both the concurrent source Release() UAF and destination eviction race.
  • cargo test -p victory-gpu --lib texture:: -- --test-threads=1 (RTX 5090): 4 passed, 0 failed (including lias_copy_survives_source_resource_release_after_alias_source and lias_source_and_copy_handle_concurrent_release_and_destination_eviction).
  • cargo test -p victory-gpu --lib resource:: -- --test-threads=1 (RTX 5090): 18 passed, 0 failed.
  • VICTORY_FORCE_WARP=1 cargo test -p victory-gpu --lib texture::tests::alias_ -- --test-threads=1 (WARP): 2 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).

**@⁠Dukestar69** on GitHub · 2026-11-26 14:29 UTC · [original](https://github.com/Marvisak/VIctory/pull/3245#pullrequestreview-6075836948) · **approved these changes** Reviewed the diff and verified locally on Windows 11 x64 (RTX 5090 + WARP): - Cloning Arc<HostTexture> inside lias_source under TEXTURES_AT and pinning o_copy once in lias_copy (plus ransition_copy / ensure_copy_state / host_writes_copy) cleanly closes both the concurrent source Release() UAF and destination eviction race. - cargo test -p victory-gpu --lib texture:: -- --test-threads=1 (RTX 5090): 4 passed, 0 failed (including lias_copy_survives_source_resource_release_after_alias_source and lias_source_and_copy_handle_concurrent_release_and_destination_eviction). - cargo test -p victory-gpu --lib resource:: -- --test-threads=1 (RTX 5090): 18 passed, 0 failed. - VICTORY_FORCE_WARP=1 cargo test -p victory-gpu --lib texture::tests::alias_ -- --test-threads=1 (WARP): 2 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).
Author
Owner

@⁠KainanP on GitHub · 2026-11-26 14:50 UTC · original

Heads-up: #3258 (a per-draw CPU optimization) also adds a path that moves a host copy the caller already holds (HostList::transition_held / list::ensure_held_state). It touches the same lines of HostList::transition and list::ensure_state as this PR's transition_copy / ensure_copy_state, so the two conflict textually. Whichever lands second should use the other's helper. I'll rebase #3258 if this one goes in first.

AI use: Claude Code (Claude Opus 5.5) drafted this comment.

**@⁠KainanP** on GitHub · 2026-11-26 14:50 UTC · [original](https://github.com/Marvisak/VIctory/pull/3245#issuecomment-7195403723) Heads-up: #3258 (a per-draw CPU optimization) also adds a path that moves a host copy the caller already holds (`HostList::transition_held` / `list::ensure_held_state`). It touches the same lines of `HostList::transition` and `list::ensure_state` as this PR's `transition_copy` / `ensure_copy_state`, so the two conflict textually. Whichever lands second should use the other's helper. I'll rebase #3258 if this one goes in first. _AI use: Claude Code (Claude Opus 5.5) drafted this comment._
Author
Owner

@⁠fuzeofficlal on GitHub · 2026-11-26 16:41 UTC · original

Thanks for the heads-up @⁠KainanP! Sounds great — whichever lands first, the other can cleanly reuse the shared held-texture transition helper.

**@⁠fuzeofficlal** on GitHub · 2026-11-26 16:41 UTC · [original](https://github.com/Marvisak/VIctory/pull/3245#issuecomment-7197966589) Thanks for the heads-up @⁠KainanP! Sounds great — whichever lands first, the other can cleanly reuse the shared held-texture transition helper.
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/3245/head:github-bot/gh-3245'
git switch 'github-bot/gh-3245'

Merge

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