1 // Copyright (c) 2020 Google LLC
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_local_variables.h"
16
17 #include "source/fuzz/fuzzer_util.h"
18 #include "source/fuzz/transformation_add_local_variable.h"
19 #include "source/fuzz/transformation_add_type_pointer.h"
20
21 namespace spvtools {
22 namespace fuzz {
23
FuzzerPassAddLocalVariables(opt::IRContext * ir_context,TransformationContext * transformation_context,FuzzerContext * fuzzer_context,protobufs::TransformationSequence * transformations)24 FuzzerPassAddLocalVariables::FuzzerPassAddLocalVariables(
25 opt::IRContext* ir_context, TransformationContext* transformation_context,
26 FuzzerContext* fuzzer_context,
27 protobufs::TransformationSequence* transformations)
28 : FuzzerPass(ir_context, transformation_context, fuzzer_context,
29 transformations) {}
30
Apply()31 void FuzzerPassAddLocalVariables::Apply() {
32 auto basic_type_ids_and_pointers =
33 GetAvailableBasicTypesAndPointers(SpvStorageClassFunction);
34
35 // These are the basic types that are available to this fuzzer pass.
36 auto& basic_types = basic_type_ids_and_pointers.first;
37
38 // These are the pointers to those basic types that are *initially* available
39 // to the fuzzer pass. The fuzzer pass might add pointer types in cases where
40 // none are available for a given basic type.
41 auto& basic_type_to_pointers = basic_type_ids_and_pointers.second;
42
43 // Consider every function in the module.
44 for (auto& function : *GetIRContext()->module()) {
45 // Probabilistically keep adding random variables to this function.
46 while (GetFuzzerContext()->ChoosePercentage(
47 GetFuzzerContext()->GetChanceOfAddingLocalVariable())) {
48 // Choose a random basic type; the new variable's type will be a pointer
49 // to this basic type.
50 uint32_t basic_type =
51 basic_types[GetFuzzerContext()->RandomIndex(basic_types)];
52 uint32_t pointer_type;
53 std::vector<uint32_t>& available_pointers_to_basic_type =
54 basic_type_to_pointers.at(basic_type);
55 // Determine whether there is at least one pointer to this basic type.
56 if (available_pointers_to_basic_type.empty()) {
57 // There is not. Make one, to use here, and add it to the available
58 // pointers for the basic type so that future variables can potentially
59 // use it.
60 pointer_type = GetFuzzerContext()->GetFreshId();
61 ApplyTransformation(TransformationAddTypePointer(
62 pointer_type, SpvStorageClassFunction, basic_type));
63 available_pointers_to_basic_type.push_back(pointer_type);
64 } else {
65 // There is - grab one.
66 pointer_type =
67 available_pointers_to_basic_type[GetFuzzerContext()->RandomIndex(
68 available_pointers_to_basic_type)];
69 }
70 ApplyTransformation(TransformationAddLocalVariable(
71 GetFuzzerContext()->GetFreshId(), pointer_type, function.result_id(),
72 FindOrCreateZeroConstant(basic_type, false), true));
73 }
74 }
75 }
76
77 } // namespace fuzz
78 } // namespace spvtools
79