GPU: add synthetic unit tests for query.rs and signature.rs

(cherry picked from commit 0be58e62d24181a9eb60244a7e4d878a3627d401)
This commit is contained in:
给伊莉雅补魔 authored and local committed 2026-11-25 01:03:00 -06:00
1 parent ed07828ce2
commit 3af6d8fb0e
2 files changed
+289

No files matched your search

+87
View File
@@ -70,3 +70,90 @@ static FUNCTIONS: Table<24> = {
f[7] = object::parent_device as *const (); // 0x38
Table(f)
};
#[cfg(test)]
mod tests {
use super::*;
use crate::object::{E_NOINTERFACE, E_POINTER, S_FALSE, S_OK, slot};
#[test]
fn null_descriptions_are_rejected() {
let mut heap = std::ptr::null();
assert_eq!(
create(std::ptr::null(), std::ptr::null(), &HEAP_IIDS[0], &mut heap),
E_INVALIDARG
);
assert!(heap.is_null());
// The description is checked first: a probe with no room for the
// heap fails on it rather than answering S_FALSE.
assert_eq!(
create(
std::ptr::null(),
std::ptr::null(),
&HEAP_IIDS[0],
std::ptr::null_mut()
),
E_INVALIDARG
);
}
#[test]
fn creation_without_an_out_pointer_only_probes() {
let desc = Desc {
kind: 1, // timestamps
count: 64,
nodes: 0,
};
// A known interface with no out pointer asks whether creation would
// succeed; the heap is dropped instead of handed out.
assert_eq!(
create(std::ptr::null(), &desc, &HEAP_IIDS[0], std::ptr::null_mut()),
S_FALSE
);
// A null interface id is tolerated: no interface, no heap.
let mut heap = std::ptr::null();
assert_eq!(
create(std::ptr::null(), &desc, std::ptr::null(), &mut heap),
E_NOINTERFACE
);
assert!(heap.is_null());
}
#[test]
fn heaps_answer_their_interfaces_and_count_references() {
let desc = Desc {
kind: 0, // occlusion
count: 8,
nodes: 0,
};
let null = std::ptr::null();
let mut heap = null;
assert_eq!(create(null, &desc, &HEAP_IIDS[0], &mut heap), S_OK);
assert!(!heap.is_null());
let query = slot::<extern "system" fn(This, *const Iid, *mut This) -> HRESULT>(heap, 0x0);
// Every interface the heap answers to hands out the same object.
for iid in &HEAP_IIDS {
let mut out = null;
assert_eq!(query(heap, iid, &mut out), S_OK);
assert_eq!(out as usize, heap as usize);
}
// An unknown interface writes null behind and reports it missing.
let unknown = [0u8; 16];
let mut out = heap;
assert_eq!(query(heap, &unknown, &mut out), E_NOINTERFACE);
assert!(out.is_null());
// A null interface id is tolerated rather than matched.
let mut out = heap;
assert_eq!(query(heap, std::ptr::null(), &mut out), E_NOINTERFACE);
assert!(out.is_null());
// No room for the answer is a pointer error.
assert_eq!(query(heap, &unknown, std::ptr::null_mut()), E_POINTER);
// References: one from creation, four from the queries, one added;
// each release hands back what is left, the last frees the heap.
assert_eq!(slot::<extern "system" fn(This) -> u32>(heap, 0x8)(heap), 6);
let release = slot::<extern "system" fn(This) -> u32>(heap, 0x10);
for left in [5, 4, 3, 2, 1, 0] {
assert_eq!(release(heap), left);
}
}
}
+202
View File
@@ -92,3 +92,205 @@ static FUNCTIONS: Table<24> = {
f[7] = object::parent_device as *const (); // 0x38
Table(f)
};
#[cfg(test)]
mod tests {
use super::*;
use crate::object::{E_NOINTERFACE, E_POINTER, S_FALSE, S_OK, slot};
#[test]
fn null_or_argument_free_descriptions_are_rejected() {
let null = std::ptr::null();
let mut signature = null;
// No description at all.
assert_eq!(
create(
null,
std::ptr::null(),
null,
&SIGNATURE_IIDS[0],
&mut signature
),
E_INVALIDARG
);
assert!(signature.is_null());
// A count of arguments with nothing behind the pointer.
let argumentless = Desc {
stride: 16,
count: 3,
arguments: std::ptr::null(),
nodes: 0,
};
assert_eq!(
create(
null,
&argumentless,
null,
&SIGNATURE_IIDS[0],
&mut signature
),
E_INVALIDARG
);
assert!(signature.is_null());
}
#[test]
fn signatures_without_arguments_stay_empty() {
let null = std::ptr::null();
// Zero arguments and no pointer is a valid, if useless, signature.
let empty = Desc {
stride: 16,
count: 0,
arguments: std::ptr::null(),
nodes: 0,
};
let mut signature = null;
assert_eq!(
create(null, &empty, null, &SIGNATURE_IIDS[0], &mut signature),
S_OK
);
let (stride, arguments) = layout(signature).expect("one of our signatures");
assert_eq!(stride, 16);
assert!(arguments.is_empty());
assert_eq!(
slot::<extern "system" fn(This) -> u32>(signature, 0x10)(signature),
0
);
// The argument pointer is not read when the count is zero.
let none: [[u32; 4]; 0] = [];
let unread = Desc {
stride: 16,
count: 0,
arguments: none.as_ptr(),
nodes: 0,
};
let mut signature = null;
assert_eq!(
create(null, &unread, null, &SIGNATURE_IIDS[1], &mut signature),
S_OK
);
let (_, arguments) = layout(signature).expect("one of our signatures");
assert!(arguments.is_empty());
assert_eq!(
slot::<extern "system" fn(This) -> u32>(signature, 0x10)(signature),
0
);
}
#[test]
fn arguments_are_copied_out_of_title_memory() {
// A title reuses the memory its description lived in, while
// ExecuteIndirect reads the layout long after creation.
assert!(
layout(std::ptr::null()).is_none(),
"no signature, no layout"
);
let draw = [0u32, 3, 1, 0]; // three vertices, one instance
let dispatch = [2u32, 4, 1, 1]; // dispatch 4x1x1 groups
let expected = [draw, dispatch];
let mut title_memory = expected;
let desc = Desc {
stride: 20,
count: 2,
arguments: title_memory.as_ptr(),
nodes: 0,
};
let null = std::ptr::null();
let mut signature = null;
assert_eq!(
create(null, &desc, null, &SIGNATURE_IIDS[0], &mut signature),
S_OK
);
title_memory.fill([9, 9, 9, 9]);
let (stride, arguments) = layout(signature).expect("one of our signatures");
assert_eq!(stride, 20);
assert_eq!(arguments, &expected[..]);
assert_eq!(
slot::<extern "system" fn(This) -> u32>(signature, 0x10)(signature),
0
);
}
#[test]
fn signatures_answer_their_interfaces_and_count_references() {
let arguments = [[2u32, 4, 1, 1]]; // dispatch 4x1x1 groups
let desc = Desc {
stride: 12,
count: 1,
arguments: arguments.as_ptr(),
nodes: 0,
};
let null = std::ptr::null();
let mut signature = null;
assert_eq!(
create(null, &desc, null, &SIGNATURE_IIDS[0], &mut signature),
S_OK
);
assert!(!signature.is_null());
let query =
slot::<extern "system" fn(This, *const Iid, *mut This) -> HRESULT>(signature, 0x0);
// Every interface the signature answers to hands out the same object.
for iid in &SIGNATURE_IIDS {
let mut out = null;
assert_eq!(query(signature, iid, &mut out), S_OK);
assert_eq!(out as usize, signature as usize);
}
// An unknown interface writes null behind and reports it missing.
let unknown = [0u8; 16];
let mut out = signature;
assert_eq!(query(signature, &unknown, &mut out), E_NOINTERFACE);
assert!(out.is_null());
// A null interface id is tolerated rather than matched.
let mut out = signature;
assert_eq!(query(signature, std::ptr::null(), &mut out), E_NOINTERFACE);
assert!(out.is_null());
// No room for the answer is a pointer error.
assert_eq!(query(signature, &unknown, std::ptr::null_mut()), E_POINTER);
// References: one from creation, four from the queries, one added;
// each release hands back what is left, the last frees the signature.
assert_eq!(
slot::<extern "system" fn(This) -> u32>(signature, 0x8)(signature),
6
);
let release = slot::<extern "system" fn(This) -> u32>(signature, 0x10);
for left in [5, 4, 3, 2, 1, 0] {
assert_eq!(release(signature), left);
}
}
#[test]
fn creation_without_an_out_pointer_only_probes() {
let arguments = [[2u32, 4, 1, 1]]; // dispatch 4x1x1 groups
let desc = Desc {
stride: 12,
count: 1,
arguments: arguments.as_ptr(),
nodes: 0,
};
// A known interface with no out pointer asks whether creation would
// succeed; the signature is dropped instead of handed out.
assert_eq!(
create(
std::ptr::null(),
&desc,
std::ptr::null(),
&SIGNATURE_IIDS[0],
std::ptr::null_mut()
),
S_FALSE
);
// A null interface id is tolerated: no interface, no signature.
let mut signature = std::ptr::null();
assert_eq!(
create(
std::ptr::null(),
&desc,
std::ptr::null(),
std::ptr::null(),
&mut signature
),
E_NOINTERFACE
);
assert!(signature.is_null());
}
}