Debian Codegraph @

test/fuzz/transformation_replace_copy_object_with_store_load_test.cpp

raw
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_replace_copy_object_with_store_load.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
25 TEST(TransformationReplaceCopyObjectWithStoreLoad, BasicScenarios) {
26 std::string reference_shader = R"(
27 OpCapability Shader
28 %1 = OpExtInstImport "GLSL.std.450"
29 OpMemoryModel Logical GLSL450
30 OpEntryPoint Fragment %4 "main" %23
31 OpExecutionMode %4 OriginUpperLeft
32 OpSource ESSL 310
33 OpName %4 "main"
34 OpName %8 "a"
35 OpName %10 "b"
36 OpName %14 "c"
37 OpName %16 "d"
38 OpName %18 "e"
39 OpName %23 "f"
40 %2 = OpTypeVoid
41 %3 = OpTypeFunction %2
42 %6 = OpTypeInt 32 1
43 %7 = OpTypePointer Function %6
44 %9 = OpConstant %6 2
45 %11 = OpConstant %6 3
46 %12 = OpTypeFloat 32
47 %13 = OpTypePointer Function %12
48 %15 = OpConstant %12 2
49 %17 = OpConstant %12 3
50 %22 = OpTypePointer Private %12
51 %23 = OpVariable %22 Private
52 %4 = OpFunction %2 None %3
53 %5 = OpLabel
54 %8 = OpVariable %7 Function
55 %10 = OpVariable %7 Function
56 %14 = OpVariable %13 Function
57 %16 = OpVariable %13 Function
58 %18 = OpVariable %7 Function
59 OpStore %8 %9
60 OpStore %10 %11
61 OpStore %14 %15
62 OpStore %16 %17
63 %19 = OpLoad %6 %8
64 %20 = OpLoad %6 %10
65 %21 = OpIAdd %6 %19 %20
66 OpStore %18 %21
67 %24 = OpLoad %12 %14
68 %25 = OpLoad %12 %16
69 %26 = OpFMul %12 %24 %25
70 OpStore %23 %26
71 %27 = OpCopyObject %6 %21
72 %28 = OpCopyObject %12 %26
73 %40 = OpCopyObject %13 %14
74 OpReturn
75 OpFunctionEnd
76 )";
77
78 const auto env = SPV_ENV_UNIVERSAL_1_4;
79 const auto consumer = nullptr;
80 const auto context =
81 BuildModule(env, consumer, reference_shader, kFuzzAssembleOption);
82
83 spvtools::ValidatorOptions validator_options;
84 TransformationContext transformation_context(
85 MakeUnique<FactManager>(context.get()), validator_options);
86 ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(context.get(), validator_options,
87 kConsoleMessageConsumer));
88
89 // Invalid: fresh_variable_id=10 is not fresh.
90 auto transformation_invalid_1 = TransformationReplaceCopyObjectWithStoreLoad(
91 27, 10, (uint32_t)spv::StorageClass::Function, 9);
92 ASSERT_FALSE(transformation_invalid_1.IsApplicable(context.get(),
93 transformation_context));
94
95 // Invalid: copy_object_result_id=26 is not a CopyObject instruction.
96 auto transformation_invalid_2 = TransformationReplaceCopyObjectWithStoreLoad(
97 26, 30, (uint32_t)spv::StorageClass::Function, 9);
98 ASSERT_FALSE(transformation_invalid_2.IsApplicable(context.get(),
99 transformation_context));
100
101 // Invalid: copy_object_result_id=40 is of type pointer.
102 auto transformation_invalid_3 = TransformationReplaceCopyObjectWithStoreLoad(
103 40, 30, (uint32_t)spv::StorageClass::Function, 9);
104 ASSERT_FALSE(transformation_invalid_3.IsApplicable(context.get(),
105 transformation_context));
106
107 // Invalid: Pointer type instruction in this storage class pointing to the
108 // value type is not defined.
109 auto transformation_invalid_4 = TransformationReplaceCopyObjectWithStoreLoad(
110 40, 30, (uint32_t)spv::StorageClass::Private, 9);
111 ASSERT_FALSE(transformation_invalid_4.IsApplicable(context.get(),
112 transformation_context));
113
114 // Invalid: initializer_id=15 has the wrong type relative to the OpCopyObject
115 // instruction.
116 auto transformation_invalid_5 = TransformationReplaceCopyObjectWithStoreLoad(
117 27, 30, (uint32_t)spv::StorageClass::Function, 15);
118 ASSERT_FALSE(transformation_invalid_5.IsApplicable(context.get(),
119 transformation_context));
120
121 // Invalid: spv::StorageClass::Uniform is not applicable to the
122 // transformation.
123 auto transformation_invalid_6 = TransformationReplaceCopyObjectWithStoreLoad(
124 27, 30, (uint32_t)spv::StorageClass::Uniform, 9);
125 ASSERT_FALSE(transformation_invalid_6.IsApplicable(context.get(),
126 transformation_context));
127
128 auto transformation_valid_1 = TransformationReplaceCopyObjectWithStoreLoad(
129 27, 30, (uint32_t)spv::StorageClass::Function, 9);
130 ASSERT_TRUE(transformation_valid_1.IsApplicable(context.get(),
131 transformation_context));
132 ApplyAndCheckFreshIds(transformation_valid_1, context.get(),
133 &transformation_context);
134 ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(context.get(), validator_options,
135 kConsoleMessageConsumer));
136
137 auto transformation_valid_2 = TransformationReplaceCopyObjectWithStoreLoad(
138 28, 32, (uint32_t)spv::StorageClass::Private, 15);
139 ASSERT_TRUE(transformation_valid_2.IsApplicable(context.get(),
140 transformation_context));
141 ApplyAndCheckFreshIds(transformation_valid_2, context.get(),
142 &transformation_context);
143 ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(context.get(), validator_options,
144 kConsoleMessageConsumer));
145
146 std::string after_transformation = R"(
147 OpCapability Shader
148 %1 = OpExtInstImport "GLSL.std.450"
149 OpMemoryModel Logical GLSL450
150 OpEntryPoint Fragment %4 "main" %23 %32
151 OpExecutionMode %4 OriginUpperLeft
152 OpSource ESSL 310
153 OpName %4 "main"
154 OpName %8 "a"
155 OpName %10 "b"
156 OpName %14 "c"
157 OpName %16 "d"
158 OpName %18 "e"
159 OpName %23 "f"
160 %2 = OpTypeVoid
161 %3 = OpTypeFunction %2
162 %6 = OpTypeInt 32 1
163 %7 = OpTypePointer Function %6
164 %9 = OpConstant %6 2
165 %11 = OpConstant %6 3
166 %12 = OpTypeFloat 32
167 %13 = OpTypePointer Function %12
168 %15 = OpConstant %12 2
169 %17 = OpConstant %12 3
170 %22 = OpTypePointer Private %12
171 %23 = OpVariable %22 Private
172 %32 = OpVariable %22 Private %15
173 %4 = OpFunction %2 None %3
174 %5 = OpLabel
175 %30 = OpVariable %7 Function %9
176 %8 = OpVariable %7 Function
177 %10 = OpVariable %7 Function
178 %14 = OpVariable %13 Function
179 %16 = OpVariable %13 Function
180 %18 = OpVariable %7 Function
181 OpStore %8 %9
182 OpStore %10 %11
183 OpStore %14 %15
184 OpStore %16 %17
185 %19 = OpLoad %6 %8
186 %20 = OpLoad %6 %10
187 %21 = OpIAdd %6 %19 %20
188 OpStore %18 %21
189 %24 = OpLoad %12 %14
190 %25 = OpLoad %12 %16
191 %26 = OpFMul %12 %24 %25
192 OpStore %23 %26
193 OpStore %30 %21
194 %27 = OpLoad %6 %30
195 OpStore %32 %26
196 %28 = OpLoad %12 %32
197 %40 = OpCopyObject %13 %14
198 OpReturn
199 OpFunctionEnd
200 )";
201 ASSERT_TRUE(IsEqual(env, after_transformation, context.get()));
202 }
203
204 TEST(TransformationReplaceCopyObjectWithStoreLoad, IrrelevantIdsAndDeadBlocks) {
205 std::string reference_shader = R"(
206 OpCapability Shader
207 %1 = OpExtInstImport "GLSL.std.450"
208 OpMemoryModel Logical GLSL450
209 OpEntryPoint Fragment %4 "main"
210 OpExecutionMode %4 OriginUpperLeft
211 OpSource ESSL 320
212 %2 = OpTypeVoid
213 %3 = OpTypeFunction %2
214 %6 = OpTypeInt 32 1
215 %30 = OpTypePointer Function %6
216 %10 = OpConstant %6 0
217 %11 = OpConstant %6 1
218 %13 = OpTypeBool
219 %14 = OpConstantFalse %13
220 %4 = OpFunction %2 None %3
221 %5 = OpLabel
222 OpSelectionMerge %16 None
223 OpBranchConditional %14 %15 %16
224 %15 = OpLabel
225 %50 = OpCopyObject %6 %10
226 OpBranch %16
227 %16 = OpLabel
228 %51 = OpCopyObject %6 %11
229 OpReturn
230 OpFunctionEnd
231 )";
232
233 const auto env = SPV_ENV_UNIVERSAL_1_4;
234 const auto consumer = nullptr;
235 const auto context =
236 BuildModule(env, consumer, reference_shader, kFuzzAssembleOption);
237
238 spvtools::ValidatorOptions validator_options;
239 TransformationContext transformation_context(
240 MakeUnique<FactManager>(context.get()), validator_options);
241 ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(context.get(), validator_options,
242 kConsoleMessageConsumer));
243
244 transformation_context.GetFactManager()->AddFactBlockIsDead(15);
245 transformation_context.GetFactManager()->AddFactIdIsIrrelevant(11);
246
247 auto transformation_1 = TransformationReplaceCopyObjectWithStoreLoad(
248 50, 100, (uint32_t)spv::StorageClass::Function, 10);
249 ASSERT_TRUE(
250 transformation_1.IsApplicable(context.get(), transformation_context));
251 ApplyAndCheckFreshIds(transformation_1, context.get(),
252 &transformation_context);
253 ASSERT_FALSE(transformation_context.GetFactManager()->IsSynonymous(
254 MakeDataDescriptor(100, {}), MakeDataDescriptor(50, {})));
255
256 auto transformation_2 = TransformationReplaceCopyObjectWithStoreLoad(
257 51, 101, (uint32_t)spv::StorageClass::Function, 10);
258 ASSERT_TRUE(
259 transformation_2.IsApplicable(context.get(), transformation_context));
260 ApplyAndCheckFreshIds(transformation_2, context.get(),
261 &transformation_context);
262 ASSERT_FALSE(transformation_context.GetFactManager()->IsSynonymous(
263 MakeDataDescriptor(101, {}), MakeDataDescriptor(51, {})));
264 }
265
266 } // namespace
267 } // namespace fuzz
268 } // namespace spvtools