transformation_add_global_undef_test.cpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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_global_undef.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(TransformationAddGlobalUndefTest, 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. %4 = OpFunction %2 None %3
  38. %5 = OpLabel
  39. OpReturn
  40. OpFunctionEnd
  41. )";
  42. const auto env = SPV_ENV_UNIVERSAL_1_4;
  43. const auto consumer = nullptr;
  44. const auto context = BuildModule(env, consumer, shader, kFuzzAssembleOption);
  45. spvtools::ValidatorOptions validator_options;
  46. ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(context.get(), validator_options,
  47. kConsoleMessageConsumer));
  48. TransformationContext transformation_context(
  49. MakeUnique<FactManager>(context.get()), validator_options);
  50. // Id already in use
  51. ASSERT_FALSE(TransformationAddGlobalUndef(4, 11).IsApplicable(
  52. context.get(), transformation_context));
  53. // %1 is not a type
  54. ASSERT_FALSE(TransformationAddGlobalUndef(100, 1).IsApplicable(
  55. context.get(), transformation_context));
  56. // %3 is a function type
  57. ASSERT_FALSE(TransformationAddGlobalUndef(100, 3).IsApplicable(
  58. context.get(), transformation_context));
  59. {
  60. // %100 = OpUndef %6
  61. TransformationAddGlobalUndef transformation(100, 6);
  62. ASSERT_EQ(nullptr, context->get_def_use_mgr()->GetDef(100));
  63. ASSERT_TRUE(
  64. transformation.IsApplicable(context.get(), transformation_context));
  65. ApplyAndCheckFreshIds(transformation, context.get(),
  66. &transformation_context);
  67. ASSERT_EQ(spv::Op::OpUndef,
  68. context->get_def_use_mgr()->GetDef(100)->opcode());
  69. }
  70. TransformationAddGlobalUndef transformations[] = {
  71. // %101 = OpUndef %7
  72. TransformationAddGlobalUndef(101, 7),
  73. // %102 = OpUndef %8
  74. TransformationAddGlobalUndef(102, 8),
  75. // %103 = OpUndef %9
  76. TransformationAddGlobalUndef(103, 9),
  77. // %104 = OpUndef %10
  78. TransformationAddGlobalUndef(104, 10),
  79. // %105 = OpUndef %11
  80. TransformationAddGlobalUndef(105, 11)};
  81. for (auto& transformation : transformations) {
  82. ASSERT_TRUE(
  83. transformation.IsApplicable(context.get(), transformation_context));
  84. ApplyAndCheckFreshIds(transformation, context.get(),
  85. &transformation_context);
  86. }
  87. ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(context.get(), validator_options,
  88. kConsoleMessageConsumer));
  89. std::string after_transformation = R"(
  90. OpCapability Shader
  91. %1 = OpExtInstImport "GLSL.std.450"
  92. OpMemoryModel Logical GLSL450
  93. OpEntryPoint Fragment %4 "main"
  94. OpExecutionMode %4 OriginUpperLeft
  95. OpSource ESSL 310
  96. %2 = OpTypeVoid
  97. %3 = OpTypeFunction %2
  98. %6 = OpTypeFloat 32
  99. %7 = OpTypeInt 32 1
  100. %8 = OpTypeVector %6 2
  101. %9 = OpTypeVector %6 3
  102. %10 = OpTypeVector %6 4
  103. %11 = OpTypeVector %7 2
  104. %100 = OpUndef %6
  105. %101 = OpUndef %7
  106. %102 = OpUndef %8
  107. %103 = OpUndef %9
  108. %104 = OpUndef %10
  109. %105 = OpUndef %11
  110. %4 = OpFunction %2 None %3
  111. %5 = OpLabel
  112. OpReturn
  113. OpFunctionEnd
  114. )";
  115. ASSERT_TRUE(IsEqual(env, after_transformation, context.get()));
  116. }
  117. } // namespace
  118. } // namespace fuzz
  119. } // namespace spvtools