dawn.node: Fix \0 in entryPoint names for constants/fragment/vertex
The same fix that was applied to GPUProgrammableStage needs to also be
applied to overridable constant names. The previous fix also only worked
for compute entry-points because they were the only Dawn type using
GPUProgrammableStage when dawn.json copies all the members in
GPUVertexState and GPUFragmentState as well. So the fix must be reused
there as well.
Bug: None
Change-Id: Ie51e7e2992ce4fccf8d69a6debe99538cd8bb75d
Reviewed-on: https://dawn-review.googlesource.com/c/dawn/+/159365
Reviewed-by: Ben Clayton <bclayton@google.com>
Kokoro: Kokoro <noreply+kokoro@google.com>
Commit-Queue: Corentin Wallez <cwallez@chromium.org>
diff --git a/src/dawn/node/binding/Converter.cpp b/src/dawn/node/binding/Converter.cpp
index eb37ee7..ad4062b 100644
--- a/src/dawn/node/binding/Converter.cpp
+++ b/src/dawn/node/binding/Converter.cpp
@@ -769,19 +769,9 @@
out = {};
out.module = *in.module.As<GPUShaderModule>();
- // Replace nulls in the entryPoint name with another character that's disallowed in
+ // Replace nulls in the entryPoint name with another character that's disallowed in WGSL
// identifiers. This is so that using "main\0" doesn't match an entryPoint named "main".
- // TODO(dawn:1345): Replace with a way to size strings explicitly in webgpu.h
- char* entryPoint = Allocate<char>(in.entryPoint.size() + 1);
- entryPoint[in.entryPoint.size()] = '\0';
- for (size_t i = 0; i < in.entryPoint.size(); i++) {
- if (in.entryPoint[i] == '\0') {
- entryPoint[i] = '#';
- } else {
- entryPoint[i] = in.entryPoint[i];
- }
- }
- out.entryPoint = entryPoint;
+ out.entryPoint = ConvertStringReplacingNull(in.entryPoint);
return Convert(out.constants, out.constantCount, in.constants);
}
@@ -789,7 +779,11 @@
bool Converter::Convert(wgpu::ConstantEntry& out,
const std::string& in_name,
wgpu::interop::GPUPipelineConstantValue in_value) {
- return Convert(out.key, in_name) && Convert(out.value, in_value);
+ // Replace nulls in the key with another character that's disallowed in WGSL identifiers.
+ // This is so that using "c\0" doesn't match a constant named "c".
+ out.key = ConvertStringReplacingNull(in_name);
+
+ return Convert(out.value, in_value);
}
bool Converter::Convert(wgpu::BlendComponent& out, const interop::GPUBlendComponent& in) {
@@ -920,9 +914,13 @@
bool Converter::Convert(wgpu::FragmentState& out, const interop::GPUFragmentState& in) {
out = {};
+
+ // Replace nulls in the entryPoint name with another character that's disallowed in WGSL
+ // identifiers. This is so that using "main\0" doesn't match an entryPoint named "main".
+ out.entryPoint = ConvertStringReplacingNull(in.entryPoint);
+
return Convert(out.targets, out.targetCount, in.targets) && //
Convert(out.module, in.module) && //
- Convert(out.entryPoint, in.entryPoint) && //
Convert(out.constants, out.constantCount, in.constants);
}
@@ -1065,10 +1063,14 @@
bool Converter::Convert(wgpu::VertexState& out, const interop::GPUVertexState& in) {
out = {};
+
+ // Replace nulls in the entryPoint name with another character that's disallowed in WGSL
+ // identifiers. This is so that using "main\0" doesn't match an entryPoint named "main".
+ out.entryPoint = ConvertStringReplacingNull(in.entryPoint);
+
wgpu::VertexBufferLayout* outBuffers = nullptr;
if (!Convert(out.module, in.module) || //
!Convert(outBuffers, out.bufferCount, in.buffers) || //
- !Convert(out.entryPoint, in.entryPoint) || //
!Convert(out.constants, out.constantCount, in.constants)) {
return false;
}
@@ -1625,6 +1627,21 @@
return true;
}
+char* Converter::ConvertStringReplacingNull(std::string_view in) {
+ char* out = Allocate<char>(in.size() + 1);
+ out[in.size()] = '\0';
+
+ for (size_t i = 0; i < in.size(); i++) {
+ if (in[i] == '\0') {
+ out[i] = '#';
+ } else {
+ out[i] = in[i];
+ }
+ }
+
+ return out;
+}
+
bool Converter::Throw(std::string&& message) {
return Throw(Napi::Error::New(env, message));
}
diff --git a/src/dawn/node/binding/Converter.h b/src/dawn/node/binding/Converter.h
index 6ffeed0..0630648 100644
--- a/src/dawn/node/binding/Converter.h
+++ b/src/dawn/node/binding/Converter.h
@@ -438,6 +438,10 @@
return Convert(out_els, out_count, in.value());
}
+ // JS strings can contain the null character, replace it with some other invalid character
+ // to preserve the creation of errors in this case.
+ char* ConvertStringReplacingNull(std::string_view in);
+
Napi::Env env;
wgpu::Device device = nullptr;