transformation_copy_object.cpp 6.3 KB

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