Debian Codegraph @

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