transformation_add_constant_null_test.cpp 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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. #include "source/fuzz/transformation_add_constant_null.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(TransformationAddConstantNullTest, BasicTest) {
  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. %2 = OpTypeVoid
  30. %3 = OpTypeFunction %2
  31. %6 = OpTypeFloat 32
  32. %7 = OpTypeInt 32 1
  33. %8 = OpTypeVector %6 2
  34. %9 = OpTypeVector %6 3
  35. %10 = OpTypeVector %6 4
  36. %11 = OpTypeVector %7 2
  37. %20 = OpTypeSampler
  38. %21 = OpTypeImage %6 2D 0 0 0 0 Rgba32f
  39. %22 = OpTypeSampledImage %21
  40. %4 = OpFunction %2 None %3
  41. %5 = OpLabel
  42. OpReturn
  43. OpFunctionEnd
  44. )";
  45. const auto env = SPV_ENV_UNIVERSAL_1_4;
  46. const auto consumer = nullptr;
  47. const auto context = BuildModule(env, consumer, shader, kFuzzAssembleOption);
  48. spvtools::ValidatorOptions validator_options;
  49. ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(context.get(), validator_options,
  50. kConsoleMessageConsumer));
  51. TransformationContext transformation_context(
  52. MakeUnique<FactManager>(context.get()), validator_options);
  53. // Id already in use
  54. ASSERT_FALSE(TransformationAddConstantNull(4, 11).IsApplicable(
  55. context.get(), transformation_context));
  56. // %1 is not a type
  57. ASSERT_FALSE(TransformationAddConstantNull(100, 1).IsApplicable(
  58. context.get(), transformation_context));
  59. // %3 is a function type
  60. ASSERT_FALSE(TransformationAddConstantNull(100, 3).IsApplicable(
  61. context.get(), transformation_context));
  62. // %20 is a sampler type
  63. ASSERT_FALSE(TransformationAddConstantNull(100, 20).IsApplicable(
  64. context.get(), transformation_context));
  65. // %21 is an image type
  66. ASSERT_FALSE(TransformationAddConstantNull(100, 21).IsApplicable(
  67. context.get(), transformation_context));
  68. // %22 is a sampled image type
  69. ASSERT_FALSE(TransformationAddConstantNull(100, 22).IsApplicable(
  70. context.get(), transformation_context));
  71. {
  72. // %100 = OpConstantNull %6
  73. TransformationAddConstantNull transformation(100, 6);
  74. ASSERT_EQ(nullptr, context->get_def_use_mgr()->GetDef(100));
  75. ASSERT_EQ(nullptr, context->get_constant_mgr()->FindDeclaredConstant(100));
  76. ASSERT_TRUE(
  77. transformation.IsApplicable(context.get(), transformation_context));
  78. ApplyAndCheckFreshIds(transformation, context.get(),
  79. &transformation_context);
  80. ASSERT_EQ(spv::Op::OpConstantNull,
  81. context->get_def_use_mgr()->GetDef(100)->opcode());
  82. ASSERT_EQ(
  83. 0.0F,
  84. context->get_constant_mgr()->FindDeclaredConstant(100)->GetFloat());
  85. }
  86. TransformationAddConstantNull transformations[] = {
  87. // %101 = OpConstantNull %7
  88. TransformationAddConstantNull(101, 7),
  89. // %102 = OpConstantNull %8
  90. TransformationAddConstantNull(102, 8),
  91. // %103 = OpConstantNull %9
  92. TransformationAddConstantNull(103, 9),
  93. // %104 = OpConstantNull %10
  94. TransformationAddConstantNull(104, 10),
  95. // %105 = OpConstantNull %11
  96. TransformationAddConstantNull(105, 11)};
  97. for (auto& transformation : transformations) {
  98. ASSERT_TRUE(
  99. transformation.IsApplicable(context.get(), transformation_context));
  100. ApplyAndCheckFreshIds(transformation, context.get(),
  101. &transformation_context);
  102. }
  103. ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(context.get(), validator_options,
  104. kConsoleMessageConsumer));
  105. std::string after_transformation = R"(
  106. OpCapability Shader
  107. %1 = OpExtInstImport "GLSL.std.450"
  108. OpMemoryModel Logical GLSL450
  109. OpEntryPoint Fragment %4 "main"
  110. OpExecutionMode %4 OriginUpperLeft
  111. OpSource ESSL 310
  112. %2 = OpTypeVoid
  113. %3 = OpTypeFunction %2
  114. %6 = OpTypeFloat 32
  115. %7 = OpTypeInt 32 1
  116. %8 = OpTypeVector %6 2
  117. %9 = OpTypeVector %6 3
  118. %10 = OpTypeVector %6 4
  119. %11 = OpTypeVector %7 2
  120. %20 = OpTypeSampler
  121. %21 = OpTypeImage %6 2D 0 0 0 0 Rgba32f
  122. %22 = OpTypeSampledImage %21
  123. %100 = OpConstantNull %6
  124. %101 = OpConstantNull %7
  125. %102 = OpConstantNull %8
  126. %103 = OpConstantNull %9
  127. %104 = OpConstantNull %10
  128. %105 = OpConstantNull %11
  129. %4 = OpFunction %2 None %3
  130. %5 = OpLabel
  131. OpReturn
  132. OpFunctionEnd
  133. )";
  134. ASSERT_TRUE(IsEqual(env, after_transformation, context.get()));
  135. }
  136. } // namespace
  137. } // namespace fuzz
  138. } // namespace spvtools