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_constant_boolean.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(TransformationAddConstantBooleanTest, NeitherPresentInitiallyAddBoth)25 TEST(TransformationAddConstantBooleanTest, NeitherPresentInitiallyAddBoth) {
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                OpName %4 "main"
34           %2 = OpTypeVoid
35           %6 = OpTypeBool
36           %3 = OpTypeFunction %2
37           %4 = OpFunction %2 None %3
38           %5 = OpLabel
39                OpReturn
40                OpFunctionEnd
41   )";
42 
43   const auto env = SPV_ENV_UNIVERSAL_1_3;
44   const auto consumer = nullptr;
45   const auto context = BuildModule(env, consumer, shader, kFuzzAssembleOption);
46   spvtools::ValidatorOptions validator_options;
47   ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(context.get(), validator_options,
48                                                kConsoleMessageConsumer));
49 
50   TransformationContext transformation_context(
51       MakeUnique<FactManager>(context.get()), validator_options);
52   // True and false can both be added as neither is present.
53   ASSERT_TRUE(TransformationAddConstantBoolean(7, true, false)
54                   .IsApplicable(context.get(), transformation_context));
55   ASSERT_TRUE(TransformationAddConstantBoolean(7, false, false)
56                   .IsApplicable(context.get(), transformation_context));
57 
58   // Irrelevant true and false can both be added as neither is present.
59   ASSERT_TRUE(TransformationAddConstantBoolean(7, true, true)
60                   .IsApplicable(context.get(), transformation_context));
61   ASSERT_TRUE(TransformationAddConstantBoolean(7, false, true)
62                   .IsApplicable(context.get(), transformation_context));
63 
64   // Id 5 is already taken.
65   ASSERT_FALSE(TransformationAddConstantBoolean(5, true, false)
66                    .IsApplicable(context.get(), transformation_context));
67 
68   auto add_true = TransformationAddConstantBoolean(7, true, false);
69   auto add_false = TransformationAddConstantBoolean(8, false, false);
70 
71   ASSERT_TRUE(add_true.IsApplicable(context.get(), transformation_context));
72   ASSERT_EQ(nullptr, context->get_def_use_mgr()->GetDef(7));
73   ASSERT_EQ(nullptr, context->get_constant_mgr()->FindDeclaredConstant(7));
74   ApplyAndCheckFreshIds(add_true, context.get(), &transformation_context);
75   ASSERT_EQ(spv::Op::OpConstantTrue,
76             context->get_def_use_mgr()->GetDef(7)->opcode());
77   ASSERT_TRUE(context->get_constant_mgr()
78                   ->FindDeclaredConstant(7)
79                   ->AsBoolConstant()
80                   ->value());
81   ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(context.get(), validator_options,
82                                                kConsoleMessageConsumer));
83 
84   // Having added true, we cannot add it again with the same id.
85   ASSERT_FALSE(add_true.IsApplicable(context.get(), transformation_context));
86   // But we can add it with a different id.
87   auto add_true_again = TransformationAddConstantBoolean(100, true, false);
88   ASSERT_TRUE(
89       add_true_again.IsApplicable(context.get(), transformation_context));
90   ApplyAndCheckFreshIds(add_true_again, context.get(), &transformation_context);
91   ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(context.get(), validator_options,
92                                                kConsoleMessageConsumer));
93 
94   ASSERT_TRUE(add_false.IsApplicable(context.get(), transformation_context));
95   ApplyAndCheckFreshIds(add_false, context.get(), &transformation_context);
96   ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(context.get(), validator_options,
97                                                kConsoleMessageConsumer));
98 
99   // Having added false, we cannot add it again with the same id.
100   ASSERT_FALSE(add_false.IsApplicable(context.get(), transformation_context));
101   // But we can add it with a different id.
102   auto add_false_again = TransformationAddConstantBoolean(101, false, false);
103   ASSERT_TRUE(
104       add_false_again.IsApplicable(context.get(), transformation_context));
105   ApplyAndCheckFreshIds(add_false_again, context.get(),
106                         &transformation_context);
107   ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(context.get(), validator_options,
108                                                kConsoleMessageConsumer));
109 
110   // We can create an irrelevant OpConstantTrue.
111   TransformationAddConstantBoolean irrelevant_true(102, true, true);
112   ASSERT_TRUE(
113       irrelevant_true.IsApplicable(context.get(), transformation_context));
114   ApplyAndCheckFreshIds(irrelevant_true, context.get(),
115                         &transformation_context);
116   ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(context.get(), validator_options,
117                                                kConsoleMessageConsumer));
118 
119   // We can create an irrelevant OpConstantFalse.
120   TransformationAddConstantBoolean irrelevant_false(103, false, true);
121   ASSERT_TRUE(
122       irrelevant_false.IsApplicable(context.get(), transformation_context));
123   ApplyAndCheckFreshIds(irrelevant_false, context.get(),
124                         &transformation_context);
125   ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(context.get(), validator_options,
126                                                kConsoleMessageConsumer));
127 
128   ASSERT_FALSE(transformation_context.GetFactManager()->IdIsIrrelevant(100));
129   ASSERT_FALSE(transformation_context.GetFactManager()->IdIsIrrelevant(101));
130   ASSERT_TRUE(transformation_context.GetFactManager()->IdIsIrrelevant(102));
131   ASSERT_TRUE(transformation_context.GetFactManager()->IdIsIrrelevant(103));
132 
133   std::string after_transformation = R"(
134                OpCapability Shader
135           %1 = OpExtInstImport "GLSL.std.450"
136                OpMemoryModel Logical GLSL450
137                OpEntryPoint Fragment %4 "main"
138                OpExecutionMode %4 OriginUpperLeft
139                OpSource ESSL 310
140                OpName %4 "main"
141           %2 = OpTypeVoid
142           %6 = OpTypeBool
143           %3 = OpTypeFunction %2
144           %7 = OpConstantTrue %6
145         %100 = OpConstantTrue %6
146           %8 = OpConstantFalse %6
147         %101 = OpConstantFalse %6
148         %102 = OpConstantTrue %6
149         %103 = OpConstantFalse %6
150           %4 = OpFunction %2 None %3
151           %5 = OpLabel
152                OpReturn
153                OpFunctionEnd
154   )";
155 
156   ASSERT_TRUE(IsEqual(env, after_transformation, context.get()));
157 }
158 
TEST(TransformationAddConstantBooleanTest, NoOpTypeBoolPresent)159 TEST(TransformationAddConstantBooleanTest, NoOpTypeBoolPresent) {
160   std::string shader = R"(
161                OpCapability Shader
162           %1 = OpExtInstImport "GLSL.std.450"
163                OpMemoryModel Logical GLSL450
164                OpEntryPoint Fragment %4 "main"
165                OpExecutionMode %4 OriginUpperLeft
166                OpSource ESSL 310
167                OpName %4 "main"
168           %2 = OpTypeVoid
169           %3 = OpTypeFunction %2
170           %4 = OpFunction %2 None %3
171           %5 = OpLabel
172                OpReturn
173                OpFunctionEnd
174   )";
175 
176   const auto env = SPV_ENV_UNIVERSAL_1_3;
177   const auto consumer = nullptr;
178   const auto context = BuildModule(env, consumer, shader, kFuzzAssembleOption);
179   spvtools::ValidatorOptions validator_options;
180   ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(context.get(), validator_options,
181                                                kConsoleMessageConsumer));
182   TransformationContext transformation_context(
183       MakeUnique<FactManager>(context.get()), validator_options);
184   // Neither true nor false can be added as OpTypeBool is not present.
185   ASSERT_FALSE(TransformationAddConstantBoolean(6, true, false)
186                    .IsApplicable(context.get(), transformation_context));
187   ASSERT_FALSE(TransformationAddConstantBoolean(6, false, false)
188                    .IsApplicable(context.get(), transformation_context));
189 
190   // This does not depend on whether the constant is relevant or not.
191   ASSERT_FALSE(TransformationAddConstantBoolean(6, true, true)
192                    .IsApplicable(context.get(), transformation_context));
193   ASSERT_FALSE(TransformationAddConstantBoolean(6, false, true)
194                    .IsApplicable(context.get(), transformation_context));
195 }
196 
197 }  // namespace
198 }  // namespace fuzz
199 }  // namespace spvtools
200