Debian Codegraph @

test/fuzz/transformation_add_copy_memory_test.cpp

raw
1 // Copyright (c) 2020 Vasyl Teliman
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_add_copy_memory.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(TransformationAddCopyMemoryTest, 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"
33 OpExecutionMode %4 OriginUpperLeft
34 OpSource ESSL 310
35 OpDecorate %19 RelaxedPrecision
36 OpMemberDecorate %66 0 RelaxedPrecision
37 OpDecorate %69 RelaxedPrecision
38 %2 = OpTypeVoid
39 %3 = OpTypeFunction %2
40 %6 = OpTypeBool
41 %7 = OpTypePointer Function %6
42 %78 = OpTypePointer Private %6
43 %8 = OpTypeFunction %6 %7
44 %17 = OpTypeInt 32 1
45 %18 = OpTypePointer Function %17
46 %79 = OpTypePointer Private %17
47 %20 = OpConstant %17 0
48 %21 = OpTypeFloat 32
49 %22 = OpTypePointer Function %21
50 %80 = OpTypePointer Private %21
51 %24 = OpConstant %21 0
52 %25 = OpConstantFalse %6
53 %32 = OpConstantTrue %6
54 %33 = OpTypeVector %21 4
55 %34 = OpTypePointer Function %33
56 %81 = OpTypePointer Private %33
57 %36 = OpConstantComposite %33 %24 %24 %24 %24
58 %37 = OpTypeMatrix %33 4
59 %84 = OpConstantComposite %37 %36 %36 %36 %36
60 %38 = OpTypePointer Function %37
61 %82 = OpTypePointer Private %37
62 %44 = OpConstant %21 1
63 %66 = OpTypeStruct %17 %21 %6 %33 %37
64 %85 = OpConstantComposite %66 %20 %24 %25 %36 %84
65 %67 = OpTypePointer Function %66
66 %83 = OpTypePointer Private %66
67 %86 = OpVariable %79 Private %20
68 %88 = OpConstantNull %79
69 %4 = OpFunction %2 None %3
70 %5 = OpLabel
71 %19 = OpVariable %18 Function
72 %23 = OpVariable %22 Function
73 %26 = OpVariable %7 Function
74 %30 = OpVariable %7 Function
75 %35 = OpVariable %34 Function
76 %39 = OpVariable %38 Function
77 %68 = OpVariable %67 Function
78 OpStore %19 %20
79 OpStore %23 %24
80 OpStore %26 %25
81 %27 = OpFunctionCall %6 %10 %26
82 OpSelectionMerge %29 None
83 OpBranchConditional %27 %28 %31
84 %28 = OpLabel
85 %89 = OpCopyObject %18 %19
86 OpBranch %29
87 %31 = OpLabel
88 OpBranch %29
89 %76 = OpLabel
90 %77 = OpLogicalEqual %6 %25 %32
91 OpBranch %29
92 %29 = OpLabel
93 %75 = OpPhi %6 %25 %31 %32 %28 %77 %76
94 OpStore %30 %75
95 %40 = OpLoad %33 %35
96 %41 = OpLoad %33 %35
97 %42 = OpLoad %33 %35
98 %43 = OpLoad %33 %35
99 %45 = OpCompositeExtract %21 %40 0
100 %46 = OpCompositeExtract %21 %40 1
101 %47 = OpCompositeExtract %21 %40 2
102 %48 = OpCompositeExtract %21 %40 3
103 %49 = OpCompositeExtract %21 %41 0
104 %50 = OpCompositeExtract %21 %41 1
105 %51 = OpCompositeExtract %21 %41 2
106 %52 = OpCompositeExtract %21 %41 3
107 %53 = OpCompositeExtract %21 %42 0
108 %54 = OpCompositeExtract %21 %42 1
109 %55 = OpCompositeExtract %21 %42 2
110 %56 = OpCompositeExtract %21 %42 3
111 %57 = OpCompositeExtract %21 %43 0
112 %58 = OpCompositeExtract %21 %43 1
113 %59 = OpCompositeExtract %21 %43 2
114 %60 = OpCompositeExtract %21 %43 3
115 %61 = OpCompositeConstruct %33 %45 %46 %47 %48
116 %62 = OpCompositeConstruct %33 %49 %50 %51 %52
117 %63 = OpCompositeConstruct %33 %53 %54 %55 %56
118 %64 = OpCompositeConstruct %33 %57 %58 %59 %60
119 %65 = OpCompositeConstruct %37 %61 %62 %63 %64
120 OpStore %39 %65
121 %69 = OpLoad %17 %19
122 %70 = OpLoad %21 %23
123 %71 = OpLoad %6 %30
124 %72 = OpLoad %33 %35
125 %73 = OpLoad %37 %39
126 %74 = OpCompositeConstruct %66 %69 %70 %71 %72 %73
127 OpStore %68 %74
128 OpReturn
129 OpFunctionEnd
130 %10 = OpFunction %6 None %8
131 %9 = OpFunctionParameter %7
132 %11 = OpLabel
133 %12 = OpVariable %7 Function
134 %13 = OpLoad %6 %9
135 OpStore %12 %13
136 %14 = OpLoad %6 %12
137 OpReturnValue %14
138 OpFunctionEnd
139 )";
140
141 const auto env = SPV_ENV_UNIVERSAL_1_3;
142 const auto consumer = nullptr;
143 const auto context = BuildModule(env, consumer, shader, kFuzzAssembleOption);
144 spvtools::ValidatorOptions validator_options;
145 validator_options.SetRelaxLogicalPointer(true);
146 ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(context.get(), validator_options,
147 kConsoleMessageConsumer));
148 TransformationContext transformation_context(
149 MakeUnique<FactManager>(context.get()), validator_options);
150 // Target id is not fresh (59).
151 ASSERT_FALSE(TransformationAddCopyMemory(
152 MakeInstructionDescriptor(27, spv::Op::OpFunctionCall, 0),
153 59, 19, spv::StorageClass::Private, 20)
154 .IsApplicable(context.get(), transformation_context));
155
156 // Instruction descriptor is invalid (id 90 is undefined).
157 ASSERT_FALSE(TransformationAddCopyMemory(
158 MakeInstructionDescriptor(90, spv::Op::OpVariable, 0), 90,
159 19, spv::StorageClass::Private, 20)
160 .IsApplicable(context.get(), transformation_context));
161
162 // Cannot insert OpCopyMemory before OpPhi.
163 ASSERT_FALSE(TransformationAddCopyMemory(
164 MakeInstructionDescriptor(75, spv::Op::OpPhi, 0), 90, 19,
165 spv::StorageClass::Private, 20)
166 .IsApplicable(context.get(), transformation_context));
167
168 // Source instruction is invalid.
169 ASSERT_FALSE(TransformationAddCopyMemory(
170 MakeInstructionDescriptor(27, spv::Op::OpFunctionCall, 0),
171 90, 76, spv::StorageClass::Private, 0)
172 .IsApplicable(context.get(), transformation_context));
173
174 // Source instruction's type doesn't exist.
175 ASSERT_FALSE(TransformationAddCopyMemory(
176 MakeInstructionDescriptor(27, spv::Op::OpFunctionCall, 0),
177 90, 5, spv::StorageClass::Private, 0)
178 .IsApplicable(context.get(), transformation_context));
179
180 // Source instruction's type is invalid.
181 ASSERT_FALSE(TransformationAddCopyMemory(
182 MakeInstructionDescriptor(41, spv::Op::OpLoad, 0), 90, 40,
183 spv::StorageClass::Private, 0)
184 .IsApplicable(context.get(), transformation_context));
185
186 // Source instruction is OpConstantNull.
187 ASSERT_FALSE(TransformationAddCopyMemory(
188 MakeInstructionDescriptor(41, spv::Op::OpLoad, 0), 90, 88,
189 spv::StorageClass::Private, 0)
190 .IsApplicable(context.get(), transformation_context));
191
192 // Storage class is invalid.
193 ASSERT_FALSE(TransformationAddCopyMemory(
194 MakeInstructionDescriptor(27, spv::Op::OpFunctionCall, 0),
195 90, 19, spv::StorageClass::Workgroup, 20)
196 .IsApplicable(context.get(), transformation_context));
197
198 // Initializer is 0.
199 ASSERT_FALSE(TransformationAddCopyMemory(
200 MakeInstructionDescriptor(27, spv::Op::OpFunctionCall, 0),
201 90, 19, spv::StorageClass::Private, 0)
202 .IsApplicable(context.get(), transformation_context));
203
204 // Initializer has wrong type.
205 ASSERT_FALSE(TransformationAddCopyMemory(
206 MakeInstructionDescriptor(27, spv::Op::OpFunctionCall, 0),
207 90, 19, spv::StorageClass::Private, 25)
208 .IsApplicable(context.get(), transformation_context));
209
210 // Source and target instructions are in different functions.
211 ASSERT_FALSE(TransformationAddCopyMemory(
212 MakeInstructionDescriptor(13, spv::Op::OpLoad, 0), 90, 19,
213 spv::StorageClass::Private, 20)
214 .IsApplicable(context.get(), transformation_context));
215
216 // Source instruction doesn't dominate the target instruction.
217 ASSERT_FALSE(TransformationAddCopyMemory(
218 MakeInstructionDescriptor(77, spv::Op::OpLogicalEqual, 0),
219 90, 89, spv::StorageClass::Private, 20)
220 .IsApplicable(context.get(), transformation_context));
221
222 // Source and target instructions are the same.
223 ASSERT_FALSE(TransformationAddCopyMemory(
224 MakeInstructionDescriptor(19, spv::Op::OpVariable, 0), 90,
225 19, spv::StorageClass::Private, 20)
226 .IsApplicable(context.get(), transformation_context));
227
228 // Correct transformations.
229 uint32_t fresh_id = 90;
230 auto descriptor = MakeInstructionDescriptor(27, spv::Op::OpFunctionCall, 0);
231 std::vector<uint32_t> source_ids = {19, 23, 26, 30, 35, 39, 68, 86};
232 std::vector<uint32_t> initializers = {20, 24, 25, 25, 36, 84, 85, 20};
233 std::vector<spv::StorageClass> storage_classes = {
234 spv::StorageClass::Private, spv::StorageClass::Function};
235 for (size_t i = 0, n = source_ids.size(); i < n; ++i) {
236 TransformationAddCopyMemory transformation(
237 descriptor, fresh_id, source_ids[i],
238 storage_classes[i % storage_classes.size()], initializers[i]);
239 ASSERT_TRUE(
240 transformation.IsApplicable(context.get(), transformation_context));
241 ApplyAndCheckFreshIds(transformation, context.get(),
242 &transformation_context);
243 ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(
244 context.get(), validator_options, kConsoleMessageConsumer));
245 ASSERT_TRUE(
246 transformation_context.GetFactManager()->PointeeValueIsIrrelevant(
247 fresh_id));
248 fresh_id++;
249 }
250
251 std::string expected = R"(
252 OpCapability Shader
253 OpCapability VariablePointers
254 %1 = OpExtInstImport "GLSL.std.450"
255 OpMemoryModel Logical GLSL450
256 OpEntryPoint Fragment %4 "main"
257 OpExecutionMode %4 OriginUpperLeft
258 OpSource ESSL 310
259 OpDecorate %19 RelaxedPrecision
260 OpMemberDecorate %66 0 RelaxedPrecision
261 OpDecorate %69 RelaxedPrecision
262 %2 = OpTypeVoid
263 %3 = OpTypeFunction %2
264 %6 = OpTypeBool
265 %7 = OpTypePointer Function %6
266 %78 = OpTypePointer Private %6
267 %8 = OpTypeFunction %6 %7
268 %17 = OpTypeInt 32 1
269 %18 = OpTypePointer Function %17
270 %79 = OpTypePointer Private %17
271 %20 = OpConstant %17 0
272 %21 = OpTypeFloat 32
273 %22 = OpTypePointer Function %21
274 %80 = OpTypePointer Private %21
275 %24 = OpConstant %21 0
276 %25 = OpConstantFalse %6
277 %32 = OpConstantTrue %6
278 %33 = OpTypeVector %21 4
279 %34 = OpTypePointer Function %33
280 %81 = OpTypePointer Private %33
281 %36 = OpConstantComposite %33 %24 %24 %24 %24
282 %37 = OpTypeMatrix %33 4
283 %84 = OpConstantComposite %37 %36 %36 %36 %36
284 %38 = OpTypePointer Function %37
285 %82 = OpTypePointer Private %37
286 %44 = OpConstant %21 1
287 %66 = OpTypeStruct %17 %21 %6 %33 %37
288 %85 = OpConstantComposite %66 %20 %24 %25 %36 %84
289 %67 = OpTypePointer Function %66
290 %83 = OpTypePointer Private %66
291 %86 = OpVariable %79 Private %20
292 %88 = OpConstantNull %79
293 %90 = OpVariable %79 Private %20
294 %92 = OpVariable %78 Private %25
295 %94 = OpVariable %81 Private %36
296 %96 = OpVariable %83 Private %85
297 %4 = OpFunction %2 None %3
298 %5 = OpLabel
299 %97 = OpVariable %18 Function %20
300 %95 = OpVariable %38 Function %84
301 %93 = OpVariable %7 Function %25
302 %91 = OpVariable %22 Function %24
303 %19 = OpVariable %18 Function
304 %23 = OpVariable %22 Function
305 %26 = OpVariable %7 Function
306 %30 = OpVariable %7 Function
307 %35 = OpVariable %34 Function
308 %39 = OpVariable %38 Function
309 %68 = OpVariable %67 Function
310 OpStore %19 %20
311 OpStore %23 %24
312 OpStore %26 %25
313 OpCopyMemory %90 %19
314 OpCopyMemory %91 %23
315 OpCopyMemory %92 %26
316 OpCopyMemory %93 %30
317 OpCopyMemory %94 %35
318 OpCopyMemory %95 %39
319 OpCopyMemory %96 %68
320 OpCopyMemory %97 %86
321 %27 = OpFunctionCall %6 %10 %26
322 OpSelectionMerge %29 None
323 OpBranchConditional %27 %28 %31
324 %28 = OpLabel
325 %89 = OpCopyObject %18 %19
326 OpBranch %29
327 %31 = OpLabel
328 OpBranch %29
329 %76 = OpLabel
330 %77 = OpLogicalEqual %6 %25 %32
331 OpBranch %29
332 %29 = OpLabel
333 %75 = OpPhi %6 %25 %31 %32 %28 %77 %76
334 OpStore %30 %75
335 %40 = OpLoad %33 %35
336 %41 = OpLoad %33 %35
337 %42 = OpLoad %33 %35
338 %43 = OpLoad %33 %35
339 %45 = OpCompositeExtract %21 %40 0
340 %46 = OpCompositeExtract %21 %40 1
341 %47 = OpCompositeExtract %21 %40 2
342 %48 = OpCompositeExtract %21 %40 3
343 %49 = OpCompositeExtract %21 %41 0
344 %50 = OpCompositeExtract %21 %41 1
345 %51 = OpCompositeExtract %21 %41 2
346 %52 = OpCompositeExtract %21 %41 3
347 %53 = OpCompositeExtract %21 %42 0
348 %54 = OpCompositeExtract %21 %42 1
349 %55 = OpCompositeExtract %21 %42 2
350 %56 = OpCompositeExtract %21 %42 3
351 %57 = OpCompositeExtract %21 %43 0
352 %58 = OpCompositeExtract %21 %43 1
353 %59 = OpCompositeExtract %21 %43 2
354 %60 = OpCompositeExtract %21 %43 3
355 %61 = OpCompositeConstruct %33 %45 %46 %47 %48
356 %62 = OpCompositeConstruct %33 %49 %50 %51 %52
357 %63 = OpCompositeConstruct %33 %53 %54 %55 %56
358 %64 = OpCompositeConstruct %33 %57 %58 %59 %60
359 %65 = OpCompositeConstruct %37 %61 %62 %63 %64
360 OpStore %39 %65
361 %69 = OpLoad %17 %19
362 %70 = OpLoad %21 %23
363 %71 = OpLoad %6 %30
364 %72 = OpLoad %33 %35
365 %73 = OpLoad %37 %39
366 %74 = OpCompositeConstruct %66 %69 %70 %71 %72 %73
367 OpStore %68 %74
368 OpReturn
369 OpFunctionEnd
370 %10 = OpFunction %6 None %8
371 %9 = OpFunctionParameter %7
372 %11 = OpLabel
373 %12 = OpVariable %7 Function
374 %13 = OpLoad %6 %9
375 OpStore %12 %13
376 %14 = OpLoad %6 %12
377 OpReturnValue %14
378 OpFunctionEnd
379 )";
380
381 ASSERT_TRUE(IsEqual(env, expected, context.get()));
382 }
383
384 TEST(TransformationAddCopyMemoryTest, DisallowBufferBlockDecoration) {
385 std::string shader = R"(
386 OpCapability Shader
387 %1 = OpExtInstImport "GLSL.std.450"
388 OpMemoryModel Logical GLSL450
389 OpEntryPoint GLCompute %4 "main"
390 OpExecutionMode %4 LocalSize 1 1 1
391 OpSource ESSL 320
392 OpName %4 "main"
393 OpName %7 "buf"
394 OpMemberName %7 0 "a"
395 OpMemberName %7 1 "b"
396 OpName %9 ""
397 OpMemberDecorate %7 0 Offset 0
398 OpMemberDecorate %7 1 Offset 4
399 OpDecorate %7 BufferBlock
400 OpDecorate %9 DescriptorSet 0
401 OpDecorate %9 Binding 0
402 %2 = OpTypeVoid
403 %3 = OpTypeFunction %2
404 %6 = OpTypeInt 32 1
405 %10 = OpConstant %6 42
406 %7 = OpTypeStruct %6 %6
407 %8 = OpTypePointer Uniform %7
408 %9 = OpVariable %8 Uniform
409 %50 = OpUndef %7
410 %4 = OpFunction %2 None %3
411 %5 = OpLabel
412 OpReturn
413 OpFunctionEnd
414 )";
415
416 const auto env = SPV_ENV_UNIVERSAL_1_0;
417 const auto consumer = nullptr;
418 const auto context = BuildModule(env, consumer, shader, kFuzzAssembleOption);
419 spvtools::ValidatorOptions validator_options;
420 ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(context.get(), validator_options,
421 kConsoleMessageConsumer));
422 TransformationContext transformation_context(
423 MakeUnique<FactManager>(context.get()), validator_options);
424 ASSERT_FALSE(TransformationAddCopyMemory(
425 MakeInstructionDescriptor(5, spv::Op::OpReturn, 0), 100, 9,
426 spv::StorageClass::Private, 50)
427 .IsApplicable(context.get(), transformation_context));
428 }
429
430 TEST(TransformationAddCopyMemoryTest, DisallowBlockDecoration) {
431 std::string shader = R"(
432 OpCapability Shader
433 %1 = OpExtInstImport "GLSL.std.450"
434 OpMemoryModel Logical GLSL450
435 OpEntryPoint GLCompute %4 "main" %9
436 OpExecutionMode %4 LocalSize 1 1 1
437 OpSource ESSL 320
438 OpName %4 "main"
439 OpName %7 "buf"
440 OpMemberName %7 0 "a"
441 OpMemberName %7 1 "b"
442 OpName %9 ""
443 OpMemberDecorate %7 0 Offset 0
444 OpMemberDecorate %7 1 Offset 4
445 OpDecorate %7 Block
446 OpDecorate %9 DescriptorSet 0
447 OpDecorate %9 Binding 0
448 %2 = OpTypeVoid
449 %3 = OpTypeFunction %2
450 %6 = OpTypeInt 32 1
451 %10 = OpConstant %6 42
452 %7 = OpTypeStruct %6 %6
453 %8 = OpTypePointer StorageBuffer %7
454 %9 = OpVariable %8 StorageBuffer
455 %50 = OpUndef %7
456 %4 = OpFunction %2 None %3
457 %5 = OpLabel
458 OpReturn
459 OpFunctionEnd
460 )";
461
462 const auto env = SPV_ENV_UNIVERSAL_1_5;
463 const auto consumer = nullptr;
464 const auto context = BuildModule(env, consumer, shader, kFuzzAssembleOption);
465 spvtools::ValidatorOptions validator_options;
466 ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(context.get(), validator_options,
467 kConsoleMessageConsumer));
468 TransformationContext transformation_context(
469 MakeUnique<FactManager>(context.get()), validator_options);
470 ASSERT_FALSE(TransformationAddCopyMemory(
471 MakeInstructionDescriptor(5, spv::Op::OpReturn, 0), 100, 9,
472 spv::StorageClass::Private, 50)
473 .IsApplicable(context.get(), transformation_context));
474 }
475
476 } // namespace
477 } // namespace fuzz
478 } // namespace spvtools