Skip to content

Commit 410d87e

Browse files
workingjubileejanhohenheimBrezak
authored andcommitted
Do not feed &"" to D3DCompile (#5812)
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]>
1 parent 87576b7 commit 410d87e

File tree

2 files changed

+17
-11
lines changed

2 files changed

+17
-11
lines changed

wgpu-hal/src/dx12/device.rs

+1-6
Original file line numberDiff line numberDiff line change
@@ -245,12 +245,7 @@ impl super::Device {
245245
.as_ref()
246246
.map_err(|e| crate::PipelineError::Linkage(stage_bit, format!("{e}")))?;
247247

248-
let source_name = stage
249-
.module
250-
.raw_name
251-
.as_ref()
252-
.and_then(|cstr| cstr.to_str().ok())
253-
.unwrap_or_default();
248+
let source_name = stage.module.raw_name.as_deref();
254249

255250
// Compile with DXC if available, otherwise fall back to FXC
256251
let (result, log_level) = if let Some(ref dxc_container) = self.dxc_container {

wgpu-hal/src/dx12/shader_compilation.rs

+16-5
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use std::ffi::CStr;
12
use std::ptr;
23

34
pub(super) use dxc::{compile_dxc, get_dxc_container, DxcContainer};
@@ -13,8 +14,8 @@ use crate::auxil::dxgi::result::HResult;
1314

1415
pub(super) fn compile_fxc(
1516
device: &super::Device,
16-
source: &String,
17-
source_name: &str,
17+
source: &str,
18+
source_name: Option<&CStr>,
1819
raw_ep: &std::ffi::CString,
1920
stage_bit: wgt::ShaderStages,
2021
full_stage: String,
@@ -32,13 +33,17 @@ pub(super) fn compile_fxc(
3233
{
3334
compile_flags |= d3dcompiler::D3DCOMPILE_DEBUG | d3dcompiler::D3DCOMPILE_SKIP_OPTIMIZATION;
3435
}
36+
37+
// If no name has been set, D3DCompile wants the null pointer.
38+
let source_name = source_name.map(|cstr| cstr.as_ptr()).unwrap_or(ptr::null());
39+
3540
let mut error = d3d12::Blob::null();
3641
let hr = unsafe {
3742
profiling::scope!("d3dcompiler::D3DCompile");
3843
d3dcompiler::D3DCompile(
3944
source.as_ptr().cast(),
4045
source.len(),
41-
source_name.as_ptr().cast(),
46+
source_name.cast(),
4247
ptr::null(),
4348
ptr::null_mut(),
4449
raw_ep.as_ptr(),
@@ -78,6 +83,7 @@ pub(super) fn compile_fxc(
7883
// The Dxc implementation is behind a feature flag so that users who don't want to use dxc can disable the feature.
7984
#[cfg(feature = "dxc_shader_compiler")]
8085
mod dxc {
86+
use std::ffi::CStr;
8187
use std::path::PathBuf;
8288

8389
// Destructor order should be fine since _dxil and _dxc don't rely on each other.
@@ -132,7 +138,7 @@ mod dxc {
132138
pub(crate) fn compile_dxc(
133139
device: &crate::dx12::Device,
134140
source: &str,
135-
source_name: &str,
141+
source_name: Option<&CStr>,
136142
raw_ep: &str,
137143
stage_bit: wgt::ShaderStages,
138144
full_stage: String,
@@ -166,6 +172,10 @@ mod dxc {
166172
Err(e) => return (Err(e), log::Level::Error),
167173
};
168174

175+
let source_name = source_name
176+
.and_then(|cstr| cstr.to_str().ok())
177+
.unwrap_or("");
178+
169179
let compiled = dxc_container.compiler.compile(
170180
&blob,
171181
source_name,
@@ -263,6 +273,7 @@ mod dxc {
263273
// These are stubs for when the `dxc_shader_compiler` feature is disabled.
264274
#[cfg(not(feature = "dxc_shader_compiler"))]
265275
mod dxc {
276+
use std::ffi::CStr;
266277
use std::path::PathBuf;
267278

268279
pub(crate) struct DxcContainer {}
@@ -280,7 +291,7 @@ mod dxc {
280291
pub(crate) fn compile_dxc(
281292
_device: &crate::dx12::Device,
282293
_source: &str,
283-
_source_name: &str,
294+
_source_name: Option<&CStr>,
284295
_raw_ep: &str,
285296
_stage_bit: wgt::ShaderStages,
286297
_full_stage: String,

0 commit comments

Comments
 (0)