fuzzer_pass_add_synonyms.cpp 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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 "source/fuzz/fuzzer_pass_add_synonyms.h"
  15. #include "source/fuzz/fuzzer_context.h"
  16. #include "source/fuzz/fuzzer_util.h"
  17. #include "source/fuzz/instruction_descriptor.h"
  18. #include "source/fuzz/transformation_add_synonym.h"
  19. namespace spvtools {
  20. namespace fuzz {
  21. FuzzerPassAddSynonyms::FuzzerPassAddSynonyms(
  22. opt::IRContext* ir_context, TransformationContext* transformation_context,
  23. FuzzerContext* fuzzer_context,
  24. protobufs::TransformationSequence* transformations)
  25. : FuzzerPass(ir_context, transformation_context, fuzzer_context,
  26. transformations) {}
  27. FuzzerPassAddSynonyms::~FuzzerPassAddSynonyms() = default;
  28. void FuzzerPassAddSynonyms::Apply() {
  29. ForEachInstructionWithInstructionDescriptor(
  30. [this](opt::Function* function, opt::BasicBlock* block,
  31. opt::BasicBlock::iterator inst_it,
  32. const protobufs::InstructionDescriptor& instruction_descriptor) {
  33. if (GetTransformationContext()->GetFactManager()->BlockIsDead(
  34. block->id())) {
  35. // Don't create synonyms in dead blocks.
  36. return;
  37. }
  38. // Skip |inst_it| if we can't insert anything above it. OpIAdd is just
  39. // a representative of some instruction that might be produced by the
  40. // transformation.
  41. if (!fuzzerutil::CanInsertOpcodeBeforeInstruction(SpvOpIAdd, inst_it)) {
  42. return;
  43. }
  44. if (!GetFuzzerContext()->ChoosePercentage(
  45. GetFuzzerContext()->GetChanceOfAddingSynonyms())) {
  46. return;
  47. }
  48. auto synonym_type = GetFuzzerContext()->GetRandomSynonymType();
  49. // Select all instructions that can be used to create a synonym to.
  50. auto available_instructions = FindAvailableInstructions(
  51. function, block, inst_it,
  52. [synonym_type, this](opt::IRContext* ir_context,
  53. opt::Instruction* inst) {
  54. // Check that we can create a synonym to |inst| as described by
  55. // the |synonym_type| and insert it before |inst_it|.
  56. return TransformationAddSynonym::IsInstructionValid(
  57. ir_context, *GetTransformationContext(), inst, synonym_type);
  58. });
  59. if (available_instructions.empty()) {
  60. return;
  61. }
  62. const auto* existing_synonym =
  63. available_instructions[GetFuzzerContext()->RandomIndex(
  64. available_instructions)];
  65. // Make sure the module contains all instructions required to apply the
  66. // transformation.
  67. switch (synonym_type) {
  68. case protobufs::TransformationAddSynonym::ADD_ZERO:
  69. case protobufs::TransformationAddSynonym::SUB_ZERO:
  70. case protobufs::TransformationAddSynonym::LOGICAL_OR:
  71. // Create a zero constant to be used as an operand of the synonymous
  72. // instruction.
  73. FindOrCreateZeroConstant(existing_synonym->type_id(), false);
  74. break;
  75. case protobufs::TransformationAddSynonym::MUL_ONE:
  76. case protobufs::TransformationAddSynonym::LOGICAL_AND: {
  77. const auto* existing_synonym_type =
  78. GetIRContext()->get_type_mgr()->GetType(
  79. existing_synonym->type_id());
  80. assert(existing_synonym_type && "Instruction has invalid type");
  81. if (const auto* vector = existing_synonym_type->AsVector()) {
  82. auto element_type_id =
  83. GetIRContext()->get_type_mgr()->GetId(vector->element_type());
  84. assert(element_type_id && "Vector's element type is invalid");
  85. auto one_word = vector->element_type()->AsFloat()
  86. ? fuzzerutil::FloatToWord(1)
  87. : 1u;
  88. FindOrCreateCompositeConstant(
  89. std::vector<uint32_t>(
  90. vector->element_count(),
  91. FindOrCreateConstant({one_word}, element_type_id, false)),
  92. existing_synonym->type_id(), false);
  93. } else {
  94. FindOrCreateConstant(
  95. {existing_synonym_type->AsFloat() ? fuzzerutil::FloatToWord(1)
  96. : 1u},
  97. existing_synonym->type_id(), false);
  98. }
  99. } break;
  100. default:
  101. // This assertion will fail if some SynonymType is missing from the
  102. // switch statement.
  103. assert(
  104. !TransformationAddSynonym::IsAdditionalConstantRequired(
  105. synonym_type) &&
  106. "|synonym_type| requires an additional constant to be present "
  107. "in the module");
  108. break;
  109. }
  110. ApplyTransformation(TransformationAddSynonym(
  111. existing_synonym->result_id(), synonym_type,
  112. GetFuzzerContext()->GetFreshId(), instruction_descriptor));
  113. });
  114. }
  115. } // namespace fuzz
  116. } // namespace spvtools