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_add_local_variable.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(TransformationAddLocalVariableTest,BasicTest)25 TEST(TransformationAddLocalVariableTest, 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 = OpTypeInt 32 1
36           %7 = OpTypeStruct %6 %6
37           %8 = OpTypePointer Function %7
38          %10 = OpConstant %6 1
39          %11 = OpConstant %6 2
40          %12 = OpConstantComposite %7 %10 %11
41          %13 = OpTypeFloat 32
42          %14 = OpTypeInt 32 0
43          %15 = OpConstant %14 3
44          %16 = OpTypeArray %13 %15
45          %17 = OpTypeBool
46          %18 = OpTypeStruct %16 %7 %17
47          %19 = OpTypePointer Function %18
48          %21 = OpConstant %13 1
49          %22 = OpConstant %13 2
50          %23 = OpConstant %13 4
51          %24 = OpConstantComposite %16 %21 %22 %23
52          %25 = OpConstant %6 5
53          %26 = OpConstant %6 6
54          %27 = OpConstantComposite %7 %25 %26
55          %28 = OpConstantFalse %17
56          %29 = OpConstantComposite %18 %24 %27 %28
57          %30 = OpTypeVector %13 2
58          %31 = OpTypePointer Function %30
59          %33 = OpConstantComposite %30 %21 %21
60          %34 = OpTypeVector %17 3
61          %35 = OpTypePointer Function %34
62          %37 = OpConstantTrue %17
63          %38 = OpConstantComposite %34 %37 %28 %28
64          %39 = OpTypeVector %13 4
65          %40 = OpTypeMatrix %39 3
66          %41 = OpTypePointer Function %40
67          %43 = OpConstantComposite %39 %21 %22 %23 %21
68          %44 = OpConstantComposite %39 %22 %23 %21 %22
69          %45 = OpConstantComposite %39 %23 %21 %22 %23
70          %46 = OpConstantComposite %40 %43 %44 %45
71          %50 = OpTypePointer Function %14
72          %51 = OpConstantNull %14
73           %4 = OpFunction %2 None %3
74           %5 = OpLabel
75                OpReturn
76                OpFunctionEnd
77   )";
78 
79   const auto env = SPV_ENV_UNIVERSAL_1_4;
80   const auto consumer = nullptr;
81   const auto context = BuildModule(env, consumer, shader, kFuzzAssembleOption);
82   spvtools::ValidatorOptions validator_options;
83   ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(context.get(), validator_options,
84                                                kConsoleMessageConsumer));
85   TransformationContext transformation_context(
86       MakeUnique<FactManager>(context.get()), validator_options);
87   // A few cases of inapplicable transformations:
88   // Id 4 is already in use
89   ASSERT_FALSE(TransformationAddLocalVariable(4, 50, 4, 51, true)
90                    .IsApplicable(context.get(), transformation_context));
91   // Type mismatch between initializer and pointer
92   ASSERT_FALSE(TransformationAddLocalVariable(105, 46, 4, 51, true)
93                    .IsApplicable(context.get(), transformation_context));
94   // Id 5 is not a function
95   ASSERT_FALSE(TransformationAddLocalVariable(105, 50, 5, 51, true)
96                    .IsApplicable(context.get(), transformation_context));
97 
98   // %105 = OpVariable %50 Function %51
99   {
100     TransformationAddLocalVariable transformation(105, 50, 4, 51, true);
101     ASSERT_TRUE(
102         transformation.IsApplicable(context.get(), transformation_context));
103     ApplyAndCheckFreshIds(transformation, context.get(),
104                           &transformation_context);
105   }
106 
107   // %104 = OpVariable %41 Function %46
108   {
109     TransformationAddLocalVariable transformation(104, 41, 4, 46, false);
110     ASSERT_TRUE(
111         transformation.IsApplicable(context.get(), transformation_context));
112     ApplyAndCheckFreshIds(transformation, context.get(),
113                           &transformation_context);
114   }
115 
116   // %103 = OpVariable %35 Function %38
117   {
118     TransformationAddLocalVariable transformation(103, 35, 4, 38, true);
119     ASSERT_TRUE(
120         transformation.IsApplicable(context.get(), transformation_context));
121     ApplyAndCheckFreshIds(transformation, context.get(),
122                           &transformation_context);
123   }
124 
125   // %102 = OpVariable %31 Function %33
126   {
127     TransformationAddLocalVariable transformation(102, 31, 4, 33, false);
128     ASSERT_TRUE(
129         transformation.IsApplicable(context.get(), transformation_context));
130     ApplyAndCheckFreshIds(transformation, context.get(),
131                           &transformation_context);
132   }
133 
134   // %101 = OpVariable %19 Function %29
135   {
136     TransformationAddLocalVariable transformation(101, 19, 4, 29, true);
137     ASSERT_TRUE(
138         transformation.IsApplicable(context.get(), transformation_context));
139     ApplyAndCheckFreshIds(transformation, context.get(),
140                           &transformation_context);
141   }
142 
143   // %100 = OpVariable %8 Function %12
144   {
145     TransformationAddLocalVariable transformation(100, 8, 4, 12, false);
146     ASSERT_TRUE(
147         transformation.IsApplicable(context.get(), transformation_context));
148     ApplyAndCheckFreshIds(transformation, context.get(),
149                           &transformation_context);
150   }
151 
152   ASSERT_FALSE(
153       transformation_context.GetFactManager()->PointeeValueIsIrrelevant(100));
154   ASSERT_TRUE(
155       transformation_context.GetFactManager()->PointeeValueIsIrrelevant(101));
156   ASSERT_FALSE(
157       transformation_context.GetFactManager()->PointeeValueIsIrrelevant(102));
158   ASSERT_TRUE(
159       transformation_context.GetFactManager()->PointeeValueIsIrrelevant(103));
160   ASSERT_FALSE(
161       transformation_context.GetFactManager()->PointeeValueIsIrrelevant(104));
162   ASSERT_TRUE(
163       transformation_context.GetFactManager()->PointeeValueIsIrrelevant(105));
164 
165   std::string after_transformation = R"(
166                OpCapability Shader
167           %1 = OpExtInstImport "GLSL.std.450"
168                OpMemoryModel Logical GLSL450
169                OpEntryPoint Fragment %4 "main"
170                OpExecutionMode %4 OriginUpperLeft
171                OpSource ESSL 310
172           %2 = OpTypeVoid
173           %3 = OpTypeFunction %2
174           %6 = OpTypeInt 32 1
175           %7 = OpTypeStruct %6 %6
176           %8 = OpTypePointer Function %7
177          %10 = OpConstant %6 1
178          %11 = OpConstant %6 2
179          %12 = OpConstantComposite %7 %10 %11
180          %13 = OpTypeFloat 32
181          %14 = OpTypeInt 32 0
182          %15 = OpConstant %14 3
183          %16 = OpTypeArray %13 %15
184          %17 = OpTypeBool
185          %18 = OpTypeStruct %16 %7 %17
186          %19 = OpTypePointer Function %18
187          %21 = OpConstant %13 1
188          %22 = OpConstant %13 2
189          %23 = OpConstant %13 4
190          %24 = OpConstantComposite %16 %21 %22 %23
191          %25 = OpConstant %6 5
192          %26 = OpConstant %6 6
193          %27 = OpConstantComposite %7 %25 %26
194          %28 = OpConstantFalse %17
195          %29 = OpConstantComposite %18 %24 %27 %28
196          %30 = OpTypeVector %13 2
197          %31 = OpTypePointer Function %30
198          %33 = OpConstantComposite %30 %21 %21
199          %34 = OpTypeVector %17 3
200          %35 = OpTypePointer Function %34
201          %37 = OpConstantTrue %17
202          %38 = OpConstantComposite %34 %37 %28 %28
203          %39 = OpTypeVector %13 4
204          %40 = OpTypeMatrix %39 3
205          %41 = OpTypePointer Function %40
206          %43 = OpConstantComposite %39 %21 %22 %23 %21
207          %44 = OpConstantComposite %39 %22 %23 %21 %22
208          %45 = OpConstantComposite %39 %23 %21 %22 %23
209          %46 = OpConstantComposite %40 %43 %44 %45
210          %50 = OpTypePointer Function %14
211          %51 = OpConstantNull %14
212           %4 = OpFunction %2 None %3
213           %5 = OpLabel
214         %100 = OpVariable %8 Function %12
215         %101 = OpVariable %19 Function %29
216         %102 = OpVariable %31 Function %33
217         %103 = OpVariable %35 Function %38
218         %104 = OpVariable %41 Function %46
219         %105 = OpVariable %50 Function %51
220                OpReturn
221                OpFunctionEnd
222   )";
223   ASSERT_TRUE(IsEqual(env, after_transformation, context.get()));
224 }
225 
226 }  // namespace
227 }  // namespace fuzz
228 }  // namespace spvtools
229