• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright (c) 2021 Shiyu Liu
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_functions.h"
16 
17 #include "source/fuzz/fuzzer_context.h"
18 #include "source/fuzz/transformation_swap_two_functions.h"
19 
20 namespace spvtools {
21 namespace fuzz {
22 
FuzzerPassSwapFunctions(opt::IRContext * ir_context,TransformationContext * transformation_context,FuzzerContext * fuzzer_context,protobufs::TransformationSequence * transformations,bool ignore_inapplicable_transformations)23 FuzzerPassSwapFunctions::FuzzerPassSwapFunctions(
24     opt::IRContext* ir_context, TransformationContext* transformation_context,
25     FuzzerContext* fuzzer_context,
26     protobufs::TransformationSequence* transformations,
27     bool ignore_inapplicable_transformations)
28     : FuzzerPass(ir_context, transformation_context, fuzzer_context,
29                  transformations, ignore_inapplicable_transformations) {}
30 
Apply()31 void FuzzerPassSwapFunctions::Apply() {
32   // Collect all function ids in a module.
33   std::vector<uint32_t> function_ids;
34   for (auto& function : *GetIRContext()->module()) {
35     function_ids.emplace_back(function.result_id());
36   }
37 
38   // Iterate through every combination of id i & j where i!=j.
39   for (size_t i = 0; i < function_ids.size(); ++i) {
40     for (size_t j = i + 1; j < function_ids.size(); ++j) {
41       // Perform function swap randomly.
42       if (!GetFuzzerContext()->ChoosePercentage(
43               GetFuzzerContext()->GetChanceOfSwappingFunctions())) {
44         continue;
45       }
46       TransformationSwapTwoFunctions transformation(function_ids[i],
47                                                     function_ids[j]);
48     }
49   }
50 }
51 
52 }  // namespace fuzz
53 }  // namespace spvtools
54