dawn: Add WGPU_STRLEN = SIZE_MAX to tag null terminated WGPUStringView

Bug: 42241188
Change-Id: Ieef31e3c42869d5616bfeef7f612b0ad4de205fc
Reviewed-on: https://dawn-review.googlesource.com/c/dawn/+/207254
Commit-Queue: Corentin Wallez <cwallez@chromium.org>
Reviewed-by: Kai Ninomiya <kainino@chromium.org>
diff --git a/generator/templates/dawn/cpp_macros.tmpl b/generator/templates/dawn/cpp_macros.tmpl
index 2539505..4870661 100644
--- a/generator/templates/dawn/cpp_macros.tmpl
+++ b/generator/templates/dawn/cpp_macros.tmpl
@@ -34,7 +34,7 @@
     // NOLINTNEXTLINE(runtime/explicit) allow implicit construction
     inline constexpr {{CppType}}(const char* s) {
         this->data = s;
-        this->length = SIZE_MAX;  // use strlen
+        this->length = WGPU_STRLEN;  // use strlen
     }
     inline constexpr {{CppType}}(const char* data, size_t length) {
         this->data = data;
@@ -46,12 +46,12 @@
         // NOLINTNEXTLINE(runtime/explicit) allow implicit construction
         inline constexpr {{CppType}}(std::nullptr_t) {
             this->data = nullptr;
-            this->length = SIZE_MAX;
+            this->length = WGPU_STRLEN;
         }
         // NOLINTNEXTLINE(runtime/explicit) allow implicit construction
         inline constexpr {{CppType}}(std::nullopt_t) {
             this->data = nullptr;
-            this->length = SIZE_MAX;
+            this->length = WGPU_STRLEN;
         }
     {% endif %}
 {% endmacro %}
diff --git a/generator/templates/dawn/native/ProcTable.cpp b/generator/templates/dawn/native/ProcTable.cpp
index 6bb34d2..55863c0 100644
--- a/generator/templates/dawn/native/ProcTable.cpp
+++ b/generator/templates/dawn/native/ProcTable.cpp
@@ -161,7 +161,7 @@
     WGPUProc NativeGetProcAddress2(WGPUDevice, WGPUStringView procName);
 
     WGPUProc NativeGetProcAddress(WGPUDevice device, const char* procName) {
-        return NativeGetProcAddress2(device, WGPUStringView{procName, SIZE_MAX});
+        return NativeGetProcAddress2(device, WGPUStringView{procName, WGPU_STRLEN});
     }
 
     WGPUProc NativeGetProcAddress2(WGPUDevice, WGPUStringView cProcName) {
@@ -169,7 +169,7 @@
             return nullptr;
         }
 
-        std::string_view procName(cProcName.data, cProcName.length != SIZE_MAX ? cProcName.length : strlen(cProcName.data));
+        std::string_view procName(cProcName.data, cProcName.length != WGPU_STRLEN ? cProcName.length : strlen(cProcName.data));
 
         const ProcEntry* entry = std::lower_bound(&sProcMap[0], &sProcMap[sProcMapSize], procName,
             [](const ProcEntry &a, const std::string_view& b) -> bool {
diff --git a/generator/templates/dawn/native/api_structs.cpp b/generator/templates/dawn/native/api_structs.cpp
index 7ad8827..96eb1c1 100644
--- a/generator/templates/dawn/native/api_structs.cpp
+++ b/generator/templates/dawn/native/api_structs.cpp
@@ -171,7 +171,7 @@
 
     StringView::operator std::string_view() const {
         const bool isNull = this->data == nullptr;
-        const bool useStrlen = this->length == SIZE_MAX;
+        const bool useStrlen = this->length == WGPU_STRLEN;
         DAWN_ASSERT(!(isNull && useStrlen));
         return std::string_view(this->data, isNull      ? 0
                                             : useStrlen ? std::strlen(this->data)
@@ -180,7 +180,7 @@
 
     NullableStringView::operator std::optional<std::string_view>() const {
         const bool isNull = this->data == nullptr;
-        const bool useStrlen = this->length == SIZE_MAX;
+        const bool useStrlen = this->length == WGPU_STRLEN;
         if (isNull && useStrlen) {
             return std::nullopt;
         }
diff --git a/generator/templates/dawn/wire/WireCmd.cpp b/generator/templates/dawn/wire/WireCmd.cpp
index 172fe55..8f78f69 100644
--- a/generator/templates/dawn/wire/WireCmd.cpp
+++ b/generator/templates/dawn/wire/WireCmd.cpp
@@ -736,8 +736,8 @@
 
 // Manually define serialization and deserialization for WGPUStringView because
 // it has a special encoding where:
-//  { .data = nullptr, .length = SIZE_MAX }  --> nil
-//  { .data = non-null, .length = SIZE_MAX } --> null-terminated, use strlen
+//  { .data = nullptr, .length = WGPU_STRLEN }  --> nil
+//  { .data = non-null, .length = WGPU_STRLEN } --> null-terminated, use strlen
 //  { .data = ..., .length = 0 }             --> ""
 //  { .data = ..., .length > 0 }             --> string of size `length`
 struct WGPUStringViewTransfer {
@@ -747,7 +747,7 @@
 
 size_t WGPUStringViewGetExtraRequiredSize(const WGPUStringView& record) {
     size_t size = record.length;
-    if (size == SIZE_MAX) {
+    if (size == WGPU_STRLEN) {
         // This is a null-terminated string, or it's nil.
         size = record.data ? std::strlen(record.data) : 0;
     }
@@ -767,7 +767,7 @@
         transfer->length = length;
         return WireResult::Success;
     }
-    if (length == SIZE_MAX) {
+    if (length == WGPU_STRLEN) {
         length = std::strlen(record.data);
     }
     if (length > 0) {
@@ -788,12 +788,12 @@
     bool has_data = transfer->has_data;
     uint64_t length = transfer->length;
 
-    if (length > SIZE_MAX) {
+    if (length > WGPU_STRLEN) {
         return WireResult::FatalError;
     }
     if (!has_data) {
         record->data = nullptr;
-        if (length != 0 && length != SIZE_MAX) {
+        if (length != 0 && length != WGPU_STRLEN) {
             // Invalid string.
             return WireResult::FatalError;
         }
diff --git a/generator/templates/dawn/wire/client/ApiProcs.cpp b/generator/templates/dawn/wire/client/ApiProcs.cpp
index 54689b2..7e891a5 100644
--- a/generator/templates/dawn/wire/client/ApiProcs.cpp
+++ b/generator/templates/dawn/wire/client/ApiProcs.cpp
@@ -138,7 +138,7 @@
 WGPUProc {{as_cMethodNamespaced(None, Name('get proc address 2'), Name('dawn wire client'))}}(WGPUDevice, WGPUStringView procName);
 
 DAWN_WIRE_EXPORT WGPUProc {{as_cMethodNamespaced(None, Name('get proc address'), Name('dawn wire client'))}}(WGPUDevice device, const char* procName) {
-    return {{as_cMethodNamespaced(None, Name('get proc address 2'), Name('dawn wire client'))}}(device, {procName, SIZE_MAX});
+    return {{as_cMethodNamespaced(None, Name('get proc address 2'), Name('dawn wire client'))}}(device, {procName, WGPU_STRLEN});
 }
 
 DAWN_WIRE_EXPORT WGPUProc {{as_cMethodNamespaced(None, Name('get proc address 2'), Name('dawn wire client'))}}(WGPUDevice, WGPUStringView cProcName) {
@@ -146,7 +146,7 @@
         return nullptr;
     }
 
-    std::string_view procName(cProcName.data, cProcName.length != SIZE_MAX ? cProcName.length : strlen(cProcName.data));
+    std::string_view procName(cProcName.data, cProcName.length != WGPU_STRLEN ? cProcName.length : strlen(cProcName.data));
 
     const ProcEntry* entry = std::lower_bound(&sProcMap[0], &sProcMap[sProcMapSize], procName,
         [](const ProcEntry &a, const std::string_view& b) -> bool {
diff --git a/generator/templates/dawn/wire/server/ServerDoers.cpp b/generator/templates/dawn/wire/server/ServerDoers.cpp
index 43957e7..721df97 100644
--- a/generator/templates/dawn/wire/server/ServerDoers.cpp
+++ b/generator/templates/dawn/wire/server/ServerDoers.cpp
@@ -57,7 +57,7 @@
                         {% set MemberName = as_varName(member.name) %}
                         {% if member.type.name.get() == "string view" %}
                             // String views must not be nullable.
-                            if ({{MemberName}}.data == nullptr && {{MemberName}}.length == SIZE_MAX) {
+                            if ({{MemberName}}.data == nullptr && {{MemberName}}.length == WGPU_STRLEN) {
                                 return WireResult::FatalError;
                             }
                         {% endif %}
diff --git a/src/dawn/dawn.json b/src/dawn/dawn.json
index d51c44d..d2c73f2 100644
--- a/src/dawn/dawn.json
+++ b/src/dawn/dawn.json
@@ -618,14 +618,14 @@
         "category": "structure",
         "members": [
             {"name": "data", "type": "char", "annotation": "const*", "optional": true},
-            {"name": "length", "type": "size_t", "default": "SIZE_MAX"}
+            {"name": "length", "type": "size_t", "default": "WGPU_STRLEN"}
         ]
     },
     "nullable string view": {
         "category": "structure",
         "members": [
             {"name": "data", "type": "char", "annotation": "const*", "optional": true},
-            {"name": "length", "type": "size_t", "default": "SIZE_MAX"}
+            {"name": "length", "type": "size_t", "default": "WGPU_STRLEN"}
         ]
     },
     "buffer": {
@@ -4775,6 +4775,11 @@
         "type": "uint32_t",
         "value":  "UINT32_MAX"
     },
+    "strlen" : {
+        "category": "constant",
+        "type": "size_t",
+        "value":  "SIZE_MAX"
+    },
     "ObjectType": {
       "_comment": "Only used for the wire",
       "category": "native"
diff --git a/src/dawn/tests/unittests/CppAPITests.cpp b/src/dawn/tests/unittests/CppAPITests.cpp
index dcb72a0..d66ce3a 100644
--- a/src/dawn/tests/unittests/CppAPITests.cpp
+++ b/src/dawn/tests/unittests/CppAPITests.cpp
@@ -38,22 +38,22 @@
     {
         wgpu::NullableStringView s;
         EXPECT_EQ(s.data, nullptr);
-        EXPECT_EQ(s.length, SIZE_MAX);
+        EXPECT_EQ(s.length, WGPU_STRLEN);
     }
     {
         wgpu::NullableStringView s{};
         EXPECT_EQ(s.data, nullptr);
-        EXPECT_EQ(s.length, SIZE_MAX);
+        EXPECT_EQ(s.length, WGPU_STRLEN);
     }
     {
         wgpu::NullableStringView s = {};
         EXPECT_EQ(s.data, nullptr);
-        EXPECT_EQ(s.length, SIZE_MAX);
+        EXPECT_EQ(s.length, WGPU_STRLEN);
     }
     {
         wgpu::NullableStringView s = wgpu::NullableStringView();
         EXPECT_EQ(s.data, nullptr);
-        EXPECT_EQ(s.length, SIZE_MAX);
+        EXPECT_EQ(s.length, WGPU_STRLEN);
     }
 
     // Test that resetting the string, clears both data and length.
@@ -62,13 +62,13 @@
         wgpu::NullableStringView s(sv);
         s = {};
         EXPECT_EQ(s.data, nullptr);
-        EXPECT_EQ(s.length, SIZE_MAX);
+        EXPECT_EQ(s.length, WGPU_STRLEN);
     }
     {
         wgpu::NullableStringView s(sv);
         s = wgpu::NullableStringView();
         EXPECT_EQ(s.data, nullptr);
-        EXPECT_EQ(s.length, SIZE_MAX);
+        EXPECT_EQ(s.length, WGPU_STRLEN);
     }
 }
 
@@ -77,22 +77,22 @@
     {
         wgpu::NullableStringView s("hello world!");
         EXPECT_STREQ(s.data, "hello world!");
-        EXPECT_EQ(s.length, SIZE_MAX);
+        EXPECT_EQ(s.length, WGPU_STRLEN);
     }
     {
         wgpu::NullableStringView s{"hello world!"};
         EXPECT_STREQ(s.data, "hello world!");
-        EXPECT_EQ(s.length, SIZE_MAX);
+        EXPECT_EQ(s.length, WGPU_STRLEN);
     }
     {
         wgpu::NullableStringView s = {"hello world!"};
         EXPECT_STREQ(s.data, "hello world!");
-        EXPECT_EQ(s.length, SIZE_MAX);
+        EXPECT_EQ(s.length, WGPU_STRLEN);
     }
     {
         wgpu::NullableStringView s = wgpu::NullableStringView("hello world!");
         EXPECT_STREQ(s.data, "hello world!");
-        EXPECT_EQ(s.length, SIZE_MAX);
+        EXPECT_EQ(s.length, WGPU_STRLEN);
     }
 
     // Test that setting to a cstr clears the length.
@@ -101,7 +101,7 @@
         wgpu::NullableStringView s(sv);
         s = "other str";
         EXPECT_STREQ(s.data, "other str");
-        EXPECT_EQ(s.length, SIZE_MAX);
+        EXPECT_EQ(s.length, WGPU_STRLEN);
     }
 }
 
@@ -160,22 +160,22 @@
     {
         wgpu::NullableStringView s(nullptr);
         EXPECT_EQ(s.data, nullptr);
-        EXPECT_EQ(s.length, SIZE_MAX);
+        EXPECT_EQ(s.length, WGPU_STRLEN);
     }
     {
         wgpu::NullableStringView s{nullptr};
         EXPECT_EQ(s.data, nullptr);
-        EXPECT_EQ(s.length, SIZE_MAX);
+        EXPECT_EQ(s.length, WGPU_STRLEN);
     }
     {
         wgpu::NullableStringView s = {nullptr};
         EXPECT_EQ(s.data, nullptr);
-        EXPECT_EQ(s.length, SIZE_MAX);
+        EXPECT_EQ(s.length, WGPU_STRLEN);
     }
     {
         wgpu::NullableStringView s = wgpu::NullableStringView(nullptr);
         EXPECT_EQ(s.data, nullptr);
-        EXPECT_EQ(s.length, SIZE_MAX);
+        EXPECT_EQ(s.length, WGPU_STRLEN);
     }
 
     // Test that setting to nullptr, clears both data and length.
@@ -184,7 +184,7 @@
         wgpu::NullableStringView s(sv);
         s = nullptr;
         EXPECT_EQ(s.data, nullptr);
-        EXPECT_EQ(s.length, SIZE_MAX);
+        EXPECT_EQ(s.length, WGPU_STRLEN);
     }
 }
 
@@ -193,22 +193,22 @@
     {
         wgpu::NullableStringView s(std::nullopt);
         EXPECT_EQ(s.data, nullptr);
-        EXPECT_EQ(s.length, SIZE_MAX);
+        EXPECT_EQ(s.length, WGPU_STRLEN);
     }
     {
         wgpu::NullableStringView s{std::nullopt};
         EXPECT_EQ(s.data, nullptr);
-        EXPECT_EQ(s.length, SIZE_MAX);
+        EXPECT_EQ(s.length, WGPU_STRLEN);
     }
     {
         wgpu::NullableStringView s = {std::nullopt};
         EXPECT_EQ(s.data, nullptr);
-        EXPECT_EQ(s.length, SIZE_MAX);
+        EXPECT_EQ(s.length, WGPU_STRLEN);
     }
     {
         wgpu::NullableStringView s = wgpu::NullableStringView(std::nullopt);
         EXPECT_EQ(s.data, nullptr);
-        EXPECT_EQ(s.length, SIZE_MAX);
+        EXPECT_EQ(s.length, WGPU_STRLEN);
     }
 
     // Test that setting to std::nullopt, clears both data and length.
@@ -217,7 +217,7 @@
         wgpu::NullableStringView s(sv);
         s = std::nullopt;
         EXPECT_EQ(s.data, nullptr);
-        EXPECT_EQ(s.length, SIZE_MAX);
+        EXPECT_EQ(s.length, WGPU_STRLEN);
     }
 }
 
diff --git a/src/dawn/tests/unittests/GetProcAddressTests.cpp b/src/dawn/tests/unittests/GetProcAddressTests.cpp
index 57e96f5..c74a53f 100644
--- a/src/dawn/tests/unittests/GetProcAddressTests.cpp
+++ b/src/dawn/tests/unittests/GetProcAddressTests.cpp
@@ -131,13 +131,13 @@
 
 // Test GetProcAddress with and without devices on some valid examples
 TEST_P(GetProcAddressTests, ValidExamples) {
-    ASSERT_EQ(mProcs.getProcAddress2(nullptr, {"wgpuDeviceCreateBuffer", SIZE_MAX}),
+    ASSERT_EQ(mProcs.getProcAddress2(nullptr, {"wgpuDeviceCreateBuffer", WGPU_STRLEN}),
               reinterpret_cast<WGPUProc>(mProcs.deviceCreateBuffer));
-    ASSERT_EQ(mProcs.getProcAddress2(mDevice.Get(), {"wgpuDeviceCreateBuffer", SIZE_MAX}),
+    ASSERT_EQ(mProcs.getProcAddress2(mDevice.Get(), {"wgpuDeviceCreateBuffer", WGPU_STRLEN}),
               reinterpret_cast<WGPUProc>(mProcs.deviceCreateBuffer));
-    ASSERT_EQ(mProcs.getProcAddress2(nullptr, {"wgpuQueueSubmit", SIZE_MAX}),
+    ASSERT_EQ(mProcs.getProcAddress2(nullptr, {"wgpuQueueSubmit", WGPU_STRLEN}),
               reinterpret_cast<WGPUProc>(mProcs.queueSubmit));
-    ASSERT_EQ(mProcs.getProcAddress2(mDevice.Get(), {"wgpuQueueSubmit", SIZE_MAX}),
+    ASSERT_EQ(mProcs.getProcAddress2(mDevice.Get(), {"wgpuQueueSubmit", WGPU_STRLEN}),
               reinterpret_cast<WGPUProc>(mProcs.queueSubmit));
     // Test a longer string, truncated correctly with the length field.
     ASSERT_EQ(mProcs.getProcAddress2(mDevice.Get(), {"wgpuQueueSubmitExtraString", 15}),
@@ -146,41 +146,42 @@
 
 // Test GetProcAddress with and without devices on nullptr procName
 TEST_P(GetProcAddressTests, Nullptr) {
-    ASSERT_EQ(mProcs.getProcAddress2(nullptr, {nullptr, SIZE_MAX}), nullptr);
-    ASSERT_EQ(mProcs.getProcAddress2(mDevice.Get(), {nullptr, SIZE_MAX}), nullptr);
+    ASSERT_EQ(mProcs.getProcAddress2(nullptr, {nullptr, WGPU_STRLEN}), nullptr);
+    ASSERT_EQ(mProcs.getProcAddress2(mDevice.Get(), {nullptr, WGPU_STRLEN}), nullptr);
 }
 
 // Test GetProcAddress with and without devices on some invalid
 TEST_P(GetProcAddressTests, InvalidExamples) {
-    ASSERT_EQ(mProcs.getProcAddress2(nullptr, {"wgpuDeviceDoSomething", SIZE_MAX}), nullptr);
-    ASSERT_EQ(mProcs.getProcAddress2(mDevice.Get(), {"wgpuDeviceDoSomething", SIZE_MAX}), nullptr);
+    ASSERT_EQ(mProcs.getProcAddress2(nullptr, {"wgpuDeviceDoSomething", WGPU_STRLEN}), nullptr);
+    ASSERT_EQ(mProcs.getProcAddress2(mDevice.Get(), {"wgpuDeviceDoSomething", WGPU_STRLEN}),
+              nullptr);
 
     // Test a "valid" string, truncated to not match with the length field.
     ASSERT_EQ(mProcs.getProcAddress2(mDevice.Get(), {"wgpuQueueSubmit", 14}), nullptr);
 
     // Trigger the condition where lower_bound will return the end of the procMap.
-    ASSERT_EQ(mProcs.getProcAddress2(nullptr, {"zzzzzzz", SIZE_MAX}), nullptr);
-    ASSERT_EQ(mProcs.getProcAddress2(mDevice.Get(), {"zzzzzzz", SIZE_MAX}), nullptr);
-    ASSERT_EQ(mProcs.getProcAddress2(nullptr, {"ZZ", SIZE_MAX}), nullptr);
-    ASSERT_EQ(mProcs.getProcAddress2(mDevice.Get(), {"ZZ", SIZE_MAX}), nullptr);
+    ASSERT_EQ(mProcs.getProcAddress2(nullptr, {"zzzzzzz", WGPU_STRLEN}), nullptr);
+    ASSERT_EQ(mProcs.getProcAddress2(mDevice.Get(), {"zzzzzzz", WGPU_STRLEN}), nullptr);
+    ASSERT_EQ(mProcs.getProcAddress2(nullptr, {"ZZ", WGPU_STRLEN}), nullptr);
+    ASSERT_EQ(mProcs.getProcAddress2(mDevice.Get(), {"ZZ", WGPU_STRLEN}), nullptr);
 
     // Some more potential corner cases.
-    ASSERT_EQ(mProcs.getProcAddress2(nullptr, {"", SIZE_MAX}), nullptr);
-    ASSERT_EQ(mProcs.getProcAddress2(mDevice.Get(), {"", SIZE_MAX}), nullptr);
-    ASSERT_EQ(mProcs.getProcAddress2(nullptr, {"0", SIZE_MAX}), nullptr);
-    ASSERT_EQ(mProcs.getProcAddress2(mDevice.Get(), {"0", SIZE_MAX}), nullptr);
+    ASSERT_EQ(mProcs.getProcAddress2(nullptr, {"", WGPU_STRLEN}), nullptr);
+    ASSERT_EQ(mProcs.getProcAddress2(mDevice.Get(), {"", WGPU_STRLEN}), nullptr);
+    ASSERT_EQ(mProcs.getProcAddress2(nullptr, {"0", WGPU_STRLEN}), nullptr);
+    ASSERT_EQ(mProcs.getProcAddress2(mDevice.Get(), {"0", WGPU_STRLEN}), nullptr);
 }
 
 // Test that GetProcAddress supports freestanding function that are handled specially
 TEST_P(GetProcAddressTests, FreeStandingFunctions) {
-    ASSERT_EQ(mProcs.getProcAddress2(nullptr, {"wgpuGetProcAddress", SIZE_MAX}),
+    ASSERT_EQ(mProcs.getProcAddress2(nullptr, {"wgpuGetProcAddress", WGPU_STRLEN}),
               reinterpret_cast<WGPUProc>(mProcs.getProcAddress));
-    ASSERT_EQ(mProcs.getProcAddress2(mDevice.Get(), {"wgpuGetProcAddress", SIZE_MAX}),
+    ASSERT_EQ(mProcs.getProcAddress2(mDevice.Get(), {"wgpuGetProcAddress", WGPU_STRLEN}),
               reinterpret_cast<WGPUProc>(mProcs.getProcAddress));
 
-    ASSERT_EQ(mProcs.getProcAddress2(nullptr, {"wgpuCreateInstance", SIZE_MAX}),
+    ASSERT_EQ(mProcs.getProcAddress2(nullptr, {"wgpuCreateInstance", WGPU_STRLEN}),
               reinterpret_cast<WGPUProc>(mProcs.createInstance));
-    ASSERT_EQ(mProcs.getProcAddress2(mDevice.Get(), {"wgpuCreateInstance", SIZE_MAX}),
+    ASSERT_EQ(mProcs.getProcAddress2(mDevice.Get(), {"wgpuCreateInstance", WGPU_STRLEN}),
               reinterpret_cast<WGPUProc>(mProcs.createInstance));
 }
 
diff --git a/src/dawn/tests/unittests/wire/WireArgumentTests.cpp b/src/dawn/tests/unittests/wire/WireArgumentTests.cpp
index ee11864..c7ffc0c 100644
--- a/src/dawn/tests/unittests/wire/WireArgumentTests.cpp
+++ b/src/dawn/tests/unittests/wire/WireArgumentTests.cpp
@@ -220,7 +220,7 @@
     vsModule.SetLabel(std::nullopt);
     EXPECT_CALL(api, ShaderModuleSetLabel2(apiVsModule,
                                            AllOf(Field(&WGPUStringView::data, nullptr),
-                                                 Field(&WGPUStringView::length, Eq(SIZE_MAX)))));
+                                                 Field(&WGPUStringView::length, Eq(WGPU_STRLEN)))));
     FlushClient();
 }