From d723284fd75157b062296e53ac10bacd0b7b7601 Mon Sep 17 00:00:00 2001 From: crastinator <141459787+crastinator@users.noreply.github.com> Date: Wed, 25 Nov 2026 20:41:41 +0200 Subject: [PATCH] Recompiler: an empty DXC blob reads as no bytes (#2413) DXC can hand back an empty blob with a null buffer pointer (an empty error buffer when a compile fails, or an empty result). compile_here passed that pointer to slice::from_raw_parts, which requires a non-null pointer even for length 0: undefined behaviour, and a debug build aborts on the precondition check. Both places now read the blob through blob_bytes, which gives an empty slice for a null pointer or a zero size. Part of #2413 (the dxil.dll handle left loaded after a failed candidate is a separate change). --- crates/xcl/src/gpu/recomp/dxc.rs | 27 ++++++++++++++++++++++++--- 1 file changed, 24 insertions(+), 3 deletions(-) diff --git a/crates/xcl/src/gpu/recomp/dxc.rs b/crates/xcl/src/gpu/recomp/dxc.rs index 0a7a79b090..e93d469120 100644 --- a/crates/xcl/src/gpu/recomp/dxc.rs +++ b/crates/xcl/src/gpu/recomp/dxc.rs @@ -203,6 +203,14 @@ pub fn toolchain() -> Toolchain { mod version_tests { use super::*; + #[test] + fn an_empty_blob_with_a_null_pointer_reads_as_no_bytes() { + assert!(unsafe { bytes_at(std::ptr::null(), 0) }.is_empty()); + assert!(unsafe { bytes_at(std::ptr::null(), 16) }.is_empty()); + let data = [1u8, 2, 3]; + assert_eq!(unsafe { bytes_at(data.as_ptr(), data.len()) }, &data); + } + #[test] fn versions_parse_and_compare_as_numbers() { assert_eq!(parse_version("10.0.26100.0"), Some([10, 0, 26100, 0])); @@ -526,8 +534,7 @@ fn compile_here(src: &str, entry: &str, profile: &str, extra: &[&str]) -> Result .GetErrorBuffer() .ok() .map(|b| { - let s = std::slice::from_raw_parts(b.GetBufferPointer() as *const u8, b.GetBufferSize()); - String::from_utf8_lossy(s).trim_end_matches('\0').to_string() + String::from_utf8_lossy(blob_bytes(&b)).trim_end_matches('\0').to_string() }) .unwrap_or_default(); if status == windows::Win32::Foundation::E_OUTOFMEMORY { @@ -536,7 +543,21 @@ fn compile_here(src: &str, entry: &str, profile: &str, extra: &[&str]) -> Result return Err(format!("dxc: {msg}")); } let blob: IDxcBlob = r.GetResult().map_err(|e| format!("{e}"))?; - Ok(std::slice::from_raw_parts(blob.GetBufferPointer() as *const u8, blob.GetBufferSize()).to_vec()) + Ok(blob_bytes(&blob).to_vec()) + } +} + +/// The bytes of a DXC blob. An empty blob may have a null pointer, which `slice::from_raw_parts` +/// does not allow even for length 0. +unsafe fn blob_bytes(b: &IDxcBlob) -> &[u8] { + bytes_at(b.GetBufferPointer() as *const u8, b.GetBufferSize()) +} + +unsafe fn bytes_at<'a>(p: *const u8, n: usize) -> &'a [u8] { + if p.is_null() || n == 0 { + &[] + } else { + std::slice::from_raw_parts(p, n) } }