• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright (c) 2021 Mostafa Ashraf
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_permute_function_variables.h"
16 
17 #include <algorithm>
18 #include <numeric>
19 #include <vector>
20 
21 #include "source/fuzz/fuzzer_context.h"
22 #include "source/fuzz/fuzzer_util.h"
23 #include "source/fuzz/instruction_descriptor.h"
24 #include "source/fuzz/transformation_swap_function_variables.h"
25 
26 namespace spvtools {
27 namespace fuzz {
28 
FuzzerPassPermuteFunctionVariables(opt::IRContext * ir_context,TransformationContext * transformation_context,FuzzerContext * fuzzer_context,protobufs::TransformationSequence * transformations,bool ignore_inapplicable_transformations)29 FuzzerPassPermuteFunctionVariables::FuzzerPassPermuteFunctionVariables(
30     opt::IRContext* ir_context, TransformationContext* transformation_context,
31     FuzzerContext* fuzzer_context,
32     protobufs::TransformationSequence* transformations,
33     bool ignore_inapplicable_transformations)
34     : FuzzerPass(ir_context, transformation_context, fuzzer_context,
35                  transformations, ignore_inapplicable_transformations) {
36 }  // Here we call parent constructor.
37 
Apply()38 void FuzzerPassPermuteFunctionVariables::Apply() {
39   // Permuting OpVariable instructions in each function.
40   for (auto& function : *GetIRContext()->module()) {
41     if (!GetFuzzerContext()->ChoosePercentage(
42             GetFuzzerContext()->GetChanceOfPermutingFunctionVariables())) {
43       continue;
44     }
45 
46     auto first_block = function.entry().get();
47 
48     std::vector<opt::Instruction*> variables;
49     for (auto& instruction : *first_block) {
50       if (instruction.opcode() == SpvOpVariable) {
51         variables.push_back(&instruction);
52       }
53     }
54     if (variables.size() <= 1) {
55       continue;
56     }
57     do {
58       uint32_t instruction_1_index = GetFuzzerContext()->RandomIndex(variables);
59       uint32_t instruction_2_index = GetFuzzerContext()->RandomIndex(variables);
60 
61       if (instruction_1_index != instruction_2_index) {
62         ApplyTransformation(TransformationSwapFunctionVariables(
63             variables[instruction_1_index]->result_id(),
64             variables[instruction_2_index]->result_id()));
65       }
66 
67     } while (GetFuzzerContext()->ChoosePercentage(
68                  GetFuzzerContext()
69                      ->GetChanceOfSwappingAnotherPairOfFunctionVariables()) &&
70              variables.size() > 2);
71   }
72 }
73 
74 }  // namespace fuzz
75 }  // namespace spvtools
76