aboutsummaryrefslogtreecommitdiff
path: root/test/fuzz/transformation_function_call_test.cpp
blob: b27b3ca978570afb1bfe2412c0ae510d25cce53d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
// Copyright (c) 2020 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#include "source/fuzz/transformation_function_call.h"

#include "gtest/gtest.h"
#include "source/fuzz/fuzzer_util.h"
#include "source/fuzz/instruction_descriptor.h"
#include "test/fuzz/fuzz_test_util.h"

namespace spvtools {
namespace fuzz {
namespace {

TEST(TransformationFunctionCallTest, BasicTest) {
  std::string shader = R"(
               OpCapability Shader
          %1 = OpExtInstImport "GLSL.std.450"
               OpMemoryModel Logical GLSL450
               OpEntryPoint Fragment %4 "main"
               OpExecutionMode %4 OriginUpperLeft
               OpSource ESSL 310
          %2 = OpTypeVoid
          %3 = OpTypeFunction %2
          %6 = OpTypeInt 32 1
          %7 = OpTypePointer Function %6
          %8 = OpTypeFunction %6 %7
         %12 = OpTypeFloat 32
         %13 = OpTypePointer Function %12
         %14 = OpTypeFunction %6 %7 %13
         %27 = OpConstant %6 1
         %50 = OpConstant %12 1
         %57 = OpTypeBool
         %58 = OpConstantFalse %57
        %204 = OpUndef %6
          %4 = OpFunction %2 None %3
          %5 = OpLabel
         %61 = OpVariable %7 Function
         %62 = OpVariable %7 Function
         %65 = OpVariable %13 Function
         %66 = OpVariable %7 Function
         %68 = OpVariable %13 Function
         %71 = OpVariable %7 Function
         %72 = OpVariable %13 Function
         %73 = OpVariable %7 Function
         %75 = OpVariable %13 Function
         %78 = OpVariable %7 Function
         %98 = OpAccessChain %7 %71
         %99 = OpCopyObject %7 %71
               OpSelectionMerge %60 None
               OpBranchConditional %58 %59 %60
         %59 = OpLabel
               OpBranch %60
         %60 = OpLabel
               OpReturn
               OpFunctionEnd
         %10 = OpFunction %6 None %8
          %9 = OpFunctionParameter %7
         %11 = OpLabel
         %26 = OpLoad %6 %9
         %28 = OpIAdd %6 %26 %27
               OpSelectionMerge %97 None
               OpBranchConditional %58 %96 %97
         %96 = OpLabel
               OpBranch %97
         %97 = OpLabel
               OpReturnValue %28
               OpFunctionEnd
         %17 = OpFunction %6 None %14
         %15 = OpFunctionParameter %7
         %16 = OpFunctionParameter %13
         %18 = OpLabel
         %31 = OpVariable %7 Function
         %32 = OpLoad %6 %15
               OpStore %31 %32
         %33 = OpFunctionCall %6 %10 %31
               OpReturnValue %33
               OpFunctionEnd
         %21 = OpFunction %6 None %14
         %19 = OpFunctionParameter %7
         %20 = OpFunctionParameter %13
         %22 = OpLabel
         %36 = OpLoad %6 %19
         %37 = OpLoad %12 %20
         %38 = OpConvertFToS %6 %37
         %39 = OpIAdd %6 %36 %38
               OpReturnValue %39
               OpFunctionEnd
         %24 = OpFunction %6 None %8
         %23 = OpFunctionParameter %7
         %25 = OpLabel
         %44 = OpVariable %7 Function
         %46 = OpVariable %13 Function
         %51 = OpVariable %7 Function
         %52 = OpVariable %13 Function
         %42 = OpLoad %6 %23
         %43 = OpConvertSToF %12 %42
         %45 = OpLoad %6 %23
               OpStore %44 %45
               OpStore %46 %43
         %47 = OpFunctionCall %6 %17 %44 %46
         %48 = OpLoad %6 %23
         %49 = OpIAdd %6 %48 %27
               OpStore %51 %49
               OpStore %52 %50
         %53 = OpFunctionCall %6 %17 %51 %52
         %54 = OpIAdd %6 %47 %53
               OpReturnValue %54
               OpFunctionEnd
        %200 = OpFunction %6 None %14
        %201 = OpFunctionParameter %7
        %202 = OpFunctionParameter %13
        %203 = OpLabel
               OpSelectionMerge %206 None
               OpBranchConditional %58 %205 %206
        %205 = OpLabel
               OpBranch %206
        %206 = OpLabel
               OpReturnValue %204
               OpFunctionEnd
  )";

  const auto env = SPV_ENV_UNIVERSAL_1_4;
  const auto consumer = nullptr;
  const auto context = BuildModule(env, consumer, shader, kFuzzAssembleOption);
  spvtools::ValidatorOptions validator_options;
  ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(context.get(), validator_options,
                                               kConsoleMessageConsumer));
  TransformationContext transformation_context(
      MakeUnique<FactManager>(context.get()), validator_options);
  transformation_context.GetFactManager()->AddFactBlockIsDead(59);
  transformation_context.GetFactManager()->AddFactBlockIsDead(11);
  transformation_context.GetFactManager()->AddFactBlockIsDead(18);
  transformation_context.GetFactManager()->AddFactBlockIsDead(25);
  transformation_context.GetFactManager()->AddFactBlockIsDead(96);
  transformation_context.GetFactManager()->AddFactBlockIsDead(205);
  transformation_context.GetFactManager()->AddFactFunctionIsLivesafe(21);
  transformation_context.GetFactManager()->AddFactFunctionIsLivesafe(200);
  transformation_context.GetFactManager()->AddFactValueOfPointeeIsIrrelevant(
      71);
  transformation_context.GetFactManager()->AddFactValueOfPointeeIsIrrelevant(
      72);
  transformation_context.GetFactManager()->AddFactValueOfPointeeIsIrrelevant(
      19);
  transformation_context.GetFactManager()->AddFactValueOfPointeeIsIrrelevant(
      20);
  transformation_context.GetFactManager()->AddFactValueOfPointeeIsIrrelevant(
      23);
  transformation_context.GetFactManager()->AddFactValueOfPointeeIsIrrelevant(
      44);
  transformation_context.GetFactManager()->AddFactValueOfPointeeIsIrrelevant(
      46);
  transformation_context.GetFactManager()->AddFactValueOfPointeeIsIrrelevant(
      51);
  transformation_context.GetFactManager()->AddFactValueOfPointeeIsIrrelevant(
      52);

  // Livesafe functions with argument types: 21(7, 13), 200(7, 13)
  // Non-livesafe functions with argument types: 4(), 10(7), 17(7, 13), 24(7)
  // Call graph edges:
  //    17 -> 10
  //    24 -> 17

  // Bad transformations
  // Too many arguments
  ASSERT_FALSE(
      TransformationFunctionCall(100, 21, {71, 72, 71},
                                 MakeInstructionDescriptor(59, SpvOpBranch, 0))
          .IsApplicable(context.get(), transformation_context));
  // Too few arguments
  ASSERT_FALSE(TransformationFunctionCall(
                   100, 21, {71}, MakeInstructionDescriptor(59, SpvOpBranch, 0))
                   .IsApplicable(context.get(), transformation_context));
  // Arguments are the wrong way around (types do not match)
  ASSERT_FALSE(
      TransformationFunctionCall(100, 21, {72, 71},
                                 MakeInstructionDescriptor(59, SpvOpBranch, 0))
          .IsApplicable(context.get(), transformation_context));
  // 21 is not an appropriate argument
  ASSERT_FALSE(
      TransformationFunctionCall(100, 21, {21, 72},
                                 MakeInstructionDescriptor(59, SpvOpBranch, 0))
          .IsApplicable(context.get(), transformation_context));
  // 300 does not exist
  ASSERT_FALSE(
      TransformationFunctionCall(100, 21, {300, 72},
                                 MakeInstructionDescriptor(59, SpvOpBranch, 0))
          .IsApplicable(context.get(), transformation_context));
  // 71 is not a function
  ASSERT_FALSE(
      TransformationFunctionCall(100, 71, {71, 72},
                                 MakeInstructionDescriptor(59, SpvOpBranch, 0))
          .IsApplicable(context.get(), transformation_context));
  // 500 does not exist
  ASSERT_FALSE(
      TransformationFunctionCall(100, 500, {71, 72},
                                 MakeInstructionDescriptor(59, SpvOpBranch, 0))
          .IsApplicable(context.get(), transformation_context));
  // Id is not fresh
  ASSERT_FALSE(
      TransformationFunctionCall(21, 21, {71, 72},
                                 MakeInstructionDescriptor(59, SpvOpBranch, 0))
          .IsApplicable(context.get(), transformation_context));
  // Access chain as pointer parameter
  ASSERT_FALSE(
      TransformationFunctionCall(100, 21, {98, 72},
                                 MakeInstructionDescriptor(59, SpvOpBranch, 0))
          .IsApplicable(context.get(), transformation_context));
  // Copied object as pointer parameter
  ASSERT_FALSE(
      TransformationFunctionCall(100, 21, {99, 72},
                                 MakeInstructionDescriptor(59, SpvOpBranch, 0))
          .IsApplicable(context.get(), transformation_context));
  // Non-livesafe called from original live block
  ASSERT_FALSE(
      TransformationFunctionCall(
          100, 10, {71}, MakeInstructionDescriptor(99, SpvOpSelectionMerge, 0))
          .IsApplicable(context.get(), transformation_context));
  // Non-livesafe called from livesafe function
  ASSERT_FALSE(
      TransformationFunctionCall(
          100, 10, {19}, MakeInstructionDescriptor(38, SpvOpConvertFToS, 0))
          .IsApplicable(context.get(), transformation_context));
  // Livesafe function called with pointer to non-arbitrary local variable
  ASSERT_FALSE(
      TransformationFunctionCall(
          100, 21, {61, 72}, MakeInstructionDescriptor(38, SpvOpConvertFToS, 0))
          .IsApplicable(context.get(), transformation_context));
  // Direct recursion
  ASSERT_FALSE(TransformationFunctionCall(
                   100, 4, {}, MakeInstructionDescriptor(59, SpvOpBranch, 0))
                   .IsApplicable(context.get(), transformation_context));
  // Indirect recursion
  ASSERT_FALSE(TransformationFunctionCall(
                   100, 24, {9}, MakeInstructionDescriptor(96, SpvOpBranch, 0))
                   .IsApplicable(context.get(), transformation_context));
  // Parameter 23 is not available at the call site
  ASSERT_FALSE(
      TransformationFunctionCall(104, 10, {23},
                                 MakeInstructionDescriptor(205, SpvOpBranch, 0))
          .IsApplicable(context.get(), transformation_context));

  // Good transformations
  {
    // Livesafe called from dead block: fine
    TransformationFunctionCall transformation(
        100, 21, {71, 72}, MakeInstructionDescriptor(59, SpvOpBranch, 0));
    ASSERT_TRUE(
        transformation.IsApplicable(context.get(), transformation_context));
    ApplyAndCheckFreshIds(transformation, context.get(),
                          &transformation_context);
    ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(
        context.get(), validator_options, kConsoleMessageConsumer));
  }
  {
    // Livesafe called from original live block: fine
    TransformationFunctionCall transformation(
        101, 21, {71, 72}, MakeInstructionDescriptor(98, SpvOpAccessChain, 0));
    ASSERT_TRUE(
        transformation.IsApplicable(context.get(), transformation_context));
    ApplyAndCheckFreshIds(transformation, context.get(),
                          &transformation_context);
    ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(
        context.get(), validator_options, kConsoleMessageConsumer));
  }
  {
    // Livesafe called from livesafe function: fine
    TransformationFunctionCall transformation(
        102, 200, {19, 20}, MakeInstructionDescriptor(36, SpvOpLoad, 0));
    ASSERT_TRUE(
        transformation.IsApplicable(context.get(), transformation_context));
    ApplyAndCheckFreshIds(transformation, context.get(),
                          &transformation_context);
    ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(
        context.get(), validator_options, kConsoleMessageConsumer));
  }
  {
    // Dead called from dead block in injected function: fine
    TransformationFunctionCall transformation(
        103, 10, {23}, MakeInstructionDescriptor(45, SpvOpLoad, 0));
    ASSERT_TRUE(
        transformation.IsApplicable(context.get(), transformation_context));
    ApplyAndCheckFreshIds(transformation, context.get(),
                          &transformation_context);
    ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(
        context.get(), validator_options, kConsoleMessageConsumer));
  }
  {
    // Non-livesafe called from dead block in livesafe function: OK
    TransformationFunctionCall transformation(
        104, 10, {201}, MakeInstructionDescriptor(205, SpvOpBranch, 0));
    ASSERT_TRUE(
        transformation.IsApplicable(context.get(), transformation_context));
    ApplyAndCheckFreshIds(transformation, context.get(),
                          &transformation_context);
    ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(
        context.get(), validator_options, kConsoleMessageConsumer));
  }
  {
    // Livesafe called from dead block with non-arbitrary parameter
    TransformationFunctionCall transformation(
        105, 21, {62, 65}, MakeInstructionDescriptor(59, SpvOpBranch, 0));
    ASSERT_TRUE(
        transformation.IsApplicable(context.get(), transformation_context));
    ApplyAndCheckFreshIds(transformation, context.get(),
                          &transformation_context);
    ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(
        context.get(), validator_options, kConsoleMessageConsumer));
  }

  std::string after_transformation = R"(
               OpCapability Shader
          %1 = OpExtInstImport "GLSL.std.450"
               OpMemoryModel Logical GLSL450
               OpEntryPoint Fragment %4 "main"
               OpExecutionMode %4 OriginUpperLeft
               OpSource ESSL 310
          %2 = OpTypeVoid
          %3 = OpTypeFunction %2
          %6 = OpTypeInt 32 1
          %7 = OpTypePointer Function %6
          %8 = OpTypeFunction %6 %7
         %12 = OpTypeFloat 32
         %13 = OpTypePointer Function %12
         %14 = OpTypeFunction %6 %7 %13
         %27 = OpConstant %6 1
         %50 = OpConstant %12 1
         %57 = OpTypeBool
         %58 = OpConstantFalse %57
        %204 = OpUndef %6
          %4 = OpFunction %2 None %3
          %5 = OpLabel
         %61 = OpVariable %7 Function
         %62 = OpVariable %7 Function
         %65 = OpVariable %13 Function
         %66 = OpVariable %7 Function
         %68 = OpVariable %13 Function
         %71 = OpVariable %7 Function
         %72 = OpVariable %13 Function
         %73 = OpVariable %7 Function
         %75 = OpVariable %13 Function
         %78 = OpVariable %7 Function
        %101 = OpFunctionCall %6 %21 %71 %72
         %98 = OpAccessChain %7 %71
         %99 = OpCopyObject %7 %71
               OpSelectionMerge %60 None
               OpBranchConditional %58 %59 %60
         %59 = OpLabel
        %100 = OpFunctionCall %6 %21 %71 %72
        %105 = OpFunctionCall %6 %21 %62 %65
               OpBranch %60
         %60 = OpLabel
               OpReturn
               OpFunctionEnd
         %10 = OpFunction %6 None %8
          %9 = OpFunctionParameter %7
         %11 = OpLabel
         %26 = OpLoad %6 %9
         %28 = OpIAdd %6 %26 %27
               OpSelectionMerge %97 None
               OpBranchConditional %58 %96 %97
         %96 = OpLabel
               OpBranch %97
         %97 = OpLabel
               OpReturnValue %28
               OpFunctionEnd
         %17 = OpFunction %6 None %14
         %15 = OpFunctionParameter %7
         %16 = OpFunctionParameter %13
         %18 = OpLabel
         %31 = OpVariable %7 Function
         %32 = OpLoad %6 %15
               OpStore %31 %32
         %33 = OpFunctionCall %6 %10 %31
               OpReturnValue %33
               OpFunctionEnd
         %21 = OpFunction %6 None %14
         %19 = OpFunctionParameter %7
         %20 = OpFunctionParameter %13
         %22 = OpLabel
        %102 = OpFunctionCall %6 %200 %19 %20
         %36 = OpLoad %6 %19
         %37 = OpLoad %12 %20
         %38 = OpConvertFToS %6 %37
         %39 = OpIAdd %6 %36 %38
               OpReturnValue %39
               OpFunctionEnd
         %24 = OpFunction %6 None %8
         %23 = OpFunctionParameter %7
         %25 = OpLabel
         %44 = OpVariable %7 Function
         %46 = OpVariable %13 Function
         %51 = OpVariable %7 Function
         %52 = OpVariable %13 Function
         %42 = OpLoad %6 %23
         %43 = OpConvertSToF %12 %42
        %103 = OpFunctionCall %6 %10 %23
         %45 = OpLoad %6 %23
               OpStore %44 %45
               OpStore %46 %43
         %47 = OpFunctionCall %6 %17 %44 %46
         %48 = OpLoad %6 %23
         %49 = OpIAdd %6 %48 %27
               OpStore %51 %49
               OpStore %52 %50
         %53 = OpFunctionCall %6 %17 %51 %52
         %54 = OpIAdd %6 %47 %53
               OpReturnValue %54
               OpFunctionEnd
        %200 = OpFunction %6 None %14
        %201 = OpFunctionParameter %7
        %202 = OpFunctionParameter %13
        %203 = OpLabel
               OpSelectionMerge %206 None
               OpBranchConditional %58 %205 %206
        %205 = OpLabel
        %104 = OpFunctionCall %6 %10 %201
               OpBranch %206
        %206 = OpLabel
               OpReturnValue %204
               OpFunctionEnd
  )";
  ASSERT_TRUE(IsEqual(env, after_transformation, context.get()));
}

TEST(TransformationFunctionCallTest, DoNotInvokeEntryPoint) {
  std::string shader = R"(
               OpCapability Shader
          %1 = OpExtInstImport "GLSL.std.450"
               OpMemoryModel Logical GLSL450
               OpEntryPoint Fragment %4 "main"
               OpExecutionMode %4 OriginUpperLeft
               OpSource ESSL 310
          %2 = OpTypeVoid
          %3 = OpTypeFunction %2
          %4 = OpFunction %2 None %3
          %5 = OpLabel
               OpReturn
               OpFunctionEnd
         %10 = OpFunction %2 None %3
         %11 = OpLabel
               OpReturn
               OpFunctionEnd
  )";

  const auto env = SPV_ENV_UNIVERSAL_1_4;
  const auto consumer = nullptr;
  const auto context = BuildModule(env, consumer, shader, kFuzzAssembleOption);
  spvtools::ValidatorOptions validator_options;
  ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(context.get(), validator_options,
                                               kConsoleMessageConsumer));
  TransformationContext transformation_context(
      MakeUnique<FactManager>(context.get()), validator_options);
  transformation_context.GetFactManager()->AddFactBlockIsDead(11);

  // 4 is an entry point, so it is not legal for it to be the target of a call.
  ASSERT_FALSE(TransformationFunctionCall(
                   100, 4, {}, MakeInstructionDescriptor(11, SpvOpReturn, 0))
                   .IsApplicable(context.get(), transformation_context));
}

}  // namespace
}  // namespace fuzz
}  // namespace spvtools