Repository navigation
D3D12.X: sanitize null-resource SRV, image UAV, and RTV formats #3319
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3226,11 +3226,23 @@ unsafe fn dev_create_srv_inner(this: *mut c_void, res: *mut c_void, desc: *const | |
| let mut copy = desc.as_ref().copied(); | ||
| if let Some(c) = copy.as_mut() { | ||
| c.Format = windows::Win32::Graphics::Dxgi::Common::DXGI_FORMAT(super::device_x::pc_view_format(c.Format.0 as u32) as i32); | ||
| if res.is_null() | ||
| && c.Format == windows::Win32::Graphics::Dxgi::Common::DXGI_FORMAT_UNKNOWN | ||
| && view_dim_to_image_type(c.ViewDimension.0).is_some() | ||
| { | ||
| c.Format = windows::Win32::Graphics::Dxgi::Common::DXGI_FORMAT_R32_FLOAT; | ||
| if res.is_null() && view_dim_to_image_type(c.ViewDimension.0).is_some() { | ||
| let f = match c.Format.0 as u32 { | ||
| 0 => 41, // UNKNOWN -> R32_FLOAT | ||
| 19 | 20 => 21, // R32G8X24_TYPELESS / D32_FLOAT_S8X24_UINT -> R32_FLOAT_X8X24_TYPELESS | ||
| 39 | 40 => 41, // R32_TYPELESS / D32_FLOAT -> R32_FLOAT | ||
| 44 | 45 => 46, // R24G8_TYPELESS / D24_UNORM_S8_UINT -> R24_UNORM_X8_TYPELESS | ||
| 53 | 55 => 56, // R16_TYPELESS / D16_UNORM -> R16_UNORM | ||
| f => f, | ||
| }; | ||
| c.Format = windows::Win32::Graphics::Dxgi::Common::DXGI_FORMAT(f as i32); | ||
| if matches!(f, 22 | 47) { | ||
| if c.ViewDimension == D3D12_SRV_DIMENSION_TEXTURE2D { | ||
| c.Anonymous.Texture2D.PlaneSlice = 1; | ||
| } else if c.ViewDimension == D3D12_SRV_DIMENSION_TEXTURE2DARRAY { | ||
| c.Anonymous.Texture2DArray.PlaneSlice = 1; | ||
| } | ||
| } | ||
| } | ||
| } | ||
| // a depth buffer's view in the formats the PC has for it (a D24S8 format itself, R16_UNORM of | ||
|
|
@@ -3673,11 +3685,16 @@ unsafe extern "system" fn dev_create_uav_x(this: *mut c_void, res: *mut c_void, | |
| if let Some(rd) = rd_opt.as_ref() { | ||
| let rf = rd.Format.0 as u32; | ||
| c.Format = windows::Win32::Graphics::Dxgi::Common::DXGI_FORMAT(super::device_x::pc_shader_view_format(rf, c.Format.0 as u32) as i32); | ||
| } else if view_dim_to_image_type(c.ViewDimension.0).is_some() { | ||
| let mapped = super::device_x::pc_uav_linear_format(super::device_x::pc_target_format(c.Format.0 as u32)); | ||
| let fmt = if mapped == 0 || !super::device_x::pc_typed_uav_supported(this, mapped as i32) { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The null image UAV fallback relies on |
||
| windows::Win32::Graphics::Dxgi::Common::DXGI_FORMAT_R32_FLOAT.0 as u32 | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. When the null UAV format falls back to R32_FLOAT, the console's PlaneSlice is kept. A null Texture2D/Array UAV with PlaneSlice=1 and a depth/stencil format becomes R32_FLOAT with plane 1, which is out of range for a one-plane format, so the debug layer reports it and the device can be removed. The SRV branch has the same issue when a non-stencil format keeps PlaneSlice != 0. |
||
| } else { | ||
| mapped | ||
| }; | ||
| c.Format = windows::Win32::Graphics::Dxgi::Common::DXGI_FORMAT(fmt as i32); | ||
| } else { | ||
| c.Format = windows::Win32::Graphics::Dxgi::Common::DXGI_FORMAT(super::device_x::pc_view_format(c.Format.0 as u32) as i32); | ||
| if c.Format == windows::Win32::Graphics::Dxgi::Common::DXGI_FORMAT_UNKNOWN && view_dim_to_image_type(c.ViewDimension.0).is_some() { | ||
| c.Format = windows::Win32::Graphics::Dxgi::Common::DXGI_FORMAT_R32_FLOAT; | ||
| } | ||
| } | ||
| } | ||
| let desc = copy.as_ref().map_or(desc, |c| c as *const _); | ||
|
|
@@ -11006,8 +11023,16 @@ unsafe extern "system" fn dev_create_rtv(this: *mut c_void, res: *mut c_void, de | |
| // a view in a console-only format, or of a target made R8G8B8A8 (`device_x::pc_resource_desc`) | ||
| let mut copy = (!desc.is_null()).then(|| (desc as *const D3D12_RENDER_TARGET_VIEW_DESC).read_unaligned()); | ||
| if let Some(c) = copy.as_mut() { | ||
| let rf = ID3D12Resource::from_raw_borrowed(&res).map_or(0, |r| r.GetDesc().Format.0 as u32); | ||
| c.Format = windows::Win32::Graphics::Dxgi::Common::DXGI_FORMAT(super::device_x::pc_shader_view_format(rf, c.Format.0 as u32) as i32); | ||
| let f = if res.is_null() { | ||
| match super::device_x::pc_target_format(c.Format.0 as u32) { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The null RTV path only applies |
||
| 0 => windows::Win32::Graphics::Dxgi::Common::DXGI_FORMAT_R8G8B8A8_UNORM.0 as u32, | ||
| f => f, | ||
| } | ||
| } else { | ||
| let rf = ID3D12Resource::from_raw_borrowed(&res).map_or(0, |r| r.GetDesc().Format.0 as u32); | ||
| super::device_x::pc_shader_view_format(rf, c.Format.0 as u32) | ||
| }; | ||
| c.Format = windows::Win32::Graphics::Dxgi::Common::DXGI_FORMAT(f as i32); | ||
| } | ||
| let desc = copy.as_ref().map_or(desc, |c| c as *const _ as *const c_void); | ||
| f(this, res, desc, handle); | ||
|
|
@@ -15008,3 +15033,172 @@ mod heap_release_order_tests { | |
| } | ||
| } | ||
|
|
||
| #[cfg(test)] | ||
| mod null_view_format_tests { | ||
| use super::*; | ||
| use windows::Win32::Graphics::Direct3D::D3D_FEATURE_LEVEL_12_0; | ||
| use windows::Win32::Graphics::Dxgi::{CreateDXGIFactory1, IDXGIAdapter1, IDXGIFactory4}; | ||
| use windows::Win32::Graphics::Dxgi::Common::DXGI_FORMAT; | ||
|
|
||
| unsafe fn collect_d3d12_errors(dev: &ID3D12Device) -> Vec<String> { | ||
| let Ok(q) = dev.cast::<ID3D12InfoQueue>() else { | ||
| return Vec::new(); | ||
| }; | ||
| let n = q.GetNumStoredMessages(); | ||
| let mut errs = Vec::new(); | ||
| for i in 0..n { | ||
| let mut len = 0usize; | ||
| if q.GetMessage(i, None, &mut len).is_err() || len == 0 { | ||
| continue; | ||
| } | ||
| let mut buf = vec![0u8; len]; | ||
| if q.GetMessage(i, Some(buf.as_mut_ptr() as *mut D3D12_MESSAGE), &mut len).is_ok() { | ||
| let msg = &*(buf.as_ptr() as *const D3D12_MESSAGE); | ||
| if matches!(msg.Severity, D3D12_MESSAGE_SEVERITY_CORRUPTION | D3D12_MESSAGE_SEVERITY_ERROR) { | ||
| let desc = if msg.pDescription.is_null() { | ||
| String::new() | ||
| } else { | ||
| std::ffi::CStr::from_ptr(msg.pDescription as *const i8).to_string_lossy().into_owned() | ||
| }; | ||
| errs.push(format!("[id={}] {}", msg.ID.0, desc)); | ||
| } | ||
| } | ||
| } | ||
| q.ClearStoredMessages(); | ||
| errs | ||
| } | ||
|
|
||
| /// Child-isolated WARP + D3D12 debug-layer test verifying null-resource SRVs, image UAVs, | ||
| /// and RTVs sanitize console/depth/sRGB/promoted/unsupported formats without D3D12 errors | ||
| /// or device removal. | ||
| #[test] | ||
| fn null_srv_uav_and_rtv_sanitize_console_and_incompatible_formats() { | ||
| const CHILD: &str = "XCL_NULL_VIEW_FORMAT_CHILD"; | ||
| if std::env::var_os(CHILD).is_none() { | ||
| let exe = std::env::current_exe().unwrap(); | ||
| let output = std::process::Command::new(exe) | ||
| .args(["--exact", "hle::d3d12x::real::null_view_format_tests::null_srv_uav_and_rtv_sanitize_console_and_incompatible_formats", "--nocapture"]) | ||
| .env(CHILD, "1") | ||
| .env("XCL_COHERENT_MB", "64") | ||
| .output() | ||
| .unwrap(); | ||
| assert!( | ||
| output.status.success(), | ||
| "child failed ({:?}):\nstdout:\n{}\nstderr:\n{}", | ||
| output.status, | ||
| String::from_utf8_lossy(&output.stdout), | ||
| String::from_utf8_lossy(&output.stderr) | ||
| ); | ||
| return; | ||
| } | ||
|
|
||
| unsafe { | ||
| let mut debug: Option<ID3D12Debug> = None; | ||
| let _ = D3D12GetDebugInterface(&mut debug); | ||
| if let Some(d) = debug { | ||
| d.EnableDebugLayer(); | ||
| } | ||
| let factory: IDXGIFactory4 = CreateDXGIFactory1().expect("CreateDXGIFactory1"); | ||
| let warp: IDXGIAdapter1 = factory.EnumWarpAdapter().expect("EnumWarpAdapter"); | ||
| let mut dev_opt: Option<ID3D12Device> = None; | ||
| D3D12CreateDevice(&warp, D3D_FEATURE_LEVEL_12_0, &mut dev_opt).expect("WARP D3D12CreateDevice"); | ||
| let dev = dev_opt.unwrap(); | ||
| assert!(crate::gpu::uma::get().is_some(), "UMA init failed in child"); | ||
| adopt(dev.as_raw(), Kind::Device); | ||
| let _ = collect_d3d12_errors(&dev); | ||
|
|
||
| // Create a shader-visible console view heap (32 slots) and an RTV heap (8 slots). | ||
| let view_desc = [D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV.0 as u32, 32, D3D12_DESCRIPTOR_HEAP_FLAG_SHADER_VISIBLE.0 as u32, 0]; | ||
| let mut view_heap_raw: *mut c_void = std::ptr::null_mut(); | ||
| let hr = dev_create_descriptor_heap_x(dev.as_raw(), &view_desc as *const [u32; 4], &ID3D12DescriptorHeap::IID as *const _ as *const GUID, &mut view_heap_raw); | ||
| assert!(hr >= 0 && !view_heap_raw.is_null(), "CreateDescriptorHeap view hr={hr:#x}"); | ||
| let view_heap = ID3D12DescriptorHeap::from_raw(view_heap_raw); | ||
| let view_base = super::super::console_heap::start(view_heap.as_raw() as usize).expect("view heap start"); | ||
| let view_slot = |k: u64| view_base + k * super::super::console_heap::SLOT as u64; | ||
|
|
||
| let rtv_desc = D3D12_DESCRIPTOR_HEAP_DESC { | ||
| Type: D3D12_DESCRIPTOR_HEAP_TYPE_RTV, | ||
| NumDescriptors: 8, | ||
| Flags: D3D12_DESCRIPTOR_HEAP_FLAG_NONE, | ||
| NodeMask: 0, | ||
| }; | ||
| let rtv_heap: ID3D12DescriptorHeap = dev.CreateDescriptorHeap(&rtv_desc).expect("RTV heap"); | ||
| let rtv_base = rtv_heap.GetCPUDescriptorHandleForHeapStart().ptr; | ||
| let rtv_inc = dev.GetDescriptorHandleIncrementSize(D3D12_DESCRIPTOR_HEAP_TYPE_RTV) as usize; | ||
|
|
||
| // 1. Null SRVs with depth, depth-typeless, console D16S8/X16G8, and stencil PlaneSlice=0 formats. | ||
| let null_srv_formats: &[(i32, D3D12_SRV_DIMENSION)] = &[ | ||
| (118, D3D12_SRV_DIMENSION_TEXTURE2D), // console D16_UNORM_S8_UINT -> D24S8 -> R24_UNORM_X8_TYPELESS | ||
| (40, D3D12_SRV_DIMENSION_TEXTURE2D), // D32_FLOAT -> R32_FLOAT | ||
| (45, D3D12_SRV_DIMENSION_TEXTURE2D), // D24_UNORM_S8_UINT -> R24_UNORM_X8_TYPELESS | ||
| (55, D3D12_SRV_DIMENSION_TEXTURE2D), // D16_UNORM -> R16_UNORM | ||
| (20, D3D12_SRV_DIMENSION_TEXTURE2D), // D32_FLOAT_S8X24_UINT -> R32_FLOAT_X8X24_TYPELESS | ||
| (39, D3D12_SRV_DIMENSION_TEXTURE2D), // R32_TYPELESS -> R32_FLOAT | ||
| (44, D3D12_SRV_DIMENSION_TEXTURE2D), // R24G8_TYPELESS -> R24_UNORM_X8_TYPELESS | ||
| (53, D3D12_SRV_DIMENSION_TEXTURE2D), // R16_TYPELESS -> R16_UNORM | ||
| (19, D3D12_SRV_DIMENSION_TEXTURE2D), // R32G8X24_TYPELESS -> R32_FLOAT_X8X24_TYPELESS | ||
| (120, D3D12_SRV_DIMENSION_TEXTURE2D), // console X16_TYPELESS_G8_UINT -> X24_TYPELESS_G8_UINT (PlaneSlice=1) | ||
| (22, D3D12_SRV_DIMENSION_TEXTURE2DARRAY), // X32_TYPELESS_G8X24_UINT (PlaneSlice=1) | ||
| ]; | ||
| for (k, &(fmt, dim)) in null_srv_formats.iter().enumerate() { | ||
| let mut d = D3D12_SHADER_RESOURCE_VIEW_DESC { | ||
| Format: DXGI_FORMAT(fmt), | ||
| ViewDimension: dim, | ||
| Shader4ComponentMapping: D3D12_DEFAULT_SHADER_4_COMPONENT_MAPPING, | ||
| ..Default::default() | ||
| }; | ||
| if dim == D3D12_SRV_DIMENSION_TEXTURE2D { | ||
| d.Anonymous.Texture2D = D3D12_TEX2D_SRV { MostDetailedMip: 0, MipLevels: 1, PlaneSlice: 0, ResourceMinLODClamp: 0.0 }; | ||
| } else { | ||
| d.Anonymous.Texture2DArray = D3D12_TEX2D_ARRAY_SRV { MostDetailedMip: 0, MipLevels: 1, FirstArraySlice: 0, ArraySize: 1, PlaneSlice: 0, ResourceMinLODClamp: 0.0 }; | ||
| } | ||
| dev_create_srv_x(dev.as_raw(), std::ptr::null_mut(), &d, view_slot(k as u64)); | ||
| let errs = collect_d3d12_errors(&dev); | ||
| assert!(errs.is_empty(), "null SRV fmt={fmt} dim={} emitted D3D12 errors: {errs:#?}", dim.0); | ||
| } | ||
|
|
||
| // 2. Null image UAVs with sRGB, promoted target, depth, BC, and unsupported formats. | ||
| let null_uav_formats: &[i32] = &[ | ||
| 29, // R8G8B8A8_UNORM_SRGB -> R8G8B8A8_UNORM | ||
| 91, // B8G8R8A8_UNORM_SRGB -> B8G8R8A8_UNORM (or R32_FLOAT if unsupported) | ||
| 93, // B8G8R8X8_UNORM_SRGB | ||
| 67, // R9G9B9E5_SHAREDEXP -> R11G11B10_FLOAT (or R32_FLOAT) | ||
| 86, // B5G5R5A1_UNORM -> R8G8B8A8_UNORM | ||
| 115, // B4G4R4A4_UNORM -> R8G8B8A8_UNORM | ||
| 85, // B5G6R5_UNORM -> R32_FLOAT | ||
| 88, // B8G8R8X8_UNORM -> R32_FLOAT | ||
| 40, // D32_FLOAT -> R32_FLOAT | ||
| 118, // console D16_UNORM_S8_UINT -> R32_FLOAT | ||
| 71, // BC1_UNORM -> R32_FLOAT | ||
| ]; | ||
| for (k, &fmt) in null_uav_formats.iter().enumerate() { | ||
| let d = D3D12_UNORDERED_ACCESS_VIEW_DESC { | ||
| Format: DXGI_FORMAT(fmt), | ||
| ViewDimension: D3D12_UAV_DIMENSION_TEXTURE2D, | ||
| Anonymous: D3D12_UNORDERED_ACCESS_VIEW_DESC_0 { Texture2D: D3D12_TEX2D_UAV { MipSlice: 0, PlaneSlice: 0 } }, | ||
| }; | ||
| dev_create_uav_checked_x(dev.as_raw(), std::ptr::null_mut(), std::ptr::null_mut(), &d, view_slot(16 + k as u64)); | ||
| let errs = collect_d3d12_errors(&dev); | ||
| assert!(errs.is_empty(), "null UAV fmt={fmt} emitted D3D12 errors: {errs:#?}"); | ||
| } | ||
|
|
||
| // 3. Null RTVs with promoted target formats and UNKNOWN. | ||
| let null_rtv_formats: &[i32] = &[67, 115, 86, 0]; | ||
| for (k, &fmt) in null_rtv_formats.iter().enumerate() { | ||
| let d = D3D12_RENDER_TARGET_VIEW_DESC { | ||
| Format: DXGI_FORMAT(fmt), | ||
| ViewDimension: D3D12_RTV_DIMENSION_TEXTURE2D, | ||
| Anonymous: D3D12_RENDER_TARGET_VIEW_DESC_0 { Texture2D: D3D12_TEX2D_RTV { MipSlice: 0, PlaneSlice: 0 } }, | ||
| }; | ||
| dev_create_rtv(dev.as_raw(), std::ptr::null_mut(), &d as *const _ as *const c_void, rtv_base + k * rtv_inc); | ||
| let errs = collect_d3d12_errors(&dev); | ||
| assert!(errs.is_empty(), "null RTV fmt={fmt} emitted D3D12 errors: {errs:#?}"); | ||
| } | ||
|
|
||
| assert!(dev.GetDeviceRemovedReason().is_ok(), "WARP device removed after null views"); | ||
| drop(view_heap); | ||
| drop(rtv_heap); | ||
| } | ||
| } | ||
| } | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The null SRV sanitizer only rewrites depth-family typeless and depth formats. Color and BC typeless formats fall through
f => f, so a null Texture2D SRV with R8G8B8A8_TYPELESS (27), R16G16B16A16_TYPELESS (9) or BC1_TYPELESS (70) still reaches CreateShaderResourceView typeless. D3D12 rejects that with the same 0x887A0001 removal this PR is meant to stop.