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_parameter_with_global.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_parameter_with_global.h"
23
24 namespace spvtools {
25 namespace fuzz {
26
FuzzerPassReplaceParameterWithGlobal(opt::IRContext * ir_context,TransformationContext * transformation_context,FuzzerContext * fuzzer_context,protobufs::TransformationSequence * transformations,bool ignore_inapplicable_transformations)27 FuzzerPassReplaceParameterWithGlobal::FuzzerPassReplaceParameterWithGlobal(
28 opt::IRContext* ir_context, TransformationContext* transformation_context,
29 FuzzerContext* fuzzer_context,
30 protobufs::TransformationSequence* transformations,
31 bool ignore_inapplicable_transformations)
32 : FuzzerPass(ir_context, transformation_context, fuzzer_context,
33 transformations, ignore_inapplicable_transformations) {}
34
Apply()35 void FuzzerPassReplaceParameterWithGlobal::Apply() {
36 for (const auto& function : *GetIRContext()->module()) {
37 if (fuzzerutil::FunctionIsEntryPoint(GetIRContext(),
38 function.result_id())) {
39 continue;
40 }
41
42 if (!GetFuzzerContext()->ChoosePercentage(
43 GetFuzzerContext()->GetChanceOfReplacingParametersWithGlobals())) {
44 continue;
45 }
46
47 auto params =
48 fuzzerutil::GetParameters(GetIRContext(), function.result_id());
49
50 // Make sure at least one parameter can be replaced. Also checks that the
51 // function has at least one parameter.
52 if (std::none_of(params.begin(), params.end(),
53 [this](const opt::Instruction* param) {
54 return TransformationReplaceParameterWithGlobal::
55 IsParameterTypeSupported(GetIRContext(),
56 param->type_id());
57 })) {
58 continue;
59 }
60
61 // Select id of a parameter to replace.
62 const opt::Instruction* replaced_param;
63 uint32_t param_type_id;
64 do {
65 replaced_param = GetFuzzerContext()->RemoveAtRandomIndex(¶ms);
66 param_type_id = replaced_param->type_id();
67 assert(param_type_id && "Parameter has invalid type");
68 } while (
69 !TransformationReplaceParameterWithGlobal::IsParameterTypeSupported(
70 GetIRContext(), param_type_id));
71
72 assert(replaced_param && "Unable to find a parameter to replace");
73
74 // Make sure type id for the global variable exists in the module.
75 FindOrCreatePointerType(replaced_param->type_id(), SpvStorageClassPrivate);
76
77 // Make sure initializer for the global variable exists in the module.
78 FindOrCreateZeroConstant(replaced_param->type_id(), false);
79
80 ApplyTransformation(TransformationReplaceParameterWithGlobal(
81 GetFuzzerContext()->GetFreshId(), replaced_param->result_id(),
82 GetFuzzerContext()->GetFreshId()));
83 }
84 }
85
86 } // namespace fuzz
87 } // namespace spvtools
88