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

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

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


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.
<!-- greyfinch-sync:Marvisak/VIctory:issue:6628720670 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/3245). Originally posted by [fuzeofficlal](https://github.com/fuzeofficlal) on 2026-11-26T14:24:45Z. --- ### 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`.
Dukestar69 commented 2026-11-26 14:29:14 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by Dukestar69 on 2026-11-26T14:29:14Z.

Review: APPROVED


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

<!-- greyfinch-sync:Marvisak/VIctory:review:6075836948 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/3245#pullrequestreview-6075836948). Originally posted by [Dukestar69](https://github.com/Dukestar69) on 2026-11-26T14:29:14Z. **Review:** APPROVED --- 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).
KainanP commented 2026-11-26 15:02:35 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by KainanP on 2026-11-26T14:50:20Z.


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.

<!-- greyfinch-sync:Marvisak/VIctory:comment:7195403723 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/3245#issuecomment-7195403723). Originally posted by [KainanP](https://github.com/KainanP) on 2026-11-26T14:50:20Z. --- 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._
fuzeofficlal commented 2026-11-26 16:41:13 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by fuzeofficlal on 2026-11-26T16:41:13Z.


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

<!-- greyfinch-sync:Marvisak/VIctory:comment:7197966589 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/3245#issuecomment-7197966589). Originally posted by [fuzeofficlal](https://github.com/fuzeofficlal) on 2026-11-26T16:41:13Z. --- 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 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/3245:upstream-pr/3245'
git switch 'upstream-pr/3245'

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