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:
1 parent
f1650b7e45
commit
51b9d09b34
3 files changed
+84
-16
No files matched your search
@@ -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) {
|
||||
|
||||
@@ -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::<extern "system" fn(This) -> u32>(o, 0x10)(o);
|
||||
for object in [buffer, list, allocator] {
|
||||
assert_eq!(release(object), 0);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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::*;
|
||||
|
||||
Reference in new issue
Block a user