1 // Copyright (c) 2020 Vasyl Teliman
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_composite_extract.h"
16
17 #include "source/fuzz/available_instructions.h"
18 #include "source/fuzz/fuzzer_context.h"
19 #include "source/fuzz/fuzzer_util.h"
20 #include "source/fuzz/instruction_descriptor.h"
21 #include "source/fuzz/transformation_composite_extract.h"
22
23 namespace spvtools {
24 namespace fuzz {
25
FuzzerPassAddCompositeExtract(opt::IRContext * ir_context,TransformationContext * transformation_context,FuzzerContext * fuzzer_context,protobufs::TransformationSequence * transformations)26 FuzzerPassAddCompositeExtract::FuzzerPassAddCompositeExtract(
27 opt::IRContext* ir_context, TransformationContext* transformation_context,
28 FuzzerContext* fuzzer_context,
29 protobufs::TransformationSequence* transformations)
30 : FuzzerPass(ir_context, transformation_context, fuzzer_context,
31 transformations) {}
32
Apply()33 void FuzzerPassAddCompositeExtract::Apply() {
34 std::vector<const protobufs::DataDescriptor*> composite_synonyms;
35 for (const auto* dd :
36 GetTransformationContext()->GetFactManager()->GetAllSynonyms()) {
37 // |dd| must describe a component of a composite.
38 if (!dd->index().empty()) {
39 composite_synonyms.push_back(dd);
40 }
41 }
42
43 AvailableInstructions available_composites(
44 GetIRContext(), [](opt::IRContext* ir_context, opt::Instruction* inst) {
45 return inst->type_id() && inst->result_id() &&
46 fuzzerutil::IsCompositeType(
47 ir_context->get_type_mgr()->GetType(inst->type_id()));
48 });
49
50 ForEachInstructionWithInstructionDescriptor(
51 [this, &available_composites, &composite_synonyms](
52 opt::Function* /*unused*/, opt::BasicBlock* /*unused*/,
53 opt::BasicBlock::iterator inst_it,
54 const protobufs::InstructionDescriptor& instruction_descriptor) {
55 if (!fuzzerutil::CanInsertOpcodeBeforeInstruction(SpvOpCompositeExtract,
56 inst_it)) {
57 return;
58 }
59
60 if (!GetFuzzerContext()->ChoosePercentage(
61 GetFuzzerContext()->GetChanceOfAddingCompositeExtract())) {
62 return;
63 }
64
65 std::vector<const protobufs::DataDescriptor*> available_synonyms;
66 for (const auto* dd : composite_synonyms) {
67 if (fuzzerutil::IdIsAvailableBeforeInstruction(
68 GetIRContext(), &*inst_it, dd->object())) {
69 available_synonyms.push_back(dd);
70 }
71 }
72
73 auto candidate_composites =
74 available_composites.GetAvailableBeforeInstruction(&*inst_it);
75
76 if (available_synonyms.empty() && candidate_composites.empty()) {
77 return;
78 }
79
80 uint32_t composite_id = 0;
81 std::vector<uint32_t> indices;
82
83 if (available_synonyms.empty() || (!candidate_composites.empty() &&
84 GetFuzzerContext()->ChooseEven())) {
85 const auto* inst =
86 candidate_composites[GetFuzzerContext()->RandomIndex(
87 candidate_composites)];
88 composite_id = inst->result_id();
89
90 auto type_id = inst->type_id();
91 do {
92 uint32_t number_of_members = 0;
93
94 const auto* type_inst =
95 GetIRContext()->get_def_use_mgr()->GetDef(type_id);
96 assert(type_inst && "Composite instruction has invalid type id");
97
98 switch (type_inst->opcode()) {
99 case SpvOpTypeArray:
100 number_of_members =
101 fuzzerutil::GetArraySize(*type_inst, GetIRContext());
102 break;
103 case SpvOpTypeVector:
104 case SpvOpTypeMatrix:
105 number_of_members = type_inst->GetSingleWordInOperand(1);
106 break;
107 case SpvOpTypeStruct:
108 number_of_members = type_inst->NumInOperands();
109 break;
110 default:
111 assert(false && "|type_inst| is not a composite");
112 return;
113 }
114
115 if (number_of_members == 0) {
116 return;
117 }
118
119 indices.push_back(
120 GetFuzzerContext()->GetRandomCompositeExtractIndex(
121 number_of_members));
122
123 switch (type_inst->opcode()) {
124 case SpvOpTypeArray:
125 case SpvOpTypeVector:
126 case SpvOpTypeMatrix:
127 type_id = type_inst->GetSingleWordInOperand(0);
128 break;
129 case SpvOpTypeStruct:
130 type_id = type_inst->GetSingleWordInOperand(indices.back());
131 break;
132 default:
133 assert(false && "|type_inst| is not a composite");
134 return;
135 }
136 } while (fuzzerutil::IsCompositeType(
137 GetIRContext()->get_type_mgr()->GetType(type_id)) &&
138 GetFuzzerContext()->ChoosePercentage(
139 GetFuzzerContext()
140 ->GetChanceOfGoingDeeperToExtractComposite()));
141 } else {
142 const auto* dd = available_synonyms[GetFuzzerContext()->RandomIndex(
143 available_synonyms)];
144
145 composite_id = dd->object();
146 indices.assign(dd->index().begin(), dd->index().end());
147 }
148
149 assert(composite_id != 0 && !indices.empty() &&
150 "Composite object should have been chosen correctly");
151
152 ApplyTransformation(TransformationCompositeExtract(
153 instruction_descriptor, GetFuzzerContext()->GetFreshId(),
154 composite_id, indices));
155 });
156 }
157
158 } // namespace fuzz
159 } // namespace spvtools
160