• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright (c) 2020 Vasyl Teliman
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_swap_conditional_branch_operands.h"
16 
17 #include "source/fuzz/fuzzer_context.h"
18 #include "source/fuzz/fuzzer_util.h"
19 #include "source/fuzz/instruction_descriptor.h"
20 #include "source/fuzz/transformation_swap_conditional_branch_operands.h"
21 
22 namespace spvtools {
23 namespace fuzz {
24 
25 FuzzerPassSwapBranchConditionalOperands::
FuzzerPassSwapBranchConditionalOperands(opt::IRContext * ir_context,TransformationContext * transformation_context,FuzzerContext * fuzzer_context,protobufs::TransformationSequence * transformations)26     FuzzerPassSwapBranchConditionalOperands(
27         opt::IRContext* ir_context,
28         TransformationContext* transformation_context,
29         FuzzerContext* fuzzer_context,
30         protobufs::TransformationSequence* transformations)
31     : FuzzerPass(ir_context, transformation_context, fuzzer_context,
32                  transformations) {}
33 
Apply()34 void FuzzerPassSwapBranchConditionalOperands::Apply() {
35   ForEachInstructionWithInstructionDescriptor(
36       [this](opt::Function* /*unused*/, opt::BasicBlock* /*unused*/,
37              opt::BasicBlock::iterator inst_it,
38              const protobufs::InstructionDescriptor& instruction_descriptor) {
39         const auto& inst = *inst_it;
40 
41         if (inst.opcode() != SpvOpBranchConditional) {
42           return;
43         }
44 
45         if (!GetFuzzerContext()->ChoosePercentage(
46                 GetFuzzerContext()
47                     ->GetChanceOfSwappingConditionalBranchOperands())) {
48           return;
49         }
50 
51         ApplyTransformation(TransformationSwapConditionalBranchOperands(
52             instruction_descriptor, GetFuzzerContext()->GetFreshId()));
53       });
54 }
55 
56 }  // namespace fuzz
57 }  // namespace spvtools
58