Debian Codegraph @

test/fuzz/fuzzer_pass_donate_modules_test.cpp

raw
1 // Copyright (c) 2019 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/fuzzer_pass_donate_modules.h"
16
17 #include <algorithm>
18
19 #include "gtest/gtest.h"
20 #include "source/fuzz/pseudo_random_generator.h"
21 #include "test/fuzz/fuzz_test_util.h"
22
23 namespace spvtools {
24 namespace fuzz {
25 namespace {
26
27 TEST(FuzzerPassDonateModulesTest, BasicDonation) {
28 std::string recipient_shader = R"(
29 OpCapability Shader
30 %1 = OpExtInstImport "GLSL.std.450"
31 OpMemoryModel Logical GLSL450
32 OpEntryPoint Fragment %4 "main"
33 OpExecutionMode %4 OriginUpperLeft
34 OpSource ESSL 310
35 OpName %4 "main"
36 OpName %10 "m"
37 OpName %16 "v"
38 OpDecorate %16 RelaxedPrecision
39 OpDecorate %20 RelaxedPrecision
40 %2 = OpTypeVoid
41 %3 = OpTypeFunction %2
42 %6 = OpTypeFloat 32
43 %7 = OpTypeVector %6 3
44 %8 = OpTypeMatrix %7 2
45 %9 = OpTypePointer Private %8
46 %10 = OpVariable %9 Private
47 %11 = OpTypeInt 32 1
48 %12 = OpConstant %11 0
49 %13 = OpTypeInt 32 0
50 %14 = OpTypeVector %13 4
51 %15 = OpTypePointer Private %14
52 %16 = OpVariable %15 Private
53 %17 = OpConstant %13 2
54 %18 = OpTypePointer Private %13
55 %22 = OpConstant %13 0
56 %23 = OpTypePointer Private %6
57 %4 = OpFunction %2 None %3
58 %5 = OpLabel
59 %19 = OpAccessChain %18 %16 %17
60 %20 = OpLoad %13 %19
61 %21 = OpConvertUToF %6 %20
62 %24 = OpAccessChain %23 %10 %12 %22
63 OpStore %24 %21
64 OpReturn
65 OpFunctionEnd
66 )";
67
68 std::string donor_shader = R"(
69 OpCapability Shader
70 %1 = OpExtInstImport "GLSL.std.450"
71 OpMemoryModel Logical GLSL450
72 OpEntryPoint Fragment %4 "main"
73 OpExecutionMode %4 OriginUpperLeft
74 OpSource ESSL 310
75 OpName %4 "main"
76 OpName %12 "bar(mf24;"
77 OpName %11 "m"
78 OpName %20 "foo(vu4;"
79 OpName %19 "v"
80 OpName %23 "x"
81 OpName %26 "param"
82 OpName %29 "result"
83 OpName %31 "i"
84 OpName %81 "param"
85 %2 = OpTypeVoid
86 %3 = OpTypeFunction %2
87 %6 = OpTypeFloat 32
88 %7 = OpTypeVector %6 4
89 %8 = OpTypeMatrix %7 2
90 %9 = OpTypePointer Function %8
91 %10 = OpTypeFunction %6 %9
92 %14 = OpTypeInt 32 0
93 %15 = OpTypeVector %14 4
94 %16 = OpTypePointer Function %15
95 %17 = OpTypeInt 32 1
96 %18 = OpTypeFunction %17 %16
97 %22 = OpTypePointer Function %17
98 %24 = OpConstant %14 2
99 %25 = OpConstantComposite %15 %24 %24 %24 %24
100 %28 = OpTypePointer Function %6
101 %30 = OpConstant %6 0
102 %32 = OpConstant %17 0
103 %39 = OpConstant %17 10
104 %40 = OpTypeBool
105 %43 = OpConstant %17 3
106 %50 = OpConstant %17 1
107 %55 = OpConstant %14 0
108 %56 = OpTypePointer Function %14
109 %59 = OpConstant %14 1
110 %65 = OpConstant %17 2
111 %68 = OpConstant %6 1
112 %69 = OpConstant %6 2
113 %70 = OpConstant %6 3
114 %71 = OpConstant %6 4
115 %72 = OpConstant %14 3
116 %76 = OpConstant %6 6
117 %77 = OpConstant %6 7
118 %4 = OpFunction %2 None %3
119 %5 = OpLabel
120 %23 = OpVariable %22 Function
121 %26 = OpVariable %16 Function
122 OpStore %26 %25
123 %27 = OpFunctionCall %17 %20 %26
124 OpStore %23 %27
125 OpReturn
126 OpFunctionEnd
127 %12 = OpFunction %6 None %10
128 %11 = OpFunctionParameter %9
129 %13 = OpLabel
130 %29 = OpVariable %28 Function
131 %31 = OpVariable %22 Function
132 OpStore %29 %30
133 OpStore %31 %32
134 OpBranch %33
135 %33 = OpLabel
136 OpLoopMerge %35 %36 None
137 OpBranch %37
138 %37 = OpLabel
139 %38 = OpLoad %17 %31
140 %41 = OpSLessThan %40 %38 %39
141 OpBranchConditional %41 %34 %35
142 %34 = OpLabel
143 %42 = OpLoad %17 %31
144 %44 = OpExtInst %17 %1 SClamp %42 %32 %43
145 %45 = OpAccessChain %28 %11 %32 %44
146 %46 = OpLoad %6 %45
147 %47 = OpLoad %6 %29
148 %48 = OpFAdd %6 %47 %46
149 OpStore %29 %48
150 OpBranch %36
151 %36 = OpLabel
152 %49 = OpLoad %17 %31
153 %51 = OpIAdd %17 %49 %50
154 OpStore %31 %51
155 OpBranch %33
156 %35 = OpLabel
157 %52 = OpLoad %6 %29
158 OpReturnValue %52
159 OpFunctionEnd
160 %20 = OpFunction %17 None %18
161 %19 = OpFunctionParameter %16
162 %21 = OpLabel
163 %81 = OpVariable %9 Function
164 %57 = OpAccessChain %56 %19 %55
165 %58 = OpLoad %14 %57
166 %60 = OpAccessChain %56 %19 %59
167 %61 = OpLoad %14 %60
168 %62 = OpUGreaterThan %40 %58 %61
169 OpSelectionMerge %64 None
170 OpBranchConditional %62 %63 %67
171 %63 = OpLabel
172 OpReturnValue %65
173 %67 = OpLabel
174 %73 = OpAccessChain %56 %19 %72
175 %74 = OpLoad %14 %73
176 %75 = OpConvertUToF %6 %74
177 %78 = OpCompositeConstruct %7 %30 %68 %69 %70
178 %79 = OpCompositeConstruct %7 %71 %75 %76 %77
179 %80 = OpCompositeConstruct %8 %78 %79
180 OpStore %81 %80
181 %82 = OpFunctionCall %6 %12 %81
182 %83 = OpConvertFToS %17 %82
183 OpReturnValue %83
184 %64 = OpLabel
185 %85 = OpUndef %17
186 OpReturnValue %85
187 OpFunctionEnd
188 )";
189
190 const auto env = SPV_ENV_UNIVERSAL_1_3;
191 const auto consumer = nullptr;
192 spvtools::ValidatorOptions validator_options;
193
194 const auto recipient_context =
195 BuildModule(env, consumer, recipient_shader, kFuzzAssembleOption);
196 ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(
197 recipient_context.get(), validator_options, kConsoleMessageConsumer));
198
199 const auto donor_context =
200 BuildModule(env, consumer, donor_shader, kFuzzAssembleOption);
201 ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(
202 donor_context.get(), validator_options, kConsoleMessageConsumer));
203
204 TransformationContext transformation_context(
205 MakeUnique<FactManager>(recipient_context.get()), validator_options);
206
207 FuzzerContext fuzzer_context(MakeUnique<PseudoRandomGenerator>(0), 100,
208 false);
209 protobufs::TransformationSequence transformation_sequence;
210
211 FuzzerPassDonateModules fuzzer_pass(recipient_context.get(),
212 &transformation_context, &fuzzer_context,
213 &transformation_sequence, false, {});
214
215 fuzzer_pass.DonateSingleModule(donor_context.get(), false);
216
217 // We just check that the result is valid. Checking to what it should be
218 // exactly equal to would be very fragile.
219 ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(
220 recipient_context.get(), validator_options, kConsoleMessageConsumer));
221 }
222
223 TEST(FuzzerPassDonateModulesTest, DonationWithUniforms) {
224 // This test checks that when donating a shader that contains uniforms,
225 // uniform variables and associated pointer types are demoted from having
226 // Uniform storage class to Private storage class.
227 std::string recipient_and_donor_shader = R"(
228 OpCapability Shader
229 %1 = OpExtInstImport "GLSL.std.450"
230 OpMemoryModel Logical GLSL450
231 OpEntryPoint Fragment %4 "main"
232 OpExecutionMode %4 OriginUpperLeft
233 OpSource ESSL 310
234 OpMemberDecorate %9 0 Offset 0
235 OpDecorate %9 Block
236 OpDecorate %11 DescriptorSet 0
237 OpDecorate %11 Binding 0
238 OpMemberDecorate %19 0 Offset 0
239 OpDecorate %19 Block
240 OpDecorate %21 DescriptorSet 0
241 OpDecorate %21 Binding 1
242 %2 = OpTypeVoid
243 %3 = OpTypeFunction %2
244 %6 = OpTypeFloat 32
245 %7 = OpTypePointer Function %6
246 %9 = OpTypeStruct %6
247 %10 = OpTypePointer Uniform %9
248 %11 = OpVariable %10 Uniform
249 %12 = OpTypeInt 32 1
250 %13 = OpConstant %12 0
251 %14 = OpTypePointer Uniform %6
252 %17 = OpTypePointer Function %12
253 %19 = OpTypeStruct %12
254 %20 = OpTypePointer Uniform %19
255 %21 = OpVariable %20 Uniform
256 %22 = OpTypePointer Uniform %12
257 %4 = OpFunction %2 None %3
258 %5 = OpLabel
259 %8 = OpVariable %7 Function
260 %18 = OpVariable %17 Function
261 %15 = OpAccessChain %14 %11 %13
262 %16 = OpLoad %6 %15
263 OpStore %8 %16
264 %23 = OpAccessChain %22 %21 %13
265 %24 = OpLoad %12 %23
266 OpStore %18 %24
267 OpReturn
268 OpFunctionEnd
269 )";
270
271 const auto env = SPV_ENV_UNIVERSAL_1_3;
272 const auto consumer = nullptr;
273 spvtools::ValidatorOptions validator_options;
274
275 const auto recipient_context = BuildModule(
276 env, consumer, recipient_and_donor_shader, kFuzzAssembleOption);
277 ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(
278 recipient_context.get(), validator_options, kConsoleMessageConsumer));
279
280 const auto donor_context = BuildModule(
281 env, consumer, recipient_and_donor_shader, kFuzzAssembleOption);
282 ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(
283 donor_context.get(), validator_options, kConsoleMessageConsumer));
284
285 TransformationContext transformation_context(
286 MakeUnique<FactManager>(recipient_context.get()), validator_options);
287
288 FuzzerContext fuzzer_context(MakeUnique<PseudoRandomGenerator>(0), 100,
289 false);
290 protobufs::TransformationSequence transformation_sequence;
291
292 FuzzerPassDonateModules fuzzer_pass(recipient_context.get(),
293 &transformation_context, &fuzzer_context,
294 &transformation_sequence, false, {});
295
296 fuzzer_pass.DonateSingleModule(donor_context.get(), false);
297
298 ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(
299 recipient_context.get(), validator_options, kConsoleMessageConsumer));
300
301 std::string after_transformation = R"(
302 OpCapability Shader
303 %1 = OpExtInstImport "GLSL.std.450"
304 OpMemoryModel Logical GLSL450
305 OpEntryPoint Fragment %4 "main"
306 OpExecutionMode %4 OriginUpperLeft
307 OpSource ESSL 310
308 OpMemberDecorate %9 0 Offset 0
309 OpDecorate %9 Block
310 OpDecorate %11 DescriptorSet 0
311 OpDecorate %11 Binding 0
312 OpMemberDecorate %19 0 Offset 0
313 OpDecorate %19 Block
314 OpDecorate %21 DescriptorSet 0
315 OpDecorate %21 Binding 1
316 %2 = OpTypeVoid
317 %3 = OpTypeFunction %2
318 %6 = OpTypeFloat 32
319 %7 = OpTypePointer Function %6
320 %9 = OpTypeStruct %6
321 %10 = OpTypePointer Uniform %9
322 %11 = OpVariable %10 Uniform
323 %12 = OpTypeInt 32 1
324 %13 = OpConstant %12 0
325 %14 = OpTypePointer Uniform %6
326 %17 = OpTypePointer Function %12
327 %19 = OpTypeStruct %12
328 %20 = OpTypePointer Uniform %19
329 %21 = OpVariable %20 Uniform
330 %22 = OpTypePointer Uniform %12
331 %100 = OpTypePointer Function %6
332 %101 = OpTypeStruct %6
333 %102 = OpTypePointer Private %101
334 %104 = OpConstant %6 0
335 %105 = OpConstantComposite %101 %104
336 %103 = OpVariable %102 Private %105
337 %106 = OpConstant %12 0
338 %107 = OpTypePointer Private %6
339 %108 = OpTypePointer Function %12
340 %109 = OpTypeStruct %12
341 %110 = OpTypePointer Private %109
342 %112 = OpConstantComposite %109 %13
343 %111 = OpVariable %110 Private %112
344 %113 = OpTypePointer Private %12
345 %4 = OpFunction %2 None %3
346 %5 = OpLabel
347 %8 = OpVariable %7 Function
348 %18 = OpVariable %17 Function
349 %15 = OpAccessChain %14 %11 %13
350 %16 = OpLoad %6 %15
351 OpStore %8 %16
352 %23 = OpAccessChain %22 %21 %13
353 %24 = OpLoad %12 %23
354 OpStore %18 %24
355 OpReturn
356 OpFunctionEnd
357 %114 = OpFunction %2 None %3
358 %115 = OpLabel
359 %116 = OpVariable %100 Function %104
360 %117 = OpVariable %108 Function %13
361 %118 = OpAccessChain %107 %103 %106
362 %119 = OpLoad %6 %118
363 OpStore %116 %119
364 %120 = OpAccessChain %113 %111 %106
365 %121 = OpLoad %12 %120
366 OpStore %117 %121
367 OpReturn
368 OpFunctionEnd
369 )";
370 ASSERT_TRUE(IsEqual(env, after_transformation, recipient_context.get()));
371 }
372
373 TEST(FuzzerPassDonateModulesTest, DonationWithInputAndOutputVariables) {
374 // This test checks that when donating a shader that contains input and output
375 // variables, such variables and associated pointer types are demoted to have
376 // the Private storage class.
377 std::string recipient_and_donor_shader = R"(
378 OpCapability Shader
379 %1 = OpExtInstImport "GLSL.std.450"
380 OpMemoryModel Logical GLSL450
381 OpEntryPoint Fragment %4 "main" %9 %11
382 OpExecutionMode %4 OriginUpperLeft
383 OpSource ESSL 310
384 OpDecorate %9 Location 0
385 OpDecorate %11 Location 1
386 %2 = OpTypeVoid
387 %3 = OpTypeFunction %2
388 %6 = OpTypeFloat 32
389 %7 = OpTypeVector %6 4
390 %8 = OpTypePointer Output %7
391 %9 = OpVariable %8 Output
392 %10 = OpTypePointer Input %7
393 %11 = OpVariable %10 Input
394 %4 = OpFunction %2 None %3
395 %5 = OpLabel
396 %12 = OpLoad %7 %11
397 OpStore %9 %12
398 OpReturn
399 OpFunctionEnd
400 )";
401
402 const auto env = SPV_ENV_UNIVERSAL_1_3;
403 const auto consumer = nullptr;
404 spvtools::ValidatorOptions validator_options;
405
406 const auto recipient_context = BuildModule(
407 env, consumer, recipient_and_donor_shader, kFuzzAssembleOption);
408 ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(
409 recipient_context.get(), validator_options, kConsoleMessageConsumer));
410
411 const auto donor_context = BuildModule(
412 env, consumer, recipient_and_donor_shader, kFuzzAssembleOption);
413 ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(
414 donor_context.get(), validator_options, kConsoleMessageConsumer));
415
416 TransformationContext transformation_context(
417 MakeUnique<FactManager>(recipient_context.get()), validator_options);
418
419 FuzzerContext fuzzer_context(MakeUnique<PseudoRandomGenerator>(0), 100,
420 false);
421 protobufs::TransformationSequence transformation_sequence;
422
423 FuzzerPassDonateModules fuzzer_pass(recipient_context.get(),
424 &transformation_context, &fuzzer_context,
425 &transformation_sequence, false, {});
426
427 fuzzer_pass.DonateSingleModule(donor_context.get(), false);
428
429 ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(
430 recipient_context.get(), validator_options, kConsoleMessageConsumer));
431
432 std::string after_transformation = R"(
433 OpCapability Shader
434 %1 = OpExtInstImport "GLSL.std.450"
435 OpMemoryModel Logical GLSL450
436 OpEntryPoint Fragment %4 "main" %9 %11
437 OpExecutionMode %4 OriginUpperLeft
438 OpSource ESSL 310
439 OpDecorate %9 Location 0
440 OpDecorate %11 Location 1
441 %2 = OpTypeVoid
442 %3 = OpTypeFunction %2
443 %6 = OpTypeFloat 32
444 %7 = OpTypeVector %6 4
445 %8 = OpTypePointer Output %7
446 %9 = OpVariable %8 Output
447 %10 = OpTypePointer Input %7
448 %11 = OpVariable %10 Input
449 %100 = OpTypePointer Private %7
450 %102 = OpConstant %6 0
451 %103 = OpConstantComposite %7 %102 %102 %102 %102
452 %101 = OpVariable %100 Private %103
453 %104 = OpTypePointer Private %7
454 %105 = OpVariable %104 Private %103
455 %4 = OpFunction %2 None %3
456 %5 = OpLabel
457 %12 = OpLoad %7 %11
458 OpStore %9 %12
459 OpReturn
460 OpFunctionEnd
461 %106 = OpFunction %2 None %3
462 %107 = OpLabel
463 %108 = OpLoad %7 %105
464 OpStore %101 %108
465 OpReturn
466 OpFunctionEnd
467 )";
468 ASSERT_TRUE(IsEqual(env, after_transformation, recipient_context.get()));
469 }
470
471 TEST(FuzzerPassDonateModulesTest, DonateFunctionTypeWithDifferentPointers) {
472 std::string recipient_and_donor_shader = R"(
473 OpCapability Shader
474 %1 = OpExtInstImport "GLSL.std.450"
475 OpMemoryModel Logical GLSL450
476 OpEntryPoint Fragment %4 "main"
477 OpExecutionMode %4 OriginUpperLeft
478 OpSource ESSL 310
479 %2 = OpTypeVoid
480 %3 = OpTypeFunction %2
481 %6 = OpTypeInt 32 0
482 %7 = OpTypePointer Function %6
483 %8 = OpTypeFunction %2 %7
484 %4 = OpFunction %2 None %3
485 %5 = OpLabel
486 %9 = OpVariable %7 Function
487 %10 = OpFunctionCall %2 %11 %9
488 OpReturn
489 OpFunctionEnd
490 %11 = OpFunction %2 None %8
491 %12 = OpFunctionParameter %7
492 %13 = OpLabel
493 OpReturn
494 OpFunctionEnd
495 )";
496
497 const auto env = SPV_ENV_UNIVERSAL_1_5;
498 const auto consumer = nullptr;
499 spvtools::ValidatorOptions validator_options;
500
501 const auto recipient_context = BuildModule(
502 env, consumer, recipient_and_donor_shader, kFuzzAssembleOption);
503 ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(
504 recipient_context.get(), validator_options, kConsoleMessageConsumer));
505
506 const auto donor_context = BuildModule(
507 env, consumer, recipient_and_donor_shader, kFuzzAssembleOption);
508 ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(
509 donor_context.get(), validator_options, kConsoleMessageConsumer));
510
511 TransformationContext transformation_context(
512 MakeUnique<FactManager>(recipient_context.get()), validator_options);
513
514 FuzzerContext fuzzer_context(MakeUnique<PseudoRandomGenerator>(0), 100,
515 false);
516 protobufs::TransformationSequence transformation_sequence;
517
518 FuzzerPassDonateModules fuzzer_pass(recipient_context.get(),
519 &transformation_context, &fuzzer_context,
520 &transformation_sequence, false, {});
521
522 fuzzer_pass.DonateSingleModule(donor_context.get(), false);
523
524 // We just check that the result is valid. Checking to what it should be
525 // exactly equal to would be very fragile.
526 ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(
527 recipient_context.get(), validator_options, kConsoleMessageConsumer));
528 }
529
530 TEST(FuzzerPassDonateModulesTest, DonateOpConstantNull) {
531 std::string recipient_shader = R"(
532 OpCapability Shader
533 OpCapability ImageQuery
534 OpCapability VariablePointers
535 %1 = OpExtInstImport "GLSL.std.450"
536 OpMemoryModel Logical GLSL450
537 OpEntryPoint Fragment %4 "main"
538 OpExecutionMode %4 OriginUpperLeft
539 OpSource ESSL 320
540 OpSourceExtension "GL_EXT_samplerless_texture_functions"
541 %2 = OpTypeVoid
542 %3 = OpTypeFunction %2
543 %4 = OpFunction %2 None %3
544 %5 = OpLabel
545 OpReturn
546 OpFunctionEnd
547 )";
548
549 std::string donor_shader = R"(
550 OpCapability Shader
551 OpCapability ImageQuery
552 OpCapability VariablePointers
553 %1 = OpExtInstImport "GLSL.std.450"
554 OpMemoryModel Logical GLSL450
555 OpEntryPoint Fragment %4 "main"
556 OpExecutionMode %4 OriginUpperLeft
557 OpSource ESSL 320
558 %2 = OpTypeVoid
559 %3 = OpTypeFunction %2
560 %6 = OpTypeFloat 32
561 %7 = OpTypePointer Private %6
562 %8 = OpConstantNull %7
563 %4 = OpFunction %2 None %3
564 %5 = OpLabel
565 OpReturn
566 OpFunctionEnd
567 )";
568
569 const auto env = SPV_ENV_UNIVERSAL_1_3;
570 const auto consumer = nullptr;
571 spvtools::ValidatorOptions validator_options;
572 validator_options.SetRelaxLogicalPointer(true);
573
574 const auto recipient_context =
575 BuildModule(env, consumer, recipient_shader, kFuzzAssembleOption);
576 ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(
577 recipient_context.get(), validator_options, kConsoleMessageConsumer));
578
579 const auto donor_context =
580 BuildModule(env, consumer, donor_shader, kFuzzAssembleOption);
581 ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(
582 donor_context.get(), validator_options, kConsoleMessageConsumer));
583
584 TransformationContext transformation_context(
585 MakeUnique<FactManager>(recipient_context.get()), validator_options);
586
587 FuzzerContext fuzzer_context(MakeUnique<PseudoRandomGenerator>(0), 100,
588 false);
589 protobufs::TransformationSequence transformation_sequence;
590
591 FuzzerPassDonateModules fuzzer_pass(recipient_context.get(),
592 &transformation_context, &fuzzer_context,
593 &transformation_sequence, false, {});
594
595 fuzzer_pass.DonateSingleModule(donor_context.get(), false);
596
597 // We just check that the result is valid. Checking to what it should be
598 // exactly equal to would be very fragile.
599 ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(
600 recipient_context.get(), validator_options, kConsoleMessageConsumer));
601 }
602
603 TEST(FuzzerPassDonateModulesTest, DonateCodeThatUsesImages) {
604 std::string recipient_shader = R"(
605 OpCapability Shader
606 OpCapability ImageQuery
607 %1 = OpExtInstImport "GLSL.std.450"
608 OpMemoryModel Logical GLSL450
609 OpEntryPoint Fragment %4 "main"
610 OpExecutionMode %4 OriginUpperLeft
611 OpSource ESSL 320
612 OpSourceExtension "GL_EXT_samplerless_texture_functions"
613 %2 = OpTypeVoid
614 %3 = OpTypeFunction %2
615 %4 = OpFunction %2 None %3
616 %5 = OpLabel
617 OpReturn
618 OpFunctionEnd
619 )";
620
621 std::string donor_shader = R"(
622 OpCapability Shader
623 OpCapability ImageQuery
624 %1 = OpExtInstImport "GLSL.std.450"
625 OpMemoryModel Logical GLSL450
626 OpEntryPoint Fragment %4 "main"
627 OpExecutionMode %4 OriginUpperLeft
628 OpSource ESSL 320
629 OpSourceExtension "GL_EXT_samplerless_texture_functions"
630 OpName %4 "main"
631 OpName %10 "mySampler"
632 OpName %21 "myTexture"
633 OpName %33 "v"
634 OpDecorate %10 RelaxedPrecision
635 OpDecorate %10 DescriptorSet 0
636 OpDecorate %10 Binding 0
637 OpDecorate %11 RelaxedPrecision
638 OpDecorate %21 RelaxedPrecision
639 OpDecorate %21 DescriptorSet 0
640 OpDecorate %21 Binding 1
641 OpDecorate %22 RelaxedPrecision
642 OpDecorate %34 RelaxedPrecision
643 OpDecorate %40 RelaxedPrecision
644 OpDecorate %42 RelaxedPrecision
645 OpDecorate %43 RelaxedPrecision
646 %2 = OpTypeVoid
647 %3 = OpTypeFunction %2
648 %6 = OpTypeFloat 32
649 %7 = OpTypeImage %6 2D 0 0 0 1 Unknown
650 %8 = OpTypeSampledImage %7
651 %9 = OpTypePointer UniformConstant %8
652 %10 = OpVariable %9 UniformConstant
653 %12 = OpTypeInt 32 1
654 %13 = OpConstant %12 2
655 %15 = OpTypeVector %12 2
656 %17 = OpTypeInt 32 0
657 %18 = OpConstant %17 0
658 %20 = OpTypePointer UniformConstant %7
659 %21 = OpVariable %20 UniformConstant
660 %23 = OpConstant %12 1
661 %25 = OpConstant %17 1
662 %27 = OpTypeBool
663 %31 = OpTypeVector %6 4
664 %32 = OpTypePointer Function %31
665 %35 = OpConstantComposite %15 %23 %23
666 %36 = OpConstant %12 3
667 %37 = OpConstant %12 4
668 %38 = OpConstantComposite %15 %36 %37
669 %4 = OpFunction %2 None %3
670 %5 = OpLabel
671 %33 = OpVariable %32 Function
672 %11 = OpLoad %8 %10
673 %14 = OpImage %7 %11
674 %16 = OpImageQuerySizeLod %15 %14 %13
675 %19 = OpCompositeExtract %12 %16 0
676 %22 = OpLoad %7 %21
677 %24 = OpImageQuerySizeLod %15 %22 %23
678 %26 = OpCompositeExtract %12 %24 1
679 %28 = OpSGreaterThan %27 %19 %26
680 OpSelectionMerge %30 None
681 OpBranchConditional %28 %29 %41
682 %29 = OpLabel
683 %34 = OpLoad %8 %10
684 %39 = OpImage %7 %34
685 %40 = OpImageFetch %31 %39 %35 Lod|ConstOffset %13 %38
686 OpStore %33 %40
687 OpBranch %30
688 %41 = OpLabel
689 %42 = OpLoad %7 %21
690 %43 = OpImageFetch %31 %42 %35 Lod|ConstOffset %13 %38
691 OpStore %33 %43
692 OpBranch %30
693 %30 = OpLabel
694 OpReturn
695 OpFunctionEnd
696 )";
697
698 const auto env = SPV_ENV_UNIVERSAL_1_3;
699 const auto consumer = nullptr;
700 spvtools::ValidatorOptions validator_options;
701
702 const auto recipient_context =
703 BuildModule(env, consumer, recipient_shader, kFuzzAssembleOption);
704 ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(
705 recipient_context.get(), validator_options, kConsoleMessageConsumer));
706
707 const auto donor_context =
708 BuildModule(env, consumer, donor_shader, kFuzzAssembleOption);
709 ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(
710 donor_context.get(), validator_options, kConsoleMessageConsumer));
711
712 TransformationContext transformation_context(
713 MakeUnique<FactManager>(recipient_context.get()), validator_options);
714
715 FuzzerContext fuzzer_context(MakeUnique<PseudoRandomGenerator>(0), 100,
716 false);
717 protobufs::TransformationSequence transformation_sequence;
718
719 FuzzerPassDonateModules fuzzer_pass(recipient_context.get(),
720 &transformation_context, &fuzzer_context,
721 &transformation_sequence, false, {});
722
723 fuzzer_pass.DonateSingleModule(donor_context.get(), false);
724
725 // We just check that the result is valid. Checking to what it should be
726 // exactly equal to would be very fragile.
727 ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(
728 recipient_context.get(), validator_options, kConsoleMessageConsumer));
729 }
730
731 TEST(FuzzerPassDonateModulesTest, DonateCodeThatUsesSampler) {
732 std::string recipient_shader = R"(
733 OpCapability Shader
734 OpCapability ImageQuery
735 %1 = OpExtInstImport "GLSL.std.450"
736 OpMemoryModel Logical GLSL450
737 OpEntryPoint Fragment %4 "main"
738 OpExecutionMode %4 OriginUpperLeft
739 OpSource ESSL 320
740 OpSourceExtension "GL_EXT_samplerless_texture_functions"
741 %2 = OpTypeVoid
742 %3 = OpTypeFunction %2
743 %4 = OpFunction %2 None %3
744 %5 = OpLabel
745 OpReturn
746 OpFunctionEnd
747 )";
748
749 std::string donor_shader = R"(
750 OpCapability Shader
751 OpCapability ImageQuery
752 %1 = OpExtInstImport "GLSL.std.450"
753 OpMemoryModel Logical GLSL450
754 OpEntryPoint Fragment %4 "main"
755 OpExecutionMode %4 OriginUpperLeft
756 OpSource ESSL 320
757 OpDecorate %16 DescriptorSet 0
758 OpDecorate %16 Binding 0
759 OpDecorate %12 DescriptorSet 0
760 OpDecorate %12 Binding 64
761 %2 = OpTypeVoid
762 %3 = OpTypeFunction %2
763 %23 = OpTypeFloat 32
764 %6 = OpTypeImage %23 2D 2 0 0 1 Unknown
765 %47 = OpTypePointer UniformConstant %6
766 %12 = OpVariable %47 UniformConstant
767 %15 = OpTypeSampler
768 %55 = OpTypePointer UniformConstant %15
769 %17 = OpTypeSampledImage %6
770 %16 = OpVariable %55 UniformConstant
771 %37 = OpTypeVector %23 4
772 %109 = OpConstant %23 0
773 %66 = OpConstantComposite %37 %109 %109 %109 %109
774 %56 = OpTypeBool
775 %54 = OpConstantTrue %56
776 %4 = OpFunction %2 None %3
777 %5 = OpLabel
778 OpBranch %50
779 %50 = OpLabel
780 %51 = OpPhi %37 %66 %5 %111 %53
781 OpLoopMerge %52 %53 None
782 OpBranchConditional %54 %53 %52
783 %53 = OpLabel
784 %106 = OpLoad %6 %12
785 %107 = OpLoad %15 %16
786 %110 = OpSampledImage %17 %106 %107
787 %111 = OpImageSampleImplicitLod %37 %110 %66 Bias %109
788 OpBranch %50
789 %52 = OpLabel
790 OpReturn
791 OpFunctionEnd
792 )";
793
794 const auto env = SPV_ENV_UNIVERSAL_1_3;
795 const auto consumer = nullptr;
796 spvtools::ValidatorOptions validator_options;
797
798 const auto recipient_context =
799 BuildModule(env, consumer, recipient_shader, kFuzzAssembleOption);
800 ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(
801 recipient_context.get(), validator_options, kConsoleMessageConsumer));
802
803 const auto donor_context =
804 BuildModule(env, consumer, donor_shader, kFuzzAssembleOption);
805 ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(
806 donor_context.get(), validator_options, kConsoleMessageConsumer));
807
808 TransformationContext transformation_context(
809 MakeUnique<FactManager>(recipient_context.get()), validator_options);
810
811 FuzzerContext fuzzer_context(MakeUnique<PseudoRandomGenerator>(0), 100,
812 false);
813 protobufs::TransformationSequence transformation_sequence;
814
815 FuzzerPassDonateModules fuzzer_pass(recipient_context.get(),
816 &transformation_context, &fuzzer_context,
817 &transformation_sequence, false, {});
818
819 fuzzer_pass.DonateSingleModule(donor_context.get(), false);
820
821 // We just check that the result is valid. Checking to what it should be
822 // exactly equal to would be very fragile.
823 ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(
824 recipient_context.get(), validator_options, kConsoleMessageConsumer));
825 }
826
827 TEST(FuzzerPassDonateModulesTest, DonateCodeThatUsesImageStructField) {
828 std::string recipient_shader = R"(
829 OpCapability Shader
830 OpCapability ImageQuery
831 %1 = OpExtInstImport "GLSL.std.450"
832 OpMemoryModel Logical GLSL450
833 OpEntryPoint Fragment %4 "main"
834 OpExecutionMode %4 OriginUpperLeft
835 OpSource ESSL 320
836 OpSourceExtension "GL_EXT_samplerless_texture_functions"
837 %2 = OpTypeVoid
838 %3 = OpTypeFunction %2
839 %4 = OpFunction %2 None %3
840 %5 = OpLabel
841 OpReturn
842 OpFunctionEnd
843 )";
844
845 std::string donor_shader = R"(
846 OpCapability Shader
847 OpCapability ImageQuery
848 %1 = OpExtInstImport "GLSL.std.450"
849 OpMemoryModel Logical GLSL450
850 OpEntryPoint Fragment %4 "main"
851 OpExecutionMode %4 OriginUpperLeft
852 OpSource ESSL 320
853 OpSourceExtension "GL_EXT_samplerless_texture_functions"
854 OpName %4 "main"
855 OpName %10 "mySampler"
856 OpName %21 "myTexture"
857 OpName %33 "v"
858 OpDecorate %10 RelaxedPrecision
859 OpDecorate %10 DescriptorSet 0
860 OpDecorate %10 Binding 0
861 OpDecorate %11 RelaxedPrecision
862 OpDecorate %21 RelaxedPrecision
863 OpDecorate %21 DescriptorSet 0
864 OpDecorate %21 Binding 1
865 OpDecorate %22 RelaxedPrecision
866 OpDecorate %34 RelaxedPrecision
867 OpDecorate %40 RelaxedPrecision
868 OpDecorate %42 RelaxedPrecision
869 OpDecorate %43 RelaxedPrecision
870 %2 = OpTypeVoid
871 %3 = OpTypeFunction %2
872 %6 = OpTypeFloat 32
873 %7 = OpTypeImage %6 2D 0 0 0 1 Unknown
874 %8 = OpTypeSampledImage %7
875 %9 = OpTypePointer UniformConstant %8
876 %10 = OpVariable %9 UniformConstant
877 %12 = OpTypeInt 32 1
878 %13 = OpConstant %12 2
879 %15 = OpTypeVector %12 2
880 %17 = OpTypeInt 32 0
881 %18 = OpConstant %17 0
882 %20 = OpTypePointer UniformConstant %7
883 %21 = OpVariable %20 UniformConstant
884 %23 = OpConstant %12 1
885 %25 = OpConstant %17 1
886 %27 = OpTypeBool
887 %31 = OpTypeVector %6 4
888 %32 = OpTypePointer Function %31
889 %35 = OpConstantComposite %15 %23 %23
890 %36 = OpConstant %12 3
891 %37 = OpConstant %12 4
892 %38 = OpConstantComposite %15 %36 %37
893 %201 = OpTypeStruct %7 %7
894 %4 = OpFunction %2 None %3
895 %5 = OpLabel
896 %33 = OpVariable %32 Function
897 %11 = OpLoad %8 %10
898 %14 = OpImage %7 %11
899 %22 = OpLoad %7 %21
900 %200 = OpCompositeConstruct %201 %14 %22
901 %202 = OpCompositeExtract %7 %200 0
902 %203 = OpCompositeExtract %7 %200 1
903 %24 = OpImageQuerySizeLod %15 %203 %23
904 %16 = OpImageQuerySizeLod %15 %202 %13
905 %26 = OpCompositeExtract %12 %24 1
906 %19 = OpCompositeExtract %12 %16 0
907 %28 = OpSGreaterThan %27 %19 %26
908 OpSelectionMerge %30 None
909 OpBranchConditional %28 %29 %41
910 %29 = OpLabel
911 %34 = OpLoad %8 %10
912 %39 = OpImage %7 %34
913 %40 = OpImageFetch %31 %39 %35 Lod|ConstOffset %13 %38
914 OpStore %33 %40
915 OpBranch %30
916 %41 = OpLabel
917 %42 = OpLoad %7 %21
918 %43 = OpImageFetch %31 %42 %35 Lod|ConstOffset %13 %38
919 OpStore %33 %43
920 OpBranch %30
921 %30 = OpLabel
922 OpReturn
923 OpFunctionEnd
924 )";
925
926 const auto env = SPV_ENV_UNIVERSAL_1_3;
927 const auto consumer = nullptr;
928 spvtools::ValidatorOptions validator_options;
929
930 const auto recipient_context =
931 BuildModule(env, consumer, recipient_shader, kFuzzAssembleOption);
932 ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(
933 recipient_context.get(), validator_options, kConsoleMessageConsumer));
934
935 const auto donor_context =
936 BuildModule(env, consumer, donor_shader, kFuzzAssembleOption);
937 ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(
938 donor_context.get(), validator_options, kConsoleMessageConsumer));
939
940 TransformationContext transformation_context(
941 MakeUnique<FactManager>(recipient_context.get()), validator_options);
942
943 FuzzerContext fuzzer_context(MakeUnique<PseudoRandomGenerator>(0), 100,
944 false);
945 protobufs::TransformationSequence transformation_sequence;
946
947 FuzzerPassDonateModules fuzzer_pass(recipient_context.get(),
948 &transformation_context, &fuzzer_context,
949 &transformation_sequence, false, {});
950
951 fuzzer_pass.DonateSingleModule(donor_context.get(), false);
952
953 // We just check that the result is valid. Checking to what it should be
954 // exactly equal to would be very fragile.
955 ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(
956 recipient_context.get(), validator_options, kConsoleMessageConsumer));
957 }
958
959 TEST(FuzzerPassDonateModulesTest, DonateCodeThatUsesImageFunctionParameter) {
960 std::string recipient_shader = R"(
961 OpCapability Shader
962 OpCapability ImageQuery
963 %1 = OpExtInstImport "GLSL.std.450"
964 OpMemoryModel Logical GLSL450
965 OpEntryPoint Fragment %4 "main"
966 OpExecutionMode %4 OriginUpperLeft
967 OpSource ESSL 320
968 OpSourceExtension "GL_EXT_samplerless_texture_functions"
969 %2 = OpTypeVoid
970 %3 = OpTypeFunction %2
971 %4 = OpFunction %2 None %3
972 %5 = OpLabel
973 OpReturn
974 OpFunctionEnd
975 )";
976
977 std::string donor_shader = R"(
978 OpCapability Shader
979 OpCapability ImageQuery
980 %1 = OpExtInstImport "GLSL.std.450"
981 OpMemoryModel Logical GLSL450
982 OpEntryPoint Fragment %4 "main"
983 OpExecutionMode %4 OriginUpperLeft
984 OpSource ESSL 320
985 OpSourceExtension "GL_EXT_samplerless_texture_functions"
986 OpName %4 "main"
987 OpName %10 "mySampler"
988 OpName %21 "myTexture"
989 OpName %33 "v"
990 OpDecorate %10 RelaxedPrecision
991 OpDecorate %10 DescriptorSet 0
992 OpDecorate %10 Binding 0
993 OpDecorate %11 RelaxedPrecision
994 OpDecorate %21 RelaxedPrecision
995 OpDecorate %21 DescriptorSet 0
996 OpDecorate %21 Binding 1
997 OpDecorate %22 RelaxedPrecision
998 OpDecorate %34 RelaxedPrecision
999 OpDecorate %40 RelaxedPrecision
1000 OpDecorate %42 RelaxedPrecision
1001 OpDecorate %43 RelaxedPrecision
1002 %2 = OpTypeVoid
1003 %3 = OpTypeFunction %2
1004 %6 = OpTypeFloat 32
1005 %7 = OpTypeImage %6 2D 0 0 0 1 Unknown
1006 %8 = OpTypeSampledImage %7
1007 %9 = OpTypePointer UniformConstant %8
1008 %10 = OpVariable %9 UniformConstant
1009 %12 = OpTypeInt 32 1
1010 %13 = OpConstant %12 2
1011 %15 = OpTypeVector %12 2
1012 %17 = OpTypeInt 32 0
1013 %18 = OpConstant %17 0
1014 %20 = OpTypePointer UniformConstant %7
1015 %21 = OpVariable %20 UniformConstant
1016 %23 = OpConstant %12 1
1017 %25 = OpConstant %17 1
1018 %27 = OpTypeBool
1019 %31 = OpTypeVector %6 4
1020 %32 = OpTypePointer Function %31
1021 %35 = OpConstantComposite %15 %23 %23
1022 %36 = OpConstant %12 3
1023 %37 = OpConstant %12 4
1024 %38 = OpConstantComposite %15 %36 %37
1025 %201 = OpTypeFunction %15 %7 %12
1026 %4 = OpFunction %2 None %3
1027 %5 = OpLabel
1028 %33 = OpVariable %32 Function
1029 %11 = OpLoad %8 %10
1030 %14 = OpImage %7 %11
1031 %16 = OpFunctionCall %15 %200 %14 %13
1032 %19 = OpCompositeExtract %12 %16 0
1033 %22 = OpLoad %7 %21
1034 %24 = OpImageQuerySizeLod %15 %22 %23
1035 %26 = OpCompositeExtract %12 %24 1
1036 %28 = OpSGreaterThan %27 %19 %26
1037 OpSelectionMerge %30 None
1038 OpBranchConditional %28 %29 %41
1039 %29 = OpLabel
1040 %34 = OpLoad %8 %10
1041 %39 = OpImage %7 %34
1042 %40 = OpImageFetch %31 %39 %35 Lod|ConstOffset %13 %38
1043 OpStore %33 %40
1044 OpBranch %30
1045 %41 = OpLabel
1046 %42 = OpLoad %7 %21
1047 %43 = OpImageFetch %31 %42 %35 Lod|ConstOffset %13 %38
1048 OpStore %33 %43
1049 OpBranch %30
1050 %30 = OpLabel
1051 OpReturn
1052 OpFunctionEnd
1053 %200 = OpFunction %15 None %201
1054 %202 = OpFunctionParameter %7
1055 %203 = OpFunctionParameter %12
1056 %204 = OpLabel
1057 %205 = OpImageQuerySizeLod %15 %202 %203
1058 OpReturnValue %205
1059 OpFunctionEnd
1060 )";
1061
1062 const auto env = SPV_ENV_UNIVERSAL_1_3;
1063 const auto consumer = nullptr;
1064 spvtools::ValidatorOptions validator_options;
1065
1066 const auto recipient_context =
1067 BuildModule(env, consumer, recipient_shader, kFuzzAssembleOption);
1068 ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(
1069 recipient_context.get(), validator_options, kConsoleMessageConsumer));
1070
1071 const auto donor_context =
1072 BuildModule(env, consumer, donor_shader, kFuzzAssembleOption);
1073 ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(
1074 donor_context.get(), validator_options, kConsoleMessageConsumer));
1075
1076 TransformationContext transformation_context(
1077 MakeUnique<FactManager>(recipient_context.get()), validator_options);
1078
1079 FuzzerContext fuzzer_context(MakeUnique<PseudoRandomGenerator>(0), 100,
1080 false);
1081 protobufs::TransformationSequence transformation_sequence;
1082
1083 FuzzerPassDonateModules fuzzer_pass(recipient_context.get(),
1084 &transformation_context, &fuzzer_context,
1085 &transformation_sequence, false, {});
1086
1087 fuzzer_pass.DonateSingleModule(donor_context.get(), false);
1088
1089 // We just check that the result is valid. Checking to what it should be
1090 // exactly equal to would be very fragile.
1091 ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(
1092 recipient_context.get(), validator_options, kConsoleMessageConsumer));
1093 }
1094
1095 TEST(FuzzerPassDonateModulesTest, DonateShaderWithImageStorageClass) {
1096 std::string recipient_shader = R"(
1097 OpCapability Shader
1098 OpCapability ImageQuery
1099 %1 = OpExtInstImport "GLSL.std.450"
1100 OpMemoryModel Logical GLSL450
1101 OpEntryPoint Fragment %4 "main"
1102 OpExecutionMode %4 OriginUpperLeft
1103 OpSource ESSL 320
1104 OpSourceExtension "GL_EXT_samplerless_texture_functions"
1105 %2 = OpTypeVoid
1106 %3 = OpTypeFunction %2
1107 %4 = OpFunction %2 None %3
1108 %5 = OpLabel
1109 OpReturn
1110 OpFunctionEnd
1111 )";
1112
1113 std::string donor_shader = R"(
1114 OpCapability Shader
1115 OpCapability SampledBuffer
1116 OpCapability ImageBuffer
1117 %1 = OpExtInstImport "GLSL.std.450"
1118 OpMemoryModel Logical GLSL450
1119 OpEntryPoint Fragment %2 "MainPSPacked"
1120 OpExecutionMode %2 OriginUpperLeft
1121 OpDecorate %18 DescriptorSet 0
1122 OpDecorate %18 Binding 128
1123 %49 = OpTypeInt 32 0
1124 %50 = OpTypeFloat 32
1125 %58 = OpConstant %50 1
1126 %66 = OpConstant %49 0
1127 %87 = OpTypeVector %50 2
1128 %88 = OpConstantComposite %87 %58 %58
1129 %17 = OpTypeImage %49 2D 2 0 0 2 R32ui
1130 %118 = OpTypePointer UniformConstant %17
1131 %123 = OpTypeVector %49 2
1132 %132 = OpTypeVoid
1133 %133 = OpTypeFunction %132
1134 %142 = OpTypePointer Image %49
1135 %18 = OpVariable %118 UniformConstant
1136 %2 = OpFunction %132 None %133
1137 %153 = OpLabel
1138 %495 = OpConvertFToU %123 %88
1139 %501 = OpImageTexelPointer %142 %18 %495 %66
1140 OpReturn
1141 OpFunctionEnd
1142 )";
1143
1144 const auto env = SPV_ENV_UNIVERSAL_1_3;
1145 const auto consumer = nullptr;
1146 spvtools::ValidatorOptions validator_options;
1147
1148 const auto recipient_context =
1149 BuildModule(env, consumer, recipient_shader, kFuzzAssembleOption);
1150 ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(
1151 recipient_context.get(), validator_options, kConsoleMessageConsumer));
1152
1153 const auto donor_context =
1154 BuildModule(env, consumer, donor_shader, kFuzzAssembleOption);
1155 ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(
1156 donor_context.get(), validator_options, kConsoleMessageConsumer));
1157
1158 TransformationContext transformation_context(
1159 MakeUnique<FactManager>(recipient_context.get()), validator_options);
1160
1161 FuzzerContext fuzzer_context(MakeUnique<PseudoRandomGenerator>(0), 100,
1162 false);
1163 protobufs::TransformationSequence transformation_sequence;
1164
1165 FuzzerPassDonateModules fuzzer_pass(recipient_context.get(),
1166 &transformation_context, &fuzzer_context,
1167 &transformation_sequence, false, {});
1168
1169 fuzzer_pass.DonateSingleModule(donor_context.get(), true);
1170
1171 // We just check that the result is valid. Checking to what it should be
1172 // exactly equal to would be very fragile.
1173 ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(
1174 recipient_context.get(), validator_options, kConsoleMessageConsumer));
1175 }
1176
1177 TEST(FuzzerPassDonateModulesTest, DonateComputeShaderWithRuntimeArray) {
1178 std::string recipient_shader = R"(
1179 OpCapability Shader
1180 %1 = OpExtInstImport "GLSL.std.450"
1181 OpMemoryModel Logical GLSL450
1182 OpEntryPoint GLCompute %4 "main"
1183 OpExecutionMode %4 LocalSize 1 1 1
1184 OpSource ESSL 310
1185 %2 = OpTypeVoid
1186 %3 = OpTypeFunction %2
1187 %4 = OpFunction %2 None %3
1188 %5 = OpLabel
1189 OpReturn
1190 OpFunctionEnd
1191 )";
1192
1193 std::string donor_shader = R"(
1194 OpCapability Shader
1195 %1 = OpExtInstImport "GLSL.std.450"
1196 OpMemoryModel Logical GLSL450
1197 OpEntryPoint GLCompute %4 "main"
1198 OpExecutionMode %4 LocalSize 1 1 1
1199 OpSource ESSL 310
1200 OpDecorate %9 ArrayStride 4
1201 OpMemberDecorate %10 0 Offset 0
1202 OpDecorate %10 BufferBlock
1203 OpDecorate %12 DescriptorSet 0
1204 OpDecorate %12 Binding 0
1205 %2 = OpTypeVoid
1206 %3 = OpTypeFunction %2
1207 %6 = OpTypeInt 32 1
1208 %7 = OpTypePointer Function %6
1209 %9 = OpTypeRuntimeArray %6
1210 %10 = OpTypeStruct %9
1211 %11 = OpTypePointer Uniform %10
1212 %12 = OpVariable %11 Uniform
1213 %13 = OpTypeInt 32 0
1214 %16 = OpConstant %6 0
1215 %18 = OpConstant %6 1
1216 %20 = OpTypePointer Uniform %6
1217 %4 = OpFunction %2 None %3
1218 %5 = OpLabel
1219 %8 = OpVariable %7 Function
1220 %14 = OpArrayLength %13 %12 0
1221 %15 = OpBitcast %6 %14
1222 OpStore %8 %15
1223 %17 = OpLoad %6 %8
1224 %19 = OpISub %6 %17 %18
1225 %21 = OpAccessChain %20 %12 %16 %19
1226 OpStore %21 %16
1227 OpReturn
1228 OpFunctionEnd
1229 )";
1230
1231 const auto env = SPV_ENV_UNIVERSAL_1_3;
1232 const auto consumer = nullptr;
1233 spvtools::ValidatorOptions validator_options;
1234
1235 const auto recipient_context =
1236 BuildModule(env, consumer, recipient_shader, kFuzzAssembleOption);
1237 ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(
1238 recipient_context.get(), validator_options, kConsoleMessageConsumer));
1239
1240 const auto donor_context =
1241 BuildModule(env, consumer, donor_shader, kFuzzAssembleOption);
1242 ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(
1243 donor_context.get(), validator_options, kConsoleMessageConsumer));
1244
1245 TransformationContext transformation_context(
1246 MakeUnique<FactManager>(recipient_context.get()), validator_options);
1247
1248 FuzzerContext fuzzer_context(MakeUnique<PseudoRandomGenerator>(0), 100,
1249 false);
1250 protobufs::TransformationSequence transformation_sequence;
1251
1252 FuzzerPassDonateModules fuzzer_pass(recipient_context.get(),
1253 &transformation_context, &fuzzer_context,
1254 &transformation_sequence, false, {});
1255
1256 fuzzer_pass.DonateSingleModule(donor_context.get(), false);
1257
1258 // We just check that the result is valid. Checking to what it should be
1259 // exactly equal to would be very fragile.
1260 ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(
1261 recipient_context.get(), validator_options, kConsoleMessageConsumer));
1262 }
1263
1264 TEST(FuzzerPassDonateModulesTest, DonateComputeShaderWithRuntimeArrayLivesafe) {
1265 std::string recipient_shader = R"(
1266 OpCapability Shader
1267 %1 = OpExtInstImport "GLSL.std.450"
1268 OpMemoryModel Logical GLSL450
1269 OpEntryPoint GLCompute %4 "main"
1270 OpExecutionMode %4 LocalSize 1 1 1
1271 OpSource ESSL 310
1272 %2 = OpTypeVoid
1273 %3 = OpTypeFunction %2
1274 %4 = OpFunction %2 None %3
1275 %5 = OpLabel
1276 OpReturn
1277 OpFunctionEnd
1278 )";
1279
1280 std::string donor_shader = R"(
1281 OpCapability Shader
1282 %1 = OpExtInstImport "GLSL.std.450"
1283 OpMemoryModel Logical GLSL450
1284 OpEntryPoint GLCompute %4 "main"
1285 OpExecutionMode %4 LocalSize 1 1 1
1286 OpSource ESSL 310
1287 OpDecorate %16 ArrayStride 4
1288 OpMemberDecorate %17 0 Offset 0
1289 OpDecorate %17 BufferBlock
1290 OpDecorate %19 DescriptorSet 0
1291 OpDecorate %19 Binding 0
1292 %2 = OpTypeVoid
1293 %3 = OpTypeFunction %2
1294 %6 = OpTypeInt 32 1
1295 %7 = OpTypePointer Function %6
1296 %9 = OpConstant %6 0
1297 %16 = OpTypeRuntimeArray %6
1298 %17 = OpTypeStruct %16
1299 %18 = OpTypePointer Uniform %17
1300 %19 = OpVariable %18 Uniform
1301 %20 = OpTypeInt 32 0
1302 %23 = OpTypeBool
1303 %26 = OpConstant %6 32
1304 %27 = OpTypePointer Uniform %6
1305 %30 = OpConstant %6 1
1306 %4 = OpFunction %2 None %3
1307 %5 = OpLabel
1308 %8 = OpVariable %7 Function
1309 OpStore %8 %9
1310 OpBranch %10
1311 %10 = OpLabel
1312 OpLoopMerge %12 %13 None
1313 OpBranch %14
1314 %14 = OpLabel
1315 %15 = OpLoad %6 %8
1316 %21 = OpArrayLength %20 %19 0
1317 %22 = OpBitcast %6 %21
1318 %24 = OpSLessThan %23 %15 %22
1319 OpBranchConditional %24 %11 %12
1320 %11 = OpLabel
1321 %25 = OpLoad %6 %8
1322 %28 = OpAccessChain %27 %19 %9 %25
1323 OpStore %28 %26
1324 OpBranch %13
1325 %13 = OpLabel
1326 %29 = OpLoad %6 %8
1327 %31 = OpIAdd %6 %29 %30
1328 OpStore %8 %31
1329 OpBranch %10
1330 %12 = OpLabel
1331 OpReturn
1332 OpFunctionEnd
1333 )";
1334
1335 const auto env = SPV_ENV_UNIVERSAL_1_3;
1336 const auto consumer = nullptr;
1337 spvtools::ValidatorOptions validator_options;
1338
1339 const auto recipient_context =
1340 BuildModule(env, consumer, recipient_shader, kFuzzAssembleOption);
1341 ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(
1342 recipient_context.get(), validator_options, kConsoleMessageConsumer));
1343
1344 const auto donor_context =
1345 BuildModule(env, consumer, donor_shader, kFuzzAssembleOption);
1346 ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(
1347 donor_context.get(), validator_options, kConsoleMessageConsumer));
1348
1349 TransformationContext transformation_context(
1350 MakeUnique<FactManager>(recipient_context.get()), validator_options);
1351
1352 FuzzerContext fuzzer_context(MakeUnique<PseudoRandomGenerator>(0), 100,
1353 false);
1354 protobufs::TransformationSequence transformation_sequence;
1355
1356 FuzzerPassDonateModules fuzzer_pass(recipient_context.get(),
1357 &transformation_context, &fuzzer_context,
1358 &transformation_sequence, false, {});
1359
1360 fuzzer_pass.DonateSingleModule(donor_context.get(), true);
1361
1362 // We just check that the result is valid. Checking to what it should be
1363 // exactly equal to would be very fragile.
1364 ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(
1365 recipient_context.get(), validator_options, kConsoleMessageConsumer));
1366 }
1367
1368 TEST(FuzzerPassDonateModulesTest, DonateComputeShaderWithWorkgroupVariables) {
1369 std::string recipient_shader = R"(
1370 OpCapability Shader
1371 %1 = OpExtInstImport "GLSL.std.450"
1372 OpMemoryModel Logical GLSL450
1373 OpEntryPoint GLCompute %4 "main"
1374 OpExecutionMode %4 LocalSize 1 1 1
1375 OpSource ESSL 310
1376 %2 = OpTypeVoid
1377 %3 = OpTypeFunction %2
1378 %4 = OpFunction %2 None %3
1379 %5 = OpLabel
1380 OpReturn
1381 OpFunctionEnd
1382 )";
1383
1384 std::string donor_shader = R"(
1385 OpCapability Shader
1386 %1 = OpExtInstImport "GLSL.std.450"
1387 OpMemoryModel Logical GLSL450
1388 OpEntryPoint GLCompute %4 "main"
1389 OpExecutionMode %4 LocalSize 1 1 1
1390 OpSource ESSL 310
1391 %2 = OpTypeVoid
1392 %3 = OpTypeFunction %2
1393 %6 = OpTypeInt 32 1
1394 %7 = OpTypePointer Workgroup %6
1395 %8 = OpVariable %7 Workgroup
1396 %9 = OpConstant %6 2
1397 %10 = OpVariable %7 Workgroup
1398 %4 = OpFunction %2 None %3
1399 %5 = OpLabel
1400 OpStore %8 %9
1401 %11 = OpLoad %6 %8
1402 OpStore %10 %11
1403 OpReturn
1404 OpFunctionEnd
1405 )";
1406
1407 const auto env = SPV_ENV_UNIVERSAL_1_3;
1408 const auto consumer = nullptr;
1409 spvtools::ValidatorOptions validator_options;
1410
1411 const auto recipient_context =
1412 BuildModule(env, consumer, recipient_shader, kFuzzAssembleOption);
1413 ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(
1414 recipient_context.get(), validator_options, kConsoleMessageConsumer));
1415
1416 const auto donor_context =
1417 BuildModule(env, consumer, donor_shader, kFuzzAssembleOption);
1418 ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(
1419 donor_context.get(), validator_options, kConsoleMessageConsumer));
1420
1421 TransformationContext transformation_context(
1422 MakeUnique<FactManager>(recipient_context.get()), validator_options);
1423
1424 FuzzerContext fuzzer_context(MakeUnique<PseudoRandomGenerator>(0), 100,
1425 false);
1426 protobufs::TransformationSequence transformation_sequence;
1427
1428 FuzzerPassDonateModules fuzzer_pass(recipient_context.get(),
1429 &transformation_context, &fuzzer_context,
1430 &transformation_sequence, false, {});
1431
1432 fuzzer_pass.DonateSingleModule(donor_context.get(), true);
1433
1434 // We just check that the result is valid. Checking to what it should be
1435 // exactly equal to would be very fragile.
1436 ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(
1437 recipient_context.get(), validator_options, kConsoleMessageConsumer));
1438 }
1439
1440 TEST(FuzzerPassDonateModulesTest, DonateComputeShaderWithAtomics) {
1441 std::string recipient_shader = R"(
1442 OpCapability Shader
1443 %1 = OpExtInstImport "GLSL.std.450"
1444 OpMemoryModel Logical GLSL450
1445 OpEntryPoint GLCompute %4 "main"
1446 OpExecutionMode %4 LocalSize 1 1 1
1447 OpSource ESSL 310
1448 %2 = OpTypeVoid
1449 %3 = OpTypeFunction %2
1450 %4 = OpFunction %2 None %3
1451 %5 = OpLabel
1452 OpReturn
1453 OpFunctionEnd
1454 )";
1455
1456 std::string donor_shader = R"(
1457 OpCapability Shader
1458 %1 = OpExtInstImport "GLSL.std.450"
1459 OpMemoryModel Logical GLSL450
1460 OpEntryPoint GLCompute %4 "main"
1461 OpExecutionMode %4 LocalSize 1 1 1
1462 OpSource ESSL 310
1463 OpMemberDecorate %9 0 Offset 0
1464 OpDecorate %9 BufferBlock
1465 OpDecorate %11 DescriptorSet 0
1466 OpDecorate %11 Binding 0
1467 %2 = OpTypeVoid
1468 %3 = OpTypeFunction %2
1469 %6 = OpTypeInt 32 0
1470 %7 = OpTypePointer Function %6
1471 %9 = OpTypeStruct %6
1472 %10 = OpTypePointer Uniform %9
1473 %11 = OpVariable %10 Uniform
1474 %12 = OpTypeInt 32 1
1475 %13 = OpConstant %12 0
1476 %14 = OpTypePointer Uniform %6
1477 %16 = OpConstant %6 1
1478 %17 = OpConstant %6 0
1479 %4 = OpFunction %2 None %3
1480 %5 = OpLabel
1481 %8 = OpVariable %7 Function
1482 %15 = OpAccessChain %14 %11 %13
1483 %18 = OpAtomicIAdd %6 %15 %16 %17 %16
1484 OpStore %8 %18
1485 %19 = OpAccessChain %14 %11 %13
1486 %20 = OpLoad %6 %8
1487 %21 = OpAtomicUMin %6 %19 %16 %17 %20
1488 OpStore %8 %21
1489 %22 = OpAccessChain %14 %11 %13
1490 %23 = OpLoad %6 %8
1491 %24 = OpAtomicUMax %6 %22 %16 %17 %23
1492 OpStore %8 %24
1493 %25 = OpAccessChain %14 %11 %13
1494 %26 = OpLoad %6 %8
1495 %27 = OpAtomicAnd %6 %25 %16 %17 %26
1496 OpStore %8 %27
1497 %28 = OpAccessChain %14 %11 %13
1498 %29 = OpLoad %6 %8
1499 %30 = OpAtomicOr %6 %28 %16 %17 %29
1500 OpStore %8 %30
1501 %31 = OpAccessChain %14 %11 %13
1502 %32 = OpLoad %6 %8
1503 %33 = OpAtomicXor %6 %31 %16 %17 %32
1504 OpStore %8 %33
1505 %34 = OpAccessChain %14 %11 %13
1506 %35 = OpLoad %6 %8
1507 %36 = OpAtomicExchange %6 %34 %16 %17 %35
1508 OpStore %8 %36
1509 %37 = OpAccessChain %14 %11 %13
1510 %38 = OpLoad %6 %8
1511 %39 = OpAtomicCompareExchange %6 %37 %16 %17 %17 %16 %38
1512 OpStore %8 %39
1513 OpReturn
1514 OpFunctionEnd
1515 )";
1516
1517 const auto env = SPV_ENV_UNIVERSAL_1_3;
1518 const auto consumer = nullptr;
1519 spvtools::ValidatorOptions validator_options;
1520
1521 const auto recipient_context =
1522 BuildModule(env, consumer, recipient_shader, kFuzzAssembleOption);
1523 ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(
1524 recipient_context.get(), validator_options, kConsoleMessageConsumer));
1525
1526 const auto donor_context =
1527 BuildModule(env, consumer, donor_shader, kFuzzAssembleOption);
1528 ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(
1529 donor_context.get(), validator_options, kConsoleMessageConsumer));
1530
1531 TransformationContext transformation_context(
1532 MakeUnique<FactManager>(recipient_context.get()), validator_options);
1533
1534 FuzzerContext fuzzer_context(MakeUnique<PseudoRandomGenerator>(0), 100,
1535 false);
1536 protobufs::TransformationSequence transformation_sequence;
1537
1538 FuzzerPassDonateModules fuzzer_pass(recipient_context.get(),
1539 &transformation_context, &fuzzer_context,
1540 &transformation_sequence, false, {});
1541
1542 fuzzer_pass.DonateSingleModule(donor_context.get(), true);
1543
1544 // We just check that the result is valid. Checking to what it should be
1545 // exactly equal to would be very fragile.
1546 ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(
1547 recipient_context.get(), validator_options, kConsoleMessageConsumer));
1548 }
1549
1550 TEST(FuzzerPassDonateModulesTest, Miscellaneous1) {
1551 std::string recipient_shader = R"(
1552 OpCapability Shader
1553 %1 = OpExtInstImport "GLSL.std.450"
1554 OpMemoryModel Logical GLSL450
1555 OpEntryPoint Fragment %4 "main"
1556 OpExecutionMode %4 OriginUpperLeft
1557 OpSource ESSL 310
1558 %2 = OpTypeVoid
1559 %3 = OpTypeFunction %2
1560 %4 = OpFunction %2 None %3
1561 %5 = OpLabel
1562 OpReturn
1563 OpFunctionEnd
1564 )";
1565
1566 std::string donor_shader = R"(
1567 OpCapability Shader
1568 %1 = OpExtInstImport "GLSL.std.450"
1569 OpMemoryModel Logical GLSL450
1570 OpEntryPoint Fragment %4 "main"
1571 OpExecutionMode %4 OriginUpperLeft
1572 OpSource ESSL 310
1573 OpName %4 "main"
1574 OpName %6 "foo("
1575 OpName %10 "x"
1576 OpName %12 "i"
1577 OpName %33 "i"
1578 OpName %42 "j"
1579 OpDecorate %10 RelaxedPrecision
1580 OpDecorate %12 RelaxedPrecision
1581 OpDecorate %19 RelaxedPrecision
1582 OpDecorate %23 RelaxedPrecision
1583 OpDecorate %24 RelaxedPrecision
1584 OpDecorate %25 RelaxedPrecision
1585 OpDecorate %26 RelaxedPrecision
1586 OpDecorate %27 RelaxedPrecision
1587 OpDecorate %28 RelaxedPrecision
1588 OpDecorate %30 RelaxedPrecision
1589 OpDecorate %33 RelaxedPrecision
1590 OpDecorate %39 RelaxedPrecision
1591 OpDecorate %42 RelaxedPrecision
1592 OpDecorate %49 RelaxedPrecision
1593 OpDecorate %52 RelaxedPrecision
1594 OpDecorate %53 RelaxedPrecision
1595 OpDecorate %58 RelaxedPrecision
1596 OpDecorate %59 RelaxedPrecision
1597 OpDecorate %60 RelaxedPrecision
1598 OpDecorate %63 RelaxedPrecision
1599 OpDecorate %64 RelaxedPrecision
1600 %2 = OpTypeVoid
1601 %3 = OpTypeFunction %2
1602 %8 = OpTypeInt 32 1
1603 %9 = OpTypePointer Function %8
1604 %11 = OpConstant %8 2
1605 %13 = OpConstant %8 0
1606 %20 = OpConstant %8 100
1607 %21 = OpTypeBool
1608 %29 = OpConstant %8 1
1609 %40 = OpConstant %8 10
1610 %43 = OpConstant %8 20
1611 %61 = OpConstant %8 4
1612 %4 = OpFunction %2 None %3
1613 %5 = OpLabel
1614 %33 = OpVariable %9 Function
1615 %42 = OpVariable %9 Function
1616 %32 = OpFunctionCall %2 %6
1617 OpStore %33 %13
1618 OpBranch %34
1619 %34 = OpLabel
1620 OpLoopMerge %36 %37 None
1621 OpBranch %38
1622 %38 = OpLabel
1623 %39 = OpLoad %8 %33
1624 %41 = OpSLessThan %21 %39 %40
1625 OpBranchConditional %41 %35 %36
1626 %35 = OpLabel
1627 OpStore %42 %43
1628 OpBranch %44
1629 %44 = OpLabel
1630 OpLoopMerge %46 %47 None
1631 OpBranch %48
1632 %48 = OpLabel
1633 %49 = OpLoad %8 %42
1634 %50 = OpSGreaterThan %21 %49 %13
1635 OpBranchConditional %50 %45 %46
1636 %45 = OpLabel
1637 %51 = OpFunctionCall %2 %6
1638 %52 = OpLoad %8 %42
1639 %53 = OpISub %8 %52 %29
1640 OpStore %42 %53
1641 OpBranch %47
1642 %47 = OpLabel
1643 OpBranch %44
1644 %46 = OpLabel
1645 OpBranch %54
1646 %54 = OpLabel
1647 OpLoopMerge %56 %57 None
1648 OpBranch %55
1649 %55 = OpLabel
1650 %58 = OpLoad %8 %33
1651 %59 = OpIAdd %8 %58 %29
1652 OpStore %33 %59
1653 OpBranch %57
1654 %57 = OpLabel
1655 %60 = OpLoad %8 %33
1656 %62 = OpSLessThan %21 %60 %61
1657 OpBranchConditional %62 %54 %56
1658 %56 = OpLabel
1659 OpBranch %37
1660 %37 = OpLabel
1661 %63 = OpLoad %8 %33
1662 %64 = OpIAdd %8 %63 %29
1663 OpStore %33 %64
1664 OpBranch %34
1665 %36 = OpLabel
1666 OpReturn
1667 OpFunctionEnd
1668 %6 = OpFunction %2 None %3
1669 %7 = OpLabel
1670 %10 = OpVariable %9 Function
1671 %12 = OpVariable %9 Function
1672 OpStore %10 %11
1673 OpStore %12 %13
1674 OpBranch %14
1675 %14 = OpLabel
1676 OpLoopMerge %16 %17 None
1677 OpBranch %18
1678 %18 = OpLabel
1679 %19 = OpLoad %8 %12
1680 %22 = OpSLessThan %21 %19 %20
1681 OpBranchConditional %22 %15 %16
1682 %15 = OpLabel
1683 %23 = OpLoad %8 %12
1684 %24 = OpLoad %8 %10
1685 %25 = OpIAdd %8 %24 %23
1686 OpStore %10 %25
1687 %26 = OpLoad %8 %10
1688 %27 = OpIMul %8 %26 %11
1689 OpStore %10 %27
1690 OpBranch %17
1691 %17 = OpLabel
1692 %28 = OpLoad %8 %12
1693 %30 = OpIAdd %8 %28 %29
1694 OpStore %12 %30
1695 OpBranch %14
1696 %16 = OpLabel
1697 OpReturn
1698 OpFunctionEnd
1699 )";
1700
1701 const auto env = SPV_ENV_UNIVERSAL_1_5;
1702 const auto consumer = nullptr;
1703 spvtools::ValidatorOptions validator_options;
1704
1705 const auto recipient_context =
1706 BuildModule(env, consumer, recipient_shader, kFuzzAssembleOption);
1707 ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(
1708 recipient_context.get(), validator_options, kConsoleMessageConsumer));
1709
1710 const auto donor_context =
1711 BuildModule(env, consumer, donor_shader, kFuzzAssembleOption);
1712 ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(
1713 donor_context.get(), validator_options, kConsoleMessageConsumer));
1714
1715 TransformationContext transformation_context(
1716 MakeUnique<FactManager>(recipient_context.get()), validator_options);
1717
1718 FuzzerContext fuzzer_context(MakeUnique<PseudoRandomGenerator>(0), 100,
1719 false);
1720 protobufs::TransformationSequence transformation_sequence;
1721
1722 FuzzerPassDonateModules fuzzer_pass(recipient_context.get(),
1723 &transformation_context, &fuzzer_context,
1724 &transformation_sequence, false, {});
1725
1726 fuzzer_pass.DonateSingleModule(donor_context.get(), false);
1727
1728 // We just check that the result is valid. Checking to what it should be
1729 // exactly equal to would be very fragile.
1730 ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(
1731 recipient_context.get(), validator_options, kConsoleMessageConsumer));
1732 }
1733
1734 TEST(FuzzerPassDonateModulesTest, OpSpecConstantInstructions) {
1735 std::string donor_shader = R"(
1736 OpCapability Shader
1737 %1 = OpExtInstImport "GLSL.std.450"
1738 OpMemoryModel Logical GLSL450
1739 OpEntryPoint Fragment %4 "main"
1740 OpExecutionMode %4 OriginUpperLeft
1741 OpSource ESSL 310
1742 %2 = OpTypeVoid
1743 %3 = OpTypeFunction %2
1744 %6 = OpTypeBool
1745 %7 = OpTypeInt 32 1
1746 %8 = OpTypeStruct %6 %6 %7
1747 %9 = OpSpecConstantTrue %6
1748 %10 = OpSpecConstantFalse %6
1749 %11 = OpSpecConstant %7 2
1750 %12 = OpSpecConstantComposite %8 %9 %10 %11
1751 %13 = OpSpecConstantOp %6 LogicalEqual %9 %10
1752 %4 = OpFunction %2 None %3
1753 %5 = OpLabel
1754 OpReturn
1755 OpFunctionEnd
1756 )";
1757
1758 std::string recipient_shader = R"(
1759 OpCapability Shader
1760 %1 = OpExtInstImport "GLSL.std.450"
1761 OpMemoryModel Logical GLSL450
1762 OpEntryPoint Fragment %4 "main"
1763 OpExecutionMode %4 OriginUpperLeft
1764 OpSource ESSL 310
1765 %2 = OpTypeVoid
1766 %3 = OpTypeFunction %2
1767 %4 = OpFunction %2 None %3
1768 %5 = OpLabel
1769 OpReturn
1770 OpFunctionEnd
1771 )";
1772
1773 const auto env = SPV_ENV_UNIVERSAL_1_3;
1774 const auto consumer = nullptr;
1775 spvtools::ValidatorOptions validator_options;
1776
1777 const auto recipient_context =
1778 BuildModule(env, consumer, recipient_shader, kFuzzAssembleOption);
1779 ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(
1780 recipient_context.get(), validator_options, kConsoleMessageConsumer));
1781
1782 const auto donor_context =
1783 BuildModule(env, consumer, donor_shader, kFuzzAssembleOption);
1784 ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(
1785 donor_context.get(), validator_options, kConsoleMessageConsumer));
1786
1787 TransformationContext transformation_context(
1788 MakeUnique<FactManager>(recipient_context.get()), validator_options);
1789
1790 FuzzerContext fuzzer_context(MakeUnique<PseudoRandomGenerator>(0), 100,
1791 false);
1792 protobufs::TransformationSequence transformation_sequence;
1793
1794 FuzzerPassDonateModules fuzzer_pass(recipient_context.get(),
1795 &transformation_context, &fuzzer_context,
1796 &transformation_sequence, false, {});
1797
1798 fuzzer_pass.DonateSingleModule(donor_context.get(), false);
1799
1800 // Check that the module is valid first.
1801 ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(
1802 recipient_context.get(), validator_options, kConsoleMessageConsumer));
1803
1804 std::string expected_shader = R"(
1805 OpCapability Shader
1806 %1 = OpExtInstImport "GLSL.std.450"
1807 OpMemoryModel Logical GLSL450
1808 OpEntryPoint Fragment %4 "main"
1809 OpExecutionMode %4 OriginUpperLeft
1810 OpSource ESSL 310
1811 %2 = OpTypeVoid
1812 %3 = OpTypeFunction %2
1813 %100 = OpTypeBool
1814 %101 = OpTypeInt 32 1
1815 %102 = OpTypeStruct %100 %100 %101
1816 %103 = OpConstantTrue %100
1817 %104 = OpConstantFalse %100
1818 %105 = OpConstant %101 2
1819 %106 = OpConstantComposite %102 %103 %104 %105
1820 %107 = OpSpecConstantOp %100 LogicalEqual %103 %104
1821 %4 = OpFunction %2 None %3
1822 %5 = OpLabel
1823 OpReturn
1824 OpFunctionEnd
1825 %108 = OpFunction %2 None %3
1826 %109 = OpLabel
1827 OpReturn
1828 OpFunctionEnd
1829 )";
1830
1831 // Now check that the transformation has produced the expected result.
1832 ASSERT_TRUE(IsEqual(env, expected_shader, recipient_context.get()));
1833 }
1834
1835 TEST(FuzzerPassDonateModulesTest, DonationSupportsOpTypeRuntimeArray) {
1836 std::string donor_shader = R"(
1837 OpCapability Shader
1838 OpExtension "SPV_KHR_storage_buffer_storage_class"
1839 OpMemoryModel Logical GLSL450
1840 OpEntryPoint GLCompute %29 "kernel_1"
1841 OpEntryPoint GLCompute %37 "kernel_2"
1842 OpSource OpenCL_C 120
1843 OpDecorate %2 ArrayStride 4
1844 OpMemberDecorate %3 0 Offset 0
1845 OpDecorate %3 Block
1846 OpMemberDecorate %5 0 Offset 0
1847 OpMemberDecorate %6 0 Offset 0
1848 OpDecorate %6 Block
1849 OpDecorate %21 BuiltIn WorkgroupSize
1850 OpDecorate %23 DescriptorSet 0
1851 OpDecorate %23 Binding 0
1852 OpDecorate %25 SpecId 3
1853 OpDecorate %18 SpecId 0
1854 OpDecorate %19 SpecId 1
1855 OpDecorate %20 SpecId 2
1856 %1 = OpTypeInt 32 0
1857 %2 = OpTypeRuntimeArray %1
1858 %3 = OpTypeStruct %2
1859 %4 = OpTypePointer StorageBuffer %3
1860 %5 = OpTypeStruct %1
1861 %6 = OpTypeStruct %5
1862 %7 = OpTypePointer PushConstant %6
1863 %8 = OpTypeFloat 32
1864 %9 = OpTypeVoid
1865 %10 = OpTypeFunction %9
1866 %11 = OpTypePointer Workgroup %1
1867 %12 = OpTypePointer PushConstant %5
1868 %13 = OpTypePointer StorageBuffer %1
1869 %14 = OpTypeFunction %1 %1
1870 %15 = OpTypeVector %1 3
1871 %16 = OpTypePointer Private %15
1872 %17 = OpConstant %1 0
1873 %18 = OpSpecConstant %1 1
1874 %19 = OpSpecConstant %1 1
1875 %20 = OpSpecConstant %1 1
1876 %21 = OpSpecConstantComposite %15 %18 %19 %20
1877 %25 = OpSpecConstant %1 1
1878 %26 = OpTypeArray %1 %25
1879 %27 = OpTypePointer Workgroup %26
1880 %22 = OpVariable %16 Private %21
1881 %23 = OpVariable %4 StorageBuffer
1882 %24 = OpVariable %7 PushConstant
1883 %28 = OpVariable %27 Workgroup
1884 %29 = OpFunction %9 None %10
1885 %30 = OpLabel
1886 %31 = OpAccessChain %11 %28 %17
1887 %32 = OpAccessChain %12 %24 %17
1888 %33 = OpLoad %5 %32
1889 %34 = OpCompositeExtract %1 %33 0
1890 %35 = OpFunctionCall %1 %45 %34
1891 %36 = OpAccessChain %13 %23 %17 %34
1892 OpStore %36 %35
1893 OpReturn
1894 OpFunctionEnd
1895 %37 = OpFunction %9 None %10
1896 %38 = OpLabel
1897 %39 = OpAccessChain %11 %28 %17
1898 %40 = OpAccessChain %12 %24 %17
1899 %41 = OpLoad %5 %40
1900 %42 = OpCompositeExtract %1 %41 0
1901 %43 = OpFunctionCall %1 %45 %42
1902 %44 = OpAccessChain %13 %23 %17 %42
1903 OpStore %44 %43
1904 OpReturn
1905 OpFunctionEnd
1906 %45 = OpFunction %1 Pure %14
1907 %46 = OpFunctionParameter %1
1908 %47 = OpLabel
1909 %48 = OpAccessChain %11 %28 %46
1910 %49 = OpLoad %1 %48
1911 OpReturnValue %49
1912 OpFunctionEnd
1913 )";
1914
1915 std::string recipient_shader = R"(
1916 OpCapability Shader
1917 %1 = OpExtInstImport "GLSL.std.450"
1918 OpMemoryModel Logical GLSL450
1919 OpEntryPoint Fragment %4 "main"
1920 OpExecutionMode %4 OriginUpperLeft
1921 OpSource ESSL 310
1922 %2 = OpTypeVoid
1923 %3 = OpTypeFunction %2
1924 %4 = OpFunction %2 None %3
1925 %5 = OpLabel
1926 OpReturn
1927 OpFunctionEnd
1928 )";
1929
1930 const auto env = SPV_ENV_UNIVERSAL_1_0;
1931 const auto consumer = nullptr;
1932 spvtools::ValidatorOptions validator_options;
1933
1934 const auto recipient_context =
1935 BuildModule(env, consumer, recipient_shader, kFuzzAssembleOption);
1936 ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(
1937 recipient_context.get(), validator_options, kConsoleMessageConsumer));
1938
1939 const auto donor_context =
1940 BuildModule(env, consumer, donor_shader, kFuzzAssembleOption);
1941 ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(
1942 donor_context.get(), validator_options, kConsoleMessageConsumer));
1943
1944 TransformationContext transformation_context(
1945 MakeUnique<FactManager>(recipient_context.get()), validator_options);
1946
1947 FuzzerContext fuzzer_context(MakeUnique<PseudoRandomGenerator>(0), 100,
1948 false);
1949 protobufs::TransformationSequence transformation_sequence;
1950
1951 FuzzerPassDonateModules fuzzer_pass(recipient_context.get(),
1952 &transformation_context, &fuzzer_context,
1953 &transformation_sequence, false, {});
1954
1955 fuzzer_pass.DonateSingleModule(donor_context.get(), false);
1956
1957 ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(
1958 recipient_context.get(), validator_options, kConsoleMessageConsumer));
1959 }
1960
1961 TEST(FuzzerPassDonateModulesTest, HandlesCapabilities) {
1962 std::string donor_shader = R"(
1963 OpCapability VariablePointersStorageBuffer
1964 %1 = OpExtInstImport "GLSL.std.450"
1965 OpMemoryModel Logical GLSL450
1966 OpEntryPoint Fragment %4 "main"
1967 OpExecutionMode %4 OriginUpperLeft
1968 OpSource ESSL 310
1969 %2 = OpTypeVoid
1970 %3 = OpTypeFunction %2
1971 %6 = OpTypeFloat 32
1972 %11 = OpConstant %6 23
1973 %7 = OpTypePointer Function %6
1974 %4 = OpFunction %2 None %3
1975
1976 %5 = OpLabel
1977 %8 = OpVariable %7 Function
1978 OpBranch %9
1979
1980 %9 = OpLabel
1981 %10 = OpPhi %7 %8 %5
1982 OpStore %10 %11
1983 OpReturn
1984
1985 OpFunctionEnd
1986 )";
1987
1988 std::string recipient_shader = R"(
1989 OpCapability Shader
1990 %1 = OpExtInstImport "GLSL.std.450"
1991 OpMemoryModel Logical GLSL450
1992 OpEntryPoint Fragment %4 "main"
1993 OpExecutionMode %4 OriginUpperLeft
1994 OpSource ESSL 310
1995 %2 = OpTypeVoid
1996 %3 = OpTypeFunction %2
1997 %4 = OpFunction %2 None %3
1998 %5 = OpLabel
1999 OpReturn
2000 OpFunctionEnd
2001 )";
2002
2003 const auto env = SPV_ENV_UNIVERSAL_1_3;
2004 const auto consumer = nullptr;
2005 spvtools::ValidatorOptions validator_options;
2006 validator_options.SetRelaxLogicalPointer(true);
2007
2008 const auto recipient_context =
2009 BuildModule(env, consumer, recipient_shader, kFuzzAssembleOption);
2010 ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(
2011 recipient_context.get(), validator_options, kConsoleMessageConsumer));
2012
2013 const auto donor_context =
2014 BuildModule(env, consumer, donor_shader, kFuzzAssembleOption);
2015 ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(
2016 donor_context.get(), validator_options, kConsoleMessageConsumer));
2017
2018 TransformationContext transformation_context(
2019 MakeUnique<FactManager>(recipient_context.get()), validator_options);
2020
2021 FuzzerContext fuzzer_context(MakeUnique<PseudoRandomGenerator>(0), 100,
2022 false);
2023 protobufs::TransformationSequence transformation_sequence;
2024
2025 FuzzerPassDonateModules fuzzer_pass(recipient_context.get(),
2026 &transformation_context, &fuzzer_context,
2027 &transformation_sequence, false, {});
2028
2029 ASSERT_TRUE(donor_context->get_feature_mgr()->HasCapability(
2030 spv::Capability::VariablePointersStorageBuffer));
2031 ASSERT_FALSE(recipient_context->get_feature_mgr()->HasCapability(
2032 spv::Capability::VariablePointersStorageBuffer));
2033
2034 fuzzer_pass.DonateSingleModule(donor_context.get(), false);
2035
2036 // Check that recipient module hasn't changed.
2037 ASSERT_TRUE(IsEqual(env, recipient_shader, recipient_context.get()));
2038
2039 // Add the missing capability.
2040 //
2041 // We are adding VariablePointers to test the case when donor and recipient
2042 // have different OpCapability instructions but the same capabilities. In our
2043 // example, VariablePointers implicitly declares
2044 // VariablePointersStorageBuffer. Thus, two modules must be compatible.
2045 recipient_context->AddCapability(spv::Capability::VariablePointers);
2046
2047 ASSERT_TRUE(donor_context->get_feature_mgr()->HasCapability(
2048 spv::Capability::VariablePointersStorageBuffer));
2049 ASSERT_TRUE(recipient_context->get_feature_mgr()->HasCapability(
2050 spv::Capability::VariablePointersStorageBuffer));
2051
2052 fuzzer_pass.DonateSingleModule(donor_context.get(), false);
2053
2054 // Check that donation was successful.
2055 ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(
2056 recipient_context.get(), validator_options, kConsoleMessageConsumer));
2057
2058 std::string after_transformation = R"(
2059 OpCapability Shader
2060 OpCapability VariablePointers
2061 %1 = OpExtInstImport "GLSL.std.450"
2062 OpMemoryModel Logical GLSL450
2063 OpEntryPoint Fragment %4 "main"
2064 OpExecutionMode %4 OriginUpperLeft
2065 OpSource ESSL 310
2066 %2 = OpTypeVoid
2067 %3 = OpTypeFunction %2
2068 %100 = OpTypeFloat 32
2069 %101 = OpConstant %100 23
2070 %102 = OpTypePointer Function %100
2071 %105 = OpConstant %100 0
2072 %4 = OpFunction %2 None %3
2073 %5 = OpLabel
2074 OpReturn
2075 OpFunctionEnd
2076 %103 = OpFunction %2 None %3
2077 %104 = OpLabel
2078 %106 = OpVariable %102 Function %105
2079 OpBranch %107
2080 %107 = OpLabel
2081 %108 = OpPhi %102 %106 %104
2082 OpStore %108 %101
2083 OpReturn
2084 OpFunctionEnd
2085 )";
2086
2087 ASSERT_TRUE(IsEqual(env, after_transformation, recipient_context.get()));
2088 }
2089
2090 TEST(FuzzerPassDonateModulesTest, HandlesOpPhisInMergeBlock) {
2091 std::string donor_shader = R"(
2092 ; OpPhis don't support pointers without this capability
2093 ; and we need pointers to test some of the functionality
2094 OpCapability VariablePointers
2095 OpCapability Shader
2096 %1 = OpExtInstImport "GLSL.std.450"
2097 OpMemoryModel Logical GLSL450
2098 OpEntryPoint Fragment %4 "main"
2099 OpExecutionMode %4 OriginUpperLeft
2100 OpSource ESSL 310
2101 %2 = OpTypeVoid
2102 %3 = OpTypeFunction %2
2103 %14 = OpTypeBool
2104 %15 = OpConstantTrue %14
2105 %42 = OpTypePointer Function %14
2106
2107 ; back-edge block is unreachable in the CFG
2108 %4 = OpFunction %2 None %3
2109 %5 = OpLabel
2110 OpBranch %6
2111 %6 = OpLabel
2112 OpLoopMerge %8 %7 None
2113 OpBranch %8
2114 %7 = OpLabel
2115 OpBranch %6
2116 %8 = OpLabel
2117 OpReturn
2118 OpFunctionEnd
2119
2120 ; back-edge block already has an edge to the merge block
2121 %9 = OpFunction %2 None %3
2122 %10 = OpLabel
2123 OpBranch %11
2124 %11 = OpLabel
2125 OpLoopMerge %13 %12 None
2126 OpBranch %12
2127 %12 = OpLabel
2128 OpBranchConditional %15 %11 %13
2129 %13 = OpLabel
2130 OpReturn
2131 OpFunctionEnd
2132
2133 ; merge block has no OpPhis
2134 %16 = OpFunction %2 None %3
2135 %17 = OpLabel
2136 OpBranch %18
2137 %18 = OpLabel
2138 OpLoopMerge %20 %19 None
2139 OpBranchConditional %15 %19 %20
2140 %19 = OpLabel
2141 OpBranch %18
2142 %20 = OpLabel
2143 OpReturn
2144 OpFunctionEnd
2145
2146 ; merge block has OpPhis and some of their operands are available at
2147 ; the back-edge block
2148 %21 = OpFunction %2 None %3
2149 %22 = OpLabel
2150 OpBranch %23
2151 %23 = OpLabel
2152 %24 = OpCopyObject %14 %15
2153 OpLoopMerge %28 %27 None
2154 OpBranchConditional %15 %25 %28
2155 %25 = OpLabel
2156 %26 = OpCopyObject %14 %15
2157 OpBranchConditional %15 %28 %27
2158 %27 = OpLabel
2159 OpBranch %23
2160 %28 = OpLabel
2161 %29 = OpPhi %14 %24 %23 %26 %25
2162 OpReturn
2163 OpFunctionEnd
2164
2165 ; none of the OpPhis' operands dominate the back-edge block but some of
2166 ; them have basic type
2167 %30 = OpFunction %2 None %3
2168 %31 = OpLabel
2169 OpBranch %32
2170 %32 = OpLabel
2171 OpLoopMerge %40 %39 None
2172 OpBranch %33
2173 %33 = OpLabel
2174 OpSelectionMerge %38 None
2175 OpBranchConditional %15 %34 %36
2176 %34 = OpLabel
2177 %35 = OpCopyObject %14 %15
2178 OpBranchConditional %35 %38 %40
2179 %36 = OpLabel
2180 %37 = OpCopyObject %14 %15
2181 OpBranchConditional %37 %38 %40
2182 %38 = OpLabel
2183 OpBranch %39
2184 %39 = OpLabel
2185 OpBranch %32
2186 %40 = OpLabel
2187 %41 = OpPhi %14 %35 %34 %37 %36
2188 OpReturn
2189 OpFunctionEnd
2190
2191 ; none of the OpPhis' operands dominate the back-edge block and none of
2192 ; them have basic type
2193 %43 = OpFunction %2 None %3
2194 %44 = OpLabel
2195 %45 = OpVariable %42 Function
2196 OpBranch %46
2197 %46 = OpLabel
2198 OpLoopMerge %54 %53 None
2199 OpBranch %47
2200 %47 = OpLabel
2201 OpSelectionMerge %52 None
2202 OpBranchConditional %15 %48 %50
2203 %48 = OpLabel
2204 %49 = OpCopyObject %42 %45
2205 OpBranchConditional %15 %52 %54
2206 %50 = OpLabel
2207 %51 = OpCopyObject %42 %45
2208 OpBranchConditional %15 %52 %54
2209 %52 = OpLabel
2210 OpBranch %53
2211 %53 = OpLabel
2212 OpBranch %46
2213 %54 = OpLabel
2214 %55 = OpPhi %42 %49 %48 %51 %50
2215 OpReturn
2216 OpFunctionEnd
2217 )";
2218
2219 std::string recipient_shader = R"(
2220 ; OpPhis don't support pointers without this capability
2221 ; and we need pointers to test some of the functionality
2222 OpCapability VariablePointers
2223 OpCapability Shader
2224 %1 = OpExtInstImport "GLSL.std.450"
2225 OpMemoryModel Logical GLSL450
2226 OpEntryPoint Fragment %4 "main"
2227 OpExecutionMode %4 OriginUpperLeft
2228 OpSource ESSL 310
2229 %2 = OpTypeVoid
2230 %3 = OpTypeFunction %2
2231 %4 = OpFunction %2 None %3
2232 %5 = OpLabel
2233 OpReturn
2234 OpFunctionEnd
2235 )";
2236
2237 const auto env = SPV_ENV_UNIVERSAL_1_3;
2238 const auto consumer = nullptr;
2239 spvtools::ValidatorOptions validator_options;
2240 validator_options.SetRelaxLogicalPointer(true);
2241
2242 const auto recipient_context =
2243 BuildModule(env, consumer, recipient_shader, kFuzzAssembleOption);
2244 ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(
2245 recipient_context.get(), validator_options, kConsoleMessageConsumer));
2246
2247 const auto donor_context =
2248 BuildModule(env, consumer, donor_shader, kFuzzAssembleOption);
2249 ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(
2250 donor_context.get(), validator_options, kConsoleMessageConsumer));
2251
2252 TransformationContext transformation_context(
2253 MakeUnique<FactManager>(recipient_context.get()), validator_options);
2254
2255 FuzzerContext fuzzer_context(MakeUnique<PseudoRandomGenerator>(0), 100,
2256 false);
2257 protobufs::TransformationSequence transformation_sequence;
2258
2259 FuzzerPassDonateModules fuzzer_pass(recipient_context.get(),
2260 &transformation_context, &fuzzer_context,
2261 &transformation_sequence, false, {});
2262
2263 fuzzer_pass.DonateSingleModule(donor_context.get(), true);
2264
2265 // We just check that the result is valid. Checking to what it should be
2266 // exactly equal to would be very fragile.
2267 ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(
2268 recipient_context.get(), validator_options, kConsoleMessageConsumer));
2269 }
2270
2271 } // namespace
2272 } // namespace fuzz
2273 } // namespace spvtools