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
TransformationAddConstantComposite(const spvtools::fuzz::protobufs::TransformationAddConstantComposite & message)24 TransformationAddConstantComposite::TransformationAddConstantComposite(
25 const spvtools::fuzz::protobufs::TransformationAddConstantComposite&
26 message)
27 : message_(message) {}
28
TransformationAddConstantComposite(uint32_t fresh_id,uint32_t type_id,const std::vector<uint32_t> & constituent_ids)29 TransformationAddConstantComposite::TransformationAddConstantComposite(
30 uint32_t fresh_id, uint32_t type_id,
31 const std::vector<uint32_t>& constituent_ids) {
32 message_.set_fresh_id(fresh_id);
33 message_.set_type_id(type_id);
34 for (auto constituent_id : constituent_ids) {
35 message_.add_constituent_id(constituent_id);
36 }
37 }
38
IsApplicable(opt::IRContext * ir_context,const TransformationContext &) const39 bool TransformationAddConstantComposite::IsApplicable(
40 opt::IRContext* ir_context, const TransformationContext& /*unused*/) const {
41 // Check that the given id is fresh.
42 if (!fuzzerutil::IsFreshId(ir_context, message_.fresh_id())) {
43 return false;
44 }
45 // Check that the composite type id is an instruction id.
46 auto composite_type_instruction =
47 ir_context->get_def_use_mgr()->GetDef(message_.type_id());
48 if (!composite_type_instruction) {
49 return false;
50 }
51 // Gather up the operands for the composite constant, in the process checking
52 // whether the given type really defines a composite.
53 std::vector<uint32_t> constituent_type_ids;
54 switch (composite_type_instruction->opcode()) {
55 case SpvOpTypeArray:
56 for (uint32_t index = 0;
57 index <
58 fuzzerutil::GetArraySize(*composite_type_instruction, ir_context);
59 index++) {
60 constituent_type_ids.push_back(
61 composite_type_instruction->GetSingleWordInOperand(0));
62 }
63 break;
64 case SpvOpTypeMatrix:
65 case SpvOpTypeVector:
66 for (uint32_t index = 0;
67 index < composite_type_instruction->GetSingleWordInOperand(1);
68 index++) {
69 constituent_type_ids.push_back(
70 composite_type_instruction->GetSingleWordInOperand(0));
71 }
72 break;
73 case SpvOpTypeStruct:
74 composite_type_instruction->ForEachInOperand(
75 [&constituent_type_ids](const uint32_t* member_type_id) {
76 constituent_type_ids.push_back(*member_type_id);
77 });
78 break;
79 default:
80 // Not a composite type.
81 return false;
82 }
83
84 // Check that the number of provided operands matches the number of
85 // constituents required by the type.
86 if (constituent_type_ids.size() !=
87 static_cast<uint32_t>(message_.constituent_id().size())) {
88 return false;
89 }
90
91 // Check that every provided operand refers to an instruction of the
92 // corresponding constituent type.
93 for (uint32_t index = 0; index < constituent_type_ids.size(); index++) {
94 auto constituent_instruction =
95 ir_context->get_def_use_mgr()->GetDef(message_.constituent_id(index));
96 if (!constituent_instruction) {
97 return false;
98 }
99 if (constituent_instruction->type_id() != constituent_type_ids.at(index)) {
100 return false;
101 }
102 }
103 return true;
104 }
105
Apply(opt::IRContext * ir_context,TransformationContext *) const106 void TransformationAddConstantComposite::Apply(
107 opt::IRContext* ir_context, TransformationContext* /*unused*/) const {
108 opt::Instruction::OperandList in_operands;
109 for (auto constituent_id : message_.constituent_id()) {
110 in_operands.push_back({SPV_OPERAND_TYPE_ID, {constituent_id}});
111 }
112 ir_context->module()->AddGlobalValue(MakeUnique<opt::Instruction>(
113 ir_context, SpvOpConstantComposite, message_.type_id(),
114 message_.fresh_id(), in_operands));
115 fuzzerutil::UpdateModuleIdBound(ir_context, message_.fresh_id());
116 // We have added an instruction to the module, so need to be careful about the
117 // validity of existing analyses.
118 ir_context->InvalidateAnalysesExceptFor(
119 opt::IRContext::Analysis::kAnalysisNone);
120 }
121
ToMessage() const122 protobufs::Transformation TransformationAddConstantComposite::ToMessage()
123 const {
124 protobufs::Transformation result;
125 *result.mutable_add_constant_composite() = message_;
126 return result;
127 }
128
129 } // namespace fuzz
130 } // namespace spvtools
131