Skip to content

Commit

Permalink
WebGPU: Create error shader module when the shader contains \0
Browse files Browse the repository at this point in the history
This patch creates an error GPUShaderModule when the WGSL shader code
contains '\0' as the WebGPU SPEC doesn't allow having '\0' in any WGSL
shader. As any characters in WGSL comments will be ignored in Tint, we
can only do this check before passing the WGSL shader code into Tint.

Fixed: dawn:1345
Change-Id: Ic915354c36f416f4058b5c88a4579725b44827bf
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/4568588
Reviewed-by: Kai Ninomiya <kainino@chromium.org>
Reviewed-by: Corentin Wallez <cwallez@chromium.org>
Commit-Queue: Jiawei Shao <jiawei.shao@intel.com>
Cr-Commit-Position: refs/heads/main@{#1150226}
  • Loading branch information
Jiawei-Shao authored and Chromium LUCI CQ committed May 29, 2023
1 parent 06d5c4b commit 987d004
Showing 1 changed file with 18 additions and 8 deletions.
26 changes: 18 additions & 8 deletions third_party/blink/renderer/modules/webgpu/gpu_shader_module.cc
Original file line number Diff line number Diff line change
Expand Up @@ -36,16 +36,18 @@ GPUShaderModule* GPUShaderModule::Create(
WGPUShaderModuleDescriptor dawn_desc = {};

const auto* wgsl_or_spirv = webgpu_desc->code();
bool has_null_character = false;
switch (wgsl_or_spirv->GetContentType()) {
case V8UnionUSVStringOrUint32Array::ContentType::kUSVString: {
// `\0` is not allowed in WGSL but it is allowed in USVString
// By replacing `\0` with `\0xFFFD` we can safely pass this string
// to Dawn and it should generate an error.
wgsl_code = UTF8StringFromUSVStringWithNullReplacedByReplacementCodePoint(
wgsl_or_spirv->GetAsUSVString());
WTF::String wtf_wgsl_code(wgsl_or_spirv->GetAsUSVString());
wgsl_code = wtf_wgsl_code.Utf8();
wgsl_desc.chain.sType = WGPUSType_ShaderModuleWGSLDescriptor;
wgsl_desc.code = wgsl_code.c_str();
dawn_desc.nextInChain = reinterpret_cast<WGPUChainedStruct*>(&wgsl_desc);
if (wtf_wgsl_code.find('\0') != WTF::kNotFound) {
has_null_character = true;
}

break;
}
case V8UnionUSVStringOrUint32Array::ContentType::kUint32Array: {
Expand All @@ -71,9 +73,17 @@ GPUShaderModule* GPUShaderModule::Create(
dawn_desc.label = label.c_str();
}

GPUShaderModule* shader = MakeGarbageCollected<GPUShaderModule>(
device, device->GetProcs().deviceCreateShaderModule(device->GetHandle(),
&dawn_desc));
WGPUShaderModule shader_module;
if (has_null_character) {
shader_module = device->GetProcs().deviceCreateErrorShaderModule(
device->GetHandle(), &dawn_desc,
"The WGSL shader contains an illegal character '\\0'");
} else {
shader_module = device->GetProcs().deviceCreateShaderModule(
device->GetHandle(), &dawn_desc);
}
GPUShaderModule* shader =
MakeGarbageCollected<GPUShaderModule>(device, shader_module);
if (webgpu_desc->hasLabel())
shader->setLabel(webgpu_desc->label());
return shader;
Expand Down

0 comments on commit 987d004

Please sign in to comment.