fuzzer_pass_test.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. // Copyright (c) 2020 Vasyl Teliman
  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 "gtest/gtest.h"
  15. #include "source/fuzz/fuzzer_pass_add_opphi_synonyms.h"
  16. #include "source/fuzz/fuzzer_util.h"
  17. #include "source/fuzz/pseudo_random_generator.h"
  18. #include "test/fuzz/fuzz_test_util.h"
  19. namespace spvtools {
  20. namespace fuzz {
  21. namespace {
  22. class FuzzerPassMock : public FuzzerPass {
  23. public:
  24. FuzzerPassMock(opt::IRContext* ir_context,
  25. TransformationContext* transformation_context,
  26. FuzzerContext* fuzzer_context,
  27. protobufs::TransformationSequence* transformations)
  28. : FuzzerPass(ir_context, transformation_context, fuzzer_context,
  29. transformations, false) {}
  30. ~FuzzerPassMock() override = default;
  31. const std::unordered_set<uint32_t>& GetReachedInstructions() const {
  32. return reached_ids_;
  33. }
  34. void Apply() override {
  35. ForEachInstructionWithInstructionDescriptor(
  36. [this](opt::Function* /*unused*/, opt::BasicBlock* /*unused*/,
  37. opt::BasicBlock::iterator inst_it,
  38. const protobufs::InstructionDescriptor& /*unused*/) {
  39. if (inst_it->result_id()) {
  40. reached_ids_.insert(inst_it->result_id());
  41. }
  42. });
  43. }
  44. private:
  45. std::unordered_set<uint32_t> reached_ids_;
  46. };
  47. TEST(FuzzerPassTest, ForEachInstructionWithInstructionDescriptor) {
  48. std::string shader = R"(
  49. OpCapability Shader
  50. %1 = OpExtInstImport "GLSL.std.450"
  51. OpMemoryModel Logical GLSL450
  52. OpEntryPoint Fragment %4 "main"
  53. OpExecutionMode %4 OriginUpperLeft
  54. OpSource ESSL 310
  55. %2 = OpTypeVoid
  56. %3 = OpTypeFunction %2
  57. %6 = OpTypeFloat 32
  58. %4 = OpFunction %2 None %3
  59. %5 = OpLabel
  60. %7 = OpUndef %6
  61. OpReturn
  62. %8 = OpLabel
  63. %9 = OpUndef %6
  64. OpReturn
  65. OpFunctionEnd
  66. )";
  67. const auto env = SPV_ENV_UNIVERSAL_1_3;
  68. const auto consumer = nullptr;
  69. const auto context = BuildModule(env, consumer, shader, kFuzzAssembleOption);
  70. spvtools::ValidatorOptions validator_options;
  71. ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(context.get(), validator_options,
  72. kConsoleMessageConsumer));
  73. TransformationContext transformation_context(
  74. MakeUnique<FactManager>(context.get()), validator_options);
  75. // Check that %5 is reachable and %8 is unreachable as expected.
  76. const auto* dominator_analysis =
  77. context->GetDominatorAnalysis(context->GetFunction(4));
  78. ASSERT_TRUE(dominator_analysis->IsReachable(5));
  79. ASSERT_FALSE(dominator_analysis->IsReachable(8));
  80. FuzzerContext fuzzer_context(MakeUnique<PseudoRandomGenerator>(0), 100,
  81. false);
  82. protobufs::TransformationSequence transformations;
  83. FuzzerPassMock fuzzer_pass_mock(context.get(), &transformation_context,
  84. &fuzzer_context, &transformations);
  85. fuzzer_pass_mock.Apply();
  86. ASSERT_TRUE(fuzzer_pass_mock.GetReachedInstructions().count(7));
  87. ASSERT_FALSE(fuzzer_pass_mock.GetReachedInstructions().count(9));
  88. }
  89. } // namespace
  90. } // namespace fuzz
  91. } // namespace spvtools