Skip to content

Commit

Permalink
Do not feed &"" to D3DCompile
Browse files Browse the repository at this point in the history
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 <[email protected]>
Co-authored-by: Brezak <[email protected]>
  • Loading branch information
3 people committed Jun 14, 2024
1 parent d7a35ec commit a6d3d13
Showing 1 changed file with 9 additions and 1 deletion.
10 changes: 9 additions & 1 deletion wgpu-hal/src/dx12/shader_compilation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down

0 comments on commit a6d3d13

Please sign in to comment.