• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/transformation_add_global_undef.h"
16 
17 #include "gtest/gtest.h"
18 #include "source/fuzz/fuzzer_util.h"
19 #include "test/fuzz/fuzz_test_util.h"
20 
21 namespace spvtools {
22 namespace fuzz {
23 namespace {
24 
TEST(TransformationAddGlobalUndefTest,BasicTest)25 TEST(TransformationAddGlobalUndefTest, BasicTest) {
26   std::string shader = R"(
27                OpCapability Shader
28           %1 = OpExtInstImport "GLSL.std.450"
29                OpMemoryModel Logical GLSL450
30                OpEntryPoint Fragment %4 "main"
31                OpExecutionMode %4 OriginUpperLeft
32                OpSource ESSL 310
33           %2 = OpTypeVoid
34           %3 = OpTypeFunction %2
35           %6 = OpTypeFloat 32
36           %7 = OpTypeInt 32 1
37           %8 = OpTypeVector %6 2
38           %9 = OpTypeVector %6 3
39          %10 = OpTypeVector %6 4
40          %11 = OpTypeVector %7 2
41           %4 = OpFunction %2 None %3
42           %5 = OpLabel
43                OpReturn
44                OpFunctionEnd
45   )";
46 
47   const auto env = SPV_ENV_UNIVERSAL_1_4;
48   const auto consumer = nullptr;
49   const auto context = BuildModule(env, consumer, shader, kFuzzAssembleOption);
50   spvtools::ValidatorOptions validator_options;
51   ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(context.get(), validator_options,
52                                                kConsoleMessageConsumer));
53   TransformationContext transformation_context(
54       MakeUnique<FactManager>(context.get()), validator_options);
55   // Id already in use
56   ASSERT_FALSE(TransformationAddGlobalUndef(4, 11).IsApplicable(
57       context.get(), transformation_context));
58   // %1 is not a type
59   ASSERT_FALSE(TransformationAddGlobalUndef(100, 1).IsApplicable(
60       context.get(), transformation_context));
61 
62   // %3 is a function type
63   ASSERT_FALSE(TransformationAddGlobalUndef(100, 3).IsApplicable(
64       context.get(), transformation_context));
65 
66   {
67     // %100 = OpUndef %6
68     TransformationAddGlobalUndef transformation(100, 6);
69     ASSERT_EQ(nullptr, context->get_def_use_mgr()->GetDef(100));
70     ASSERT_TRUE(
71         transformation.IsApplicable(context.get(), transformation_context));
72     ApplyAndCheckFreshIds(transformation, context.get(),
73                           &transformation_context);
74     ASSERT_EQ(spv::Op::OpUndef,
75               context->get_def_use_mgr()->GetDef(100)->opcode());
76   }
77 
78   TransformationAddGlobalUndef transformations[] = {
79 
80       // %101 = OpUndef %7
81       TransformationAddGlobalUndef(101, 7),
82 
83       // %102 = OpUndef %8
84       TransformationAddGlobalUndef(102, 8),
85 
86       // %103 = OpUndef %9
87       TransformationAddGlobalUndef(103, 9),
88 
89       // %104 = OpUndef %10
90       TransformationAddGlobalUndef(104, 10),
91 
92       // %105 = OpUndef %11
93       TransformationAddGlobalUndef(105, 11)};
94 
95   for (auto& transformation : transformations) {
96     ASSERT_TRUE(
97         transformation.IsApplicable(context.get(), transformation_context));
98     ApplyAndCheckFreshIds(transformation, context.get(),
99                           &transformation_context);
100   }
101   ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(context.get(), validator_options,
102                                                kConsoleMessageConsumer));
103 
104   std::string after_transformation = R"(
105                OpCapability Shader
106           %1 = OpExtInstImport "GLSL.std.450"
107                OpMemoryModel Logical GLSL450
108                OpEntryPoint Fragment %4 "main"
109                OpExecutionMode %4 OriginUpperLeft
110                OpSource ESSL 310
111           %2 = OpTypeVoid
112           %3 = OpTypeFunction %2
113           %6 = OpTypeFloat 32
114           %7 = OpTypeInt 32 1
115           %8 = OpTypeVector %6 2
116           %9 = OpTypeVector %6 3
117          %10 = OpTypeVector %6 4
118          %11 = OpTypeVector %7 2
119         %100 = OpUndef %6
120         %101 = OpUndef %7
121         %102 = OpUndef %8
122         %103 = OpUndef %9
123         %104 = OpUndef %10
124         %105 = OpUndef %11
125           %4 = OpFunction %2 None %3
126           %5 = OpLabel
127                OpReturn
128                OpFunctionEnd
129   )";
130   ASSERT_TRUE(IsEqual(env, after_transformation, context.get()));
131 }
132 
133 }  // namespace
134 }  // namespace fuzz
135 }  // namespace spvtools
136