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