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_add_parameters.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_add_parameter.h"
21
22 namespace spvtools {
23 namespace fuzz {
24
FuzzerPassAddParameters(opt::IRContext * ir_context,TransformationContext * transformation_context,FuzzerContext * fuzzer_context,protobufs::TransformationSequence * transformations)25 FuzzerPassAddParameters::FuzzerPassAddParameters(
26 opt::IRContext* ir_context, TransformationContext* transformation_context,
27 FuzzerContext* fuzzer_context,
28 protobufs::TransformationSequence* transformations)
29 : FuzzerPass(ir_context, transformation_context, fuzzer_context,
30 transformations) {}
31
Apply()32 void FuzzerPassAddParameters::Apply() {
33 // Compute type candidates for the new parameter.
34 std::vector<uint32_t> type_candidates;
35 for (const auto& type_inst : GetIRContext()->module()->GetTypes()) {
36 if (TransformationAddParameter::IsParameterTypeSupported(
37 GetIRContext(), type_inst->result_id())) {
38 type_candidates.push_back(type_inst->result_id());
39 }
40 }
41
42 if (type_candidates.empty()) {
43 // The module contains no suitable types to use in new parameters.
44 return;
45 }
46
47 // Iterate over all functions in the module.
48 for (const auto& function : *GetIRContext()->module()) {
49 // Skip all entry-point functions - we don't want to change those.
50 if (fuzzerutil::FunctionIsEntryPoint(GetIRContext(),
51 function.result_id())) {
52 continue;
53 }
54
55 if (GetNumberOfParameters(function) >=
56 GetFuzzerContext()->GetMaximumNumberOfFunctionParameters()) {
57 continue;
58 }
59
60 if (!GetFuzzerContext()->ChoosePercentage(
61 GetFuzzerContext()->GetChanceOfAddingParameters())) {
62 continue;
63 }
64
65 auto num_new_parameters =
66 GetFuzzerContext()->GetRandomNumberOfNewParameters(
67 GetNumberOfParameters(function));
68
69 for (uint32_t i = 0; i < num_new_parameters; ++i) {
70 auto current_type_id =
71 type_candidates[GetFuzzerContext()->RandomIndex(type_candidates)];
72 auto current_type =
73 GetIRContext()->get_type_mgr()->GetType(current_type_id);
74 std::map<uint32_t, uint32_t> call_parameter_ids;
75
76 // Consider the case when a pointer type was selected.
77 if (current_type->kind() == opt::analysis::Type::kPointer) {
78 auto storage_class = fuzzerutil::GetStorageClassFromPointerType(
79 GetIRContext(), current_type_id);
80 switch (storage_class) {
81 case SpvStorageClassFunction: {
82 // In every caller find or create a local variable that has the
83 // selected type.
84 for (auto* instr :
85 fuzzerutil::GetCallers(GetIRContext(), function.result_id())) {
86 auto block = GetIRContext()->get_instr_block(instr);
87 auto function_id = block->GetParent()->result_id();
88 uint32_t variable_id =
89 FindOrCreateLocalVariable(current_type_id, function_id, true);
90 call_parameter_ids[instr->result_id()] = variable_id;
91 }
92 } break;
93 case SpvStorageClassPrivate:
94 case SpvStorageClassWorkgroup: {
95 // If there exists at least one caller, find or create a global
96 // variable that has the selected type.
97 std::vector<opt::Instruction*> callers =
98 fuzzerutil::GetCallers(GetIRContext(), function.result_id());
99 if (!callers.empty()) {
100 uint32_t variable_id =
101 FindOrCreateGlobalVariable(current_type_id, true);
102 for (auto* instr : callers) {
103 call_parameter_ids[instr->result_id()] = variable_id;
104 }
105 }
106 } break;
107 default:
108 break;
109 }
110 } else {
111 // If there exists at least one caller, find or create a zero constant
112 // that has the selected type.
113 std::vector<opt::Instruction*> callers =
114 fuzzerutil::GetCallers(GetIRContext(), function.result_id());
115 if (!callers.empty()) {
116 uint32_t constant_id =
117 FindOrCreateZeroConstant(current_type_id, true);
118 for (auto* instr :
119 fuzzerutil::GetCallers(GetIRContext(), function.result_id())) {
120 call_parameter_ids[instr->result_id()] = constant_id;
121 }
122 }
123 }
124
125 ApplyTransformation(TransformationAddParameter(
126 function.result_id(), GetFuzzerContext()->GetFreshId(),
127 current_type_id, std::move(call_parameter_ids),
128 GetFuzzerContext()->GetFreshId()));
129 }
130 }
131 }
132
GetNumberOfParameters(const opt::Function & function) const133 uint32_t FuzzerPassAddParameters::GetNumberOfParameters(
134 const opt::Function& function) const {
135 const auto* type = GetIRContext()->get_type_mgr()->GetType(
136 function.DefInst().GetSingleWordInOperand(1));
137 assert(type && type->AsFunction());
138
139 return static_cast<uint32_t>(type->AsFunction()->param_types().size());
140 }
141
142 } // namespace fuzz
143 } // namespace spvtools
144