From 7bdc97f9d2ba7fa6cc791e73e4b246d45bb00626 Mon Sep 17 00:00:00 2001 From: Claude Date: Mon, 23 Nov 2026 22:47:58 +0000 Subject: [PATCH] d3d12x null device: heap start test calls #9 / #10 with their real signature The test for descriptor heap #9 / #10 called them through a two-argument fn pointer (`this` plus a stray pointer) although they take only `this`: a Rust call through a fn pointer of another signature is undefined behaviour, even where Win64 lets it pass. Its checks were also weak: a start that is not 0 and not the stray pointer, so #9 / #10 returning the constant 1 still passed. The test now creates two heaps and reads each heap's start from the object (CreateDescriptorHeap's handle range: not 0, a different one per heap), then calls #9 and #10 as `fn(this) -> u64` and wants exactly that start. On x86_64 Windows it also calls them through a small asm shim that passes `this` in rcx and leaves a writable sentinel pointer in rdx, as console code can: the sentinel must be unchanged and rax the exact start. Checked under Wine: #9 / #10 returning 1 fails the test, and so does the old PC form that wrote the handle through rdx. Test only, the null device is unchanged. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_019twRmZMiGWQ8zSxFiGaz6b --- crates/xcl/src/hle/d3d12x/null.rs | 48 ++++++++++++++++++++++++------- 1 file changed, 38 insertions(+), 10 deletions(-) diff --git a/crates/xcl/src/hle/d3d12x/null.rs b/crates/xcl/src/hle/d3d12x/null.rs index 818a22a..c846a7f 100644 --- a/crates/xcl/src/hle/d3d12x/null.rs +++ b/crates/xcl/src/hle/d3d12x/null.rs @@ -803,17 +803,45 @@ mod tests { assert_eq!(D3D12CreateDevice(std::ptr::null_mut(), 0xC100, std::ptr::null(), &mut dev), S_OK); let desc = [0u32, 16, 1, 0]; // CBV_SRV_UAV, 16 slots, shader visible let create_heap: Fn4 = std::mem::transmute(slot(dev, 14)); - let mut h = std::ptr::null_mut(); - assert_eq!(create_heap(dev, desc.as_ptr() as *const c_void, std::ptr::null(), &mut h), S_OK); - for i in [9, 10] { - // a caller that passes only `this`, with a stray pointer left in rdx - let start: unsafe extern "system" fn(*mut c_void, *mut u64) -> u64 = std::mem::transmute(slot(h, i)); - let mut stray = 0x5A5A_5A5Au64; - let p = start(h, &mut stray); - assert_eq!(stray, 0x5A5A_5A5A, "slot {i} wrote through rdx"); - assert_ne!(p, 0, "slot {i}"); - assert_ne!(p, &mut stray as *mut u64 as u64, "slot {i} returned rdx"); + let mut heaps = [std::ptr::null_mut(); 2]; + for h in &mut heaps { + assert_eq!(create_heap(dev, desc.as_ptr() as *const c_void, std::ptr::null(), h), S_OK); + } + // the start is the heap's own handle range (CreateDescriptorHeap): not 0, one per heap + let base = heaps.map(|h| (*(h as *mut NullObj)).value.load(Ordering::Relaxed)); + assert_ne!(base[0], 0); + assert_ne!(base[0], base[1]); + for (h, base) in heaps.into_iter().zip(base) { + for i in [9, 10] { + #[cfg(all(target_arch = "x86_64", windows))] + { + let mut sentinel = 0x5A5A_5A5A_5A5A_5A5Au64; + let p = call_with_rdx(slot(h, i), h, &mut sentinel); + assert_eq!(sentinel, 0x5A5A_5A5A_5A5A_5A5A, "slot {i} wrote through rdx"); + assert_eq!(p, base, "slot {i} with rdx set"); + } + let start: unsafe extern "system" fn(*mut c_void) -> u64 = std::mem::transmute(slot(h, i)); + assert_eq!(start(h), base, "slot {i}"); + } } } } + + /// Calls `f(this)` (Win64) the way console code does, with a writable pointer left in rdx. + #[cfg(all(target_arch = "x86_64", windows))] + unsafe fn call_with_rdx(f: usize, this: *mut c_void, rdx: *mut u64) -> u64 { + let ret: u64; + // rsp is 16-aligned on entry to the asm block: 32 B of shadow space keep it so at the call + core::arch::asm!( + "sub rsp, 32", + "call {f}", + "add rsp, 32", + f = in(reg) f, + in("rcx") this, + in("rdx") rdx, + lateout("rax") ret, + clobber_abi("system"), + ); + ret + } }