Debian Codegraph @

test/fuzz/transformation_wrap_early_terminator_in_function_test.cpp

raw
1 // Copyright (c) 2020 Google LLC
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 #include "source/fuzz/transformation_wrap_early_terminator_in_function.h"
16
17 #include "gtest/gtest.h"
18 #include "source/fuzz/fuzzer_util.h"
19 #include "source/fuzz/instruction_descriptor.h"
20 #include "test/fuzz/fuzz_test_util.h"
21
22 namespace spvtools {
23 namespace fuzz {
24 namespace {
25
26 TEST(TransformationWrapEarlyTerminatorInFunctionTest, IsApplicable) {
27 std::string shader = R"(
28 OpCapability Shader
29 OpExtension "SPV_KHR_terminate_invocation"
30 %1 = OpExtInstImport "GLSL.std.450"
31 OpMemoryModel Logical GLSL450
32 OpEntryPoint Fragment %4 "main"
33 OpExecutionMode %4 OriginUpperLeft
34 OpSource ESSL 320
35 OpName %4 "main"
36 %2 = OpTypeVoid
37 %3 = OpTypeFunction %2
38 %6 = OpTypeInt 32 1
39 %7 = OpConstant %6 0
40 %90 = OpTypeBool
41 %91 = OpConstantFalse %90
42
43 %20 = OpTypeFunction %2 %6
44 %21 = OpTypeFunction %6
45
46 %4 = OpFunction %2 None %3
47 %5 = OpLabel
48 OpSelectionMerge %11 None
49 OpSwitch %7 %11 0 %8 1 %9 2 %10
50 %8 = OpLabel
51 OpKill
52 %9 = OpLabel
53 OpUnreachable
54 %10 = OpLabel
55 OpTerminateInvocation
56 %11 = OpLabel
57 OpReturn
58 OpFunctionEnd
59
60 %30 = OpFunction %2 None %3
61 %31 = OpLabel
62 OpKill
63 OpFunctionEnd
64
65 %50 = OpFunction %2 None %3
66 %51 = OpLabel
67 OpTerminateInvocation
68 OpFunctionEnd
69
70 %60 = OpFunction %6 None %21
71 %61 = OpLabel
72 OpBranch %62
73 %62 = OpLabel
74 OpKill
75 OpFunctionEnd
76
77 %70 = OpFunction %6 None %21
78 %71 = OpLabel
79 OpUnreachable
80 OpFunctionEnd
81
82 %80 = OpFunction %2 None %20
83 %81 = OpFunctionParameter %6
84 %82 = OpLabel
85 OpTerminateInvocation
86 OpFunctionEnd
87
88 )";
89
90 const auto env = SPV_ENV_UNIVERSAL_1_4;
91 const auto consumer = nullptr;
92 const auto context = BuildModule(env, consumer, shader, kFuzzAssembleOption);
93 spvtools::ValidatorOptions validator_options;
94 ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(context.get(), validator_options,
95 kConsoleMessageConsumer));
96 TransformationContext transformation_context(
97 MakeUnique<FactManager>(context.get()), validator_options);
98
99 // Bad: id is not fresh
100 ASSERT_FALSE(TransformationWrapEarlyTerminatorInFunction(
101 61, MakeInstructionDescriptor(8, spv::Op::OpKill, 0), 0)
102 .IsApplicable(context.get(), transformation_context));
103
104 // Bad: early terminator instruction descriptor does not exist
105 ASSERT_FALSE(TransformationWrapEarlyTerminatorInFunction(
106 100, MakeInstructionDescriptor(82, spv::Op::OpKill, 0), 0)
107 .IsApplicable(context.get(), transformation_context));
108
109 // Bad: early terminator instruction does not identify an early terminator
110 ASSERT_FALSE(
111 TransformationWrapEarlyTerminatorInFunction(
112 100, MakeInstructionDescriptor(5, spv::Op::OpSelectionMerge, 0), 0)
113 .IsApplicable(context.get(), transformation_context));
114
115 // Bad: no wrapper function is available
116 ASSERT_FALSE(
117 TransformationWrapEarlyTerminatorInFunction(
118 100, MakeInstructionDescriptor(9, spv::Op::OpUnreachable, 0), 0)
119 .IsApplicable(context.get(), transformation_context));
120
121 // Bad: returned value does not exist
122 ASSERT_FALSE(TransformationWrapEarlyTerminatorInFunction(
123 100, MakeInstructionDescriptor(62, spv::Op::OpKill, 0), 1000)
124 .IsApplicable(context.get(), transformation_context));
125
126 // Bad: returned value does not have a type
127 ASSERT_FALSE(TransformationWrapEarlyTerminatorInFunction(
128 100, MakeInstructionDescriptor(62, spv::Op::OpKill, 0), 61)
129 .IsApplicable(context.get(), transformation_context));
130
131 // Bad: returned value type does not match
132 ASSERT_FALSE(TransformationWrapEarlyTerminatorInFunction(
133 100, MakeInstructionDescriptor(62, spv::Op::OpKill, 0), 91)
134 .IsApplicable(context.get(), transformation_context));
135
136 // Bad: returned value is not available
137 ASSERT_FALSE(TransformationWrapEarlyTerminatorInFunction(
138 100, MakeInstructionDescriptor(62, spv::Op::OpKill, 0), 81)
139 .IsApplicable(context.get(), transformation_context));
140
141 // Bad: the OpKill being targeted is in the only available wrapper; we cannot
142 // have the wrapper call itself.
143 ASSERT_FALSE(TransformationWrapEarlyTerminatorInFunction(
144 100, MakeInstructionDescriptor(31, spv::Op::OpKill, 0), 0)
145 .IsApplicable(context.get(), transformation_context));
146 }
147
148 TEST(TransformationWrapEarlyTerminatorInFunctionTest, Apply) {
149 std::string shader = R"(
150 OpCapability Shader
151 OpExtension "SPV_KHR_terminate_invocation"
152 %1 = OpExtInstImport "GLSL.std.450"
153 OpMemoryModel Logical GLSL450
154 OpEntryPoint Fragment %4 "main"
155 OpExecutionMode %4 OriginUpperLeft
156 OpSource ESSL 320
157 OpName %4 "main"
158 %2 = OpTypeVoid
159 %3 = OpTypeFunction %2
160 %6 = OpTypeInt 32 1
161 %7 = OpConstant %6 0
162
163 %20 = OpTypeFunction %2 %6
164 %21 = OpTypeFunction %6
165
166 %4 = OpFunction %2 None %3
167 %5 = OpLabel
168 OpSelectionMerge %11 None
169 OpSwitch %7 %11 0 %8 1 %9 2 %10
170 %8 = OpLabel
171 OpKill
172 %9 = OpLabel
173 OpUnreachable
174 %10 = OpLabel
175 OpTerminateInvocation
176 %11 = OpLabel
177 OpReturn
178 OpFunctionEnd
179
180 %30 = OpFunction %2 None %3
181 %31 = OpLabel
182 OpKill
183 OpFunctionEnd
184
185 %40 = OpFunction %2 None %3
186 %41 = OpLabel
187 OpUnreachable
188 OpFunctionEnd
189
190 %50 = OpFunction %2 None %3
191 %51 = OpLabel
192 OpTerminateInvocation
193 OpFunctionEnd
194
195 %60 = OpFunction %2 None %3
196 %61 = OpLabel
197 OpBranch %62
198 %62 = OpLabel
199 OpKill
200 OpFunctionEnd
201
202 %70 = OpFunction %6 None %21
203 %71 = OpLabel
204 OpUnreachable
205 OpFunctionEnd
206
207 %80 = OpFunction %2 None %20
208 %81 = OpFunctionParameter %6
209 %82 = OpLabel
210 OpTerminateInvocation
211 OpFunctionEnd
212 )";
213
214 const auto env = SPV_ENV_UNIVERSAL_1_4;
215 const auto consumer = nullptr;
216 const auto context = BuildModule(env, consumer, shader, kFuzzAssembleOption);
217 spvtools::ValidatorOptions validator_options;
218 ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(context.get(), validator_options,
219 kConsoleMessageConsumer));
220 TransformationContext transformation_context(
221 MakeUnique<FactManager>(context.get()), validator_options);
222
223 for (auto& transformation :
224 {TransformationWrapEarlyTerminatorInFunction(
225 100, MakeInstructionDescriptor(8, spv::Op::OpKill, 0), 0),
226 TransformationWrapEarlyTerminatorInFunction(
227 101, MakeInstructionDescriptor(9, spv::Op::OpUnreachable, 0), 0),
228 TransformationWrapEarlyTerminatorInFunction(
229 102,
230 MakeInstructionDescriptor(10, spv::Op::OpTerminateInvocation, 0),
231 0),
232 TransformationWrapEarlyTerminatorInFunction(
233 103, MakeInstructionDescriptor(62, spv::Op::OpKill, 0), 0),
234 TransformationWrapEarlyTerminatorInFunction(
235 104, MakeInstructionDescriptor(71, spv::Op::OpUnreachable, 0), 7),
236 TransformationWrapEarlyTerminatorInFunction(
237 105,
238 MakeInstructionDescriptor(82, spv::Op::OpTerminateInvocation, 0),
239 0)}) {
240 ASSERT_TRUE(
241 transformation.IsApplicable(context.get(), transformation_context));
242 ApplyAndCheckFreshIds(transformation, context.get(),
243 &transformation_context);
244 }
245 ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(context.get(), validator_options,
246 kConsoleMessageConsumer));
247
248 std::string after_transformation = R"(
249 OpCapability Shader
250 OpExtension "SPV_KHR_terminate_invocation"
251 %1 = OpExtInstImport "GLSL.std.450"
252 OpMemoryModel Logical GLSL450
253 OpEntryPoint Fragment %4 "main"
254 OpExecutionMode %4 OriginUpperLeft
255 OpSource ESSL 320
256 OpName %4 "main"
257 %2 = OpTypeVoid
258 %3 = OpTypeFunction %2
259 %6 = OpTypeInt 32 1
260 %7 = OpConstant %6 0
261
262 %20 = OpTypeFunction %2 %6
263 %21 = OpTypeFunction %6
264
265 %4 = OpFunction %2 None %3
266 %5 = OpLabel
267 OpSelectionMerge %11 None
268 OpSwitch %7 %11 0 %8 1 %9 2 %10
269 %8 = OpLabel
270 %100 = OpFunctionCall %2 %30
271 OpReturn
272 %9 = OpLabel
273 %101 = OpFunctionCall %2 %40
274 OpReturn
275 %10 = OpLabel
276 %102 = OpFunctionCall %2 %50
277 OpReturn
278 %11 = OpLabel
279 OpReturn
280 OpFunctionEnd
281
282 %30 = OpFunction %2 None %3
283 %31 = OpLabel
284 OpKill
285 OpFunctionEnd
286
287 %40 = OpFunction %2 None %3
288 %41 = OpLabel
289 OpUnreachable
290 OpFunctionEnd
291
292 %50 = OpFunction %2 None %3
293 %51 = OpLabel
294 OpTerminateInvocation
295 OpFunctionEnd
296
297 %60 = OpFunction %2 None %3
298 %61 = OpLabel
299 OpBranch %62
300 %62 = OpLabel
301 %103 = OpFunctionCall %2 %30
302 OpReturn
303 OpFunctionEnd
304
305 %70 = OpFunction %6 None %21
306 %71 = OpLabel
307 %104 = OpFunctionCall %2 %40
308 OpReturnValue %7
309 OpFunctionEnd
310
311 %80 = OpFunction %2 None %20
312 %81 = OpFunctionParameter %6
313 %82 = OpLabel
314 %105 = OpFunctionCall %2 %50
315 OpReturn
316 OpFunctionEnd
317 )";
318 ASSERT_TRUE(IsEqual(env, after_transformation, context.get()));
319 }
320
321 } // namespace
322 } // namespace fuzz
323 } // namespace spvtools