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