From 51b9d09b34fde93367fa3ba51e9da569786bd969 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Manuel=20P=C3=A9rez?= Date: Tue, 24 Nov 2026 00:50:24 +0100 Subject: [PATCH] GPU: hold SHADER_HEAPS across descriptor mirror and check query offsets mirror() previously dropped the SHADER_HEAPS lock inside shader_slot() before using the returned &Heap, allowing a concurrent Heap::drop to free the heap while mirror() accessed it. Also guards copy_simple() against invalid descriptor kinds, rejects placed heaps whose address+size wraps u64, and checks offset+count*bytes in resolve_queries() against overflow (with a regression test). (cherry picked from commit 628b95da5e738046d00995743e07137e48936ba6) --- crates/victory-gpu/src/descriptor.rs | 34 ++++++++++++++----------- crates/victory-gpu/src/list.rs | 29 +++++++++++++++++++++- crates/victory-gpu/src/resource.rs | 37 ++++++++++++++++++++++++++++ 3 files changed, 84 insertions(+), 16 deletions(-) diff --git a/crates/victory-gpu/src/descriptor.rs b/crates/victory-gpu/src/descriptor.rs index 3485f89..5f6ab90 100644 --- a/crates/victory-gpu/src/descriptor.rs +++ b/crates/victory-gpu/src/descriptor.rs @@ -128,23 +128,22 @@ const SHADER_VISIBLE: u32 = 1; #[cfg(windows)] static SHADER_HEAPS: std::sync::Mutex> = std::sync::Mutex::new(Vec::new()); -/// The shader-visible heap holding `handle`, and the descriptor's index. -#[cfg(windows)] -fn shader_slot<'a>(handle: u64) -> Option<(&'a Heap, u32)> { - let heaps = SHADER_HEAPS.lock().unwrap_or_else(|e| e.into_inner()); - let &(_, _, at) = heaps - .iter() - .find(|&&(start, end, _)| (start..end).contains(&handle))?; - // SAFETY: registered heaps are live (removed when dropped). - let heap = unsafe { &*(at as *const Heap) }; - Some((heap, heap.index(handle)?)) -} - /// Makes the host descriptor for what the title wrote at `handle`, if it -/// is in a shader-visible heap. +/// is in a shader-visible heap. Keeps `SHADER_HEAPS` locked for the duration +/// so `Heap::drop` cannot free the heap concurrently. #[cfg(windows)] fn mirror(handle: u64) { - let Some((heap, index)) = shader_slot(handle) else { + let heaps = SHADER_HEAPS.lock().unwrap_or_else(|e| e.into_inner()); + let Some(&(_, _, at)) = heaps + .iter() + .find(|&&(start, end, _)| (start..end).contains(&handle)) + else { + return; + }; + // SAFETY: registered heaps stay alive until `Heap::drop`, which takes + // the `SHADER_HEAPS` lock we hold here. + let heap = unsafe { &*(at as *const Heap) }; + let Some(index) = heap.index(handle) else { return; }; let mut pending = heap.pending.lock().unwrap_or_else(|e| e.into_inner()); @@ -977,7 +976,9 @@ pub(crate) extern "system" fn create_sampler_x(_: This, desc: *const [u32; 27], /// Device method: `count` descriptors of `kind` from `src` to `dst`. pub(crate) extern "system" fn copy_simple(_: This, count: u32, dst: u64, src: u64, kind: u32) { - let step = u64::from(increment(kind).unwrap_or(0)); + let Some(step) = increment(kind).map(u64::from) else { + return; + }; let len = u64::from(count) * step; if dst != 0 && src != 0 { // SAFETY: both ranges are descriptor slots the title owns. @@ -1084,6 +1085,9 @@ pub(crate) extern "system" fn create_heap( let placed = placement.address != 0 && placement.size == size; tracing::debug!(?desc, placed, "descriptor heap"); let (start, memory) = if placed { + if placement.address.checked_add(size).is_none() { + return E_INVALIDARG; + } (placement.address, None) } else { match Memory::zeroed(size, 64) { diff --git a/crates/victory-gpu/src/list.rs b/crates/victory-gpu/src/list.rs index 1eae1dd..5a9680e 100644 --- a/crates/victory-gpu/src/list.rs +++ b/crates/victory-gpu/src/list.rs @@ -1372,7 +1372,10 @@ extern "system" fn resolve_queries( PIPELINE_STATISTICS => (Written::Value(0), 88), _ => (Written::Value(0), 16), }; - if offset + u64::from(count) * bytes > size { + let Some(end) = offset.checked_add(u64::from(count) * bytes) else { + return; + }; + if end > size { return; } for i in 0..u64::from(count) { @@ -2257,4 +2260,28 @@ mod tests { assert_eq!(release(list), 0); assert_eq!(release(allocator), 0); } + + /// An offset near `u64::MAX` must not overflow and write outside the + /// query destination buffer. + #[test] + fn resolve_queries_rejects_overflowing_offset() { + let null = std::ptr::null(); + let mut allocator = null; + assert_eq!( + create_allocator(null, 0, &ALLOCATOR_IIDS[0], &mut allocator), + S_OK + ); + let mut list = null; + assert_eq!( + create_list(null, 0, 0, allocator, null, &LIST_IIDS[1], &mut list), + S_OK + ); + let buffer = crate::resource::test_buffer(64); + // Used to overflow `offset + count * 8` (panic in debug, wrap in release). + resolve_queries(list, null, 0, 0, 2, buffer, u64::MAX - 8); + let release = |o: This| slot:: u32>(o, 0x10)(o); + for object in [buffer, list, allocator] { + assert_eq!(release(object), 0); + } + } } diff --git a/crates/victory-gpu/src/resource.rs b/crates/victory-gpu/src/resource.rs index 35b7c70..db3621e 100644 --- a/crates/victory-gpu/src/resource.rs +++ b/crates/victory-gpu/src/resource.rs @@ -2856,6 +2856,43 @@ static RESOURCE_FUNCTIONS: Table<32> = { Table(f) }; +#[cfg(test)] +pub(crate) fn test_buffer(width: u64) -> This { + let heap = HeapProperties { + kind: 1, + cpu_pages: 0, + pool: 0, + creation_nodes: 0, + visible_nodes: 0, + }; + let desc = ResourceDesc { + dimension: BUFFER, + alignment: 0, + width, + height: 1, + depth_or_array_size: 1, + mip_levels: 1, + format: 0, + sample_count: 1, + sample_quality: 0, + layout: 1, + flags: 0, + }; + let mut out = std::ptr::null(); + let hr = create_committed( + std::ptr::null(), + &heap, + 0, + &desc, + 0, + std::ptr::null(), + &RESOURCE_IIDS[0], + &mut out, + ); + assert_eq!(hr, S_OK); + out +} + #[cfg(test)] mod tests { use super::*;