From 5c3ff31790711dcb010590c6c1e0889a61be27fd Mon Sep 17 00:00:00 2001 From: theaustinayers <153482135+theaustinayers@users.noreply.github.com> Date: Tue, 24 Nov 2026 03:00:18 -0500 Subject: [PATCH] D3D12.X: a released allocator leaves nothing behind in alloc_pool #897 kept, by address, which PC allocator a title's allocator stands for. The entry stayed when the allocator was released, so a later allocator at the same address went on with the old one's PC allocator - of another type or device. On CI (WARP) that failed mid_list_write64_wait64_split_avoids_cycle_and_reuses_spares: its list Reset got E_INVALIDARG after alloc_pool's own test had left an entry for an address the test's allocator then received. The allocator's last Release now drops its entries; the PC allocators made for it go with it when nothing records on them any more. Assisted-by: Claude Code (Claude Fable 5.2) --- crates/xcl/src/hle/d3d12x/alloc_pool.rs | 44 +++++++++++++++++++++++++ crates/xcl/src/hle/d3d12x/real.rs | 1 + 2 files changed, 45 insertions(+) diff --git a/crates/xcl/src/hle/d3d12x/alloc_pool.rs b/crates/xcl/src/hle/d3d12x/alloc_pool.rs index 4adba60577..4815766b33 100644 --- a/crates/xcl/src/hle/d3d12x/alloc_pool.rs +++ b/crates/xcl/src/hle/d3d12x/alloc_pool.rs @@ -140,6 +140,37 @@ pub unsafe fn executed(queue: *mut c_void, lists: &[*mut c_void]) { } } +/// The title's allocator `alloc` is gone (its last Release): nothing of it stays here – a later +/// allocator may get its address, and must not go on with this one's PC allocators. The ones +/// made here go with it (those lists still record on are left alone: not freed, not tracked). +pub unsafe fn forget(alloc: usize) { + let made: Vec = { + let mut g = STATE.lock().unwrap(); + let Some(s) = g.as_mut() else { return }; + s.types.remove(&alloc); + let mut mine: Vec = s.aside.remove(&alloc).unwrap_or_default(); + mine.extend(s.cur.remove(&alloc)); + mine.push(alloc); + let mut made = Vec::new(); + for b in mine { + let free = s.backings.get(&b).is_none_or(|x| x.free()); + if let Some(gone) = s.backings.remove(&b) { + for l in gone.users { + s.of_list.remove(&l); + } + } + if b != alloc && free { + made.push(b); + } + } + made + }; + // (outside the lock: their Release comes back here) + for b in made { + drop(ID3D12CommandAllocator::from_raw(b as *mut c_void)); + } +} + /// The title resets its allocator `alloc`. `None`: reset it as it is (nothing holds a /// recording on it, or this is off). `Some(hr)`: done here – the PC allocator in use was put /// aside with its recordings and another one took its place. @@ -265,6 +296,19 @@ mod tests { alloc.Reset().unwrap(); assert_eq!(current(a as usize), a as usize, "the allocator put aside is free again"); assert!(dev.GetDeviceRemovedReason().is_ok()); + + // the title's allocator goes while another PC allocator stands for it: nothing of + // it stays (the next allocator at its address is its own) + list.Reset(&alloc, None).unwrap(); + for_list(l as usize, a as usize, D3D12_COMMAND_LIST_TYPE_DIRECT.0); + list.Close().unwrap(); + alloc.Reset().unwrap(); + assert_ne!(current(a as usize), a as usize); + drop(lists); + drop(list); + drop(alloc); + assert_eq!(current(a as usize), a as usize, "a released allocator leaves nothing behind"); + assert!(!STATE.lock().unwrap().as_ref().unwrap().types.contains_key(&(a as usize))); } } } diff --git a/crates/xcl/src/hle/d3d12x/real.rs b/crates/xcl/src/hle/d3d12x/real.rs index 27a642ac05..5b01e85692 100644 --- a/crates/xcl/src/hle/d3d12x/real.rs +++ b/crates/xcl/src/hle/d3d12x/real.rs @@ -4861,6 +4861,7 @@ unsafe extern "system" fn alloc_release(this: *mut c_void) -> u32 { let f: unsafe extern "system" fn(*mut c_void) -> u32 = std::mem::transmute(orig_fn(this, 2)); let n = f(this); if n == 0 { + super::alloc_pool::forget(this as usize); super::list_x::on_allocator_release(this as usize); } n