Debian Codegraph @

test/fuzz/transformation_access_chain_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_access_chain.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(TransformationAccessChainTest, BasicTest) {
27 std::string shader = R"(
28 OpCapability Shader
29 OpCapability VariablePointers
30 %1 = OpExtInstImport "GLSL.std.450"
31 OpMemoryModel Logical GLSL450
32 OpEntryPoint Fragment %4 "main" %48 %54
33 OpExecutionMode %4 OriginUpperLeft
34 OpSource ESSL 310
35 %2 = OpTypeVoid
36 %3 = OpTypeFunction %2
37 %6 = OpTypeFloat 32
38 %7 = OpTypeVector %6 2
39 %50 = OpTypeMatrix %7 2
40 %70 = OpTypePointer Function %7
41 %71 = OpTypePointer Function %50
42 %8 = OpTypeStruct %7 %6
43 %9 = OpTypePointer Function %8
44 %10 = OpTypeInt 32 1
45 %11 = OpTypePointer Function %10
46 %12 = OpTypeFunction %10 %9 %11
47 %17 = OpConstant %10 0
48 %18 = OpTypeInt 32 0
49 %19 = OpConstant %18 0
50 %20 = OpTypePointer Function %6
51 %99 = OpTypePointer Private %6
52 %29 = OpConstant %6 0
53 %30 = OpConstant %6 1
54 %31 = OpConstantComposite %7 %29 %30
55 %32 = OpConstant %6 2
56 %33 = OpConstantComposite %8 %31 %32
57 %35 = OpConstant %10 10
58 %51 = OpConstant %18 10
59 %80 = OpConstant %18 0
60 %81 = OpConstant %10 1
61 %82 = OpConstant %18 2
62 %83 = OpConstant %10 3
63 %84 = OpConstant %18 4
64 %85 = OpConstant %10 5
65 %52 = OpTypeArray %50 %51
66 %53 = OpTypePointer Private %52
67 %46 = OpConstantNull %9
68 %47 = OpTypePointer Private %8
69 %48 = OpVariable %47 Private
70 %54 = OpVariable %53 Private
71 %4 = OpFunction %2 None %3
72 %5 = OpLabel
73 %28 = OpVariable %9 Function
74 %34 = OpVariable %11 Function
75 %36 = OpVariable %9 Function
76 %38 = OpVariable %11 Function
77 %44 = OpCopyObject %9 %36
78 OpStore %28 %33
79 OpStore %34 %35
80 %37 = OpLoad %8 %28
81 OpStore %36 %37
82 %39 = OpLoad %10 %34
83 OpStore %38 %39
84 %40 = OpFunctionCall %10 %15 %36 %38
85 %41 = OpLoad %10 %34
86 %42 = OpIAdd %10 %41 %40
87 OpStore %34 %42
88 OpReturn
89 OpFunctionEnd
90 %15 = OpFunction %10 None %12
91 %13 = OpFunctionParameter %9
92 %14 = OpFunctionParameter %11
93 %16 = OpLabel
94 %21 = OpAccessChain %20 %13 %17 %19
95 %43 = OpCopyObject %9 %13
96 %22 = OpLoad %6 %21
97 %23 = OpConvertFToS %10 %22
98 %24 = OpLoad %10 %14
99 %25 = OpIAdd %10 %23 %24
100 OpReturnValue %25
101 OpFunctionEnd
102 )";
103
104 const auto env = SPV_ENV_UNIVERSAL_1_4;
105 const auto consumer = nullptr;
106 const auto context = BuildModule(env, consumer, shader, kFuzzAssembleOption);
107 spvtools::ValidatorOptions validator_options;
108 validator_options.SetRelaxLogicalPointer(true);
109 ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(context.get(), validator_options,
110 kConsoleMessageConsumer));
111
112 // Types:
113 // Ptr | Pointee | Storage class | GLSL for pointee | Ids of this type
114 // ----+---------+---------------+---------------------+------------------
115 // 9 | 8 | Function | struct(vec2, float) | 28, 36, 44, 13, 43
116 // 11 | 10 | Function | int | 34, 38, 14
117 // 20 | 6 | Function | float | -
118 // 99 | 6 | Private | float | -
119 // 53 | 52 | Private | mat2x2[10] | 54
120 // 47 | 8 | Private | struct(vec2, float) | 48
121 // 70 | 7 | Function | vec2 | -
122 // 71 | 59 | Function | mat2x2 | -
123
124 // Indices 0-5 are in ids 80-85
125
126 TransformationContext transformation_context(
127 MakeUnique<FactManager>(context.get()), validator_options);
128 transformation_context.GetFactManager()->AddFactValueOfPointeeIsIrrelevant(
129 54);
130
131 // Check the case where the index type is not a 32-bit integer.
132 TransformationAccessChain invalid_index_example1(
133 101, 28, {29}, MakeInstructionDescriptor(42, spv::Op::OpReturn, 0));
134
135 // Since the index is not a 32-bit integer type but a 32-bit float type,
136 // ValidIndexComposite should return false and thus the transformation is not
137 // applicable.
138 ASSERT_FALSE(invalid_index_example1.IsApplicable(context.get(),
139 transformation_context));
140
141 // Bad: id is not fresh
142 ASSERT_FALSE(
143 TransformationAccessChain(
144 43, 43, {80}, MakeInstructionDescriptor(24, spv::Op::OpLoad, 0))
145 .IsApplicable(context.get(), transformation_context));
146
147 // Bad: pointer id does not exist
148 ASSERT_FALSE(
149 TransformationAccessChain(
150 100, 1000, {80}, MakeInstructionDescriptor(24, spv::Op::OpLoad, 0))
151 .IsApplicable(context.get(), transformation_context));
152
153 // Bad: pointer id is not a type
154 ASSERT_FALSE(
155 TransformationAccessChain(
156 100, 5, {80}, MakeInstructionDescriptor(24, spv::Op::OpLoad, 0))
157 .IsApplicable(context.get(), transformation_context));
158
159 // Bad: pointer id is not a pointer
160 ASSERT_FALSE(
161 TransformationAccessChain(
162 100, 23, {80}, MakeInstructionDescriptor(24, spv::Op::OpLoad, 0))
163 .IsApplicable(context.get(), transformation_context));
164
165 // Bad: index id does not exist
166 ASSERT_FALSE(
167 TransformationAccessChain(
168 100, 43, {1000}, MakeInstructionDescriptor(24, spv::Op::OpLoad, 0))
169 .IsApplicable(context.get(), transformation_context));
170
171 // Bad: index id is not a constant and the pointer refers to a struct
172 ASSERT_FALSE(
173 TransformationAccessChain(
174 100, 43, {24}, MakeInstructionDescriptor(25, spv::Op::OpIAdd, 0))
175 .IsApplicable(context.get(), transformation_context));
176
177 // Bad: too many indices
178 ASSERT_FALSE(TransformationAccessChain(
179 100, 43, {80, 80, 80},
180 MakeInstructionDescriptor(24, spv::Op::OpLoad, 0))
181 .IsApplicable(context.get(), transformation_context));
182
183 // Bad: index id is out of bounds when accessing a struct
184 ASSERT_FALSE(
185 TransformationAccessChain(
186 100, 43, {83, 80}, MakeInstructionDescriptor(24, spv::Op::OpLoad, 0))
187 .IsApplicable(context.get(), transformation_context));
188
189 // Bad: attempt to insert before variable
190 ASSERT_FALSE(
191 TransformationAccessChain(
192 100, 34, {}, MakeInstructionDescriptor(36, spv::Op::OpVariable, 0))
193 .IsApplicable(context.get(), transformation_context));
194
195 // Bad: OpTypeBool must be present in the module to clamp an index
196 ASSERT_FALSE(
197 TransformationAccessChain(
198 100, 36, {80, 81}, MakeInstructionDescriptor(37, spv::Op::OpStore, 0))
199 .IsApplicable(context.get(), transformation_context));
200
201 // Bad: pointer not available
202 ASSERT_FALSE(TransformationAccessChain(
203 100, 43, {80},
204 MakeInstructionDescriptor(21, spv::Op::OpAccessChain, 0))
205 .IsApplicable(context.get(), transformation_context));
206
207 // Bad: instruction descriptor does not identify anything
208 ASSERT_FALSE(
209 TransformationAccessChain(
210 100, 43, {80}, MakeInstructionDescriptor(24, spv::Op::OpLoad, 100))
211 .IsApplicable(context.get(), transformation_context));
212
213 #ifndef NDEBUG
214 // Bad: pointer is null
215 ASSERT_DEATH(
216 TransformationAccessChain(
217 100, 46, {80}, MakeInstructionDescriptor(24, spv::Op::OpLoad, 0))
218 .IsApplicable(context.get(), transformation_context),
219 "Access chains should not be created from null/undefined pointers");
220 #endif
221
222 // Bad: pointer to result type does not exist
223 ASSERT_FALSE(
224 TransformationAccessChain(
225 100, 52, {0}, MakeInstructionDescriptor(24, spv::Op::OpLoad, 0))
226 .IsApplicable(context.get(), transformation_context));
227
228 {
229 TransformationAccessChain transformation(
230 100, 43, {80}, MakeInstructionDescriptor(24, spv::Op::OpLoad, 0));
231 ASSERT_TRUE(
232 transformation.IsApplicable(context.get(), transformation_context));
233 ApplyAndCheckFreshIds(transformation, context.get(),
234 &transformation_context);
235 ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(
236 context.get(), validator_options, kConsoleMessageConsumer));
237 ASSERT_FALSE(
238 transformation_context.GetFactManager()->PointeeValueIsIrrelevant(100));
239 }
240
241 {
242 TransformationAccessChain transformation(
243 101, 28, {81}, MakeInstructionDescriptor(42, spv::Op::OpReturn, 0));
244 ASSERT_TRUE(
245 transformation.IsApplicable(context.get(), transformation_context));
246 ApplyAndCheckFreshIds(transformation, context.get(),
247 &transformation_context);
248 ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(
249 context.get(), validator_options, kConsoleMessageConsumer));
250 ASSERT_FALSE(
251 transformation_context.GetFactManager()->PointeeValueIsIrrelevant(101));
252 }
253
254 {
255 TransformationAccessChain transformation(
256 102, 44, {}, MakeInstructionDescriptor(44, spv::Op::OpStore, 0));
257 ASSERT_TRUE(
258 transformation.IsApplicable(context.get(), transformation_context));
259 ApplyAndCheckFreshIds(transformation, context.get(),
260 &transformation_context);
261 ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(
262 context.get(), validator_options, kConsoleMessageConsumer));
263 ASSERT_FALSE(
264 transformation_context.GetFactManager()->PointeeValueIsIrrelevant(103));
265 }
266
267 {
268 TransformationAccessChain transformation(
269 103, 13, {80},
270 MakeInstructionDescriptor(21, spv::Op::OpAccessChain, 0));
271 ASSERT_TRUE(
272 transformation.IsApplicable(context.get(), transformation_context));
273 ApplyAndCheckFreshIds(transformation, context.get(),
274 &transformation_context);
275 ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(
276 context.get(), validator_options, kConsoleMessageConsumer));
277 ASSERT_FALSE(
278 transformation_context.GetFactManager()->PointeeValueIsIrrelevant(104));
279 }
280
281 {
282 TransformationAccessChain transformation(
283 104, 34, {}, MakeInstructionDescriptor(44, spv::Op::OpStore, 1));
284 ASSERT_TRUE(
285 transformation.IsApplicable(context.get(), transformation_context));
286 ApplyAndCheckFreshIds(transformation, context.get(),
287 &transformation_context);
288 ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(
289 context.get(), validator_options, kConsoleMessageConsumer));
290 ASSERT_FALSE(
291 transformation_context.GetFactManager()->PointeeValueIsIrrelevant(105));
292 }
293
294 {
295 TransformationAccessChain transformation(
296 105, 38, {}, MakeInstructionDescriptor(40, spv::Op::OpFunctionCall, 0));
297 ASSERT_TRUE(
298 transformation.IsApplicable(context.get(), transformation_context));
299 ApplyAndCheckFreshIds(transformation, context.get(),
300 &transformation_context);
301 ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(
302 context.get(), validator_options, kConsoleMessageConsumer));
303 ASSERT_FALSE(
304 transformation_context.GetFactManager()->PointeeValueIsIrrelevant(106));
305 }
306
307 {
308 TransformationAccessChain transformation(
309 106, 14, {}, MakeInstructionDescriptor(24, spv::Op::OpLoad, 0));
310 ASSERT_TRUE(
311 transformation.IsApplicable(context.get(), transformation_context));
312 ApplyAndCheckFreshIds(transformation, context.get(),
313 &transformation_context);
314 ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(
315 context.get(), validator_options, kConsoleMessageConsumer));
316 ASSERT_FALSE(
317 transformation_context.GetFactManager()->PointeeValueIsIrrelevant(107));
318 }
319 {
320 // Check the case where the access chain's base pointer has the irrelevant
321 // pointee fact; the resulting access chain should inherit this fact.
322 TransformationAccessChain transformation(
323 107, 54, {}, MakeInstructionDescriptor(24, spv::Op::OpLoad, 0));
324 ASSERT_TRUE(
325 transformation.IsApplicable(context.get(), transformation_context));
326 ApplyAndCheckFreshIds(transformation, context.get(),
327 &transformation_context);
328 ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(
329 context.get(), validator_options, kConsoleMessageConsumer));
330 ASSERT_TRUE(
331 transformation_context.GetFactManager()->PointeeValueIsIrrelevant(54));
332 }
333
334 std::string after_transformation = R"(
335 OpCapability Shader
336 OpCapability VariablePointers
337 %1 = OpExtInstImport "GLSL.std.450"
338 OpMemoryModel Logical GLSL450
339 OpEntryPoint Fragment %4 "main" %48 %54
340 OpExecutionMode %4 OriginUpperLeft
341 OpSource ESSL 310
342 %2 = OpTypeVoid
343 %3 = OpTypeFunction %2
344 %6 = OpTypeFloat 32
345 %7 = OpTypeVector %6 2
346 %50 = OpTypeMatrix %7 2
347 %70 = OpTypePointer Function %7
348 %71 = OpTypePointer Function %50
349 %8 = OpTypeStruct %7 %6
350 %9 = OpTypePointer Function %8
351 %10 = OpTypeInt 32 1
352 %11 = OpTypePointer Function %10
353 %12 = OpTypeFunction %10 %9 %11
354 %17 = OpConstant %10 0
355 %18 = OpTypeInt 32 0
356 %19 = OpConstant %18 0
357 %20 = OpTypePointer Function %6
358 %99 = OpTypePointer Private %6
359 %29 = OpConstant %6 0
360 %30 = OpConstant %6 1
361 %31 = OpConstantComposite %7 %29 %30
362 %32 = OpConstant %6 2
363 %33 = OpConstantComposite %8 %31 %32
364 %35 = OpConstant %10 10
365 %51 = OpConstant %18 10
366 %80 = OpConstant %18 0
367 %81 = OpConstant %10 1
368 %82 = OpConstant %18 2
369 %83 = OpConstant %10 3
370 %84 = OpConstant %18 4
371 %85 = OpConstant %10 5
372 %52 = OpTypeArray %50 %51
373 %53 = OpTypePointer Private %52
374 %46 = OpConstantNull %9
375 %47 = OpTypePointer Private %8
376 %48 = OpVariable %47 Private
377 %54 = OpVariable %53 Private
378 %4 = OpFunction %2 None %3
379 %5 = OpLabel
380 %28 = OpVariable %9 Function
381 %34 = OpVariable %11 Function
382 %36 = OpVariable %9 Function
383 %38 = OpVariable %11 Function
384 %44 = OpCopyObject %9 %36
385 %102 = OpAccessChain %9 %44
386 OpStore %28 %33
387 %104 = OpAccessChain %11 %34
388 OpStore %34 %35
389 %37 = OpLoad %8 %28
390 OpStore %36 %37
391 %39 = OpLoad %10 %34
392 OpStore %38 %39
393 %105 = OpAccessChain %11 %38
394 %40 = OpFunctionCall %10 %15 %36 %38
395 %41 = OpLoad %10 %34
396 %42 = OpIAdd %10 %41 %40
397 OpStore %34 %42
398 %101 = OpAccessChain %20 %28 %81
399 OpReturn
400 OpFunctionEnd
401 %15 = OpFunction %10 None %12
402 %13 = OpFunctionParameter %9
403 %14 = OpFunctionParameter %11
404 %16 = OpLabel
405 %103 = OpAccessChain %70 %13 %80
406 %21 = OpAccessChain %20 %13 %17 %19
407 %43 = OpCopyObject %9 %13
408 %22 = OpLoad %6 %21
409 %23 = OpConvertFToS %10 %22
410 %100 = OpAccessChain %70 %43 %80
411 %106 = OpAccessChain %11 %14
412 %107 = OpAccessChain %53 %54
413 %24 = OpLoad %10 %14
414 %25 = OpIAdd %10 %23 %24
415 OpReturnValue %25
416 OpFunctionEnd
417 )";
418 ASSERT_TRUE(IsEqual(env, after_transformation, context.get()));
419 }
420
421 TEST(TransformationAccessChainTest, StructIndexMustBeConstant) {
422 std::string shader = R"(
423 OpCapability Shader
424 %1 = OpExtInstImport "GLSL.std.450"
425 OpMemoryModel Logical GLSL450
426 OpEntryPoint Fragment %4 "main"
427 OpExecutionMode %4 OriginUpperLeft
428 OpSource ESSL 320
429 %2 = OpTypeVoid
430 %3 = OpTypeFunction %2
431 %6 = OpTypeInt 32 1
432 %20 = OpUndef %6
433 %7 = OpTypeStruct %6 %6
434 %8 = OpTypePointer Function %7
435 %10 = OpConstant %6 0
436 %11 = OpConstant %6 2
437 %12 = OpTypePointer Function %6
438 %4 = OpFunction %2 None %3
439 %5 = OpLabel
440 %9 = OpVariable %8 Function
441 OpReturn
442 OpFunctionEnd
443 )";
444
445 const auto env = SPV_ENV_UNIVERSAL_1_4;
446 const auto consumer = nullptr;
447 const auto context = BuildModule(env, consumer, shader, kFuzzAssembleOption);
448 spvtools::ValidatorOptions validator_options;
449 ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(context.get(), validator_options,
450 kConsoleMessageConsumer));
451 TransformationContext transformation_context(
452 MakeUnique<FactManager>(context.get()), validator_options);
453 // Bad: %9 is a pointer to a struct, but %20 is not a constant.
454 ASSERT_FALSE(
455 TransformationAccessChain(
456 100, 9, {20}, MakeInstructionDescriptor(9, spv::Op::OpReturn, 0))
457 .IsApplicable(context.get(), transformation_context));
458 }
459
460 TEST(TransformationAccessChainTest, IsomorphicStructs) {
461 std::string shader = R"(
462 OpCapability Shader
463 %1 = OpExtInstImport "GLSL.std.450"
464 OpMemoryModel Logical GLSL450
465 OpEntryPoint Fragment %4 "main" %11 %12
466 OpExecutionMode %4 OriginUpperLeft
467 OpSource ESSL 310
468 %2 = OpTypeVoid
469 %3 = OpTypeFunction %2
470 %6 = OpTypeFloat 32
471 %7 = OpTypeStruct %6
472 %8 = OpTypePointer Private %7
473 %9 = OpTypeStruct %6
474 %10 = OpTypePointer Private %9
475 %11 = OpVariable %8 Private
476 %12 = OpVariable %10 Private
477 %4 = OpFunction %2 None %3
478 %5 = OpLabel
479 OpReturn
480 OpFunctionEnd
481 )";
482
483 const auto env = SPV_ENV_UNIVERSAL_1_4;
484 const auto consumer = nullptr;
485 const auto context = BuildModule(env, consumer, shader, kFuzzAssembleOption);
486 spvtools::ValidatorOptions validator_options;
487 ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(context.get(), validator_options,
488 kConsoleMessageConsumer));
489 TransformationContext transformation_context(
490 MakeUnique<FactManager>(context.get()), validator_options);
491 {
492 TransformationAccessChain transformation(
493 100, 11, {}, MakeInstructionDescriptor(5, spv::Op::OpReturn, 0));
494 ASSERT_TRUE(
495 transformation.IsApplicable(context.get(), transformation_context));
496 ApplyAndCheckFreshIds(transformation, context.get(),
497 &transformation_context);
498 ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(
499 context.get(), validator_options, kConsoleMessageConsumer));
500 }
501 {
502 TransformationAccessChain transformation(
503 101, 12, {}, MakeInstructionDescriptor(5, spv::Op::OpReturn, 0));
504 ASSERT_TRUE(
505 transformation.IsApplicable(context.get(), transformation_context));
506 ApplyAndCheckFreshIds(transformation, context.get(),
507 &transformation_context);
508 ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(
509 context.get(), validator_options, kConsoleMessageConsumer));
510 }
511
512 std::string after_transformation = R"(
513 OpCapability Shader
514 %1 = OpExtInstImport "GLSL.std.450"
515 OpMemoryModel Logical GLSL450
516 OpEntryPoint Fragment %4 "main" %11 %12
517 OpExecutionMode %4 OriginUpperLeft
518 OpSource ESSL 310
519 %2 = OpTypeVoid
520 %3 = OpTypeFunction %2
521 %6 = OpTypeFloat 32
522 %7 = OpTypeStruct %6
523 %8 = OpTypePointer Private %7
524 %9 = OpTypeStruct %6
525 %10 = OpTypePointer Private %9
526 %11 = OpVariable %8 Private
527 %12 = OpVariable %10 Private
528 %4 = OpFunction %2 None %3
529 %5 = OpLabel
530 %100 = OpAccessChain %8 %11
531 %101 = OpAccessChain %10 %12
532 OpReturn
533 OpFunctionEnd
534 )";
535 ASSERT_TRUE(IsEqual(env, after_transformation, context.get()));
536 }
537
538 TEST(TransformationAccessChainTest, ClampingVariables) {
539 std::string shader = R"(
540 OpCapability Shader
541 %1 = OpExtInstImport "GLSL.std.450"
542 OpMemoryModel Logical GLSL450
543 OpEntryPoint Fragment %2 "main" %3
544 OpExecutionMode %2 OriginUpperLeft
545 OpSource ESSL 310
546 %4 = OpTypeVoid
547 %5 = OpTypeBool
548 %6 = OpTypeFunction %4
549 %7 = OpTypeInt 32 1
550 %8 = OpTypeVector %7 4
551 %9 = OpTypePointer Function %8
552 %10 = OpConstant %7 0
553 %11 = OpConstant %7 1
554 %12 = OpConstant %7 3
555 %13 = OpConstant %7 2
556 %14 = OpConstantComposite %8 %10 %11 %12 %13
557 %15 = OpTypePointer Function %7
558 %16 = OpTypeInt 32 0
559 %17 = OpConstant %16 1
560 %18 = OpConstant %16 3
561 %19 = OpTypeStruct %8
562 %20 = OpTypePointer Function %19
563 %21 = OpConstant %7 9
564 %22 = OpConstant %16 10
565 %23 = OpTypeArray %19 %22
566 %24 = OpTypePointer Function %23
567 %25 = OpTypeFloat 32
568 %26 = OpTypeVector %25 4
569 %27 = OpTypePointer Output %26
570 %3 = OpVariable %27 Output
571 %2 = OpFunction %4 None %6
572 %28 = OpLabel
573 %29 = OpVariable %9 Function
574 %30 = OpVariable %15 Function
575 %31 = OpVariable %15 Function
576 %32 = OpVariable %20 Function
577 %33 = OpVariable %15 Function
578 %34 = OpVariable %24 Function
579 OpStore %29 %14
580 OpStore %30 %10
581 %36 = OpLoad %7 %30
582 %38 = OpLoad %8 %29
583 %39 = OpCompositeConstruct %19 %38
584 %40 = OpLoad %7 %30
585 %42 = OpLoad %8 %29
586 %43 = OpCompositeConstruct %19 %42
587 %45 = OpLoad %7 %30
588 %46 = OpLoad %7 %33
589 OpReturn
590 OpFunctionEnd
591 )";
592
593 const auto env = SPV_ENV_UNIVERSAL_1_4;
594 const auto consumer = nullptr;
595 const auto context = BuildModule(env, consumer, shader, kFuzzAssembleOption);
596 spvtools::ValidatorOptions validator_options;
597 ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(context.get(), validator_options,
598 kConsoleMessageConsumer));
599 TransformationContext transformation_context(
600 MakeUnique<FactManager>(context.get()), validator_options);
601 // Bad: no ids given for clamping
602 ASSERT_FALSE(
603 TransformationAccessChain(
604 100, 29, {17}, MakeInstructionDescriptor(36, spv::Op::OpLoad, 0))
605 .IsApplicable(context.get(), transformation_context));
606
607 // Bad: an id given for clamping is not fresh
608 ASSERT_FALSE(TransformationAccessChain(
609 100, 29, {17},
610 MakeInstructionDescriptor(36, spv::Op::OpLoad, 0),
611 {{46, 201}})
612 .IsApplicable(context.get(), transformation_context));
613
614 // Bad: an id given for clamping is not fresh
615 ASSERT_FALSE(TransformationAccessChain(
616 100, 29, {17},
617 MakeInstructionDescriptor(36, spv::Op::OpLoad, 0),
618 {{200, 46}})
619 .IsApplicable(context.get(), transformation_context));
620
621 // Bad: an id given for clamping is the same as the id for the access chain
622 ASSERT_FALSE(TransformationAccessChain(
623 100, 29, {17},
624 MakeInstructionDescriptor(36, spv::Op::OpLoad, 0),
625 {{100, 201}})
626 .IsApplicable(context.get(), transformation_context));
627
628 // Bad: the fresh ids given are not distinct
629 ASSERT_FALSE(TransformationAccessChain(
630 100, 29, {17},
631 MakeInstructionDescriptor(36, spv::Op::OpLoad, 0),
632 {{200, 200}})
633 .IsApplicable(context.get(), transformation_context));
634
635 // Bad: not enough ids given for clamping (2 pairs needed)
636 ASSERT_FALSE(TransformationAccessChain(
637 104, 34, {45, 10, 46},
638 MakeInstructionDescriptor(46, spv::Op::OpReturn, 0),
639 {{208, 209}, {209, 211}})
640 .IsApplicable(context.get(), transformation_context));
641
642 // Bad: the fresh ids given are not distinct
643 ASSERT_FALSE(TransformationAccessChain(
644 104, 34, {45, 10, 46},
645 MakeInstructionDescriptor(46, spv::Op::OpReturn, 0),
646 {{208, 209}, {209, 211}})
647 .IsApplicable(context.get(), transformation_context));
648
649 {
650 TransformationAccessChain transformation(
651 100, 29, {17}, MakeInstructionDescriptor(36, spv::Op::OpLoad, 0),
652 {{200, 201}});
653 ASSERT_TRUE(
654 transformation.IsApplicable(context.get(), transformation_context));
655 ApplyAndCheckFreshIds(transformation, context.get(),
656 &transformation_context);
657 ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(
658 context.get(), validator_options, kConsoleMessageConsumer));
659 }
660
661 {
662 TransformationAccessChain transformation(
663 101, 29, {36}, MakeInstructionDescriptor(38, spv::Op::OpLoad, 0),
664 {{202, 203}});
665 ASSERT_TRUE(
666 transformation.IsApplicable(context.get(), transformation_context));
667 ApplyAndCheckFreshIds(transformation, context.get(),
668 &transformation_context);
669 ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(
670 context.get(), validator_options, kConsoleMessageConsumer));
671 }
672
673 {
674 TransformationAccessChain transformation(
675 102, 32, {10, 40}, MakeInstructionDescriptor(42, spv::Op::OpLoad, 0),
676 {{204, 205}});
677 ASSERT_TRUE(
678 transformation.IsApplicable(context.get(), transformation_context));
679 ApplyAndCheckFreshIds(transformation, context.get(),
680 &transformation_context);
681 ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(
682 context.get(), validator_options, kConsoleMessageConsumer));
683 }
684
685 {
686 TransformationAccessChain transformation(
687 103, 34, {11}, MakeInstructionDescriptor(45, spv::Op::OpLoad, 0),
688 {{206, 207}});
689 ASSERT_TRUE(
690 transformation.IsApplicable(context.get(), transformation_context));
691 ApplyAndCheckFreshIds(transformation, context.get(),
692 &transformation_context);
693 ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(
694 context.get(), validator_options, kConsoleMessageConsumer));
695 }
696
697 {
698 TransformationAccessChain transformation(
699 104, 34, {45, 10, 46},
700 MakeInstructionDescriptor(46, spv::Op::OpReturn, 0),
701 {{208, 209}, {210, 211}});
702 ASSERT_TRUE(
703 transformation.IsApplicable(context.get(), transformation_context));
704 ApplyAndCheckFreshIds(transformation, context.get(),
705 &transformation_context);
706 ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(
707 context.get(), validator_options, kConsoleMessageConsumer));
708 }
709
710 std::string after_transformation = R"(
711 OpCapability Shader
712 %1 = OpExtInstImport "GLSL.std.450"
713 OpMemoryModel Logical GLSL450
714 OpEntryPoint Fragment %2 "main" %3
715 OpExecutionMode %2 OriginUpperLeft
716 OpSource ESSL 310
717 %4 = OpTypeVoid
718 %5 = OpTypeBool
719 %6 = OpTypeFunction %4
720 %7 = OpTypeInt 32 1
721 %8 = OpTypeVector %7 4
722 %9 = OpTypePointer Function %8
723 %10 = OpConstant %7 0
724 %11 = OpConstant %7 1
725 %12 = OpConstant %7 3
726 %13 = OpConstant %7 2
727 %14 = OpConstantComposite %8 %10 %11 %12 %13
728 %15 = OpTypePointer Function %7
729 %16 = OpTypeInt 32 0
730 %17 = OpConstant %16 1
731 %18 = OpConstant %16 3
732 %19 = OpTypeStruct %8
733 %20 = OpTypePointer Function %19
734 %21 = OpConstant %7 9
735 %22 = OpConstant %16 10
736 %23 = OpTypeArray %19 %22
737 %24 = OpTypePointer Function %23
738 %25 = OpTypeFloat 32
739 %26 = OpTypeVector %25 4
740 %27 = OpTypePointer Output %26
741 %3 = OpVariable %27 Output
742 %2 = OpFunction %4 None %6
743 %28 = OpLabel
744 %29 = OpVariable %9 Function
745 %30 = OpVariable %15 Function
746 %31 = OpVariable %15 Function
747 %32 = OpVariable %20 Function
748 %33 = OpVariable %15 Function
749 %34 = OpVariable %24 Function
750 OpStore %29 %14
751 OpStore %30 %10
752 %200 = OpULessThanEqual %5 %17 %18
753 %201 = OpSelect %16 %200 %17 %18
754 %100 = OpAccessChain %15 %29 %201
755 %36 = OpLoad %7 %30
756 %202 = OpULessThanEqual %5 %36 %12
757 %203 = OpSelect %7 %202 %36 %12
758 %101 = OpAccessChain %15 %29 %203
759 %38 = OpLoad %8 %29
760 %39 = OpCompositeConstruct %19 %38
761 %40 = OpLoad %7 %30
762 %204 = OpULessThanEqual %5 %40 %12
763 %205 = OpSelect %7 %204 %40 %12
764 %102 = OpAccessChain %15 %32 %10 %205
765 %42 = OpLoad %8 %29
766 %43 = OpCompositeConstruct %19 %42
767 %206 = OpULessThanEqual %5 %11 %21
768 %207 = OpSelect %7 %206 %11 %21
769 %103 = OpAccessChain %20 %34 %207
770 %45 = OpLoad %7 %30
771 %46 = OpLoad %7 %33
772 %208 = OpULessThanEqual %5 %45 %21
773 %209 = OpSelect %7 %208 %45 %21
774 %210 = OpULessThanEqual %5 %46 %12
775 %211 = OpSelect %7 %210 %46 %12
776 %104 = OpAccessChain %15 %34 %209 %10 %211
777 OpReturn
778 OpFunctionEnd
779 )";
780 ASSERT_TRUE(IsEqual(env, after_transformation, context.get()));
781 }
782
783 } // namespace
784 } // namespace fuzz
785 } // namespace spvtools