Debian Codegraph @

test/fuzz/transformation_mutate_pointer_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_mutate_pointer.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(TransformationMutatePointerTest, 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 %2 = OpTypeVoid
36 %3 = OpTypeFunction %2
37 %6 = OpTypeInt 32 1
38 %7 = OpTypeFloat 32
39 %34 = OpConstant %7 0
40 %36 = OpConstant %6 0
41 %14 = OpTypeVector %7 3
42 %35 = OpConstantComposite %14 %34 %34 %34
43 %15 = OpTypeMatrix %14 2
44 %8 = OpConstant %6 5
45 %9 = OpTypeArray %7 %8
46 %37 = OpConstantComposite %9 %34 %34 %34 %34 %34
47 %11 = OpTypeStruct
48 %38 = OpConstantComposite %11
49 %39 = OpConstantComposite %15 %35 %35
50 %31 = OpTypePointer Function %14
51 %10 = OpTypeStruct %7 %6 %9 %11 %15 %14
52 %40 = OpConstantComposite %10 %34 %36 %37 %38 %39 %35
53 %13 = OpTypePointer Function %10
54 %16 = OpTypePointer Private %10
55 %17 = OpTypePointer Workgroup %10
56 %18 = OpTypeStruct %16
57 %19 = OpTypePointer Private %18
58 %20 = OpVariable %16 Private
59 %21 = OpVariable %17 Workgroup
60 %22 = OpVariable %19 Private
61 %23 = OpTypePointer Output %6
62 %24 = OpVariable %23 Output
63 %27 = OpTypeFunction %2 %13
64 %33 = OpConstantNull %16
65 %4 = OpFunction %2 None %3
66 %5 = OpLabel
67 OpReturn
68 OpFunctionEnd
69 %28 = OpFunction %2 None %27
70 %29 = OpFunctionParameter %13
71 %30 = OpLabel
72 %25 = OpVariable %13 Function
73 %26 = OpAccessChain %31 %25 %8
74 OpReturn
75 OpFunctionEnd
76 )";
77
78 const auto env = SPV_ENV_UNIVERSAL_1_3;
79 const auto consumer = nullptr;
80 const auto context = BuildModule(env, consumer, shader, kFuzzAssembleOption);
81 spvtools::ValidatorOptions validator_options;
82 validator_options.SetRelaxLogicalPointer(true);
83 ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(context.get(), validator_options,
84 kConsoleMessageConsumer));
85 TransformationContext transformation_context(
86 MakeUnique<FactManager>(context.get()), validator_options);
87 transformation_context.GetFactManager()->AddFactIdIsIrrelevant(35);
88 transformation_context.GetFactManager()->AddFactIdIsIrrelevant(39);
89
90 const auto insert_before =
91 MakeInstructionDescriptor(26, spv::Op::OpReturn, 0);
92
93 // 20 is not a fresh id.
94 ASSERT_FALSE(TransformationMutatePointer(20, 20, insert_before)
95 .IsApplicable(context.get(), transformation_context));
96
97 // |insert_before| instruction descriptor is invalid.
98 ASSERT_FALSE(TransformationMutatePointer(
99 20, 70, MakeInstructionDescriptor(26, spv::Op::OpStore, 0))
100 .IsApplicable(context.get(), transformation_context));
101
102 // Can't insert OpLoad before OpVariable.
103 ASSERT_FALSE(
104 TransformationMutatePointer(
105 20, 70, MakeInstructionDescriptor(26, spv::Op::OpVariable, 0))
106 .IsApplicable(context.get(), transformation_context));
107
108 // |pointer_id| doesn't exist in the module.
109 ASSERT_FALSE(TransformationMutatePointer(70, 70, insert_before)
110 .IsApplicable(context.get(), transformation_context));
111
112 // |pointer_id| doesn't have a type id.
113 ASSERT_FALSE(TransformationMutatePointer(11, 70, insert_before)
114 .IsApplicable(context.get(), transformation_context));
115
116 // |pointer_id| is a result id of OpConstantNull.
117 ASSERT_FALSE(TransformationMutatePointer(33, 70, insert_before)
118 .IsApplicable(context.get(), transformation_context));
119
120 // |pointer_id| is not a pointer instruction.
121 ASSERT_FALSE(TransformationMutatePointer(8, 70, insert_before)
122 .IsApplicable(context.get(), transformation_context));
123
124 // |pointer_id| has invalid storage class
125 ASSERT_FALSE(TransformationMutatePointer(24, 70, insert_before)
126 .IsApplicable(context.get(), transformation_context));
127
128 // |pointer_id|'s pointee contains non-scalar and non-composite constituents.
129 ASSERT_FALSE(TransformationMutatePointer(22, 70, insert_before)
130 .IsApplicable(context.get(), transformation_context));
131
132 // There is no irrelevant zero constant to insert into the |pointer_id|.
133 ASSERT_FALSE(TransformationMutatePointer(20, 70, insert_before)
134 .IsApplicable(context.get(), transformation_context));
135
136 // |pointer_id| is not available before |insert_before|.
137 ASSERT_FALSE(
138 TransformationMutatePointer(
139 26, 70, MakeInstructionDescriptor(26, spv::Op::OpAccessChain, 0))
140 .IsApplicable(context.get(), transformation_context));
141
142 transformation_context.GetFactManager()->AddFactIdIsIrrelevant(40);
143
144 uint32_t fresh_id = 70;
145 uint32_t pointer_ids[] = {
146 20, // Mutate Private variable.
147 21, // Mutate Workgroup variable.
148 25, // Mutate Function variable.
149 29, // Mutate function parameter.
150 26, // Mutate OpAccessChain.
151 };
152
153 for (auto pointer_id : pointer_ids) {
154 TransformationMutatePointer transformation(pointer_id, fresh_id++,
155 insert_before);
156 ASSERT_TRUE(
157 transformation.IsApplicable(context.get(), transformation_context));
158 ApplyAndCheckFreshIds(transformation, context.get(),
159 &transformation_context);
160 ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(
161 context.get(), validator_options, kConsoleMessageConsumer));
162 }
163
164 std::string after_transformation = R"(
165 OpCapability Shader
166 OpCapability VariablePointers
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 = OpTypeFloat 32
176 %34 = OpConstant %7 0
177 %36 = OpConstant %6 0
178 %14 = OpTypeVector %7 3
179 %35 = OpConstantComposite %14 %34 %34 %34
180 %15 = OpTypeMatrix %14 2
181 %8 = OpConstant %6 5
182 %9 = OpTypeArray %7 %8
183 %37 = OpConstantComposite %9 %34 %34 %34 %34 %34
184 %11 = OpTypeStruct
185 %38 = OpConstantComposite %11
186 %39 = OpConstantComposite %15 %35 %35
187 %31 = OpTypePointer Function %14
188 %10 = OpTypeStruct %7 %6 %9 %11 %15 %14
189 %40 = OpConstantComposite %10 %34 %36 %37 %38 %39 %35
190 %13 = OpTypePointer Function %10
191 %16 = OpTypePointer Private %10
192 %17 = OpTypePointer Workgroup %10
193 %18 = OpTypeStruct %16
194 %19 = OpTypePointer Private %18
195 %20 = OpVariable %16 Private
196 %21 = OpVariable %17 Workgroup
197 %22 = OpVariable %19 Private
198 %23 = OpTypePointer Output %6
199 %24 = OpVariable %23 Output
200 %27 = OpTypeFunction %2 %13
201 %33 = OpConstantNull %16
202 %4 = OpFunction %2 None %3
203 %5 = OpLabel
204 OpReturn
205 OpFunctionEnd
206 %28 = OpFunction %2 None %27
207 %29 = OpFunctionParameter %13
208 %30 = OpLabel
209 %25 = OpVariable %13 Function
210 %26 = OpAccessChain %31 %25 %8
211
212 ; modified Private variable
213 %70 = OpLoad %10 %20
214 OpStore %20 %40
215 OpStore %20 %70
216
217 ; modified Workgroup variable
218 %71 = OpLoad %10 %21
219 OpStore %21 %40
220 OpStore %21 %71
221
222 ; modified Function variable
223 %72 = OpLoad %10 %25
224 OpStore %25 %40
225 OpStore %25 %72
226
227 ; modified function parameter
228 %73 = OpLoad %10 %29
229 OpStore %29 %40
230 OpStore %29 %73
231
232 ; modified OpAccessChain
233 %74 = OpLoad %14 %26
234 OpStore %26 %35
235 OpStore %26 %74
236
237 OpReturn
238 OpFunctionEnd
239 )";
240
241 ASSERT_TRUE(IsEqual(env, after_transformation, context.get()));
242 }
243
244 TEST(TransformationMutatePointerTest, HandlesUnreachableBlocks) {
245 std::string shader = R"(
246 OpCapability Shader
247 %1 = OpExtInstImport "GLSL.std.450"
248 OpMemoryModel Logical GLSL450
249 OpEntryPoint Fragment %4 "main"
250 OpExecutionMode %4 OriginUpperLeft
251 OpSource ESSL 310
252 %2 = OpTypeVoid
253 %3 = OpTypeFunction %2
254 %6 = OpTypeInt 32 1
255 %7 = OpConstant %6 0
256 %8 = OpTypePointer Function %6
257 %11 = OpTypePointer Private %6
258 %12 = OpVariable %11 Private
259 %4 = OpFunction %2 None %3
260 %5 = OpLabel
261 %9 = OpVariable %8 Function
262 OpReturn
263 %10 = OpLabel
264 OpReturn
265 OpFunctionEnd
266 )";
267
268 const auto env = SPV_ENV_UNIVERSAL_1_3;
269 const auto consumer = nullptr;
270 const auto context = BuildModule(env, consumer, shader, kFuzzAssembleOption);
271 spvtools::ValidatorOptions validator_options;
272 ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(context.get(), validator_options,
273 kConsoleMessageConsumer));
274 TransformationContext transformation_context(
275 MakeUnique<FactManager>(context.get()), validator_options);
276 transformation_context.GetFactManager()->AddFactIdIsIrrelevant(7);
277
278 ASSERT_FALSE(
279 context->GetDominatorAnalysis(context->GetFunction(4))->IsReachable(10));
280
281 const auto insert_before =
282 MakeInstructionDescriptor(10, spv::Op::OpReturn, 0);
283
284 // Can mutate a global variable in an unreachable block.
285 TransformationMutatePointer transformation(12, 50, insert_before);
286 ASSERT_TRUE(
287 transformation.IsApplicable(context.get(), transformation_context));
288 ApplyAndCheckFreshIds(transformation, context.get(), &transformation_context);
289 ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(context.get(), validator_options,
290 kConsoleMessageConsumer));
291
292 std::string after_transformation = R"(
293 OpCapability Shader
294 %1 = OpExtInstImport "GLSL.std.450"
295 OpMemoryModel Logical GLSL450
296 OpEntryPoint Fragment %4 "main"
297 OpExecutionMode %4 OriginUpperLeft
298 OpSource ESSL 310
299 %2 = OpTypeVoid
300 %3 = OpTypeFunction %2
301 %6 = OpTypeInt 32 1
302 %7 = OpConstant %6 0
303 %8 = OpTypePointer Function %6
304 %11 = OpTypePointer Private %6
305 %12 = OpVariable %11 Private
306 %4 = OpFunction %2 None %3
307 %5 = OpLabel
308 %9 = OpVariable %8 Function
309 OpReturn
310 %10 = OpLabel
311 %50 = OpLoad %6 %12
312 OpStore %12 %7
313 OpStore %12 %50
314 OpReturn
315 OpFunctionEnd
316 )";
317
318 ASSERT_TRUE(IsEqual(env, after_transformation, context.get()));
319 }
320
321 } // namespace
322 } // namespace fuzz
323 } // namespace spvtools