transformation_add_constant_boolean_test.cpp 8.7 KB

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