| 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 |
|
| 15 |
#include "source/fuzz/fuzzer_pass_add_dead_continues.h" |
| 16 |
|
| 17 |
#include "source/fuzz/fuzzer_util.h" |
| 18 |
#include "source/fuzz/transformation_add_dead_continue.h" |
| 19 |
#include "source/opt/ir_context.h" |
| 20 |
|
| 21 |
namespace spvtools { |
| 22 |
namespace fuzz { |
| 23 |
|
| 24 |
FuzzerPassAddDeadContinues::FuzzerPassAddDeadContinues( |
| 25 |
opt::IRContext* ir_context, TransformationContext* transformation_context, |
| 26 |
FuzzerContext* fuzzer_context, |
| 27 |
protobufs::TransformationSequence* transformations) |
| 28 |
: FuzzerPass(ir_context, transformation_context, fuzzer_context, |
| 29 |
transformations) {} |
| 30 |
|
| 31 |
FuzzerPassAddDeadContinues::~FuzzerPassAddDeadContinues() = default; |
| 32 |
|
| 33 |
void FuzzerPassAddDeadContinues::Apply() { |
| 34 |
// Consider every block in every function. |
| 35 |
for (auto& function : *GetIRContext()->module()) { |
| 36 |
for (auto& block : function) { |
| 37 |
// Get the label id of the continue target of the innermost loop. |
| 38 |
auto continue_block_id = |
| 39 |
block.IsLoopHeader() |
| 40 |
? block.ContinueBlockId() |
| 41 |
: GetIRContext()->GetStructuredCFGAnalysis()->LoopContinueBlock( |
| 42 |
block.id()); |
| 43 |
|
| 44 |
// This transformation is not applicable if current block is not inside a |
| 45 |
// loop. |
| 46 |
if (continue_block_id == 0) { |
| 47 |
continue; |
| 48 |
} |
| 49 |
|
| 50 |
auto* continue_block = |
| 51 |
fuzzerutil::MaybeFindBlock(GetIRContext(), continue_block_id); |
| 52 |
assert(continue_block && "Continue block is null"); |
| 53 |
|
| 54 |
// Analyze return type of each OpPhi instruction in the continue target |
| 55 |
// and provide an id for the transformation if needed. |
| 56 |
std::vector<uint32_t> phi_ids; |
| 57 |
// Check whether current block has an edge to the continue target. |
| 58 |
// If this is the case, we don't need to do anything. |
| 59 |
if (!block.IsSuccessor(continue_block)) { |
| 60 |
continue_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 with |
| 65 |
// 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 |
|
| 73 |
// Make sure the module contains a boolean constant equal to |
| 74 |
// |condition_value|. |
| 75 |
bool condition_value = GetFuzzerContext()->ChooseEven(); |
| 76 |
FindOrCreateBoolConstant(condition_value, false); |
| 77 |
|
| 78 |
// Make a transformation to add a dead continue from this node; if the |
| 79 |
// node turns out to be inappropriate (e.g. by not being in a loop) the |
| 80 |
// precondition for the transformation will fail and it will be ignored. |
| 81 |
auto candidate_transformation = TransformationAddDeadContinue( |
| 82 |
block.id(), condition_value, std::move(phi_ids)); |
| 83 |
// Probabilistically decide whether to apply the transformation in the |
| 84 |
// case that it is applicable. |
| 85 |
if (GetFuzzerContext()->ChoosePercentage( |
| 86 |
GetFuzzerContext()->GetChanceOfAddingDeadContinue())) { |
| 87 |
MaybeApplyTransformation(candidate_transformation); |
| 88 |
} |
| 89 |
} |
| 90 |
} |
| 91 |
} |
| 92 |
|
| 93 |
} // namespace fuzz |
| 94 |
} // namespace spvtools |