From 99ed48337c0d2810a334fb3e0c4d3eebf72540ac Mon Sep 17 00:00:00 2001 From: lixonik <71736299+lixonik@users.noreply.github.com> Date: Tue, 1 Dec 2026 12:35:09 +0300 Subject: [PATCH 1/4] d3d12-gpu: fix parallel unit test races in watch, descriptor, and pool --- d3d12/crates/leonida-gpu/src/descriptor.rs | 9 +++++++ d3d12/crates/leonida-gpu/src/pool.rs | 31 +++++++++++++++++++++- d3d12/crates/leonida-gpu/src/watch.rs | 10 ++++--- 3 files changed, 45 insertions(+), 5 deletions(-) diff --git a/d3d12/crates/leonida-gpu/src/descriptor.rs b/d3d12/crates/leonida-gpu/src/descriptor.rs index 1078802037..bed2cd9453 100644 --- a/d3d12/crates/leonida-gpu/src/descriptor.rs +++ b/d3d12/crates/leonida-gpu/src/descriptor.rs @@ -376,12 +376,19 @@ static SAMPLERS_AGAIN: std::sync::atomic::AtomicU64 = std::sync::atomic::AtomicU /// few hundred views made by calls; without it those host slots stayed /// null, or went on showing what they showed before. /// `LEONIDA_HEAP_SYNC=0`: not done, as before. [`SYNCED`] counts. +/// Serializes [`sync_heaps`] against tests that inspect a slot before +/// the next sync without racing the background sync thread. +#[cfg(test)] +static TEST_SYNC_LOCK: std::sync::Mutex<()> = std::sync::Mutex::new(()); + #[cfg(windows)] pub(crate) fn sync_heaps() { use std::sync::atomic::Ordering::Relaxed; if !sync_on() { return; } + #[cfg(test)] + let _test_sync = TEST_SYNC_LOCK.lock().unwrap_or_else(|e| e.into_inner()); let started = std::time::Instant::now(); let (mut pages, mut looked, mut mirrored, mut tabled, mut later) = (0u64, 0u64, 0u64, 0u64, 0u64); let (mut views, mut bytes) = (0u64, 0u64); @@ -2247,6 +2254,7 @@ mod tests { let source = elsewhere.as_mut_ptr() as u64; let sampler = [21, 3, 3, 3, 0, 1, 0, 0, 0, 0, 0, 0x7f7f_ffff, 0]; create_sampler(std::ptr::null(), &sampler, source); + let hold = TEST_SYNC_LOCK.lock().unwrap_or_else(|e| e.into_inner()); // SAFETY: the heap slot and source both hold 16 descriptor bytes. unsafe { std::ptr::copy_nonoverlapping( @@ -2256,6 +2264,7 @@ mod tests { ) }; assert!(!host.made(5)); + drop(hold); sync_heaps(); assert!(host.made(5)); } diff --git a/d3d12/crates/leonida-gpu/src/pool.rs b/d3d12/crates/leonida-gpu/src/pool.rs index c035377049..75e0a5e365 100644 --- a/d3d12/crates/leonida-gpu/src/pool.rs +++ b/d3d12/crates/leonida-gpu/src/pool.rs @@ -672,6 +672,7 @@ struct Reclaim { methods: &'static [*const (); 3], references: AtomicU32, piece: Piece, + raw: usize, } struct Methods([*const (); 3]); @@ -710,6 +711,7 @@ unsafe extern "system" fn release(this: *mut c_void) -> u32 { let gone = Box::from_raw(this.cast::()); HELD.fetch_sub(gone.piece.len, Ordering::Relaxed); lock(&TEXTURES).give(gone.piece, now()); + forget_released(gone.raw); } left } @@ -718,6 +720,13 @@ unsafe extern "system" fn release(this: *mut c_void) -> u32 { /// Textures made in the last second ([`fresh`]), and when. static RECENT: Mutex> = Mutex::new(VecDeque::new()); +/// Removes a released pooled texture pointer from `RECENT` and `UNSEEN` +/// so a new resource allocated at the same address is not mistaken for it. +fn forget_released(raw: usize) { + RECENT.lock().unwrap_or_else(|e| e.into_inner()).retain(|r| r.0 != raw); + UNSEEN.lock().unwrap_or_else(|e| e.into_inner()).remove(&raw); +} + /// Whether `texture` was made in a block in the last second. pub(crate) fn fresh(texture: &ID3D12Resource) -> bool { let raw = texture.as_raw() as usize; @@ -788,7 +797,12 @@ pub(crate) fn begun_before(texture: &ID3D12Resource) -> Option { /// Makes `texture` give `piece` back when the last reference to it goes. fn give_back_with(texture: &ID3D12Resource, piece: Piece) { const KEY: GUID = GUID::from_u128(0x6f0e2f0c_5a59_4b1e_9a3f_7f4d2c9be713); - let made = Box::into_raw(Box::new(Reclaim { methods: &RECLAIM.0, references: AtomicU32::new(1), piece })); + let made = Box::into_raw(Box::new(Reclaim { + methods: &RECLAIM.0, + references: AtomicU32::new(1), + piece, + raw: texture.as_raw() as usize, + })); // SAFETY: a COM object of ours with one reference, which this takes and // drops at the end; the texture takes its own. unsafe { @@ -1269,4 +1283,19 @@ mod tests { let (piece, memory, _) = take_unlocked(&POOL, &GROWN, &BLOCKS, (65536, 65536, Ripe::ALL), || Some(3)).unwrap(); assert_eq!((piece.block, memory), (0, 3)); } + + /// Releasing a pooled texture clears its raw pointer from `RECENT` and + /// `UNSEEN` so a new resource at the same address is not treated as fresh. + #[test] + fn released_texture_pointer_is_not_kept_in_recent_or_unseen() { + let raw = 0xDEAD_BEEF_0000usize; + RECENT.lock().unwrap_or_else(|e| e.into_inner()).push_back((raw, now())); + { + let mut unseen = UNSEEN.lock().unwrap_or_else(|e| e.into_inner()); + note_unseen(&mut unseen, raw, 10, 5); + } + forget_released(raw); + assert!(!RECENT.lock().unwrap_or_else(|e| e.into_inner()).iter().any(|r| r.0 == raw)); + assert!(unseen_begun(&UNSEEN.lock().unwrap_or_else(|e| e.into_inner()), raw, 5).is_none()); + } } diff --git a/d3d12/crates/leonida-gpu/src/watch.rs b/d3d12/crates/leonida-gpu/src/watch.rs index 7471983672..5a45fcb9d0 100644 --- a/d3d12/crates/leonida-gpu/src/watch.rs +++ b/d3d12/crates/leonida-gpu/src/watch.rs @@ -5187,10 +5187,12 @@ mod tests { assert!(!g.protected.contains(at / BLOCK) && !g.clean.contains(at / BLOCK)); } assert!(!published(at, at + BLOCK)); - // SAFETY: our view. - unsafe { UnmapViewOfFile(view) }; + // End the remapping window before returning `at` to Windows so another + // thread cannot reuse the address while `g.changing` still leaves its block alone. drop(changing); assert!(!guards().left_alone(at / BLOCK)); + // SAFETY: our view. + unsafe { UnmapViewOfFile(view) }; } /// Memory at an address where blocks were guarded or absent is not @@ -5626,7 +5628,7 @@ mod tests { use windows_sys::Win32::Foundation::CloseHandle; use windows_sys::Win32::System::Memory::{MEMORY_MAPPED_VIEW_ADDRESS, UnmapViewOfFile}; for &view in views { - title_memory_remapped(view, bytes); + title_memory_unmapping(view, bytes); // SAFETY: a view of the section mapped by `two_views`. unsafe { UnmapViewOfFile(MEMORY_MAPPED_VIEW_ADDRESS { @@ -6442,8 +6444,8 @@ mod many_askers_tests { { let _changing = super::title_memory_remapping(); super::title_memory_unmapping(one, PAGE); - unsafe { UnmapViewOfFile(MEMORY_MAPPED_VIEW_ADDRESS { Value: one as *mut _ }) }; } + unsafe { UnmapViewOfFile(MEMORY_MAPPED_VIEW_ADDRESS { Value: one as *mut _ }) }; last = [last[asked]; VIEWS]; } }); From eab9e2abf4d9e7b030120fabd14ef3a82219d075 Mon Sep 17 00:00:00 2001 From: lixonik <71736299+lixonik@users.noreply.github.com> Date: Tue, 1 Dec 2026 14:16:10 +0300 Subject: [PATCH 2/4] d3d12-gpu: place TEST_SYNC_LOCK above sync_heaps doc comment --- d3d12/crates/leonida-gpu/src/descriptor.rs | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/d3d12/crates/leonida-gpu/src/descriptor.rs b/d3d12/crates/leonida-gpu/src/descriptor.rs index bed2cd9453..8ff8f4472b 100644 --- a/d3d12/crates/leonida-gpu/src/descriptor.rs +++ b/d3d12/crates/leonida-gpu/src/descriptor.rs @@ -367,6 +367,11 @@ impl Drop for SyncedHeaps { #[cfg(windows)] static SAMPLERS_AGAIN: std::sync::atomic::AtomicU64 = std::sync::atomic::AtomicU64::new(0); +/// Serializes [`sync_heaps`] against tests that inspect a slot before +/// the next sync without racing the background sync thread. +#[cfg(test)] +static TEST_SYNC_LOCK: std::sync::Mutex<()> = std::sync::Mutex::new(()); + /// Mirrors records copied directly into placed shader-visible heaps. Calls /// to `Create*View` are already mirrored by [`mirror`]; raw CPU copies leave /// only write-watch pages to reveal which slots changed. @@ -376,11 +381,6 @@ static SAMPLERS_AGAIN: std::sync::atomic::AtomicU64 = std::sync::atomic::AtomicU /// few hundred views made by calls; without it those host slots stayed /// null, or went on showing what they showed before. /// `LEONIDA_HEAP_SYNC=0`: not done, as before. [`SYNCED`] counts. -/// Serializes [`sync_heaps`] against tests that inspect a slot before -/// the next sync without racing the background sync thread. -#[cfg(test)] -static TEST_SYNC_LOCK: std::sync::Mutex<()> = std::sync::Mutex::new(()); - #[cfg(windows)] pub(crate) fn sync_heaps() { use std::sync::atomic::Ordering::Relaxed; From bce4a898ba04ef7ce450a72936fedcde32d65540 Mon Sep 17 00:00:00 2001 From: lixonik <71736299+lixonik@users.noreply.github.com> Date: Tue, 1 Dec 2026 18:01:58 +0300 Subject: [PATCH 3/4] d3d12-gpu: test pooled texture last-reference drop clears RECENT/UNSEEN --- d3d12/crates/leonida-gpu/src/pool.rs | 47 ++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) diff --git a/d3d12/crates/leonida-gpu/src/pool.rs b/d3d12/crates/leonida-gpu/src/pool.rs index 75e0a5e365..c9e1fd0409 100644 --- a/d3d12/crates/leonida-gpu/src/pool.rs +++ b/d3d12/crates/leonida-gpu/src/pool.rs @@ -1298,4 +1298,51 @@ mod tests { assert!(!RECENT.lock().unwrap_or_else(|e| e.into_inner()).iter().any(|r| r.0 == raw)); assert!(unseen_begun(&UNSEEN.lock().unwrap_or_else(|e| e.into_inner()), raw, 5).is_none()); } + + /// Dropping the last COM reference to a pooled texture runs `Reclaim::release` + /// and removes its raw pointer from `RECENT` and `UNSEEN`. + #[test] + fn dropping_last_pooled_texture_reference_forgets_recent_and_unseen() { + struct ForcePool(bool); + impl Drop for ForcePool { + fn drop(&mut self) { + TEST_POOL_FORCE.set(self.0); + } + } + + let Some(host) = crate::host::host() else { return }; + let _force = ForcePool(TEST_POOL_FORCE.replace(true)); + let desc = D3D12_RESOURCE_DESC { + Dimension: D3D12_RESOURCE_DIMENSION_TEXTURE2D, + Width: 32, + Height: 32, + DepthOrArraySize: 1, + MipLevels: 1, + Format: windows::Win32::Graphics::Dxgi::Common::DXGI_FORMAT(28), + SampleDesc: windows::Win32::Graphics::Dxgi::Common::DXGI_SAMPLE_DESC { Count: 1, Quality: 0 }, + ..Default::default() + }; + // SAFETY: a valid 32x32 RGBA8_UNORM texture description. + let tex = unsafe { texture(&host.device, &desc, D3D12_RESOURCE_STATE_COMMON) } + .expect("pooled texture allocation"); + let raw = tex.as_raw() as usize; + // Keep our UNSEEN entry pinned above any finished submission count so + // concurrent `made_now` calls cannot evict it before `Reclaim::release`. + UNSEEN.lock().unwrap_or_else(|e| e.into_inner()).insert(raw, u64::MAX); + let second_ref = tex.clone(); + + assert!(fresh(&tex)); + assert!(UNSEEN.lock().unwrap_or_else(|e| e.into_inner()).contains_key(&raw)); + + // Dropping the first of two COM references must not trigger `Reclaim::release`. + drop(tex); + assert!(fresh(&second_ref)); + assert!(UNSEEN.lock().unwrap_or_else(|e| e.into_inner()).contains_key(&raw)); + + // Dropping the final COM reference destroys the resource and calls + // `Reclaim::release` -> `forget_released(gone.raw)`. + drop(second_ref); + assert!(!RECENT.lock().unwrap_or_else(|e| e.into_inner()).iter().any(|r| r.0 == raw)); + assert!(!UNSEEN.lock().unwrap_or_else(|e| e.into_inner()).contains_key(&raw)); + } } From 81a6e94a9966ff0270dc3c5fd8e65dddc3cdbfee Mon Sep 17 00:00:00 2001 From: lixonik <71736299+lixonik@users.noreply.github.com> Date: Tue, 1 Dec 2026 23:54:41 +0300 Subject: [PATCH 4/4] d3d12-gpu: forget released pointer before returning pool piece and make unit test hermetic --- d3d12/crates/leonida-gpu/src/pool.rs | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/d3d12/crates/leonida-gpu/src/pool.rs b/d3d12/crates/leonida-gpu/src/pool.rs index c9e1fd0409..e2b9559623 100644 --- a/d3d12/crates/leonida-gpu/src/pool.rs +++ b/d3d12/crates/leonida-gpu/src/pool.rs @@ -709,9 +709,9 @@ unsafe extern "system" fn release(this: *mut c_void) -> u32 { let left = (*this.cast::()).references.fetch_sub(1, Ordering::AcqRel) - 1; if left == 0 { let gone = Box::from_raw(this.cast::()); + forget_released(gone.raw); HELD.fetch_sub(gone.piece.len, Ordering::Relaxed); lock(&TEXTURES).give(gone.piece, now()); - forget_released(gone.raw); } left } @@ -1284,19 +1284,19 @@ mod tests { assert_eq!((piece.block, memory), (0, 3)); } - /// Releasing a pooled texture clears its raw pointer from `RECENT` and - /// `UNSEEN` so a new resource at the same address is not treated as fresh. + /// `forget_released` removes a raw texture pointer from both `RECENT` and + /// `UNSEEN` (the COM last-reference release path is covered by + /// `dropping_last_pooled_texture_reference_forgets_recent_and_unseen`). #[test] fn released_texture_pointer_is_not_kept_in_recent_or_unseen() { let raw = 0xDEAD_BEEF_0000usize; RECENT.lock().unwrap_or_else(|e| e.into_inner()).push_back((raw, now())); - { - let mut unseen = UNSEEN.lock().unwrap_or_else(|e| e.into_inner()); - note_unseen(&mut unseen, raw, 10, 5); - } + // Insert directly instead of calling `note_unseen(..., finished)` so we do + // not evict other parallel tests' entries from process-global `UNSEEN`. + UNSEEN.lock().unwrap_or_else(|e| e.into_inner()).insert(raw, u64::MAX); forget_released(raw); assert!(!RECENT.lock().unwrap_or_else(|e| e.into_inner()).iter().any(|r| r.0 == raw)); - assert!(unseen_begun(&UNSEEN.lock().unwrap_or_else(|e| e.into_inner()), raw, 5).is_none()); + assert!(!UNSEEN.lock().unwrap_or_else(|e| e.into_inner()).contains_key(&raw)); } /// Dropping the last COM reference to a pooled texture runs `Reclaim::release`