| 1 |
// Copyright (c) 2020 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 |
|
| 15 |
#include "source/fuzz/fuzzer_pass_duplicate_regions_with_selections.h" |
| 16 |
|
| 17 |
#include "source/fuzz/fuzzer_util.h" |
| 18 |
#include "source/fuzz/transformation_duplicate_region_with_selection.h" |
| 19 |
|
| 20 |
namespace spvtools { |
| 21 |
namespace fuzz { |
| 22 |
|
| 23 |
FuzzerPassDuplicateRegionsWithSelections:: |
| 24 |
FuzzerPassDuplicateRegionsWithSelections( |
| 25 |
opt::IRContext* ir_context, |
| 26 |
TransformationContext* transformation_context, |
| 27 |
FuzzerContext* fuzzer_context, |
| 28 |
protobufs::TransformationSequence* transformations) |
| 29 |
: FuzzerPass(ir_context, transformation_context, fuzzer_context, |
| 30 |
transformations) {} |
| 31 |
|
| 32 |
FuzzerPassDuplicateRegionsWithSelections:: |
| 33 |
~FuzzerPassDuplicateRegionsWithSelections() = default; |
| 34 |
|
| 35 |
void FuzzerPassDuplicateRegionsWithSelections::Apply() { |
| 36 |
// Iterate over all of the functions in the module. |
| 37 |
for (auto& function : *GetIRContext()->module()) { |
| 38 |
// Randomly decide whether to apply the transformation. |
| 39 |
if (!GetFuzzerContext()->ChoosePercentage( |
| 40 |
GetFuzzerContext()->GetChanceOfDuplicatingRegionWithSelection())) { |
| 41 |
continue; |
| 42 |
} |
| 43 |
std::vector<opt::BasicBlock*> candidate_entry_blocks; |
| 44 |
for (auto& block : function) { |
| 45 |
// We don't consider the first block to be the entry block, since it |
| 46 |
// could contain OpVariable instructions that would require additional |
| 47 |
// operations to be reassigned. |
| 48 |
// TODO(https://github.com/KhronosGroup/SPIRV-Tools/issues/3778): |
| 49 |
// Consider extending this fuzzer pass to allow the first block to be |
| 50 |
// used in duplication. |
| 51 |
if (&block == &*function.begin()) { |
| 52 |
continue; |
| 53 |
} |
| 54 |
candidate_entry_blocks.push_back(&block); |
| 55 |
} |
| 56 |
if (candidate_entry_blocks.empty()) { |
| 57 |
continue; |
| 58 |
} |
| 59 |
// Randomly choose the entry block. |
| 60 |
auto entry_block = candidate_entry_blocks[GetFuzzerContext()->RandomIndex( |
| 61 |
candidate_entry_blocks)]; |
| 62 |
auto dominator_analysis = GetIRContext()->GetDominatorAnalysis(&function); |
| 63 |
auto postdominator_analysis = |
| 64 |
GetIRContext()->GetPostDominatorAnalysis(&function); |
| 65 |
std::vector<opt::BasicBlock*> candidate_exit_blocks; |
| 66 |
for (auto postdominates_entry_block = entry_block; |
| 67 |
postdominates_entry_block != nullptr; |
| 68 |
postdominates_entry_block = postdominator_analysis->ImmediateDominator( |
| 69 |
postdominates_entry_block)) { |
| 70 |
// The candidate exit block must be dominated by the entry block and the |
| 71 |
// entry block must be post-dominated by the candidate exit block. Ignore |
| 72 |
// the block if it heads a selection construct or a loop construct. |
| 73 |
if (dominator_analysis->Dominates(entry_block, |
| 74 |
postdominates_entry_block) && |
| 75 |
!postdominates_entry_block->GetMergeInst()) { |
| 76 |
candidate_exit_blocks.push_back(postdominates_entry_block); |
| 77 |
} |
| 78 |
} |
| 79 |
if (candidate_exit_blocks.empty()) { |
| 80 |
continue; |
| 81 |
} |
| 82 |
// Randomly choose the exit block. |
| 83 |
auto exit_block = candidate_exit_blocks[GetFuzzerContext()->RandomIndex( |
| 84 |
candidate_exit_blocks)]; |
| 85 |
|
| 86 |
auto region_blocks = |
| 87 |
TransformationDuplicateRegionWithSelection::GetRegionBlocks( |
| 88 |
GetIRContext(), entry_block, exit_block); |
| 89 |
|
| 90 |
// Construct |original_label_to_duplicate_label| by iterating over all |
| 91 |
// blocks in the region. Construct |original_id_to_duplicate_id| and |
| 92 |
// |original_id_to_phi_id| by iterating over all instructions in each block. |
| 93 |
std::map<uint32_t, uint32_t> original_label_to_duplicate_label; |
| 94 |
std::map<uint32_t, uint32_t> original_id_to_duplicate_id; |
| 95 |
std::map<uint32_t, uint32_t> original_id_to_phi_id; |
| 96 |
for (auto& block : region_blocks) { |
| 97 |
original_label_to_duplicate_label[block->id()] = |
| 98 |
GetFuzzerContext()->GetFreshId(); |
| 99 |
for (auto& instr : *block) { |
| 100 |
if (instr.result_id()) { |
| 101 |
original_id_to_duplicate_id[instr.result_id()] = |
| 102 |
GetFuzzerContext()->GetFreshId(); |
| 103 |
auto final_instruction = &*exit_block->tail(); |
| 104 |
// &*exit_block->tail() is the final instruction of the region. |
| 105 |
// The instruction is available at the end of the region if and only |
| 106 |
// if it is available before this final instruction or it is the final |
| 107 |
// instruction. |
| 108 |
if ((&instr == final_instruction || |
| 109 |
fuzzerutil::IdIsAvailableBeforeInstruction( |
| 110 |
GetIRContext(), final_instruction, instr.result_id()))) { |
| 111 |
original_id_to_phi_id[instr.result_id()] = |
| 112 |
GetFuzzerContext()->GetFreshId(); |
| 113 |
} |
| 114 |
} |
| 115 |
} |
| 116 |
} |
| 117 |
// Randomly decide between value "true" or "false" for a bool constant. |
| 118 |
// Make sure the transformation has access to a bool constant to be used |
| 119 |
// while creating conditional construct. |
| 120 |
auto condition_id = |
| 121 |
FindOrCreateBoolConstant(GetFuzzerContext()->ChooseEven(), true); |
| 122 |
|
| 123 |
TransformationDuplicateRegionWithSelection transformation = |
| 124 |
TransformationDuplicateRegionWithSelection( |
| 125 |
GetFuzzerContext()->GetFreshId(), condition_id, |
| 126 |
GetFuzzerContext()->GetFreshId(), entry_block->id(), |
| 127 |
exit_block->id(), original_label_to_duplicate_label, |
| 128 |
original_id_to_duplicate_id, original_id_to_phi_id); |
| 129 |
MaybeApplyTransformation(transformation); |
| 130 |
} |
| 131 |
} |
| 132 |
} // namespace fuzz |
| 133 |
} // namespace spvtools |