From b72b472b8f18362c274b903dbf5793976c79ae1f Mon Sep 17 00:00:00 2001 From: Kalmarv <59708937+Kalmarv@users.noreply.github.com> Date: Sun, 29 Nov 2026 08:34:20 -0500 Subject: [PATCH] D3D12.X: clamp single-adapter nodeMask and console heap types in GetCustomHeapProperties The test reports SKIPPED through runner::skip_test when it gets no DXGI factory, WARP adapter or WARP device, and the parent passes the child's SKIPPED lines on, instead of passing silently. Co-Authored-By: Claude Opus 5.5 --- crates/xcl/src/hle/d3d12x/device_x.rs | 107 +++++++++++++++++++++++++- 1 file changed, 106 insertions(+), 1 deletion(-) diff --git a/crates/xcl/src/hle/d3d12x/device_x.rs b/crates/xcl/src/hle/d3d12x/device_x.rs index 398b0716e8..9148cf78ed 100644 --- a/crates/xcl/src/hle/d3d12x/device_x.rs +++ b/crates/xcl/src/hle/d3d12x/device_x.rs @@ -1111,6 +1111,20 @@ pub(super) unsafe extern "system" fn get_resource_allocation_info2_x(a: *mut c_v ret } +/// Sanitize `GetCustomHeapProperties(nodeMask, heapType)` arguments for single-adapter PC D3D12: +/// nonzero `nodeMask` values clamp to `1`, and console-only heap types above `GPU_UPLOAD` (`5`) +/// fall back to `DEFAULT` (matching `dev_create_heap`), while invalid PC inputs (`0`, `4`) stay +/// untouched so the runtime still rejects them. +fn pc_custom_heap_query(mask: u32, ty: u32) -> (u32, u32) { + let pc_mask = if mask == 0 { 0 } else { 1 }; + let pc_ty = if ty > D3D12_HEAP_TYPE_GPU_UPLOAD.0 as u32 { + D3D12_HEAP_TYPE_DEFAULT.0 as u32 + } else { + ty + }; + (pc_mask, pc_ty) +} + /// device #26 GetCustomHeapProperties(node mask, heap type) -> D3D12_HEAP_PROPERTIES (20 bytes), /// returned through a hidden pointer - like #25 called the C way (a retail game: return buffer, device, /// ...) or the C++ way. @@ -1118,8 +1132,9 @@ pub(super) unsafe extern "system" fn get_custom_heap_properties_x(a: *mut c_void let (this, ret) = if real::this_first(a, b) { (a, b as *mut [u32; 5]) } else { (b, a as *mut [u32; 5]) }; type F = unsafe extern "system" fn(*mut c_void, *mut [u32; 5], u32, u32) -> *mut [u32; 5]; let pc: F = std::mem::transmute(real::orig_fn(this, 26)); + let (pc_mask, pc_ty) = pc_custom_heap_query(mask, ty); let mut r = [0u32; 5]; - pc(this, &mut r, mask, ty); + pc(this, &mut r, pc_mask, pc_ty); if !ret.is_null() { *ret = r; } @@ -1572,6 +1587,96 @@ mod tests { assert_eq!(console_bytes(&sampled), Some(crate::hle::xg::Texture::new(&sampled).unwrap().size())); } + #[test] + fn pc_custom_heap_query_sanitizes_node_mask_and_console_heap_type() { + assert_eq!(pc_custom_heap_query(0, 1), (0, 1)); + assert_eq!(pc_custom_heap_query(1, 2), (1, 2)); + assert_eq!(pc_custom_heap_query(0x3, 1), (1, 1)); + assert_eq!(pc_custom_heap_query(u32::MAX, 3), (1, 3)); + assert_eq!(pc_custom_heap_query(0, 0), (0, 0)); + assert_eq!(pc_custom_heap_query(0, 4), (0, 4)); + assert_eq!(pc_custom_heap_query(0, 5), (0, 5)); + assert_eq!(pc_custom_heap_query(0x3, 6), (1, 1)); + assert_eq!(pc_custom_heap_query(0, 0x10), (0, 1)); + } + + #[test] + fn get_custom_heap_properties_x_sanitizes_node_mask_and_console_heap_type() { + const CHILD_ENV: &str = "XCL_TEST_CUSTOM_HEAP_PROPS_CHILD"; + if std::env::var_os(CHILD_ENV).is_none() { + let exe = std::env::current_exe().expect("test binary path"); + let out = std::process::Command::new(exe) + .env(CHILD_ENV, "1") + .args([ + "--exact", + "hle::d3d12x::device_x::tests::get_custom_heap_properties_x_sanitizes_node_mask_and_console_heap_type", + "--nocapture", + ]) + .output() + .expect("spawn child test"); + assert!( + out.status.success(), + "child test failed ({:?}):\nstdout:\n{}\nstderr:\n{}", + out.status, + String::from_utf8_lossy(&out.stdout), + String::from_utf8_lossy(&out.stderr), + ); + // (the child's skip, if it skipped) + for line in String::from_utf8_lossy(&out.stdout).lines().filter(|l| l.starts_with("SKIPPED")) { + println!("{line}"); + } + return; + } + + use windows::Win32::Graphics::Direct3D::D3D_FEATURE_LEVEL_11_0; + use windows::Win32::Graphics::Dxgi::{CreateDXGIFactory1, IDXGIFactory4}; + + let Ok(factory) = (unsafe { CreateDXGIFactory1::() }) else { + return crate::gpu::recomp::runner::skip_test("no DXGI factory"); + }; + let Ok(warp) = (unsafe { factory.EnumWarpAdapter::() }) else { + return crate::gpu::recomp::runner::skip_test("no WARP"); + }; + let Ok(dev) = crate::gpu::agility::create_device(&warp, D3D_FEATURE_LEVEL_11_0) else { + return crate::gpu::recomp::runner::skip_test("no WARP device"); + }; + + unsafe { + #[repr(C, align(8))] + struct Aligned([u32; 5]); + real::adopt(dev.as_raw(), real::Kind::Device); + + // Reference PC answers for DEFAULT (1) and UPLOAD (2) on node 0. + let mut ref_default = Aligned([0u32; 5]); + get_custom_heap_properties_x(dev.as_raw(), &mut ref_default.0 as *mut _ as *mut c_void, 0, 1); + assert_eq!(ref_default.0[0], D3D12_HEAP_TYPE_CUSTOM.0 as u32); + + let mut ref_upload = Aligned([0u32; 5]); + get_custom_heap_properties_x(dev.as_raw(), &mut ref_upload.0 as *mut _ as *mut c_void, 0, 2); + assert_eq!(ref_upload.0[0], D3D12_HEAP_TYPE_CUSTOM.0 as u32); + + // Multi-bit NodeMask (0x3) with DEFAULT (C++ argument order) must match single-adapter DEFAULT. + let mut out_mask3 = Aligned([0u32; 5]); + get_custom_heap_properties_x(dev.as_raw(), &mut out_mask3.0 as *mut _ as *mut c_void, 3, 1); + assert_eq!(out_mask3.0, ref_default.0, "mask=3 DEFAULT must clamp to single-adapter DEFAULT"); + + // All-bits NodeMask (u32::MAX) with UPLOAD in C argument order (ret, this, ...) must match UPLOAD. + let mut out_max_c = Aligned([0u32; 5]); + get_custom_heap_properties_x(&mut out_max_c.0 as *mut _ as *mut c_void, dev.as_raw(), u32::MAX, 2); + assert_eq!(out_max_c.0, ref_upload.0, "C-order mask=MAX UPLOAD must clamp to single-adapter UPLOAD"); + + // Console-only heap type (6) falls back to DEFAULT properties. + let mut out_console = Aligned([0u32; 5]); + get_custom_heap_properties_x(dev.as_raw(), &mut out_console.0 as *mut _ as *mut c_void, 0, 6); + assert_eq!(out_console.0, ref_default.0, "console heap type 6 must map to DEFAULT"); + + // Invalid PC input CUSTOM (4) must remain rejected (Type == UNKNOWN = 0). + let mut out_custom = Aligned([0xAAu32; 5]); + get_custom_heap_properties_x(dev.as_raw(), &mut out_custom.0 as *mut _ as *mut c_void, 0, 4); + assert_eq!(out_custom.0[0], 0, "CUSTOM (4) input must stay rejected"); + } + } + /// GPU: a 256x256 D16 depth buffer the console creates for unordered access too, in /// UNORDERED_ACCESS, is placed on the PC as its description and initial state say; the state /// with only the console's bits dropped is refused (the depth buffer has no UAV flag there).