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