Repository navigation
GPU: return pinned HostTexture from alias_source to avoid source Release UAF - #3245
fuzeofficlal wants to merge 1 commit into
Conversation
…ase UAF 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.
Dukestar69
left a comment
There was a problem hiding this comment.
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).
Import upstream Marvisak#3245: pin aliased HostTexture across copy
|
Heads-up: #3258 (a per-draw CPU optimization) also adds a path that moves a host copy the caller already holds ( AI use: Claude Code (Claude Opus 5.5) drafted this comment. |
Adapt upstream Marvisak#3258 to the fork's Marvisak#3245 pinned-copy helpers. Cache immutable resource flags, use one retained HostTexture for upload/staging/state, and add flag parity and repeated-read regression coverage.
…alias_source to avoid source Release UAF
|
Thanks for the heads-up @KainanP! Sounds great — whichever lands first, the other can cleanly reuse the shared held-texture transition helper. |
Problem
Use-after-free across
resource::alias_source→texture::alias_copy:resource::alias_source(placed: This)locksTEXTURES_AT, finds a GPU-written aliased textureaat the same backing address whosemade(a)differs frommade(placed), drops the clonedArc<HostTexture>and releasesTEXTURES_AT, and returns the raw unowned pointerSome(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()removesafromTEXTURES_ATand freesBox<Resource>beforetexture::alias_copyruns.alias_copythen dereferences the freedResourcein bothresource::host_resource(from)andlist::ensure_state(list, from, COPY_SOURCE).Destination identity split under concurrent eviction in
texture::alias_copy:alias_copyseparately calledresource::host_resource(to),texture::host_writes(list, to), andlist::ensure_state(list, to, COPY_DEST). Because a title-written placed destination texture is evictable beforehost_writesruns at submission, a concurrentresource::evict_unused()between those calls could recreate a newHostTextureforhost_writesandensure_statewhileCopyTextureRegionwrote into the earlier, untransitioned and unpinnedID3D12Resource.Fix
resource::alias_source(placed: This)to returnOption<Arc<HostTexture>>, cloning the sourceArc<HostTexture>while still holdingTEXTURES_AT(whichResource::dropmust acquire before dropping fields or freeingBox<Resource>).HostList::transition_copy(&self, copy: &Arc<HostTexture>, wanted: u32)(preserving depth-twin routing viatwin::route) andlist::ensure_copy_statesoalias_copytransitions and retains pinnedArc<HostTexture>values directly without dereferencingfrom: This.texture::alias_copyto takefrom: &Arc<HostTexture>, pinto_copy = resource::host_copy(to)?once at entry (skipping self-copy ifArc::ptr_eq(from, &to_copy)), and use the same pinnedfromandto_copyacrossfirst_use,GetDesc,host_writes_copy,ensure_copy_state,CopyTextureRegion, and log deduplication.crates/victory-gpu/src/draw.rs(&a) and the null-view unit test incrates/victory-gpu/src/resource.rs.Verification
crates/victory-gpu/src/texture.rs:alias_copy_survives_source_resource_release_after_alias_source: reproduces the bug deterministically on unmodifiedmain(BC1 block (0, 0)reads[0, 0]instead of[0x3f800000, 0x40000000]afterRelease(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-threadBarrier(2)race betweenalias_source+alias_copyandsrc->Release(), plusresource::give_up_now(dst)eviction betweenalias_sourceandalias_copy.VICTORY_D3D12_DEBUG=1 --features diagnostics), WARP (VICTORY_D3D12_DEBUG=1 --features diagnosticsand fullvictory-gpusuite: 248 passed, 0 failed, 1 ignored), both workspaceclippy -D warningsconfigurations,rustfmt --edition 2024, andxtaskpolicy::tests::repository_passes_policy.