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