transformation_copy_object.cpp 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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. #include "source/fuzz/transformation_copy_object.h"
  15. #include "source/fuzz/fuzzer_util.h"
  16. #include "source/opt/instruction.h"
  17. #include "source/util/make_unique.h"
  18. namespace spvtools {
  19. namespace fuzz {
  20. TransformationCopyObject::TransformationCopyObject(
  21. const protobufs::TransformationCopyObject& message)
  22. : message_(message) {}
  23. TransformationCopyObject::TransformationCopyObject(uint32_t object,
  24. uint32_t base_instruction_id,
  25. uint32_t offset,
  26. uint32_t fresh_id) {
  27. message_.set_object(object);
  28. message_.set_base_instruction_id(base_instruction_id);
  29. message_.set_offset(offset);
  30. message_.set_fresh_id(fresh_id);
  31. }
  32. bool TransformationCopyObject::IsApplicable(
  33. opt::IRContext* context, const FactManager& /*fact_manager*/) const {
  34. if (!fuzzerutil::IsFreshId(context, message_.fresh_id())) {
  35. // We require the id for the object copy to be unused.
  36. return false;
  37. }
  38. // The id of the object to be copied must exist
  39. auto object_inst = context->get_def_use_mgr()->GetDef(message_.object());
  40. if (!object_inst) {
  41. return false;
  42. }
  43. if (!IsCopyable(context, object_inst)) {
  44. return false;
  45. }
  46. auto base_instruction =
  47. context->get_def_use_mgr()->GetDef(message_.base_instruction_id());
  48. if (!base_instruction) {
  49. // The given id to insert after is not defined.
  50. return false;
  51. }
  52. auto destination_block = context->get_instr_block(base_instruction);
  53. if (!destination_block) {
  54. // The given id to insert after is not in a block.
  55. return false;
  56. }
  57. auto insert_before = fuzzerutil::GetIteratorForBaseInstructionAndOffset(
  58. destination_block, base_instruction, message_.offset());
  59. if (insert_before == destination_block->end()) {
  60. // The offset was inappropriate.
  61. return false;
  62. }
  63. if (!CanInsertCopyBefore(insert_before)) {
  64. return false;
  65. }
  66. // |message_object| must be available at the point where we want to add the
  67. // copy. It is available if it is at global scope (in which case it has no
  68. // block), or if it dominates the point of insertion but is different from the
  69. // point of insertion.
  70. //
  71. // The reason why the object needs to be different from the insertion point is
  72. // that the copy will be added *before* this point, and we do not want to
  73. // insert it before the object's defining instruction.
  74. return !context->get_instr_block(object_inst) ||
  75. (object_inst != &*insert_before &&
  76. context->GetDominatorAnalysis(destination_block->GetParent())
  77. ->Dominates(object_inst, &*insert_before));
  78. }
  79. void TransformationCopyObject::Apply(opt::IRContext* context,
  80. FactManager* fact_manager) const {
  81. // - A new instruction,
  82. // %|message_.fresh_id| = OpCopyObject %ty %|message_.object|
  83. // is added directly before the instruction at |message_.insert_after_id| +
  84. // |message_|.offset, where %ty is the type of |message_.object|.
  85. // - The fact that |message_.fresh_id| and |message_.object| are synonyms
  86. // is added to the fact manager.
  87. // The id of the object to be copied must exist
  88. auto object_inst = context->get_def_use_mgr()->GetDef(message_.object());
  89. assert(object_inst && "The object to be copied must exist.");
  90. auto base_instruction =
  91. context->get_def_use_mgr()->GetDef(message_.base_instruction_id());
  92. assert(base_instruction && "The base instruction must exist.");
  93. auto destination_block = context->get_instr_block(base_instruction);
  94. assert(destination_block && "The base instruction must be in a block.");
  95. auto insert_before = fuzzerutil::GetIteratorForBaseInstructionAndOffset(
  96. destination_block, base_instruction, message_.offset());
  97. assert(insert_before != destination_block->end() &&
  98. "There must be an instruction before which the copy can be inserted.");
  99. opt::Instruction::OperandList operands = {
  100. {SPV_OPERAND_TYPE_ID, {message_.object()}}};
  101. insert_before->InsertBefore(MakeUnique<opt::Instruction>(
  102. context, SpvOp::SpvOpCopyObject, object_inst->type_id(),
  103. message_.fresh_id(), operands));
  104. fuzzerutil::UpdateModuleIdBound(context, message_.fresh_id());
  105. context->InvalidateAnalysesExceptFor(opt::IRContext::Analysis::kAnalysisNone);
  106. protobufs::Fact fact;
  107. fact.mutable_id_synonym_fact()->set_id(message_.object());
  108. fact.mutable_id_synonym_fact()->mutable_data_descriptor()->set_object(
  109. message_.fresh_id());
  110. fact_manager->AddFact(fact, context);
  111. }
  112. protobufs::Transformation TransformationCopyObject::ToMessage() const {
  113. protobufs::Transformation result;
  114. *result.mutable_copy_object() = message_;
  115. return result;
  116. }
  117. bool TransformationCopyObject::IsCopyable(opt::IRContext* ir_context,
  118. opt::Instruction* inst) {
  119. if (!inst->HasResultId()) {
  120. // We can only apply OpCopyObject to instructions that generate ids.
  121. return false;
  122. }
  123. if (!inst->type_id()) {
  124. // We can only apply OpCopyObject to instructions that have types.
  125. return false;
  126. }
  127. // We do not copy objects that have decorations: if the copy is not
  128. // decorated analogously, using the original object vs. its copy may not be
  129. // equivalent.
  130. // TODO(afd): it would be possible to make the copy but not add an id
  131. // synonym.
  132. return ir_context->get_decoration_mgr()
  133. ->GetDecorationsFor(inst->result_id(), true)
  134. .empty();
  135. }
  136. bool TransformationCopyObject::CanInsertCopyBefore(
  137. const opt::BasicBlock::iterator& instruction_in_block) {
  138. if (instruction_in_block->PreviousNode() &&
  139. (instruction_in_block->PreviousNode()->opcode() == SpvOpLoopMerge ||
  140. instruction_in_block->PreviousNode()->opcode() == SpvOpSelectionMerge)) {
  141. // We cannot insert a copy directly after a merge instruction.
  142. return false;
  143. }
  144. if (instruction_in_block->opcode() == SpvOpVariable) {
  145. // We cannot insert a copy directly before a variable; variables in a
  146. // function must be contiguous in the entry block.
  147. return false;
  148. }
  149. // We cannot insert a copy directly before OpPhi, because OpPhi instructions
  150. // need to be contiguous at the start of a block.
  151. return instruction_in_block->opcode() != SpvOpPhi;
  152. }
  153. } // namespace fuzz
  154. } // namespace spvtools