fuzzer_pass_wrap_regions_in_selections.cpp 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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_wrap_regions_in_selections.h"
  15. #include "source/fuzz/fuzzer_context.h"
  16. #include "source/fuzz/instruction_descriptor.h"
  17. #include "source/fuzz/transformation_split_block.h"
  18. #include "source/fuzz/transformation_wrap_region_in_selection.h"
  19. namespace spvtools {
  20. namespace fuzz {
  21. FuzzerPassWrapRegionsInSelections::FuzzerPassWrapRegionsInSelections(
  22. opt::IRContext* ir_context, TransformationContext* transformation_context,
  23. FuzzerContext* fuzzer_context,
  24. protobufs::TransformationSequence* transformations,
  25. bool ignore_inapplicable_transformations)
  26. : FuzzerPass(ir_context, transformation_context, fuzzer_context,
  27. transformations, ignore_inapplicable_transformations) {}
  28. void FuzzerPassWrapRegionsInSelections::Apply() {
  29. for (auto& function : *GetIRContext()->module()) {
  30. if (!GetFuzzerContext()->ChoosePercentage(
  31. GetFuzzerContext()->GetChanceOfWrappingRegionInSelection())) {
  32. continue;
  33. }
  34. // It is easier to select an element at random from a vector than from an
  35. // instruction list.
  36. std::vector<opt::BasicBlock*> header_block_candidates;
  37. for (auto& block : function) {
  38. header_block_candidates.push_back(&block);
  39. }
  40. if (header_block_candidates.empty()) {
  41. continue;
  42. }
  43. // Try to get a header block candidate that will increase the chances of the
  44. // transformation being applicable.
  45. auto* header_block_candidate = MaybeGetHeaderBlockCandidate(
  46. header_block_candidates[GetFuzzerContext()->RandomIndex(
  47. header_block_candidates)]);
  48. if (!header_block_candidate) {
  49. continue;
  50. }
  51. std::vector<opt::BasicBlock*> merge_block_candidates;
  52. for (auto& block : function) {
  53. if (GetIRContext()->GetDominatorAnalysis(&function)->StrictlyDominates(
  54. header_block_candidate, &block) &&
  55. GetIRContext()
  56. ->GetPostDominatorAnalysis(&function)
  57. ->StrictlyDominates(&block, header_block_candidate)) {
  58. merge_block_candidates.push_back(&block);
  59. }
  60. }
  61. if (merge_block_candidates.empty()) {
  62. continue;
  63. }
  64. // Try to get a merge block candidate that will increase the chances of the
  65. // transformation being applicable.
  66. auto* merge_block_candidate = MaybeGetMergeBlockCandidate(
  67. merge_block_candidates[GetFuzzerContext()->RandomIndex(
  68. merge_block_candidates)]);
  69. if (!merge_block_candidate) {
  70. continue;
  71. }
  72. if (!TransformationWrapRegionInSelection::IsApplicableToBlockRange(
  73. GetIRContext(), header_block_candidate->id(),
  74. merge_block_candidate->id())) {
  75. continue;
  76. }
  77. // This boolean constant will be used as a condition for the
  78. // OpBranchConditional instruction. We mark it as irrelevant to be able to
  79. // replace it with a more interesting value later.
  80. auto branch_condition = GetFuzzerContext()->ChooseEven();
  81. FindOrCreateBoolConstant(branch_condition, true);
  82. ApplyTransformation(TransformationWrapRegionInSelection(
  83. header_block_candidate->id(), merge_block_candidate->id(),
  84. branch_condition));
  85. }
  86. }
  87. opt::BasicBlock*
  88. FuzzerPassWrapRegionsInSelections::MaybeGetHeaderBlockCandidate(
  89. opt::BasicBlock* header_block_candidate) {
  90. // Try to create a preheader if |header_block_candidate| is a loop header.
  91. if (header_block_candidate->IsLoopHeader()) {
  92. // GetOrCreateSimpleLoopPreheader only supports reachable blocks.
  93. return GetIRContext()->cfg()->preds(header_block_candidate->id()).size() ==
  94. 1
  95. ? nullptr
  96. : GetOrCreateSimpleLoopPreheader(header_block_candidate->id());
  97. }
  98. // Try to split |header_block_candidate| if it's already a header block.
  99. if (header_block_candidate->GetMergeInst()) {
  100. SplitBlockAfterOpPhiOrOpVariable(header_block_candidate->id());
  101. }
  102. return header_block_candidate;
  103. }
  104. opt::BasicBlock* FuzzerPassWrapRegionsInSelections::MaybeGetMergeBlockCandidate(
  105. opt::BasicBlock* merge_block_candidate) {
  106. // If |merge_block_candidate| is a merge block of some construct, try to split
  107. // it and return a newly created block.
  108. if (GetIRContext()->GetStructuredCFGAnalysis()->IsMergeBlock(
  109. merge_block_candidate->id())) {
  110. // We can't split a merge block if it's also a loop header.
  111. return merge_block_candidate->IsLoopHeader()
  112. ? nullptr
  113. : SplitBlockAfterOpPhiOrOpVariable(merge_block_candidate->id());
  114. }
  115. return merge_block_candidate;
  116. }
  117. } // namespace fuzz
  118. } // namespace spvtools