Debian Codegraph @

third_party/spirv-tools/source/fuzz/fuzzer_pass_add_local_variables.cpp

raw
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
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
31 FuzzerPassAddLocalVariables::~FuzzerPassAddLocalVariables() = default;
32
33 void FuzzerPassAddLocalVariables::Apply() {
34 auto basic_type_ids_and_pointers =
35 GetAvailableBasicTypesAndPointers(SpvStorageClassFunction);
36
37 // These are the basic types that are available to this fuzzer pass.
38 auto& basic_types = basic_type_ids_and_pointers.first;
39
40 // These are the pointers to those basic types that are *initially* available
41 // to the fuzzer pass. The fuzzer pass might add pointer types in cases where
42 // none are available for a given basic type.
43 auto& basic_type_to_pointers = basic_type_ids_and_pointers.second;
44
45 // Consider every function in the module.
46 for (auto& function : *GetIRContext()->module()) {
47 // Probabilistically keep adding random variables to this function.
48 while (GetFuzzerContext()->ChoosePercentage(
49 GetFuzzerContext()->GetChanceOfAddingLocalVariable())) {
50 // Choose a random basic type; the new variable's type will be a pointer
51 // to this basic type.
52 uint32_t basic_type =
53 basic_types[GetFuzzerContext()->RandomIndex(basic_types)];
54 uint32_t pointer_type;
55 std::vector<uint32_t>& available_pointers_to_basic_type =
56 basic_type_to_pointers.at(basic_type);
57 // Determine whether there is at least one pointer to this basic type.
58 if (available_pointers_to_basic_type.empty()) {
59 // There is not. Make one, to use here, and add it to the available
60 // pointers for the basic type so that future variables can potentially
61 // use it.
62 pointer_type = GetFuzzerContext()->GetFreshId();
63 ApplyTransformation(TransformationAddTypePointer(
64 pointer_type, SpvStorageClassFunction, basic_type));
65 available_pointers_to_basic_type.push_back(pointer_type);
66 } else {
67 // There is - grab one.
68 pointer_type =
69 available_pointers_to_basic_type[GetFuzzerContext()->RandomIndex(
70 available_pointers_to_basic_type)];
71 }
72 ApplyTransformation(TransformationAddLocalVariable(
73 GetFuzzerContext()->GetFreshId(), pointer_type, function.result_id(),
74 FindOrCreateZeroConstant(basic_type, false), true));
75 }
76 }
77 }
78
79 } // namespace fuzz
80 } // namespace spvtools