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_EQ(nullptr, context->get_def_use_mgr()->GetDef(105));
102 ASSERT_EQ(nullptr, context->get_instr_block(105));
103 ASSERT_TRUE(
104 transformation.IsApplicable(context.get(), transformation_context));
105 ApplyAndCheckFreshIds(transformation, context.get(),
106 &transformation_context);
107 ASSERT_EQ(SpvOpVariable, context->get_def_use_mgr()->GetDef(105)->opcode());
108 ASSERT_EQ(5, context->get_instr_block(105)->id());
109 }
110
111 // %104 = OpVariable %41 Function %46
112 {
113 TransformationAddLocalVariable transformation(104, 41, 4, 46, false);
114 ASSERT_TRUE(
115 transformation.IsApplicable(context.get(), transformation_context));
116 ApplyAndCheckFreshIds(transformation, context.get(),
117 &transformation_context);
118 }
119
120 // %103 = OpVariable %35 Function %38
121 {
122 TransformationAddLocalVariable transformation(103, 35, 4, 38, true);
123 ASSERT_TRUE(
124 transformation.IsApplicable(context.get(), transformation_context));
125 ApplyAndCheckFreshIds(transformation, context.get(),
126 &transformation_context);
127 }
128
129 // %102 = OpVariable %31 Function %33
130 {
131 TransformationAddLocalVariable transformation(102, 31, 4, 33, false);
132 ASSERT_TRUE(
133 transformation.IsApplicable(context.get(), transformation_context));
134 ApplyAndCheckFreshIds(transformation, context.get(),
135 &transformation_context);
136 }
137
138 // %101 = OpVariable %19 Function %29
139 {
140 TransformationAddLocalVariable transformation(101, 19, 4, 29, true);
141 ASSERT_TRUE(
142 transformation.IsApplicable(context.get(), transformation_context));
143 ApplyAndCheckFreshIds(transformation, context.get(),
144 &transformation_context);
145 }
146
147 // %100 = OpVariable %8 Function %12
148 {
149 TransformationAddLocalVariable transformation(100, 8, 4, 12, false);
150 ASSERT_TRUE(
151 transformation.IsApplicable(context.get(), transformation_context));
152 ApplyAndCheckFreshIds(transformation, context.get(),
153 &transformation_context);
154 }
155
156 ASSERT_FALSE(
157 transformation_context.GetFactManager()->PointeeValueIsIrrelevant(100));
158 ASSERT_TRUE(
159 transformation_context.GetFactManager()->PointeeValueIsIrrelevant(101));
160 ASSERT_FALSE(
161 transformation_context.GetFactManager()->PointeeValueIsIrrelevant(102));
162 ASSERT_TRUE(
163 transformation_context.GetFactManager()->PointeeValueIsIrrelevant(103));
164 ASSERT_FALSE(
165 transformation_context.GetFactManager()->PointeeValueIsIrrelevant(104));
166 ASSERT_TRUE(
167 transformation_context.GetFactManager()->PointeeValueIsIrrelevant(105));
168
169 std::string after_transformation = R"(
170 OpCapability Shader
171 %1 = OpExtInstImport "GLSL.std.450"
172 OpMemoryModel Logical GLSL450
173 OpEntryPoint Fragment %4 "main"
174 OpExecutionMode %4 OriginUpperLeft
175 OpSource ESSL 310
176 %2 = OpTypeVoid
177 %3 = OpTypeFunction %2
178 %6 = OpTypeInt 32 1
179 %7 = OpTypeStruct %6 %6
180 %8 = OpTypePointer Function %7
181 %10 = OpConstant %6 1
182 %11 = OpConstant %6 2
183 %12 = OpConstantComposite %7 %10 %11
184 %13 = OpTypeFloat 32
185 %14 = OpTypeInt 32 0
186 %15 = OpConstant %14 3
187 %16 = OpTypeArray %13 %15
188 %17 = OpTypeBool
189 %18 = OpTypeStruct %16 %7 %17
190 %19 = OpTypePointer Function %18
191 %21 = OpConstant %13 1
192 %22 = OpConstant %13 2
193 %23 = OpConstant %13 4
194 %24 = OpConstantComposite %16 %21 %22 %23
195 %25 = OpConstant %6 5
196 %26 = OpConstant %6 6
197 %27 = OpConstantComposite %7 %25 %26
198 %28 = OpConstantFalse %17
199 %29 = OpConstantComposite %18 %24 %27 %28
200 %30 = OpTypeVector %13 2
201 %31 = OpTypePointer Function %30
202 %33 = OpConstantComposite %30 %21 %21
203 %34 = OpTypeVector %17 3
204 %35 = OpTypePointer Function %34
205 %37 = OpConstantTrue %17
206 %38 = OpConstantComposite %34 %37 %28 %28
207 %39 = OpTypeVector %13 4
208 %40 = OpTypeMatrix %39 3
209 %41 = OpTypePointer Function %40
210 %43 = OpConstantComposite %39 %21 %22 %23 %21
211 %44 = OpConstantComposite %39 %22 %23 %21 %22
212 %45 = OpConstantComposite %39 %23 %21 %22 %23
213 %46 = OpConstantComposite %40 %43 %44 %45
214 %50 = OpTypePointer Function %14
215 %51 = OpConstantNull %14
216 %4 = OpFunction %2 None %3
217 %5 = OpLabel
218 %100 = OpVariable %8 Function %12
219 %101 = OpVariable %19 Function %29
220 %102 = OpVariable %31 Function %33
221 %103 = OpVariable %35 Function %38
222 %104 = OpVariable %41 Function %46
223 %105 = OpVariable %50 Function %51
224 OpReturn
225 OpFunctionEnd
226 )";
227 ASSERT_TRUE(IsEqual(env, after_transformation, context.get()));
228 }
229
230 } // namespace
231 } // namespace fuzz
232 } // namespace spvtools
233