From a3b43590b792d3aaaed6c3b200722316a3c0f7c1 Mon Sep 17 00:00:00 2001 From: bmjask <> Date: Thu, 26 Nov 2026 03:23:52 -0800 Subject: [PATCH] D3D12.X: lease exact descriptor heap slots Keep the source and shared native descriptor heaps and their device alive while a future quiescent mutation validates an exact heap generation and slot. Serialize opt-in final heap Release without taking the descriptor-write gate, and preserve the default-off path. Assisted-by: Codex (GPT-5.6-sol) --- crates/xcl/src/hle/d3d12x/console_heap.rs | 186 +++++++++++++++++++++- crates/xcl/src/hle/d3d12x/real.rs | 15 +- crates/xcl/src/hle/d3d12x/vram.rs | 65 +++++++- 3 files changed, 255 insertions(+), 11 deletions(-) diff --git a/crates/xcl/src/hle/d3d12x/console_heap.rs b/crates/xcl/src/hle/d3d12x/console_heap.rs index 15cb8a4361..575762ee71 100644 --- a/crates/xcl/src/hle/d3d12x/console_heap.rs +++ b/crates/xcl/src/hle/d3d12x/console_heap.rs @@ -76,6 +76,16 @@ impl Heap { static HEAPS: Mutex> = Mutex::new(Vec::new()); static NEXT_HEAP_GENERATION: AtomicU64 = AtomicU64::new(0); +static HEAP_LIFETIMES: Mutex<()> = Mutex::new(()); + +pub(super) type HeapLifetimeGuard = std::sync::MutexGuard<'static, ()>; + +/// Exclude final descriptor-heap `Release` while an exact heap generation is turned into +/// strong native owners. This is separate from the descriptor-write gate: hooks keep that gate +/// across driver calls which may temporarily release a heap. +pub(super) fn lock_lifetime() -> Option { + super::vram::srv_tracking_enabled().then(|| HEAP_LIFETIMES.lock().unwrap()) +} /// Shared ranges being written by lock-free single-descriptor publication, and releases which /// cannot return to the allocator until every overlapping write has finished. @@ -496,15 +506,29 @@ pub struct Taken(Heap); /// Stop finding a heap by object address without freeing memory the object may still use. pub fn take(obj: usize) -> Option { + let heap = detach(obj)?; + super::vram::forget_srv_heap(heap.generation); + Some(Taken(heap)) +} + +fn detach(obj: usize) -> Option { + let mut heaps = HEAPS.lock().unwrap(); + heaps.iter().position(|h| h.obj == obj).map(|i| heaps.swap_remove(i)) +} + +pub(super) fn generation(obj: usize) -> Option { + HEAPS.lock().unwrap().iter().find(|heap| heap.obj == obj).map(|heap| heap.generation) +} + +/// Detach bookkeeping before the native object can die. The caller holds `HEAP_LIFETIMES`, so +/// cleanup must not wait for the descriptor-write gate (a reentrant temporary Release may hold +/// that gate while waiting here). +pub(super) fn take_after_release(obj: usize, generation: Option, lifetime: Option<&HeapLifetimeGuard>) -> Option { let mut heaps = HEAPS.lock().unwrap(); - let heap = heaps.iter().position(|h| h.obj == obj).map(|i| heaps.swap_remove(i)); + let heap = heaps.iter().position(|h| h.obj == obj && Some(h.generation) == generation).map(|i| heaps.swap_remove(i))?; drop(heaps); - if let Some(heap) = heap { - super::vram::forget_srv_heap(heap.generation); - Some(Taken(heap)) - } else { - None - } + super::vram::forget_srv_heap_after_release(heap.generation, lifetime); + Some(Taken(heap)) } /// Return a taken heap's slots and shadow memory after its native object is gone. @@ -555,6 +579,79 @@ pub struct Slot { pub remaining: u32, } +/// Strong ownership and the exact handles of one view slot. Holding this value only proves CPU +/// object lifetime; a consumer must separately prove queue quiescence, take the descriptor-write +/// gate, and revalidate the tracked logical generation and words before mutating descriptors. +#[allow(dead_code)] +pub(super) struct ViewSlotLease { + pub pc_srv: u64, + pub shared_srv: u64, + pub index: u32, + _device: ID3D12Device, + _source_heap: ID3D12DescriptorHeap, + _shared_heap: Option, +} + +/// Lease the native objects behind one exact heap generation and slot. All arithmetic and both +/// CPU handles are checked while `HEAPS` (and, for a shared slot, `GLOBAL`) still identify that +/// generation. Driver calls happen only after allocator locks have been dropped. +#[allow(dead_code)] +pub(super) fn lease_view_slot(generation: u64, k: u32, pc_srv: u64, shared_srv: u64, index: u32) -> Option { + let _lifetime = lock_lifetime()?; + let (source_raw, shared_index) = { + let heaps = HEAPS.lock().unwrap(); + let h = heaps.iter().find(|h| h.generation == generation && !h.sampler && k < h.n)?; + let local_index = k.checked_mul(2)?; + let expected_pc = h.pc_cpu.checked_add(local_index as u64 * h.inc as u64)?; + if expected_pc != pc_srv { + return None; + } + let shared_index = if h.pages.is_empty() { + if shared_srv != 0 || index != local_index { + return None; + } + None + } else { + let first = *h.pages.get((k / PAGE_SLOTS) as usize)?; + if first == UNMAPPED { + return None; + } + Some(first.checked_add(2 * (k % PAGE_SLOTS))?) + }; + (h.obj as *mut std::ffi::c_void, shared_index) + }; + let shared_raw = if let Some(expected_index) = shared_index { + let global = GLOBAL.lock().unwrap(); + let global = global.as_ref()?; + if expected_index.checked_add(1)? >= global.cap + || index != expected_index + || shared_srv != global.cpu.checked_add(expected_index as u64 * global.inc as u64)? + { + return None; + } + Some((global.dev.as_raw(), global.heap.as_raw())) + } else { + None + }; + // The lifetime gate prevents source final Release, and process-global heaps are never + // replaced. Invoke COM only after allocator mutexes have been dropped. + let source_heap = unsafe { ID3D12DescriptorHeap::from_raw_borrowed(&source_raw)?.clone() }; + // The strong source owner now prevents native final Release. Leave the non-reentrant gate + // before any fallible COM work so an error or unwind can drop that owner through our hooked + // Release without trying to reacquire a gate this thread still holds. + drop(_lifetime); + let (device, shared_heap) = if let Some((device_raw, heap_raw)) = shared_raw { + let device = unsafe { ID3D12Device::from_raw_borrowed(&device_raw)?.clone() }; + let heap = unsafe { ID3D12DescriptorHeap::from_raw_borrowed(&heap_raw)?.clone() }; + (device, Some(heap)) + } else { + let mut device = None; + unsafe { source_heap.GetDevice(&mut device).ok()? }; + (device?, None) + }; + Some(ViewSlotLease { pc_srv, shared_srv, index, _device: device, _source_heap: source_heap, _shared_heap: shared_heap }) +} + /// The slot behind a console CPU handle (None: not one of XCL's console heaps). A view slot /// of a heap in the shared heap gets its page's place there now if the page has none yet. pub fn slot(cpu: u64) -> Option { @@ -1062,6 +1159,81 @@ mod tests { assert!(free.is_empty()); } + #[test] + fn exact_view_slot_lease_survives_detach_and_rejects_address_reuse() { + const CHILD: &str = "XCL_TEST_VIEW_SLOT_LEASE_CHILD"; + if std::env::var_os(CHILD).is_none() { + let output = std::process::Command::new(std::env::current_exe().unwrap()) + .args(["--exact", "hle::d3d12x::console_heap::tests::exact_view_slot_lease_survives_detach_and_rejects_address_reuse", "--nocapture"]) + .env(CHILD, "1").env("XCL_TEXTURE_BUDGET_EVICT", "1").output().unwrap(); + assert!(output.status.success(), "{}\n{}", String::from_utf8_lossy(&output.stdout), String::from_utf8_lossy(&output.stderr)); + return; + } + use std::ffi::c_void; + use std::sync::atomic::{AtomicBool, AtomicU32, Ordering}; + static RELEASE_REENTERED: AtomicBool = AtomicBool::new(false); + #[repr(C)] + struct Object { vt: *const usize, refs: AtomicU32 } + unsafe extern "system" fn add_ref(this: *mut c_void) -> u32 { + (*(this as *mut Object)).refs.fetch_add(1, Ordering::Relaxed) + 1 + } + unsafe extern "system" fn release(this: *mut c_void) -> u32 { + RELEASE_REENTERED.fetch_or(HEAP_LIFETIMES.try_lock().is_ok(), Ordering::Relaxed); + (*(this as *mut Object)).refs.fetch_sub(1, Ordering::Relaxed) - 1 + } + unsafe extern "system" fn get_device(_this: *mut c_void, _iid: *const windows::core::GUID, _out: *mut *mut c_void) -> windows::core::HRESULT { + windows::core::HRESULT(0x8000_4002u32 as i32) + } + let mut vt = [0usize; 8]; + vt[1] = add_ref as usize; + vt[2] = release as usize; + vt[7] = get_device as usize; + let mut device = Object { vt: vt.as_ptr(), refs: AtomicU32::new(1) }; + let mut global_heap = Object { vt: vt.as_ptr(), refs: AtomicU32::new(1) }; + let mut source_heap = Object { vt: vt.as_ptr(), refs: AtomicU32::new(1) }; + unsafe { + *GLOBAL.lock().unwrap() = Some(Global { + dev: ID3D12Device::from_raw((&mut device as *mut Object).cast()), + heap: ID3D12DescriptorHeap::from_raw((&mut global_heap as *mut Object).cast()), + cpu: 0x10_0000, gpu: 0x20_0000, inc: 32, cap: 128, next: 80, + free: Vec::new(), leases: PublishLeases::default(), + }); + HEAPS.lock().unwrap().push(Heap { + generation: 70, obj: &mut source_heap as *mut Object as usize, shadow: 0x5000, + n: 4, pc_cpu: 0x30_0000, pc_gpu: 0, inc: 32, sampler: false, + pages: vec![64], visible: true, memory: Memory::Uma, + }); + let lease = lease_view_slot(70, 2, 0x30_0080, 0x10_0880, 68).expect("exact slot"); + assert_eq!((source_heap.refs.load(Ordering::Relaxed), global_heap.refs.load(Ordering::Relaxed), device.refs.load(Ordering::Relaxed)), (2, 2, 2)); + assert!(lease_view_slot(70, 4, 0x30_0100, 0, 0).is_none(), "slot bound"); + assert!(lease_view_slot(70, 2, 0x30_0080, 0x10_0880, 69).is_none(), "shared index"); + + let lifetime = lock_lifetime(); + let Taken(old) = take_after_release(&mut source_heap as *mut Object as usize, Some(70), lifetime.as_ref()).unwrap(); + drop(lifetime); + HEAPS.lock().unwrap().push(Heap { generation: 71, pc_cpu: 0x40_0000, pages: vec![80], ..old }); + assert!(lease_view_slot(70, 2, 0x30_0080, 0x10_0880, 68).is_none(), "old generation at reused object address"); + let lifetime = lock_lifetime(); + assert!(take_after_release(&mut source_heap as *mut Object as usize, Some(70), lifetime.as_ref()).is_none()); + drop(lifetime); + assert!(HEAPS.lock().unwrap().iter().any(|heap| heap.generation == 71), "late cleanup must keep the replacement"); + assert_eq!((lease.pc_srv, lease.shared_srv, lease.index), (0x30_0080, 0x10_0880, 68)); + drop(lease); + assert_eq!((source_heap.refs.load(Ordering::Relaxed), global_heap.refs.load(Ordering::Relaxed), device.refs.load(Ordering::Relaxed)), (1, 1, 1)); + HEAPS.lock().unwrap().clear(); + HEAPS.lock().unwrap().push(Heap { + generation: 72, obj: &mut source_heap as *mut Object as usize, shadow: 0x6000, + n: 1, pc_cpu: 0x50_0000, pc_gpu: 0, inc: 32, sampler: false, + pages: Vec::new(), visible: true, memory: Memory::Uma, + }); + RELEASE_REENTERED.store(false, Ordering::Relaxed); + assert!(lease_view_slot(72, 0, 0x50_0000, 0, 0).is_none(), "GetDevice fails"); + assert!(RELEASE_REENTERED.load(Ordering::Relaxed), "failed lease must drop its owner outside the lifetime gate"); + HEAPS.lock().unwrap().clear(); + *GLOBAL.lock().unwrap() = None; + } + } + #[test] fn a_freed_range_at_the_cursor_continues_into_the_never_used_tail() { // the false-full shape: four freed slots end at the cursor and four more were never diff --git a/crates/xcl/src/hle/d3d12x/real.rs b/crates/xcl/src/hle/d3d12x/real.rs index 1311f40ab0..0964fcc060 100644 --- a/crates/xcl/src/hle/d3d12x/real.rs +++ b/crates/xcl/src/hle/d3d12x/real.rs @@ -2229,7 +2229,20 @@ unsafe fn release_taking_first(this: *mut c_void, mut take: impl FnMut() -> T /// descriptor heap Release (every caller): a console heap's slots go with it unsafe extern "system" fn heap_release(this: *mut c_void) -> u32 { - release_taking_first(this, || super::console_heap::take(this as usize), |h| h.into_iter().for_each(super::console_heap::free)) + let lifetime = super::console_heap::lock_lifetime(); + if lifetime.is_none() { + return release_taking_first(this, || super::console_heap::take(this as usize), |h| h.into_iter().for_each(super::console_heap::free)); + } + let generation = lifetime.as_ref().and_then(|_| super::console_heap::generation(this as usize)); + let mut taken = None; + let n = release_taking_first( + this, + || super::console_heap::take_after_release(this as usize, generation, lifetime.as_ref()), + |h| taken = h, + ); + drop(lifetime); + taken.into_iter().for_each(super::console_heap::free); + n } /// Default UAV dimension for a texture resource when `CreateUnorderedAccessView` receives a NULL desc. diff --git a/crates/xcl/src/hle/d3d12x/vram.rs b/crates/xcl/src/hle/d3d12x/vram.rs index 6c6c2066ff..d30c06f1dd 100644 --- a/crates/xcl/src/hle/d3d12x/vram.rs +++ b/crates/xcl/src/hle/d3d12x/vram.rs @@ -436,6 +436,20 @@ pub(super) fn forget_srv_heap(generation: u64) { } } +/// Remove candidates for a descriptor heap that reached final native `Release`, while the heap +/// lifetime gate still excludes snapshots. Do not take `TRACKED_SRV_WRITES`: a descriptor hook +/// may hold it across a driver call which reenters a temporary heap Release. +pub(super) fn forget_srv_heap_after_release( + generation: u64, lifetime: Option<&super::console_heap::HeapLifetimeGuard>, +) { + debug_assert!(!srv_tracking_enabled() || lifetime.is_some()); + if lifetime.is_some() { + if let Some(tracked) = TRACKED_SRVS.lock().unwrap().as_mut() { + tracked.forget_heap(generation); + } + } +} + fn forget_tracked_srvs_locked(resource: usize) { if let Some(tracked) = TRACKED_SRVS.lock().unwrap().as_mut() { tracked.forget_resource(resource); @@ -456,11 +470,15 @@ fn forget_tracked_srvs(resource: usize) { /// mutator must take that gate and revalidate the logical generation, as well as lease the exact /// heap generation, before using `pc_srv` or `shared_srv`. #[allow(dead_code)] // consumed by the later descriptor-rewrite stage -fn lease_tracked_srv_resource(slot: SrvSlot, generation: u64) -> Option<(TrackedSrv, ID3D12Resource)> { - lease_tracked_srv_resource_with(slot, generation, |resource| { +fn lease_tracked_srv_resource(slot: SrvSlot, generation: u64) -> Option<(TrackedSrv, ID3D12Resource, super::console_heap::ViewSlotLease)> { + let (entry, resource) = lease_tracked_srv_resource_with(slot, generation, |resource| { let raw = resource as *mut std::ffi::c_void; unsafe { ID3D12Resource::from_raw_borrowed(&raw).cloned() } - }) + })?; + let heap = super::console_heap::lease_view_slot( + slot.heap_generation, slot.slot, entry.pc_srv, entry.shared_srv, entry.index, + )?; + Some((entry, resource, heap)) } fn lease_tracked_srv_resource_with( @@ -2709,6 +2727,47 @@ mod tests { SRV_TRACKING_FOR_TEST.store(false, std::sync::atomic::Ordering::Relaxed); } + #[test] + fn descriptor_heap_final_cleanup_does_not_invert_reentrant_write_order() { + let _guard = VRAM_TEST_LOCK.lock().unwrap_or_else(|e| e.into_inner()); + SRV_TRACKING_FOR_TEST.store(true, std::sync::atomic::Ordering::Relaxed); + let slot = srv_slot(55, 0); + let mut tracked = TrackedSrvs::default(); + tracked.write(slot, Some(tracked_entry(0x5510, 0x90))); + *TRACKED_SRVS.lock().unwrap() = Some(tracked); + + let (held_tx, held_rx) = std::sync::mpsc::channel(); + let (clean_tx, clean_rx) = std::sync::mpsc::channel(); + let (cleaned_tx, cleaned_rx) = std::sync::mpsc::channel(); + let final_release = std::thread::spawn(move || { + let lifetime = super::super::console_heap::lock_lifetime(); + held_tx.send(()).unwrap(); + clean_rx.recv().unwrap(); + forget_srv_heap_after_release(55, lifetime.as_ref()); + cleaned_tx.send(()).unwrap(); + drop(lifetime); + }); + held_rx.recv().unwrap(); + let (write_tx, write_rx) = std::sync::mpsc::channel(); + let (drop_tx, drop_rx) = std::sync::mpsc::channel(); + let hook = std::thread::spawn(move || { + let _write = lock_tracked_srv_write(); + write_tx.send(()).unwrap(); + let lifetime = super::super::console_heap::lock_lifetime(); + drop(lifetime); + drop_rx.recv().unwrap(); + }); + write_rx.recv().unwrap(); + clean_tx.send(()).unwrap(); + cleaned_rx.recv_timeout(std::time::Duration::from_secs(1)).expect("final cleanup must not wait for WRITE"); + final_release.join().unwrap(); + drop_tx.send(()).unwrap(); + hook.join().unwrap(); + assert!(TRACKED_SRVS.lock().unwrap().as_ref().is_some_and(|tracked| tracked.by_slot.is_empty())); + TRACKED_SRVS.lock().unwrap().take(); + SRV_TRACKING_FOR_TEST.store(false, std::sync::atomic::Ordering::Relaxed); + } + #[test] fn final_release_cleanup_does_not_wait_for_unrelated_descriptor_write() { let _guard = VRAM_TEST_LOCK.lock().unwrap_or_else(|e| e.into_inner());