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_global_variables.h"
16
17 #include "source/fuzz/transformation_add_global_variable.h"
18 #include "source/fuzz/transformation_add_type_pointer.h"
19
20 namespace spvtools {
21 namespace fuzz {
22
FuzzerPassAddGlobalVariables(opt::IRContext * ir_context,TransformationContext * transformation_context,FuzzerContext * fuzzer_context,protobufs::TransformationSequence * transformations,bool ignore_inapplicable_transformations)23 FuzzerPassAddGlobalVariables::FuzzerPassAddGlobalVariables(
24 opt::IRContext* ir_context, TransformationContext* transformation_context,
25 FuzzerContext* fuzzer_context,
26 protobufs::TransformationSequence* transformations,
27 bool ignore_inapplicable_transformations)
28 : FuzzerPass(ir_context, transformation_context, fuzzer_context,
29 transformations, ignore_inapplicable_transformations) {}
30
Apply()31 void FuzzerPassAddGlobalVariables::Apply() {
32 SpvStorageClass variable_storage_class = SpvStorageClassPrivate;
33 for (auto& entry_point : GetIRContext()->module()->entry_points()) {
34 // If the execution model of some entry point is GLCompute,
35 // then the variable storage class may be Workgroup.
36 if (entry_point.GetSingleWordInOperand(0) == SpvExecutionModelGLCompute) {
37 variable_storage_class =
38 GetFuzzerContext()->ChoosePercentage(
39 GetFuzzerContext()->GetChanceOfChoosingWorkgroupStorageClass())
40 ? SpvStorageClassWorkgroup
41 : SpvStorageClassPrivate;
42 break;
43 }
44 }
45
46 auto basic_type_ids_and_pointers =
47 GetAvailableBasicTypesAndPointers(variable_storage_class);
48
49 // These are the basic types that are available to this fuzzer pass.
50 auto& basic_types = basic_type_ids_and_pointers.first;
51 if (basic_types.empty()) {
52 // There are no basic types, so there is nothing this fuzzer pass can do.
53 return;
54 }
55
56 // These are the pointers to those basic types that are *initially* available
57 // to the fuzzer pass. The fuzzer pass might add pointer types in cases where
58 // none are available for a given basic type.
59 auto& basic_type_to_pointers = basic_type_ids_and_pointers.second;
60
61 // Probabilistically keep adding global variables.
62 while (GetFuzzerContext()->ChoosePercentage(
63 GetFuzzerContext()->GetChanceOfAddingGlobalVariable())) {
64 // Choose a random basic type; the new variable's type will be a pointer to
65 // this basic type.
66 uint32_t basic_type =
67 basic_types[GetFuzzerContext()->RandomIndex(basic_types)];
68 uint32_t pointer_type_id;
69 std::vector<uint32_t>& available_pointers_to_basic_type =
70 basic_type_to_pointers.at(basic_type);
71 // Determine whether there is at least one pointer to this basic type.
72 if (available_pointers_to_basic_type.empty()) {
73 // There is not. Make one, to use here, and add it to the available
74 // pointers for the basic type so that future variables can potentially
75 // use it.
76 pointer_type_id = GetFuzzerContext()->GetFreshId();
77 available_pointers_to_basic_type.push_back(pointer_type_id);
78 ApplyTransformation(TransformationAddTypePointer(
79 pointer_type_id, variable_storage_class, basic_type));
80 } else {
81 // There is - grab one.
82 pointer_type_id =
83 available_pointers_to_basic_type[GetFuzzerContext()->RandomIndex(
84 available_pointers_to_basic_type)];
85 }
86
87 ApplyTransformation(TransformationAddGlobalVariable(
88 GetFuzzerContext()->GetFreshId(), pointer_type_id,
89 variable_storage_class,
90 variable_storage_class == SpvStorageClassPrivate
91 ? FindOrCreateZeroConstant(basic_type, false)
92 : 0,
93 true));
94 }
95 }
96
97 } // namespace fuzz
98 } // namespace spvtools
99