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)
This commit is contained in:
Manuel Pérez authored and local committed 2026-11-25 01:02:56 -06:00
1 parent 925736dbd0
commit 9e1014d498
3 files changed
+84 -16

No files matched your search

+19 -15
View File
@@ -128,23 +128,22 @@ const SHADER_VISIBLE: u32 = 1;
#[cfg(windows)]
static SHADER_HEAPS: std::sync::Mutex<Vec<(u64, u64, usize)>> = 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) {