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