transformation_composite_insert.cpp 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. // Copyright (c) 2020 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. #include "transformation_composite_insert.h"
  15. #include "source/fuzz/fuzzer_util.h"
  16. #include "source/fuzz/instruction_descriptor.h"
  17. namespace spvtools {
  18. namespace fuzz {
  19. TransformationCompositeInsert::TransformationCompositeInsert(
  20. protobufs::TransformationCompositeInsert message)
  21. : message_(std::move(message)) {}
  22. TransformationCompositeInsert::TransformationCompositeInsert(
  23. const protobufs::InstructionDescriptor& instruction_to_insert_before,
  24. uint32_t fresh_id, uint32_t composite_id, uint32_t object_id,
  25. const std::vector<uint32_t>& index) {
  26. *message_.mutable_instruction_to_insert_before() =
  27. instruction_to_insert_before;
  28. message_.set_fresh_id(fresh_id);
  29. message_.set_composite_id(composite_id);
  30. message_.set_object_id(object_id);
  31. for (auto an_index : index) {
  32. message_.add_index(an_index);
  33. }
  34. }
  35. bool TransformationCompositeInsert::IsApplicable(
  36. opt::IRContext* ir_context, const TransformationContext& /*unused*/) const {
  37. // |message_.fresh_id| must be fresh.
  38. if (!fuzzerutil::IsFreshId(ir_context, message_.fresh_id())) {
  39. return false;
  40. }
  41. // |message_.composite_id| must refer to an existing composite value.
  42. auto composite =
  43. ir_context->get_def_use_mgr()->GetDef(message_.composite_id());
  44. if (!IsCompositeInstructionSupported(ir_context, composite)) {
  45. return false;
  46. }
  47. // The indices in |message_.index| must be suitable for indexing into
  48. // |composite->type_id()|.
  49. auto component_to_be_replaced_type_id = fuzzerutil::WalkCompositeTypeIndices(
  50. ir_context, composite->type_id(), message_.index());
  51. if (component_to_be_replaced_type_id == 0) {
  52. return false;
  53. }
  54. // The instruction having the id of |message_.object_id| must be defined.
  55. auto object_instruction =
  56. ir_context->get_def_use_mgr()->GetDef(message_.object_id());
  57. if (object_instruction == nullptr || object_instruction->type_id() == 0) {
  58. return false;
  59. }
  60. // We ignore pointers for now.
  61. auto object_instruction_type =
  62. ir_context->get_type_mgr()->GetType(object_instruction->type_id());
  63. if (object_instruction_type->AsPointer() != nullptr) {
  64. return false;
  65. }
  66. // The type id of the object having |message_.object_id| and the type id of
  67. // the component of the composite at index |message_.index| must be the same.
  68. if (component_to_be_replaced_type_id != object_instruction->type_id()) {
  69. return false;
  70. }
  71. // |message_.instruction_to_insert_before| must be a defined instruction.
  72. auto instruction_to_insert_before =
  73. FindInstruction(message_.instruction_to_insert_before(), ir_context);
  74. if (instruction_to_insert_before == nullptr) {
  75. return false;
  76. }
  77. // |message_.composite_id| and |message_.object_id| must be available before
  78. // the |message_.instruction_to_insert_before|.
  79. if (!fuzzerutil::IdIsAvailableBeforeInstruction(
  80. ir_context, instruction_to_insert_before, message_.composite_id())) {
  81. return false;
  82. }
  83. if (!fuzzerutil::IdIsAvailableBeforeInstruction(
  84. ir_context, instruction_to_insert_before, message_.object_id())) {
  85. return false;
  86. }
  87. // It must be possible to insert an OpCompositeInsert before this
  88. // instruction.
  89. return fuzzerutil::CanInsertOpcodeBeforeInstruction(
  90. spv::Op::OpCompositeInsert, instruction_to_insert_before);
  91. }
  92. void TransformationCompositeInsert::Apply(
  93. opt::IRContext* ir_context,
  94. TransformationContext* transformation_context) const {
  95. // |message_.struct_fresh_id| must be fresh.
  96. assert(fuzzerutil::IsFreshId(ir_context, message_.fresh_id()) &&
  97. "|message_.fresh_id| must be fresh");
  98. std::vector<uint32_t> index =
  99. fuzzerutil::RepeatedFieldToVector(message_.index());
  100. opt::Instruction::OperandList in_operands;
  101. in_operands.push_back({SPV_OPERAND_TYPE_ID, {message_.object_id()}});
  102. in_operands.push_back({SPV_OPERAND_TYPE_ID, {message_.composite_id()}});
  103. for (auto i : index) {
  104. in_operands.push_back({SPV_OPERAND_TYPE_LITERAL_INTEGER, {i}});
  105. }
  106. auto composite_type_id =
  107. fuzzerutil::GetTypeId(ir_context, message_.composite_id());
  108. auto insert_before =
  109. FindInstruction(message_.instruction_to_insert_before(), ir_context);
  110. auto new_instruction = MakeUnique<opt::Instruction>(
  111. ir_context, spv::Op::OpCompositeInsert, composite_type_id,
  112. message_.fresh_id(), std::move(in_operands));
  113. auto new_instruction_ptr = new_instruction.get();
  114. insert_before->InsertBefore(std::move(new_instruction));
  115. fuzzerutil::UpdateModuleIdBound(ir_context, message_.fresh_id());
  116. // Inform the def-use manager about the new instruction and record its basic
  117. // block.
  118. ir_context->get_def_use_mgr()->AnalyzeInstDefUse(new_instruction_ptr);
  119. ir_context->set_instr_block(new_instruction_ptr,
  120. ir_context->get_instr_block(insert_before));
  121. // Add data synonym facts that arise from the insertion.
  122. AddDataSynonymFacts(ir_context, transformation_context);
  123. }
  124. protobufs::Transformation TransformationCompositeInsert::ToMessage() const {
  125. protobufs::Transformation result;
  126. *result.mutable_composite_insert() = message_;
  127. return result;
  128. }
  129. bool TransformationCompositeInsert::IsCompositeInstructionSupported(
  130. opt::IRContext* ir_context, opt::Instruction* instruction) {
  131. if (instruction == nullptr) {
  132. return false;
  133. }
  134. if (instruction->result_id() == 0 || instruction->type_id() == 0) {
  135. return false;
  136. }
  137. auto composite_type =
  138. ir_context->get_type_mgr()->GetType(instruction->type_id());
  139. if (!fuzzerutil::IsCompositeType(composite_type)) {
  140. return false;
  141. }
  142. // Empty composites are not supported.
  143. auto instruction_type_inst =
  144. ir_context->get_def_use_mgr()->GetDef(instruction->type_id());
  145. if (fuzzerutil::GetBoundForCompositeIndex(*instruction_type_inst,
  146. ir_context) == 0) {
  147. return false;
  148. }
  149. return true;
  150. }
  151. std::unordered_set<uint32_t> TransformationCompositeInsert::GetFreshIds()
  152. const {
  153. return {message_.fresh_id()};
  154. }
  155. void TransformationCompositeInsert::AddDataSynonymFacts(
  156. opt::IRContext* ir_context,
  157. TransformationContext* transformation_context) const {
  158. // If the result id arising from the insertion is irrelevant then do not add
  159. // any data synonym facts. (The result id can be irrelevant if the insertion
  160. // occurs in a dead block.)
  161. if (transformation_context->GetFactManager()->IdIsIrrelevant(
  162. message_.fresh_id())) {
  163. return;
  164. }
  165. // So long as the |message_.composite_id| is suitable for participating in
  166. // synonyms, every every element of the insertion result except for at the
  167. // index being inserted into is synonymous with the corresponding element of
  168. // |message_.composite_id|. In that case, for every index that is a prefix of
  169. // |index|, the components different from the one that contains the inserted
  170. // object are synonymous with corresponding elements in the original
  171. // composite.
  172. uint32_t current_node_type_id =
  173. fuzzerutil::GetTypeId(ir_context, message_.composite_id());
  174. std::vector<uint32_t> current_index;
  175. std::vector<uint32_t> index =
  176. fuzzerutil::RepeatedFieldToVector(message_.index());
  177. for (uint32_t current_level : index) {
  178. auto current_node_type_inst =
  179. ir_context->get_def_use_mgr()->GetDef(current_node_type_id);
  180. uint32_t index_to_skip = current_level;
  181. uint32_t num_of_components = fuzzerutil::GetBoundForCompositeIndex(
  182. *current_node_type_inst, ir_context);
  183. // Update the current_node_type_id.
  184. current_node_type_id = fuzzerutil::WalkOneCompositeTypeIndex(
  185. ir_context, current_node_type_id, index_to_skip);
  186. for (uint32_t i = 0; i < num_of_components; i++) {
  187. if (i == index_to_skip) {
  188. continue;
  189. }
  190. current_index.push_back(i);
  191. if (fuzzerutil::CanMakeSynonymOf(ir_context, *transformation_context,
  192. *ir_context->get_def_use_mgr()->GetDef(
  193. message_.composite_id()))) {
  194. transformation_context->GetFactManager()->AddFactDataSynonym(
  195. MakeDataDescriptor(message_.fresh_id(), current_index),
  196. MakeDataDescriptor(message_.composite_id(), current_index));
  197. }
  198. current_index.pop_back();
  199. }
  200. // Store the prefix of the |index|.
  201. current_index.push_back(current_level);
  202. }
  203. // If the object being inserted supports synonym creation then it is
  204. // synonymous with the result of the insert instruction at the given index.
  205. if (fuzzerutil::CanMakeSynonymOf(
  206. ir_context, *transformation_context,
  207. *ir_context->get_def_use_mgr()->GetDef(message_.object_id()))) {
  208. transformation_context->GetFactManager()->AddFactDataSynonym(
  209. MakeDataDescriptor(message_.object_id(), {}),
  210. MakeDataDescriptor(message_.fresh_id(), index));
  211. }
  212. }
  213. } // namespace fuzz
  214. } // namespace spvtools