1 // Copyright (c) 2020 André Perez Maselco
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_push_id_through_variable.h"
16
17 #include "source/fuzz/fuzzer_util.h"
18 #include "source/fuzz/instruction_descriptor.h"
19
20 namespace spvtools {
21 namespace fuzz {
22
TransformationPushIdThroughVariable(protobufs::TransformationPushIdThroughVariable message)23 TransformationPushIdThroughVariable::TransformationPushIdThroughVariable(
24 protobufs::TransformationPushIdThroughVariable message)
25 : message_(std::move(message)) {}
26
TransformationPushIdThroughVariable(uint32_t value_id,uint32_t value_synonym_id,uint32_t variable_id,uint32_t variable_storage_class,uint32_t initializer_id,const protobufs::InstructionDescriptor & instruction_descriptor)27 TransformationPushIdThroughVariable::TransformationPushIdThroughVariable(
28 uint32_t value_id, uint32_t value_synonym_id, uint32_t variable_id,
29 uint32_t variable_storage_class, uint32_t initializer_id,
30 const protobufs::InstructionDescriptor& instruction_descriptor) {
31 message_.set_value_id(value_id);
32 message_.set_value_synonym_id(value_synonym_id);
33 message_.set_variable_id(variable_id);
34 message_.set_variable_storage_class(variable_storage_class);
35 message_.set_initializer_id(initializer_id);
36 *message_.mutable_instruction_descriptor() = instruction_descriptor;
37 }
38
IsApplicable(opt::IRContext * ir_context,const TransformationContext &) const39 bool TransformationPushIdThroughVariable::IsApplicable(
40 opt::IRContext* ir_context, const TransformationContext& /*unused*/) const {
41 // |message_.value_synonym_id| and |message_.variable_id| must be fresh.
42 if (!fuzzerutil::IsFreshId(ir_context, message_.value_synonym_id()) ||
43 !fuzzerutil::IsFreshId(ir_context, message_.variable_id())) {
44 return false;
45 }
46
47 // The instruction to insert before must be defined.
48 auto instruction_to_insert_before =
49 FindInstruction(message_.instruction_descriptor(), ir_context);
50 if (!instruction_to_insert_before) {
51 return false;
52 }
53
54 // It must be valid to insert the OpStore and OpLoad instruction before it.
55 if (!fuzzerutil::CanInsertOpcodeBeforeInstruction(
56 SpvOpStore, instruction_to_insert_before) ||
57 !fuzzerutil::CanInsertOpcodeBeforeInstruction(
58 SpvOpLoad, instruction_to_insert_before)) {
59 return false;
60 }
61
62 // The instruction to insert before must belong to a reachable block.
63 auto basic_block = ir_context->get_instr_block(instruction_to_insert_before);
64 if (!ir_context->IsReachable(*basic_block)) {
65 return false;
66 }
67
68 // The value instruction must be defined and have a type.
69 auto value_instruction =
70 ir_context->get_def_use_mgr()->GetDef(message_.value_id());
71 if (!value_instruction || !value_instruction->type_id()) {
72 return false;
73 }
74
75 // A pointer type instruction pointing to the value type must be defined.
76 auto pointer_type_id = fuzzerutil::MaybeGetPointerType(
77 ir_context, value_instruction->type_id(),
78 static_cast<SpvStorageClass>(message_.variable_storage_class()));
79 if (!pointer_type_id) {
80 return false;
81 }
82
83 // |message_.variable_storage_class| must be private or function.
84 assert((message_.variable_storage_class() == SpvStorageClassPrivate ||
85 message_.variable_storage_class() == SpvStorageClassFunction) &&
86 "The variable storage class must be private or function.");
87
88 // Check that initializer is valid.
89 const auto* constant_inst =
90 ir_context->get_def_use_mgr()->GetDef(message_.initializer_id());
91 if (!constant_inst || !spvOpcodeIsConstant(constant_inst->opcode()) ||
92 value_instruction->type_id() != constant_inst->type_id()) {
93 return false;
94 }
95
96 // |message_.value_id| must be available at the insertion point.
97 return fuzzerutil::IdIsAvailableBeforeInstruction(
98 ir_context, instruction_to_insert_before, message_.value_id());
99 }
100
Apply(opt::IRContext * ir_context,TransformationContext * transformation_context) const101 void TransformationPushIdThroughVariable::Apply(
102 opt::IRContext* ir_context,
103 TransformationContext* transformation_context) const {
104 auto value_instruction =
105 ir_context->get_def_use_mgr()->GetDef(message_.value_id());
106
107 opt::Instruction* insert_before =
108 FindInstruction(message_.instruction_descriptor(), ir_context);
109 opt::BasicBlock* enclosing_block = ir_context->get_instr_block(insert_before);
110
111 // A pointer type instruction pointing to the value type must be defined.
112 auto pointer_type_id = fuzzerutil::MaybeGetPointerType(
113 ir_context, value_instruction->type_id(),
114 static_cast<SpvStorageClass>(message_.variable_storage_class()));
115 assert(pointer_type_id && "The required pointer type must be available.");
116
117 // Adds whether a global or local variable.
118 if (message_.variable_storage_class() == SpvStorageClassPrivate) {
119 opt::Instruction* global_variable = fuzzerutil::AddGlobalVariable(
120 ir_context, message_.variable_id(), pointer_type_id,
121 SpvStorageClassPrivate, message_.initializer_id());
122 ir_context->get_def_use_mgr()->AnalyzeInstDefUse(global_variable);
123 } else {
124 opt::Function* function =
125 ir_context
126 ->get_instr_block(
127 FindInstruction(message_.instruction_descriptor(), ir_context))
128 ->GetParent();
129 opt::Instruction* local_variable = fuzzerutil::AddLocalVariable(
130 ir_context, message_.variable_id(), pointer_type_id,
131 function->result_id(), message_.initializer_id());
132 ir_context->get_def_use_mgr()->AnalyzeInstDefUse(local_variable);
133 ir_context->set_instr_block(local_variable, &*function->entry());
134 }
135
136 // First, insert the OpLoad instruction before |instruction_descriptor| and
137 // then insert the OpStore instruction before the OpLoad instruction.
138 fuzzerutil::UpdateModuleIdBound(ir_context, message_.value_synonym_id());
139 opt::Instruction* load_instruction =
140 insert_before->InsertBefore(MakeUnique<opt::Instruction>(
141 ir_context, SpvOpLoad, value_instruction->type_id(),
142 message_.value_synonym_id(),
143 opt::Instruction::OperandList(
144 {{SPV_OPERAND_TYPE_ID, {message_.variable_id()}}})));
145 opt::Instruction* store_instruction =
146 load_instruction->InsertBefore(MakeUnique<opt::Instruction>(
147 ir_context, SpvOpStore, 0, 0,
148 opt::Instruction::OperandList(
149 {{SPV_OPERAND_TYPE_ID, {message_.variable_id()}},
150 {SPV_OPERAND_TYPE_ID, {message_.value_id()}}})));
151 ir_context->get_def_use_mgr()->AnalyzeInstDefUse(store_instruction);
152 ir_context->set_instr_block(store_instruction, enclosing_block);
153 ir_context->get_def_use_mgr()->AnalyzeInstDefUse(load_instruction);
154 ir_context->set_instr_block(load_instruction, enclosing_block);
155
156 // We should be able to create a synonym of |value_id| if it's not irrelevant.
157 if (fuzzerutil::CanMakeSynonymOf(ir_context, *transformation_context,
158 *value_instruction) &&
159 !transformation_context->GetFactManager()->IdIsIrrelevant(
160 message_.value_synonym_id())) {
161 // Adds the fact that |message_.value_synonym_id|
162 // and |message_.value_id| are synonymous.
163 transformation_context->GetFactManager()->AddFactDataSynonym(
164 MakeDataDescriptor(message_.value_synonym_id(), {}),
165 MakeDataDescriptor(message_.value_id(), {}));
166 }
167 }
168
ToMessage() const169 protobufs::Transformation TransformationPushIdThroughVariable::ToMessage()
170 const {
171 protobufs::Transformation result;
172 *result.mutable_push_id_through_variable() = message_;
173 return result;
174 }
175
GetFreshIds() const176 std::unordered_set<uint32_t> TransformationPushIdThroughVariable::GetFreshIds()
177 const {
178 return {message_.value_synonym_id(), message_.variable_id()};
179 }
180
181 } // namespace fuzz
182 } // namespace spvtools
183