fix(gpu): pin the texture a placed view aliases while the copy uses it
alias_of returned the bare pointer of the aliased texture after TARGETS_AT's lock was gone, and texture::alias then used it; the title can drop its last reference in between (use-after-free, or a copy from the next texture at the same address). alias_of now takes a reference under the lock (object::add_ref_alive) and returns a Held guard that the two copying callers (draw.rs readable, external.rs prepare_input) keep until the alias copy is recorded; a texture already being released is not returned. This tree has three lookup-only callers (draw.rs bake sources, texture/ahead.rs, resource.rs host-copy levels). They now call is_aliased, the same lookup without a pin, so none of them takes and drops a reference (and so none can end up freeing the texture inside its own work). No switch: a lifetime fix with no other behaviour change. The original test comes along (diagnostics builds with a host GPU only). Original author: CwWwWwW (fxceless/victory-fork pull request #1336, f8fab6fb). Ported by hand from CuteBooty/victory-8gb; Held and add_ref_alive follow fxceless/victory-fork d4a528ba, which this tree does not have. Co-Authored-By: Claude Fable 5.2 <[email protected]> (cherry picked from commit 6665ad8bd2147e58aed9362e41718aa385643102)
This commit is contained in:
1 parent
8e9f11d618
commit
74d6f2408b
5 files changed
+100
-7
No files matched your search
@@ -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
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -1471,6 +1471,27 @@ fn textures() -> std::sync::MutexGuard<'static, std::collections::BTreeSet<usize
|
||||
TEXTURES.lock().unwrap_or_else(|e| e.into_inner())
|
||||
}
|
||||
|
||||
/// A title texture held alive by an owned reference ([`alias_of`]); the
|
||||
/// reference is let go when this is dropped. Fork PR #1336 (CwWwWwW,
|
||||
/// f8fab6fb), as carried in CuteBooty/victory-8gb.
|
||||
#[cfg(windows)]
|
||||
pub(crate) struct Held(This);
|
||||
|
||||
#[cfg(windows)]
|
||||
impl Held {
|
||||
/// The texture the handle keeps alive.
|
||||
pub(crate) fn get(&self) -> This {
|
||||
self.0
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(windows)]
|
||||
impl Drop for Held {
|
||||
fn drop(&mut self) {
|
||||
crate::object::release_boxed::<Resource>(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<std::collections::BTreeMap<u64, usize>> =
|
||||
/// 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<This> {
|
||||
pub(crate) fn alias_of(this: This) -> Option<Held> {
|
||||
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<R>(this: This, found: impl FnOnce(This) -> R) -> Option<R> {
|
||||
use std::sync::atomic::Ordering::Relaxed;
|
||||
static ON: std::sync::OnceLock<bool> = std::sync::OnceLock::new();
|
||||
if this.is_null()
|
||||
@@ -1561,7 +1603,8 @@ pub(crate) fn alias_of(this: This) -> Option<This> {
|
||||
);
|
||||
}
|
||||
}
|
||||
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<Made> {
|
||||
// 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);
|
||||
}
|
||||
}
|
||||
@@ -114,7 +114,7 @@ fn claim(resource: This, first: u32, there: u64) -> Option<Taken> {
|
||||
|| 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();
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user