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