From a6d3d13fe22b778e3c662b5c8e7cef7d0ad1e5f7 Mon Sep 17 00:00:00 2001 From: Jubilee Young Date: Thu, 13 Jun 2024 19:30:07 -0700 Subject: [PATCH] Do not feed &"" to D3DCompile A recent change by rustc, now in 1.79-stable, makes empty str constants point to the same location: 0x01. This is an optimization of sorts, not stable behavior. Code must not rely on constants having stable addresses nor should it pass &"" to APIs expecting CStrs or NULL addresses. D3DCompile will segfault if you give it such a pointer, or worse: read random garbage addresses! Pass the NULL pointer to D3DCompile if wgpu lacks a decent CString. refs: - https://learn.microsoft.com/en-us/windows/win32/api/d3dcompiler/nf-d3dcompiler-d3dcompile Co-authored-by: Jan Hohenheim Co-authored-by: Brezak --- wgpu-hal/src/dx12/shader_compilation.rs | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/wgpu-hal/src/dx12/shader_compilation.rs b/wgpu-hal/src/dx12/shader_compilation.rs index 288fc24745..b4afe492b7 100644 --- a/wgpu-hal/src/dx12/shader_compilation.rs +++ b/wgpu-hal/src/dx12/shader_compilation.rs @@ -32,13 +32,21 @@ pub(super) fn compile_fxc( { compile_flags |= d3dcompiler::D3DCOMPILE_DEBUG | d3dcompiler::D3DCOMPILE_SKIP_OPTIMIZATION; } + + // If no name has been set, D3DCompile wants the null pointer. + let source_name = if source_name.is_empty() { + ptr::null() + } else { + source_name.as_ptr() + }; + let mut error = d3d12::Blob::null(); let hr = unsafe { profiling::scope!("d3dcompiler::D3DCompile"); d3dcompiler::D3DCompile( source.as_ptr().cast(), source.len(), - source_name.as_ptr().cast(), + source_name.cast(), ptr::null(), ptr::null_mut(), raw_ep.as_ptr(),