fuzzer_pass_add_dead_breaks.cpp 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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_add_dead_breaks.h"
  15. #include "source/fuzz/fuzzer_util.h"
  16. #include "source/fuzz/transformation_add_dead_break.h"
  17. #include "source/opt/ir_context.h"
  18. namespace spvtools {
  19. namespace fuzz {
  20. FuzzerPassAddDeadBreaks::FuzzerPassAddDeadBreaks(
  21. opt::IRContext* ir_context, TransformationContext* transformation_context,
  22. FuzzerContext* fuzzer_context,
  23. protobufs::TransformationSequence* transformations,
  24. bool ignore_inapplicable_transformations)
  25. : FuzzerPass(ir_context, transformation_context, fuzzer_context,
  26. transformations, ignore_inapplicable_transformations) {}
  27. void FuzzerPassAddDeadBreaks::Apply() {
  28. // We first collect up lots of possibly-applicable transformations.
  29. std::vector<TransformationAddDeadBreak> candidate_transformations;
  30. // We consider each function separately.
  31. for (auto& function : *GetIRContext()->module()) {
  32. // For a given function, we find all the merge blocks in that function.
  33. std::vector<opt::BasicBlock*> merge_blocks;
  34. for (auto& block : function) {
  35. auto maybe_merge_id = block.MergeBlockIdIfAny();
  36. if (maybe_merge_id) {
  37. auto merge_block =
  38. fuzzerutil::MaybeFindBlock(GetIRContext(), maybe_merge_id);
  39. assert(merge_block && "Merge block can't be null");
  40. merge_blocks.push_back(merge_block);
  41. }
  42. }
  43. // We rather aggressively consider the possibility of adding a break from
  44. // every block in the function to every merge block. Many of these will be
  45. // inapplicable as they would be illegal. That's OK - we later discard the
  46. // ones that turn out to be no good.
  47. for (auto& block : function) {
  48. for (auto* merge_block : merge_blocks) {
  49. // Populate this vector with ids that are available at the branch point
  50. // of this basic block. We will use these ids to update OpPhi
  51. // instructions later.
  52. std::vector<uint32_t> phi_ids;
  53. // Determine how we need to adjust OpPhi instructions' operands
  54. // for this transformation to be valid.
  55. //
  56. // If |block| has a branch to |merge_block|, the latter must have all of
  57. // its OpPhi instructions set up correctly - we don't need to adjust
  58. // anything.
  59. if (!block.IsSuccessor(merge_block)) {
  60. merge_block->ForEachPhiInst([this, &phi_ids](opt::Instruction* phi) {
  61. // Add an additional operand for OpPhi instruction. Use a constant
  62. // if possible, and an undef otherwise.
  63. if (fuzzerutil::CanCreateConstant(GetIRContext(), phi->type_id())) {
  64. // We mark the constant as irrelevant so that we can replace it
  65. // with a more interesting value later.
  66. phi_ids.push_back(FindOrCreateZeroConstant(phi->type_id(), true));
  67. } else {
  68. phi_ids.push_back(FindOrCreateGlobalUndef(phi->type_id()));
  69. }
  70. });
  71. }
  72. // Make sure the module has a required boolean constant to be used in
  73. // OpBranchConditional instruction.
  74. auto break_condition = GetFuzzerContext()->ChooseEven();
  75. FindOrCreateBoolConstant(break_condition, false);
  76. auto candidate_transformation = TransformationAddDeadBreak(
  77. block.id(), merge_block->id(), break_condition, std::move(phi_ids));
  78. if (candidate_transformation.IsApplicable(
  79. GetIRContext(), *GetTransformationContext())) {
  80. // Only consider a transformation as a candidate if it is applicable.
  81. candidate_transformations.push_back(
  82. std::move(candidate_transformation));
  83. }
  84. }
  85. }
  86. }
  87. // Go through the candidate transformations that were accumulated,
  88. // probabilistically deciding whether to consider each one further and
  89. // applying the still-applicable ones that are considered further.
  90. //
  91. // We iterate through the candidate transformations in a random order by
  92. // repeatedly removing a random candidate transformation from the sequence
  93. // until no candidate transformations remain. This is done because
  94. // transformations can potentially disable one another, so that iterating
  95. // through them in order would lead to a higher probability of
  96. // transformations appearing early in the sequence being applied compared
  97. // with later transformations.
  98. while (!candidate_transformations.empty()) {
  99. // Choose a random index into the sequence of remaining candidate
  100. // transformations.
  101. auto index = GetFuzzerContext()->RandomIndex(candidate_transformations);
  102. // Remove the transformation at the chosen index from the sequence.
  103. auto transformation = std::move(candidate_transformations[index]);
  104. candidate_transformations.erase(candidate_transformations.begin() + index);
  105. // Probabilistically decide whether to try to apply it vs. ignore it, in the
  106. // case that it is applicable.
  107. if (GetFuzzerContext()->ChoosePercentage(
  108. GetFuzzerContext()->GetChanceOfAddingDeadBreak())) {
  109. MaybeApplyTransformation(transformation);
  110. }
  111. }
  112. }
  113. } // namespace fuzz
  114. } // namespace spvtools