Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 51 additions & 2 deletions crates/xcl/src/hle/d3d12x/device_x.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1006,11 +1006,12 @@ fn allocation_of(parts: impl IntoIterator<Item = (u64, u64)>, mut placed: impl F
/// each resource's {offset, alignment, size} goes to the last argument when there is one.
pub(super) unsafe extern "system" fn get_resource_allocation_info2_x(a: *mut c_void, b: *mut c_void, mask: u32, n: u32, descs: *const [u8; 0x48], infos: *mut [u64; 3]) -> *mut [u64; 2] {
real::note_newer_device_method(108);
let ret = if real::this_first(a, b) { b } else { a } as *mut [u64; 2];
let c_plus = real::this_first(a, b);
let ret = if c_plus { b } else { a } as *mut [u64; 2];
let parts = (0..n as usize).map(|k| {
let mut r = [0u64; 2];
let d = if descs.is_null() { std::ptr::null() } else { descs.add(k) as *const [u8; 0x38] };
let (x, y) = if real::this_first(a, b) { (a, &mut r as *mut _ as *mut c_void) } else { (&mut r as *mut _ as *mut c_void, b) };
let (x, y) = if c_plus { (a, &mut r as *mut _ as *mut c_void) } else { (&mut r as *mut _ as *mut c_void, b) };
get_resource_allocation_info_x(x, y, mask, 1, d);
(r[0], r[1])
});
Expand Down Expand Up @@ -1548,4 +1549,52 @@ mod tests {
assert_eq!(pc_uav_linear_format(93), 87);
assert_eq!(pc_uav_linear_format(28), 28);
}

/// `GetCustomHeapProperties` hook (`get_custom_heap_properties_x`, slot 26) matches the host
/// driver in both C++ `(this, ret, …)` and C `(ret, this, …)` ABI argument orders even when
/// the `[u32; 5]` return buffer is only 4-byte aligned (#1130).
#[test]
fn custom_heap_properties_match_host_in_cpp_and_c_abi() {
use windows::core::Interface;
use windows::Win32::Graphics::Direct3D::D3D_FEATURE_LEVEL_12_0;
use windows::Win32::Graphics::Direct3D12::{D3D12CreateDevice, ID3D12Device, D3D12_HEAP_PROPERTIES, D3D12_HEAP_TYPE};
#[repr(align(8))]
struct Aligned([u32; 6]);
let mut dev: Option<ID3D12Device> = None;
if unsafe { D3D12CreateDevice(None, D3D_FEATURE_LEVEL_12_0, &mut dev) }.is_err() {
crate::gpu::recomp::runner::skip_test("no GPU");
return;
}
let dev = dev.unwrap();
let raw = dev.as_raw();
unsafe {
// capture host-driver results before adopting the device
let expected: Vec<(u32, [u32; 5])> = [1u32, 2, 3]
.into_iter()
.map(|ty| {
let host: D3D12_HEAP_PROPERTIES = dev.GetCustomHeapProperties(0, D3D12_HEAP_TYPE(ty as i32));
(ty, [host.Type.0 as u32, host.CPUPageProperty.0 as u32, host.MemoryPoolPreference.0 as u32, host.CreationNodeMask, host.VisibleNodeMask])
})
.collect();
if !real::is_adopted(raw) {
real::adopt(raw, real::Kind::Device);
}
for (ty, want) in expected {
// C++ ABI order: (this, 4-byte-aligned ret, node_mask, heap_type)
let mut buf = Aligned([0xDEAD_BEEFu32; 6]);
let ret_ptr = buf.0.as_mut_ptr().add(1) as *mut c_void;
assert_eq!((ret_ptr as usize) & 7, 4);
let out = get_custom_heap_properties_x(raw, ret_ptr, 0, ty);
assert_eq!(out as *mut c_void, ret_ptr);
assert_eq!(buf.0[1..6], want, "C++ ABI heap type {ty}");
// C ABI order: (4-byte-aligned ret, this, node_mask, heap_type)
let mut buf = Aligned([0xDEAD_BEEFu32; 6]);
let ret_ptr = buf.0.as_mut_ptr().add(1) as *mut c_void;
assert_eq!((ret_ptr as usize) & 7, 4);
let out = get_custom_heap_properties_x(ret_ptr, raw, 0, ty);
assert_eq!(out as *mut c_void, ret_ptr);
assert_eq!(buf.0[1..6], want, "C ABI heap type {ty}");
}
}
}
}
30 changes: 29 additions & 1 deletion crates/xcl/src/hle/d3d12x/real.rs
Original file line number Diff line number Diff line change
Expand Up @@ -647,7 +647,7 @@ fn module_of(addr: usize) -> usize {

/// Whether `obj` is an object XCL adopted (its vtable is one of XCL's).
pub(super) unsafe fn is_adopted(obj: *mut c_void) -> bool {
!obj.is_null() && crate::gpu::shader::readable(obj as usize, 8) && is_ext(*(obj as *const usize))
!obj.is_null() && (obj as usize) & (std::mem::align_of::<usize>() - 1) == 0 && crate::gpu::shader::readable(obj as usize, 8) && is_ext(*(obj as *const usize))
}

/// A struct-returning method's first two arguments: true when `a` is the object (the C++ way:
Expand Down Expand Up @@ -7421,6 +7421,34 @@ mod this_first_tests {
g.remove(&vtbl);
}
}

/// `is_adopted` and `this_first` with a 4-byte-aligned return buffer (e.g.
/// `D3D12_HEAP_PROPERTIES`, `[u32; 5]`): must not perform a misaligned `*const usize`
/// dereference and must never treat a misaligned buffer as an adopted COM object (#1130).
#[test]
fn misaligned_return_buffer_is_not_adopted_and_does_not_panic() {
#[repr(align(8))]
struct Aligned([u32; 6]);
let pc = [0usize; 32];
let ext = [pc.as_ptr() as usize, 0];
let vtbl = ext.as_ptr() as usize + 8;
EXTS.lock().unwrap().get_or_insert_with(HashSet::new).insert(vtbl);
let obj = Box::into_raw(Box::new([vtbl])) as *mut c_void;
let mut buf = Aligned([0u32; 6]);
let unaligned = unsafe { buf.0.as_mut_ptr().add(1) } as *mut c_void;
assert_eq!((unaligned as usize) & 7, 4, "buffer must be 4-byte-aligned only");
unsafe {
// even with a stale vtable copy at the 4-byte-aligned address, it cannot be a COM object
(unaligned as *mut usize).write_unaligned(vtbl);
assert!(!is_adopted(unaligned));
assert!(this_first(obj, unaligned));
assert!(!this_first(unaligned, obj));
drop(Box::from_raw(obj as *mut [usize; 1]));
}
if let Some(g) = EXTS.lock().unwrap().as_mut() {
g.remove(&vtbl);
}
}
}

#[cfg(test)]
Expand Down
Loading