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