| 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_adjust_selection_controls.h" |
| 16 |
|
| 17 |
#include "source/fuzz/transformation_set_selection_control.h" |
| 18 |
|
| 19 |
namespace spvtools { |
| 20 |
namespace fuzz { |
| 21 |
|
| 22 |
FuzzerPassAdjustSelectionControls::FuzzerPassAdjustSelectionControls( |
| 23 |
opt::IRContext* ir_context, TransformationContext* transformation_context, |
| 24 |
FuzzerContext* fuzzer_context, |
| 25 |
protobufs::TransformationSequence* transformations) |
| 26 |
: FuzzerPass(ir_context, transformation_context, fuzzer_context, |
| 27 |
transformations) {} |
| 28 |
|
| 29 |
FuzzerPassAdjustSelectionControls::~FuzzerPassAdjustSelectionControls() = |
| 30 |
default; |
| 31 |
|
| 32 |
void FuzzerPassAdjustSelectionControls::Apply() { |
| 33 |
// Consider every merge instruction in the module (via looking through all |
| 34 |
// functions and blocks). |
| 35 |
for (auto& function : *GetIRContext()->module()) { |
| 36 |
for (auto& block : function) { |
| 37 |
if (auto merge_inst = block.GetMergeInst()) { |
| 38 |
// Ignore the instruction if it is not a selection merge. |
| 39 |
if (merge_inst->opcode() != SpvOpSelectionMerge) { |
| 40 |
continue; |
| 41 |
} |
| 42 |
|
| 43 |
// Choose randomly whether to change the selection control for this |
| 44 |
// instruction. |
| 45 |
if (!GetFuzzerContext()->ChoosePercentage( |
| 46 |
GetFuzzerContext()->GetChanceOfAdjustingSelectionControl())) { |
| 47 |
continue; |
| 48 |
} |
| 49 |
|
| 50 |
// The choices to change the selection control to are the set of valid |
| 51 |
// controls, minus the current control. |
| 52 |
std::vector<uint32_t> choices; |
| 53 |
for (auto control : |
| 54 |
{SpvSelectionControlMaskNone, SpvSelectionControlFlattenMask, |
| 55 |
SpvSelectionControlDontFlattenMask}) { |
| 56 |
if (control == merge_inst->GetSingleWordOperand(1)) { |
| 57 |
continue; |
| 58 |
} |
| 59 |
choices.push_back(control); |
| 60 |
} |
| 61 |
|
| 62 |
// Apply the transformation and add it to the output transformation |
| 63 |
// sequence. |
| 64 |
TransformationSetSelectionControl transformation( |
| 65 |
block.id(), choices[GetFuzzerContext()->RandomIndex(choices)]); |
| 66 |
ApplyTransformation(transformation); |
| 67 |
} |
| 68 |
} |
| 69 |
} |
| 70 |
} |
| 71 |
|
| 72 |
} // namespace fuzz |
| 73 |
} // namespace spvtools |