Debian Codegraph @

third_party/spirv-tools/source/fuzz/fuzzer_pass_replace_params_with_struct.cpp

raw
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_replace_params_with_struct.h"
16
17 #include <numeric>
18 #include <vector>
19
20 #include "source/fuzz/fuzzer_context.h"
21 #include "source/fuzz/fuzzer_util.h"
22 #include "source/fuzz/transformation_replace_params_with_struct.h"
23
24 namespace spvtools {
25 namespace fuzz {
26
27 FuzzerPassReplaceParamsWithStruct::FuzzerPassReplaceParamsWithStruct(
28 opt::IRContext* ir_context, TransformationContext* transformation_context,
29 FuzzerContext* fuzzer_context,
30 protobufs::TransformationSequence* transformations)
31 : FuzzerPass(ir_context, transformation_context, fuzzer_context,
32 transformations) {}
33
34 FuzzerPassReplaceParamsWithStruct::~FuzzerPassReplaceParamsWithStruct() =
35 default;
36
37 void FuzzerPassReplaceParamsWithStruct::Apply() {
38 for (const auto& function : *GetIRContext()->module()) {
39 auto params =
40 fuzzerutil::GetParameters(GetIRContext(), function.result_id());
41
42 if (params.empty() || fuzzerutil::FunctionIsEntryPoint(
43 GetIRContext(), function.result_id())) {
44 continue;
45 }
46
47 if (!GetFuzzerContext()->ChoosePercentage(
48 GetFuzzerContext()->GetChanceOfReplacingParametersWithStruct())) {
49 continue;
50 }
51
52 std::vector<uint32_t> parameter_index(params.size());
53 std::iota(parameter_index.begin(), parameter_index.end(), 0);
54
55 // Remove the indices of unsupported parameters.
56 auto new_end =
57 std::remove_if(parameter_index.begin(), parameter_index.end(),
58 [this, &params](uint32_t index) {
59 return !TransformationReplaceParamsWithStruct::
60 IsParameterTypeSupported(GetIRContext(),
61 params[index]->type_id());
62 });
63
64 // std::remove_if changes the vector so that removed elements are placed at
65 // the end (i.e. [new_end, parameter_index.end()) is a range of removed
66 // elements). However, the size of the vector is not changed so we need to
67 // change that explicitly by popping those elements from the vector.
68 parameter_index.erase(new_end, parameter_index.end());
69
70 if (parameter_index.empty()) {
71 continue;
72 }
73
74 // Select |num_replaced_params| parameters at random. We shuffle the vector
75 // of indices for randomization and shrink it to select first
76 // |num_replaced_params| parameters.
77 auto num_replaced_params = std::min<size_t>(
78 parameter_index.size(),
79 GetFuzzerContext()->GetRandomNumberOfParametersReplacedWithStruct(
80 static_cast<uint32_t>(params.size())));
81
82 GetFuzzerContext()->Shuffle(&parameter_index);
83 parameter_index.resize(num_replaced_params);
84
85 // Make sure OpTypeStruct exists in the module.
86 std::vector<uint32_t> component_type_ids;
87 for (auto index : parameter_index) {
88 component_type_ids.push_back(params[index]->type_id());
89 }
90
91 FindOrCreateStructType(component_type_ids);
92
93 // Map parameters' indices to parameters' ids.
94 std::vector<uint32_t> parameter_id;
95 for (auto index : parameter_index) {
96 parameter_id.push_back(params[index]->result_id());
97 }
98
99 std::map<uint32_t, uint32_t> caller_id_to_fresh_id;
100 for (const auto* inst :
101 fuzzerutil::GetCallers(GetIRContext(), function.result_id())) {
102 caller_id_to_fresh_id[inst->result_id()] =
103 GetFuzzerContext()->GetFreshId();
104 }
105
106 ApplyTransformation(TransformationReplaceParamsWithStruct(
107 parameter_id, GetFuzzerContext()->GetFreshId(),
108 GetFuzzerContext()->GetFreshId(), caller_id_to_fresh_id));
109 }
110 }
111
112 } // namespace fuzz
113 } // namespace spvtools