1 // Copyright (c) 2021 Shiyu Liu
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
7 // http://www.apache.org/licenses/LICENSE-2.0
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.
15 #include "source/fuzz/fuzzer_pass_swap_functions.h"
17 #include "source/fuzz/fuzzer_context.h"
18 #include "source/fuzz/transformation_swap_two_functions.h"
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
) {}
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());
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())) {
46 TransformationSwapTwoFunctions
transformation(function_ids
[i
],
53 } // namespace spvtools