Debian Codegraph @

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

raw
1 // Copyright (c) 2019 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/transformation_add_constant_composite.h"
16
17 #include <vector>
18
19 #include "source/fuzz/fuzzer_util.h"
20
21 namespace spvtools {
22 namespace fuzz {
23
24 TransformationAddConstantComposite::TransformationAddConstantComposite(
25 const spvtools::fuzz::protobufs::TransformationAddConstantComposite&
26 message)
27 : message_(message) {}
28
29 TransformationAddConstantComposite::TransformationAddConstantComposite(
30 uint32_t fresh_id, uint32_t type_id,
31 const std::vector<uint32_t>& constituent_ids, bool is_irrelevant) {
32 message_.set_fresh_id(fresh_id);
33 message_.set_type_id(type_id);
34 message_.set_is_irrelevant(is_irrelevant);
35 for (auto constituent_id : constituent_ids) {
36 message_.add_constituent_id(constituent_id);
37 }
38 }
39
40 bool TransformationAddConstantComposite::IsApplicable(
41 opt::IRContext* ir_context, const TransformationContext& /*unused*/) const {
42 // Check that the given id is fresh.
43 if (!fuzzerutil::IsFreshId(ir_context, message_.fresh_id())) {
44 return false;
45 }
46 // Check that the composite type id is an instruction id.
47 auto composite_type_instruction =
48 ir_context->get_def_use_mgr()->GetDef(message_.type_id());
49 if (!composite_type_instruction) {
50 return false;
51 }
52 // Gather up the operands for the composite constant, in the process checking
53 // whether the given type really defines a composite and - in the case of a
54 // struct - whether its decorations are OK.
55 std::vector<uint32_t> constituent_type_ids;
56 switch (composite_type_instruction->opcode()) {
57 case SpvOpTypeArray:
58 for (uint32_t index = 0;
59 index <
60 fuzzerutil::GetArraySize(*composite_type_instruction, ir_context);
61 index++) {
62 constituent_type_ids.push_back(
63 composite_type_instruction->GetSingleWordInOperand(0));
64 }
65 break;
66 case SpvOpTypeMatrix:
67 case SpvOpTypeVector:
68 for (uint32_t index = 0;
69 index < composite_type_instruction->GetSingleWordInOperand(1);
70 index++) {
71 constituent_type_ids.push_back(
72 composite_type_instruction->GetSingleWordInOperand(0));
73 }
74 break;
75 case SpvOpTypeStruct:
76 // We do not create constants of structs decorated with Block nor
77 // BufferBlock. The SPIR-V spec does not explicitly disallow this, but it
78 // seems like a strange thing to do, so we disallow it to avoid triggering
79 // low priorty edge case issues related to it.
80 if (fuzzerutil::HasBlockOrBufferBlockDecoration(
81 ir_context, composite_type_instruction->result_id())) {
82 return false;
83 }
84 composite_type_instruction->ForEachInOperand(
85 [&constituent_type_ids](const uint32_t* member_type_id) {
86 constituent_type_ids.push_back(*member_type_id);
87 });
88 break;
89 default:
90 // Not a composite type.
91 return false;
92 }
93
94 // Check that the number of provided operands matches the number of
95 // constituents required by the type.
96 if (constituent_type_ids.size() !=
97 static_cast<uint32_t>(message_.constituent_id().size())) {
98 return false;
99 }
100
101 // Check that every provided operand refers to an instruction of the
102 // corresponding constituent type.
103 for (uint32_t index = 0; index < constituent_type_ids.size(); index++) {
104 auto constituent_instruction =
105 ir_context->get_def_use_mgr()->GetDef(message_.constituent_id(index));
106 if (!constituent_instruction) {
107 return false;
108 }
109 if (constituent_instruction->type_id() != constituent_type_ids.at(index)) {
110 return false;
111 }
112 }
113 return true;
114 }
115
116 void TransformationAddConstantComposite::Apply(
117 opt::IRContext* ir_context,
118 TransformationContext* transformation_context) const {
119 opt::Instruction::OperandList in_operands;
120 for (auto constituent_id : message_.constituent_id()) {
121 in_operands.push_back({SPV_OPERAND_TYPE_ID, {constituent_id}});
122 }
123 ir_context->module()->AddGlobalValue(MakeUnique<opt::Instruction>(
124 ir_context, SpvOpConstantComposite, message_.type_id(),
125 message_.fresh_id(), in_operands));
126 fuzzerutil::UpdateModuleIdBound(ir_context, message_.fresh_id());
127 // We have added an instruction to the module, so need to be careful about the
128 // validity of existing analyses.
129 ir_context->InvalidateAnalysesExceptFor(
130 opt::IRContext::Analysis::kAnalysisNone);
131
132 if (message_.is_irrelevant()) {
133 transformation_context->GetFactManager()->AddFactIdIsIrrelevant(
134 message_.fresh_id());
135 }
136 }
137
138 protobufs::Transformation TransformationAddConstantComposite::ToMessage()
139 const {
140 protobufs::Transformation result;
141 *result.mutable_add_constant_composite() = message_;
142 return result;
143 }
144
145 std::unordered_set<uint32_t> TransformationAddConstantComposite::GetFreshIds()
146 const {
147 return {message_.fresh_id()};
148 }
149
150 } // namespace fuzz
151 } // namespace spvtools