diff --git a/crates/victory-gpu/src/draw.rs b/crates/victory-gpu/src/draw.rs index cfc3fe2..6f2e4fb 100644 --- a/crates/victory-gpu/src/draw.rs +++ b/crates/victory-gpu/src/draw.rs @@ -2736,7 +2736,7 @@ impl State { // copies that texture's texels into it on the host, no upload. // (Lighting tables read as R9G9B9E5: 6 to 13 sources a run were // counted as failed for want of an upload.) - if crate::resource::alias_of(resource).is_some() { + if crate::resource::is_aliased(resource) { readable(list, ResourceType::Srv, &view); n[if shown { 2 } else { 5 }] += 1; if !shown { @@ -5671,7 +5671,7 @@ fn readable(list: &HostList, kind: ResourceType, view: &View) { // a texture the host writes: that texture's texels, copied on // the host (title memory does not have them). let aliased = crate::resource::alias_of(*resource as This) - .is_some_and(|source| crate::texture::alias(list, *resource as This, source)); + .is_some_and(|source| crate::texture::alias(list, *resource as This, source.get())); if !aliased && let Some(t) = crate::resource::host_copy(*resource as This) && t.flags() & 0x7 == 0 diff --git a/crates/victory-gpu/src/external.rs b/crates/victory-gpu/src/external.rs index a52cafb..c96c117 100644 --- a/crates/victory-gpu/src/external.rs +++ b/crates/victory-gpu/src/external.rs @@ -29,7 +29,7 @@ pub(crate) fn host_texture(list: &HostList, resource: This, state: u32) -> Optio /// rules a compute shader's resource view has in the renderer. pub(crate) fn prepare_input(list: &HostList, resource: This) -> Option<()> { let texture = crate::resource::host_resource(resource)?; - let aliased = crate::resource::alias_of(resource).is_some_and(|source| crate::texture::alias(list, resource, source)); + let aliased = crate::resource::alias_of(resource).is_some_and(|source| crate::texture::alias(list, resource, source.get())); if !aliased && crate::texture::title_written(texture) { crate::texture::upload_from(list, resource, 0); } diff --git a/crates/victory-gpu/src/object.rs b/crates/victory-gpu/src/object.rs index 6671d4b..736e650 100644 --- a/crates/victory-gpu/src/object.rs +++ b/crates/victory-gpu/src/object.rs @@ -71,6 +71,15 @@ pub(crate) extern "system" fn add_ref(this: This) -> u32 { unsafe { &*this }.refs.fetch_add(1, Ordering::Relaxed) + 1 } +/// A reference more to `this` only while it still has one: `false` if it is +/// being released (its count reached 0). From CuteBooty/victory-8gb (after +/// fxceless, victory-fork d4a528ba), for [`crate::resource::alias_of`]. +pub(crate) fn add_ref_alive(this: This) -> bool { + // SAFETY: `this` is one of our objects, not freed yet. + let refs = &unsafe { &*this }.refs; + refs.fetch_update(Ordering::AcqRel, Ordering::Acquire, |n| (n > 0).then(|| n + 1)).is_ok() +} + pub(crate) extern "system" fn release(this: This) -> u32 { // SAFETY: `this` is one of our objects. let refs = &unsafe { &*this }.refs; diff --git a/crates/victory-gpu/src/resource.rs b/crates/victory-gpu/src/resource.rs index 2614124..f5c893d 100644 --- a/crates/victory-gpu/src/resource.rs +++ b/crates/victory-gpu/src/resource.rs @@ -1471,6 +1471,27 @@ fn textures() -> std::sync::MutexGuard<'static, std::collections::BTreeSet This { + self.0 + } +} + +#[cfg(windows)] +impl Drop for Held { + fn drop(&mut self) { + crate::object::release_boxed::(self.0); + } +} + /// The title textures the host renders to or writes (targets, depth, /// unordered access), by the address of their memory: what a view the /// title places in that memory in another format shows ([`alias_of`]). A @@ -1499,8 +1520,29 @@ static TARGETS_AT: std::sync::Mutex> = /// all zeros from a first stage that held light (run 88). /// `VICTORY_ALIAS_COPY=0`: none is found, as before; `=1`: only textures /// of one level and one layer, as at first. +/// +/// The texture is pinned under the table's lock and handed back held: once +/// the lock is let go the title can drop its last reference, and the +/// caller's alias copy still needs it (use-after-free, or a copy from the +/// next texture at the same address). One already being released is not +/// returned. Fork PR #1336 (CwWwWwW, f8fab6fb). #[cfg(windows)] -pub(crate) fn alias_of(this: This) -> Option { +pub(crate) fn alias_of(this: This) -> Option { + alias_with(this, |other| crate::object::add_ref_alive(other).then(|| Held(other))).flatten() +} + +/// Whether [`alias_of`] would find a texture for `this`, for callers that +/// only ask: nothing is pinned, so none of them can end up dropping the +/// texture's last reference. +#[cfg(windows)] +pub(crate) fn is_aliased(this: This) -> bool { + alias_with(this, |_| ()).is_some() +} + +/// The lookup behind [`alias_of`]: `found` runs on the texture while the +/// table's lock is still held. +#[cfg(windows)] +fn alias_with(this: This, found: impl FnOnce(This) -> R) -> Option { use std::sync::atomic::Ordering::Relaxed; static ON: std::sync::OnceLock = std::sync::OnceLock::new(); if this.is_null() @@ -1561,7 +1603,8 @@ pub(crate) fn alias_of(this: This) -> Option { ); } } - same.then_some(other) + // (Still under `targets`: a texture leaves the table before it is freed.) + same.then(|| found(other)) } /// Says (once per kind of pair, at most 64 lines) that the texture the @@ -1712,7 +1755,7 @@ fn hosted(this: This, make: bool) -> Option { // The levels views show ([`held_only`]), those of the slots shaders // pick by index among them. let mut base = 0; - if shape_fits && r.host.placed.load(Relaxed) && held_only() && alias_of(this).is_none() { + if shape_fits && r.host.placed.load(Relaxed) && held_only() && !is_aliased(this) { let wanted = r.host.wanted.load(Relaxed); let viewed = crate::descriptor::host::first_viewed(r.data() as u64).unwrap_or(u32::MAX); let first = wanted.min(viewed); @@ -5226,4 +5269,45 @@ float4 lit() : SV_Target { return 1.0; } assert_eq!((more.base, d.Width, d.MipLevels), (0, 256, 9)); assert!(more.serial != copy.serial && more.grown.load(std::sync::atomic::Ordering::Relaxed)); } + + /// Fork PR #1336 (CwWwWwW, f8fab6fb): the source a placed view aliases + /// is held while the caller copies it: the title may let go of its own + /// reference between the lookup and the copy (or a recording thread + /// may), and the copy still needs it. + #[cfg(all(windows, feature = "diagnostics"))] + #[test] + fn an_alias_source_is_held_after_the_title_releases_it() { + use std::sync::atomic::Ordering::Relaxed; + + let Some(_host) = crate::host::host() else { + return; + }; + let null = std::ptr::null(); + let mut source = null; + let mut view = null; + let source_desc = ResourceDesc { + flags: 4, + ..texture(4, 4, 42, 1) + }; + assert_eq!( + create_committed(null, &PROPERTIES, 0, &source_desc, 0, null.cast(), &RESOURCE_IIDS[0], &mut source), + S_OK + ); + assert_eq!( + create_at_address(null, resource(source).data() as u64, &texture(4, 4, 67, 1), 0, null.cast(), &RESOURCE_IIDS[0], &mut view), + S_OK + ); + resource(view).host.placed.store(true, Relaxed); + assert!(is_aliased(view), "the lookup alone finds it too"); + let held = alias_of(view).expect("an alias source"); + assert_eq!(held.get(), source); + // The title lets go: the pin, not the title's reference, keeps the + // source (and the copy about to be made from it) alive. + resource_release(source); + assert!(host_resource(held.get()).is_some(), "a held source is still copyable"); + assert!(textures().contains(&(source as usize)), "the held source is not freed"); + drop(held); + assert!(!textures().contains(&(source as usize)), "the last reference frees it"); + resource_release(view); + } } diff --git a/crates/victory-gpu/src/texture/ahead.rs b/crates/victory-gpu/src/texture/ahead.rs index 2cfdde6..5f36ebb 100644 --- a/crates/victory-gpu/src/texture/ahead.rs +++ b/crates/victory-gpu/src/texture/ahead.rs @@ -114,7 +114,7 @@ fn claim(resource: This, first: u32, there: u64) -> Option { || copy.holds.lock().unwrap_or_else(|e| e.into_inner()).is_some() || !crate::pool::fresh(©.texture) || super::host_wrote(resource as usize) - || crate::resource::alias_of(resource).is_some() + || crate::resource::is_aliased(resource) { return turned_down(); }