1 // Copyright (c) 2018 Google Inc.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 // Validation tests for WebGPU env specific checks
16
17 #include <string>
18
19 #include "gmock/gmock.h"
20 #include "test/val/val_fixtures.h"
21
22 namespace spvtools {
23 namespace val {
24 namespace {
25
26 using testing::HasSubstr;
27
28 using ValidateWebGPU = spvtest::ValidateBase<bool>;
29
TEST_F(ValidateWebGPU,OpUndefIsDisallowed)30 TEST_F(ValidateWebGPU, OpUndefIsDisallowed) {
31 std::string spirv = R"(
32 OpCapability Shader
33 OpCapability VulkanMemoryModelKHR
34 OpExtension "SPV_KHR_vulkan_memory_model"
35 OpMemoryModel Logical VulkanKHR
36 OpEntryPoint Vertex %func "shader"
37 %float = OpTypeFloat 32
38 %1 = OpUndef %float
39 %void = OpTypeVoid
40 %void_f = OpTypeFunction %void
41 %func = OpFunction %void None %void_f
42 %label = OpLabel
43 OpReturn
44 OpFunctionEnd
45 )";
46
47 CompileSuccessfully(spirv);
48
49 // Control case: OpUndef is allowed in SPIR-V 1.3
50 EXPECT_EQ(SPV_SUCCESS, ValidateInstructions(SPV_ENV_UNIVERSAL_1_3));
51
52 // Control case: OpUndef is disallowed in the WebGPU env
53 EXPECT_EQ(SPV_ERROR_INVALID_BINARY, ValidateInstructions(SPV_ENV_WEBGPU_0));
54 EXPECT_THAT(getDiagnosticString(), HasSubstr("OpUndef is disallowed"));
55 }
56
TEST_F(ValidateWebGPU,OpNameIsDisallowed)57 TEST_F(ValidateWebGPU, OpNameIsDisallowed) {
58 std::string spirv = R"(
59 OpCapability Shader
60 OpCapability VulkanMemoryModelKHR
61 OpExtension "SPV_KHR_vulkan_memory_model"
62 OpMemoryModel Logical VulkanKHR
63 OpName %1 "foo"
64 %1 = OpTypeFloat 32
65 )";
66
67 CompileSuccessfully(spirv);
68
69 EXPECT_EQ(SPV_ERROR_INVALID_BINARY, ValidateInstructions(SPV_ENV_WEBGPU_0));
70 EXPECT_THAT(getDiagnosticString(),
71 HasSubstr("Debugging instructions are not allowed in the WebGPU "
72 "execution environment.\n OpName %foo \"foo\"\n"));
73 }
74
TEST_F(ValidateWebGPU,OpMemberNameIsDisallowed)75 TEST_F(ValidateWebGPU, OpMemberNameIsDisallowed) {
76 std::string spirv = R"(
77 OpCapability Shader
78 OpCapability VulkanMemoryModelKHR
79 OpExtension "SPV_KHR_vulkan_memory_model"
80 OpMemoryModel Logical VulkanKHR
81 OpMemberName %2 0 "foo"
82 %1 = OpTypeFloat 32
83 %2 = OpTypeStruct %1
84 )";
85
86 CompileSuccessfully(spirv);
87
88 EXPECT_EQ(SPV_ERROR_INVALID_BINARY, ValidateInstructions(SPV_ENV_WEBGPU_0));
89 EXPECT_THAT(getDiagnosticString(),
90 HasSubstr("Debugging instructions are not allowed in the WebGPU "
91 "execution environment.\n OpMemberName %_struct_1 0 "
92 "\"foo\"\n"));
93 }
94
TEST_F(ValidateWebGPU,OpSourceIsDisallowed)95 TEST_F(ValidateWebGPU, OpSourceIsDisallowed) {
96 std::string spirv = R"(
97 OpCapability Shader
98 OpCapability VulkanMemoryModelKHR
99 OpExtension "SPV_KHR_vulkan_memory_model"
100 OpMemoryModel Logical VulkanKHR
101 OpSource GLSL 450
102 )";
103
104 CompileSuccessfully(spirv);
105
106 EXPECT_EQ(SPV_ERROR_INVALID_BINARY, ValidateInstructions(SPV_ENV_WEBGPU_0));
107 EXPECT_THAT(getDiagnosticString(),
108 HasSubstr("Debugging instructions are not allowed in the WebGPU "
109 "execution environment.\n OpSource GLSL 450\n"));
110 }
111
112 // OpSourceContinued does not have a test case, because it requires being
113 // preceded by OpSource, which will cause a validation error.
114
TEST_F(ValidateWebGPU,OpSourceExtensionIsDisallowed)115 TEST_F(ValidateWebGPU, OpSourceExtensionIsDisallowed) {
116 std::string spirv = R"(
117 OpCapability Shader
118 OpCapability VulkanMemoryModelKHR
119 OpExtension "SPV_KHR_vulkan_memory_model"
120 OpMemoryModel Logical VulkanKHR
121 OpSourceExtension "bar"
122 )";
123
124 CompileSuccessfully(spirv);
125
126 EXPECT_EQ(SPV_ERROR_INVALID_BINARY, ValidateInstructions(SPV_ENV_WEBGPU_0));
127 EXPECT_THAT(getDiagnosticString(),
128 HasSubstr("Debugging instructions are not allowed in the WebGPU "
129 "execution environment.\n OpSourceExtension "
130 "\"bar\"\n"));
131 }
132
TEST_F(ValidateWebGPU,OpStringIsDisallowed)133 TEST_F(ValidateWebGPU, OpStringIsDisallowed) {
134 std::string spirv = R"(
135 OpCapability Shader
136 OpCapability VulkanMemoryModelKHR
137 OpExtension "SPV_KHR_vulkan_memory_model"
138 OpMemoryModel Logical VulkanKHR
139 %1 = OpString "foo"
140 )";
141
142 CompileSuccessfully(spirv);
143
144 EXPECT_EQ(SPV_ERROR_INVALID_BINARY, ValidateInstructions(SPV_ENV_WEBGPU_0));
145 EXPECT_THAT(getDiagnosticString(),
146 HasSubstr("Debugging instructions are not allowed in the WebGPU "
147 "execution environment.\n %1 = OpString \"foo\"\n"));
148 }
149
150 // OpLine does not have a test case, because it requires being preceded by
151 // OpString, which will cause a validation error.
152
TEST_F(ValidateWebGPU,OpNoLineDisallowed)153 TEST_F(ValidateWebGPU, OpNoLineDisallowed) {
154 std::string spirv = R"(
155 OpCapability Shader
156 OpCapability VulkanMemoryModelKHR
157 OpExtension "SPV_KHR_vulkan_memory_model"
158 OpMemoryModel Logical VulkanKHR
159 OpNoLine
160 )";
161
162 CompileSuccessfully(spirv);
163
164 EXPECT_EQ(SPV_ERROR_INVALID_BINARY, ValidateInstructions(SPV_ENV_WEBGPU_0));
165 EXPECT_THAT(getDiagnosticString(),
166 HasSubstr("Debugging instructions are not allowed in the WebGPU "
167 "execution environment.\n OpNoLine\n"));
168 }
169
TEST_F(ValidateWebGPU,LogicalAddressingVulkanKHRMemoryGood)170 TEST_F(ValidateWebGPU, LogicalAddressingVulkanKHRMemoryGood) {
171 std::string spirv = R"(
172 OpCapability Shader
173 OpCapability VulkanMemoryModelKHR
174 OpExtension "SPV_KHR_vulkan_memory_model"
175 OpMemoryModel Logical VulkanKHR
176 OpEntryPoint Vertex %func "shader"
177 %void = OpTypeVoid
178 %void_f = OpTypeFunction %void
179 %func = OpFunction %void None %void_f
180 %label = OpLabel
181 OpReturn
182 OpFunctionEnd
183 )";
184
185 CompileSuccessfully(spirv);
186
187 EXPECT_EQ(SPV_SUCCESS, ValidateInstructions(SPV_ENV_WEBGPU_0));
188 }
189
TEST_F(ValidateWebGPU,NonLogicalAddressingModelBad)190 TEST_F(ValidateWebGPU, NonLogicalAddressingModelBad) {
191 std::string spirv = R"(
192 OpCapability Shader
193 OpCapability VulkanMemoryModelKHR
194 OpExtension "SPV_KHR_vulkan_memory_model"
195 OpMemoryModel Physical32 VulkanKHR
196 )";
197
198 CompileSuccessfully(spirv);
199
200 EXPECT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions(SPV_ENV_WEBGPU_0));
201 EXPECT_THAT(getDiagnosticString(),
202 HasSubstr("Addressing model must be Logical for WebGPU "
203 "environment.\n OpMemoryModel Physical32 "
204 "VulkanKHR\n"));
205 }
206
TEST_F(ValidateWebGPU,NonVulkanKHRMemoryModelBad)207 TEST_F(ValidateWebGPU, NonVulkanKHRMemoryModelBad) {
208 std::string spirv = R"(
209 OpCapability Shader
210 OpMemoryModel Logical GLSL450
211 OpNoLine
212 )";
213
214 CompileSuccessfully(spirv);
215
216 EXPECT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions(SPV_ENV_WEBGPU_0));
217 EXPECT_THAT(getDiagnosticString(),
218 HasSubstr("Memory model must be VulkanKHR for WebGPU "
219 "environment.\n OpMemoryModel Logical GLSL450\n"));
220 }
221
TEST_F(ValidateWebGPU,WhitelistedExtendedInstructionsImportGood)222 TEST_F(ValidateWebGPU, WhitelistedExtendedInstructionsImportGood) {
223 std::string spirv = R"(
224 OpCapability Shader
225 OpCapability VulkanMemoryModelKHR
226 OpExtension "SPV_KHR_vulkan_memory_model"
227 %1 = OpExtInstImport "GLSL.std.450"
228 OpMemoryModel Logical VulkanKHR
229 OpEntryPoint Vertex %func "shader"
230 %void = OpTypeVoid
231 %void_f = OpTypeFunction %void
232 %func = OpFunction %void None %void_f
233 %label = OpLabel
234 OpReturn
235 OpFunctionEnd
236 )";
237
238 CompileSuccessfully(spirv);
239
240 EXPECT_EQ(SPV_SUCCESS, ValidateInstructions(SPV_ENV_WEBGPU_0));
241 }
242
TEST_F(ValidateWebGPU,NonWhitelistedExtendedInstructionsImportBad)243 TEST_F(ValidateWebGPU, NonWhitelistedExtendedInstructionsImportBad) {
244 std::string spirv = R"(
245 OpCapability Shader
246 OpCapability VulkanMemoryModelKHR
247 OpExtension "SPV_KHR_vulkan_memory_model"
248 %1 = OpExtInstImport "OpenCL.std"
249 OpMemoryModel Logical VulkanKHR
250 )";
251
252 CompileSuccessfully(spirv);
253
254 EXPECT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions(SPV_ENV_WEBGPU_0));
255 EXPECT_THAT(getDiagnosticString(),
256 HasSubstr("For WebGPU, the only valid parameter to "
257 "OpExtInstImport is \"GLSL.std.450\".\n %1 = "
258 "OpExtInstImport \"OpenCL.std\"\n"));
259 }
260
TEST_F(ValidateWebGPU,NonVulkanKHRMemoryModelExtensionBad)261 TEST_F(ValidateWebGPU, NonVulkanKHRMemoryModelExtensionBad) {
262 std::string spirv = R"(
263 OpCapability Shader
264 OpCapability VulkanMemoryModelKHR
265 OpExtension "SPV_KHR_8bit_storage"
266 OpExtension "SPV_KHR_vulkan_memory_model"
267 OpMemoryModel Logical VulkanKHR
268 )";
269
270 CompileSuccessfully(spirv);
271
272 EXPECT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions(SPV_ENV_WEBGPU_0));
273 EXPECT_THAT(getDiagnosticString(),
274 HasSubstr("For WebGPU, the only valid parameter to OpExtension "
275 "is \"SPV_KHR_vulkan_memory_model\".\n OpExtension "
276 "\"SPV_KHR_8bit_storage\"\n"));
277 }
278
279 } // namespace
280 } // namespace val
281 } // namespace spvtools
282