From 5887663c8ea23f31408e4b9dc89bede6f6ee6c36 Mon Sep 17 00:00:00 2001 From: Lucas Meurer Date: Wed, 2 Sep 2026 01:35:51 +0200 Subject: [PATCH] Fix dxc compile error when shader label contains `:` (#10134) --- CHANGELOG.md | 1 + tests/tests/wgpu-gpu/shader/mod.rs | 2 + tests/tests/wgpu-gpu/shader/module_label.rs | 63 ++++++++++++++ wgpu-hal/src/dx12/shader_compilation.rs | 96 ++++++++++++++++++++- 4 files changed, 159 insertions(+), 3 deletions(-) create mode 100644 tests/tests/wgpu-gpu/shader/module_label.rs diff --git a/CHANGELOG.md b/CHANGELOG.md index c5cb7337e..c536a3eba 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -180,6 +180,7 @@ By @sagudev in [#10109](https://github.com/gfx-rs/wgpu/pull/10109). #### DX12 - Make sure padding bytes are 0 in the destination buffer after a `copy_texture_to_buffer` when `UnrestrictedBufferTextureCopyPitchSupported` is not available. By @teoxoy in [#10005](https://github.com/gfx-rs/wgpu/pull/10005). +- Sanitize shader labels before passing them to DXC as the source name. Fixes compilation when label contains a `:`. By @lucasmerlin in [#10134](https://github.com/gfx-rs/wgpu/pull/10134). #### Vulkan diff --git a/tests/tests/wgpu-gpu/shader/mod.rs b/tests/tests/wgpu-gpu/shader/mod.rs index 89b8d3381..cf4077ab5 100644 --- a/tests/tests/wgpu-gpu/shader/mod.rs +++ b/tests/tests/wgpu-gpu/shader/mod.rs @@ -18,6 +18,7 @@ use wgpu_test::{GpuTestInitializer, TestingContext}; pub mod array_size_overrides; pub mod compilation_messages; pub mod data_builtins; +pub mod module_label; pub mod numeric_builtins; pub mod struct_layout; pub mod workgroup_size_overrides; @@ -27,6 +28,7 @@ pub fn all_tests(tests: &mut Vec) { array_size_overrides::all_tests(tests); compilation_messages::all_tests(tests); data_builtins::all_tests(tests); + module_label::all_tests(tests); numeric_builtins::all_tests(tests); struct_layout::all_tests(tests); workgroup_size_overrides::all_tests(tests); diff --git a/tests/tests/wgpu-gpu/shader/module_label.rs b/tests/tests/wgpu-gpu/shader/module_label.rs new file mode 100644 index 000000000..540bbf932 --- /dev/null +++ b/tests/tests/wgpu-gpu/shader/module_label.rs @@ -0,0 +1,63 @@ +//! Shader module labels are handed to the backend's shader compiler, which may +//! interpret them as more than an opaque string. DXC in particular reads the +//! label as the input file's path, so a label containing a `:` used to fail to +//! compile at all. + +use wgpu::{DownlevelFlags, Limits}; +use wgpu_test::{apply, gpu_test, GpuTestConfiguration, TestParameters, TestingContext}; + +pub fn all_tests(vec: &mut Vec) { + vec.push(SHADER_MODULE_LABEL_PUNCTUATION); +} + +const SHADER: &str = r#" + @compute @workgroup_size(1) + fn main() {} +"#; + +/// Labels containing punctuation that a shader compiler might read as path +/// syntax must not affect compilation. +#[apply(gpu_test!)] +static SHADER_MODULE_LABEL_PUNCTUATION: GpuTestConfiguration = GpuTestConfiguration::new() + .parameters( + TestParameters::default() + .downlevel_flags(DownlevelFlags::COMPUTE_SHADERS) + .limits(Limits::downlevel_defaults()), + ) + .run_sync(|ctx| { + for label in [ + "Renderer::shader", + "Renderer:shader", + "a::b::c", + "renderer/shader", + "C:/shader", + "shader with spaces", + "shader\twith\ttabs", + "/foo", + "-foo", + "", + ] { + create_pipeline(&ctx, label); + } + }); + +fn create_pipeline(ctx: &TestingContext, label: &str) { + let module = ctx + .device + .create_shader_module(wgpu::ShaderModuleDescriptor { + label: Some(label), + source: wgpu::ShaderSource::Wgsl(std::borrow::Cow::Borrowed(SHADER)), + }); + + // The backend compiles the shader here, not in `create_shader_module`. + let _pipeline = ctx + .device + .create_compute_pipeline(&wgpu::ComputePipelineDescriptor { + label: Some(label), + layout: None, + module: &module, + entry_point: Some("main"), + compilation_options: Default::default(), + cache: None, + }); +} diff --git a/wgpu-hal/src/dx12/shader_compilation.rs b/wgpu-hal/src/dx12/shader_compilation.rs index 7a5dc8cb9..c0503231d 100644 --- a/wgpu-hal/src/dx12/shader_compilation.rs +++ b/wgpu-hal/src/dx12/shader_compilation.rs @@ -373,9 +373,10 @@ fn compile_dxc( ) -> Result { profiling::scope!("compile_dxc"); - let source_name = source_name.and_then(|cstr| cstr.to_str().ok()); - - let source_name = source_name.map(OPCWSTR::new); + let source_name = source_name + .and_then(|cstr| cstr.to_str().ok()) + .and_then(sanitize_source_name); + let source_name = source_name.as_deref().map(OPCWSTR::new); let raw_ep = OPCWSTR::new(raw_ep); let full_stage = OPCWSTR::new(full_stage); @@ -448,3 +449,92 @@ fn compile_dxc( Ok(crate::dx12::CompiledShader::Dxc(blob)) } + +/// Prepare a shader module label for use as DXC's input file name. +/// +/// `compile_dxc` passes this as the first positional argument, which DXC reads +/// as the input file's path. Characters that it would misread there are mapped +/// to `-`: control characters and exotic whitespace, path separators, and +/// colons, which DXC takes for a drive designator when the name has no +/// separator. +/// +/// The result is anchored to the current directory, so that a label starting +/// with `-` cannot be parsed as a command-line flag. An empty label would leave +/// a bare `./`, which would error since it is a directory, so we drop those. +fn sanitize_source_name(name: &str) -> Option { + if name.is_empty() { + return None; + } + + let name: String = name + .chars() + .map(|c| { + let unsafe_char = c.is_control() + || std::path::is_separator(c) + || c == ':' + || (c.is_whitespace() && c != ' '); + if unsafe_char { + '-' + } else { + c + } + }) + .collect(); + + Some(format!("./{name}")) +} + +#[cfg(test)] +mod tests { + use super::sanitize_source_name; + + #[test] + fn sanitize_source_name_anchors_to_current_dir() { + assert_eq!(sanitize_source_name("shader").as_deref(), Some("./shader")); + assert_eq!( + sanitize_source_name("shader with spaces").as_deref(), + Some("./shader with spaces") + ); + } + + #[test] + fn sanitize_source_name_replaces_path_syntax() { + assert_eq!( + sanitize_source_name("Renderer::shader").as_deref(), + Some("./Renderer--shader") + ); + assert_eq!( + sanitize_source_name("renderer/shader").as_deref(), + Some("./renderer-shader") + ); + assert_eq!( + sanitize_source_name("renderer\\shader").as_deref(), + Some("./renderer-shader") + ); + assert_eq!( + sanitize_source_name("C:/shader").as_deref(), + Some("./C--shader") + ); + } + + #[test] + fn sanitize_source_name_defuses_command_line_flags() { + assert_eq!(sanitize_source_name("-foo").as_deref(), Some("./-foo")); + assert_eq!(sanitize_source_name("/foo").as_deref(), Some("./-foo")); + } + + #[test] + fn sanitize_source_name_replaces_control_chars() { + assert_eq!( + sanitize_source_name("shader\twith\ttabs").as_deref(), + Some("./shader-with-tabs") + ); + assert_eq!(sanitize_source_name("a\nb").as_deref(), Some("./a-b")); + assert_eq!(sanitize_source_name("a\u{7}b").as_deref(), Some("./a-b")); + } + + #[test] + fn sanitize_source_name_drops_empty() { + assert_eq!(sanitize_source_name(""), None); + } +}