fuzzer_pass_apply_id_synonyms.cpp 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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/fuzzer_pass_apply_id_synonyms.h"
  15. #include "source/fuzz/data_descriptor.h"
  16. #include "source/fuzz/fuzzer_util.h"
  17. #include "source/fuzz/id_use_descriptor.h"
  18. #include "source/fuzz/instruction_descriptor.h"
  19. #include "source/fuzz/transformation_composite_extract.h"
  20. #include "source/fuzz/transformation_replace_id_with_synonym.h"
  21. namespace spvtools {
  22. namespace fuzz {
  23. FuzzerPassApplyIdSynonyms::FuzzerPassApplyIdSynonyms(
  24. opt::IRContext* ir_context, FactManager* fact_manager,
  25. FuzzerContext* fuzzer_context,
  26. protobufs::TransformationSequence* transformations)
  27. : FuzzerPass(ir_context, fact_manager, fuzzer_context, transformations) {}
  28. FuzzerPassApplyIdSynonyms::~FuzzerPassApplyIdSynonyms() = default;
  29. void FuzzerPassApplyIdSynonyms::Apply() {
  30. for (auto id_with_known_synonyms :
  31. GetFactManager()->GetIdsForWhichSynonymsAreKnown(GetIRContext())) {
  32. // Gather up all uses of |id_with_known_synonym|, and then subsequently
  33. // iterate over these uses. We use this separation because, when
  34. // considering a given use, we might apply a transformation that will
  35. // invalidate the def-use manager.
  36. std::vector<std::pair<opt::Instruction*, uint32_t>> uses;
  37. GetIRContext()->get_def_use_mgr()->ForEachUse(
  38. id_with_known_synonyms,
  39. [&uses](opt::Instruction* use_inst, uint32_t use_index) -> void {
  40. uses.emplace_back(
  41. std::pair<opt::Instruction*, uint32_t>(use_inst, use_index));
  42. });
  43. for (auto& use : uses) {
  44. auto use_inst = use.first;
  45. auto use_index = use.second;
  46. auto block_containing_use = GetIRContext()->get_instr_block(use_inst);
  47. // The use might not be in a block; e.g. it could be a decoration.
  48. if (!block_containing_use) {
  49. continue;
  50. }
  51. if (!GetFuzzerContext()->ChoosePercentage(
  52. GetFuzzerContext()->GetChanceOfReplacingIdWithSynonym())) {
  53. continue;
  54. }
  55. // |use_index| is the absolute index of the operand. We require
  56. // the index of the operand restricted to input operands only, so
  57. // we subtract the number of non-input operands from |use_index|.
  58. uint32_t use_in_operand_index =
  59. use_index - use_inst->NumOperands() + use_inst->NumInOperands();
  60. if (!TransformationReplaceIdWithSynonym::UseCanBeReplacedWithSynonym(
  61. GetIRContext(), use_inst, use_in_operand_index)) {
  62. continue;
  63. }
  64. std::vector<const protobufs::DataDescriptor*> synonyms_to_try;
  65. for (auto& data_descriptor : GetFactManager()->GetSynonymsForId(
  66. id_with_known_synonyms, GetIRContext())) {
  67. protobufs::DataDescriptor descriptor_for_this_id =
  68. MakeDataDescriptor(id_with_known_synonyms, {});
  69. if (DataDescriptorEquals()(data_descriptor, &descriptor_for_this_id)) {
  70. // Exclude the fact that the id is synonymous with itself.
  71. continue;
  72. }
  73. synonyms_to_try.push_back(data_descriptor);
  74. }
  75. while (!synonyms_to_try.empty()) {
  76. auto synonym_index = GetFuzzerContext()->RandomIndex(synonyms_to_try);
  77. auto synonym_to_try = synonyms_to_try[synonym_index];
  78. synonyms_to_try.erase(synonyms_to_try.begin() + synonym_index);
  79. if (synonym_to_try->index_size() > 0 &&
  80. use_inst->opcode() == SpvOpPhi) {
  81. // We are trying to replace an operand to an OpPhi. This means
  82. // we cannot use a composite synonym, because that requires
  83. // extracting a component from a composite and we cannot insert
  84. // an extract instruction before an OpPhi.
  85. //
  86. // TODO(afd): We could consider inserting the extract instruction
  87. // into the relevant parent block of the OpPhi.
  88. continue;
  89. }
  90. if (!TransformationReplaceIdWithSynonym::IdsIsAvailableAtUse(
  91. GetIRContext(), use_inst, use_in_operand_index,
  92. synonym_to_try->object())) {
  93. continue;
  94. }
  95. // We either replace the use with an id known to be synonymous, or
  96. // an id that will hold the result of extracting a synonym from a
  97. // composite.
  98. uint32_t id_with_which_to_replace_use;
  99. if (synonym_to_try->index_size() == 0) {
  100. id_with_which_to_replace_use = synonym_to_try->object();
  101. } else {
  102. id_with_which_to_replace_use = GetFuzzerContext()->GetFreshId();
  103. protobufs::InstructionDescriptor instruction_to_insert_before =
  104. MakeInstructionDescriptor(GetIRContext(), use_inst);
  105. TransformationCompositeExtract composite_extract_transformation(
  106. instruction_to_insert_before, id_with_which_to_replace_use,
  107. synonym_to_try->object(),
  108. fuzzerutil::RepeatedFieldToVector(synonym_to_try->index()));
  109. assert(composite_extract_transformation.IsApplicable(
  110. GetIRContext(), *GetFactManager()) &&
  111. "Transformation should be applicable by construction.");
  112. composite_extract_transformation.Apply(GetIRContext(),
  113. GetFactManager());
  114. *GetTransformations()->add_transformation() =
  115. composite_extract_transformation.ToMessage();
  116. }
  117. TransformationReplaceIdWithSynonym replace_id_transformation(
  118. MakeIdUseDescriptorFromUse(GetIRContext(), use_inst,
  119. use_in_operand_index),
  120. id_with_which_to_replace_use);
  121. // The transformation should be applicable by construction.
  122. assert(replace_id_transformation.IsApplicable(GetIRContext(),
  123. *GetFactManager()));
  124. replace_id_transformation.Apply(GetIRContext(), GetFactManager());
  125. *GetTransformations()->add_transformation() =
  126. replace_id_transformation.ToMessage();
  127. break;
  128. }
  129. }
  130. }
  131. }
  132. } // namespace fuzz
  133. } // namespace spvtools