• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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           spv::Op::OpStore, instruction_to_insert_before) ||
57       !fuzzerutil::CanInsertOpcodeBeforeInstruction(
58           spv::Op::OpLoad, 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<spv::StorageClass>(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() ==
85               (uint32_t)spv::StorageClass::Private ||
86           message_.variable_storage_class() ==
87               (uint32_t)spv::StorageClass::Function) &&
88          "The variable storage class must be private or function.");
89 
90   // Check that initializer is valid.
91   const auto* constant_inst =
92       ir_context->get_def_use_mgr()->GetDef(message_.initializer_id());
93   if (!constant_inst || !spvOpcodeIsConstant(constant_inst->opcode()) ||
94       value_instruction->type_id() != constant_inst->type_id()) {
95     return false;
96   }
97 
98   // |message_.value_id| must be available at the insertion point.
99   return fuzzerutil::IdIsAvailableBeforeInstruction(
100       ir_context, instruction_to_insert_before, message_.value_id());
101 }
102 
Apply(opt::IRContext * ir_context,TransformationContext * transformation_context) const103 void TransformationPushIdThroughVariable::Apply(
104     opt::IRContext* ir_context,
105     TransformationContext* transformation_context) const {
106   auto value_instruction =
107       ir_context->get_def_use_mgr()->GetDef(message_.value_id());
108 
109   opt::Instruction* insert_before =
110       FindInstruction(message_.instruction_descriptor(), ir_context);
111   opt::BasicBlock* enclosing_block = ir_context->get_instr_block(insert_before);
112 
113   // A pointer type instruction pointing to the value type must be defined.
114   auto pointer_type_id = fuzzerutil::MaybeGetPointerType(
115       ir_context, value_instruction->type_id(),
116       static_cast<spv::StorageClass>(message_.variable_storage_class()));
117   assert(pointer_type_id && "The required pointer type must be available.");
118 
119   // Adds whether a global or local variable.
120   if (spv::StorageClass(message_.variable_storage_class()) ==
121       spv::StorageClass::Private) {
122     opt::Instruction* global_variable = fuzzerutil::AddGlobalVariable(
123         ir_context, message_.variable_id(), pointer_type_id,
124         spv::StorageClass::Private, message_.initializer_id());
125     ir_context->get_def_use_mgr()->AnalyzeInstDefUse(global_variable);
126   } else {
127     opt::Function* function =
128         ir_context
129             ->get_instr_block(
130                 FindInstruction(message_.instruction_descriptor(), ir_context))
131             ->GetParent();
132     opt::Instruction* local_variable = fuzzerutil::AddLocalVariable(
133         ir_context, message_.variable_id(), pointer_type_id,
134         function->result_id(), message_.initializer_id());
135     ir_context->get_def_use_mgr()->AnalyzeInstDefUse(local_variable);
136     ir_context->set_instr_block(local_variable, &*function->entry());
137   }
138 
139   // First, insert the OpLoad instruction before |instruction_descriptor| and
140   // then insert the OpStore instruction before the OpLoad instruction.
141   fuzzerutil::UpdateModuleIdBound(ir_context, message_.value_synonym_id());
142   opt::Instruction* load_instruction =
143       insert_before->InsertBefore(MakeUnique<opt::Instruction>(
144           ir_context, spv::Op::OpLoad, value_instruction->type_id(),
145           message_.value_synonym_id(),
146           opt::Instruction::OperandList(
147               {{SPV_OPERAND_TYPE_ID, {message_.variable_id()}}})));
148   opt::Instruction* store_instruction =
149       load_instruction->InsertBefore(MakeUnique<opt::Instruction>(
150           ir_context, spv::Op::OpStore, 0, 0,
151           opt::Instruction::OperandList(
152               {{SPV_OPERAND_TYPE_ID, {message_.variable_id()}},
153                {SPV_OPERAND_TYPE_ID, {message_.value_id()}}})));
154   ir_context->get_def_use_mgr()->AnalyzeInstDefUse(store_instruction);
155   ir_context->set_instr_block(store_instruction, enclosing_block);
156   ir_context->get_def_use_mgr()->AnalyzeInstDefUse(load_instruction);
157   ir_context->set_instr_block(load_instruction, enclosing_block);
158 
159   // We should be able to create a synonym of |value_id| if it's not irrelevant.
160   if (fuzzerutil::CanMakeSynonymOf(ir_context, *transformation_context,
161                                    *value_instruction) &&
162       !transformation_context->GetFactManager()->IdIsIrrelevant(
163           message_.value_synonym_id())) {
164     // Adds the fact that |message_.value_synonym_id|
165     // and |message_.value_id| are synonymous.
166     transformation_context->GetFactManager()->AddFactDataSynonym(
167         MakeDataDescriptor(message_.value_synonym_id(), {}),
168         MakeDataDescriptor(message_.value_id(), {}));
169   }
170 }
171 
ToMessage() const172 protobufs::Transformation TransformationPushIdThroughVariable::ToMessage()
173     const {
174   protobufs::Transformation result;
175   *result.mutable_push_id_through_variable() = message_;
176   return result;
177 }
178 
GetFreshIds() const179 std::unordered_set<uint32_t> TransformationPushIdThroughVariable::GetFreshIds()
180     const {
181   return {message_.value_synonym_id(), message_.variable_id()};
182 }
183 
184 }  // namespace fuzz
185 }  // namespace spvtools
186