transformation_add_copy_memory.cpp 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. // Copyright (c) 2020 Vasyl Teliman
  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_add_copy_memory.h"
  15. #include "source/fuzz/fuzzer_util.h"
  16. #include "source/fuzz/instruction_descriptor.h"
  17. #include "source/opt/instruction.h"
  18. namespace spvtools {
  19. namespace fuzz {
  20. TransformationAddCopyMemory::TransformationAddCopyMemory(
  21. protobufs::TransformationAddCopyMemory message)
  22. : message_(std::move(message)) {}
  23. TransformationAddCopyMemory::TransformationAddCopyMemory(
  24. const protobufs::InstructionDescriptor& instruction_descriptor,
  25. uint32_t fresh_id, uint32_t source_id, spv::StorageClass storage_class,
  26. uint32_t initializer_id) {
  27. *message_.mutable_instruction_descriptor() = instruction_descriptor;
  28. message_.set_fresh_id(fresh_id);
  29. message_.set_source_id(source_id);
  30. message_.set_storage_class(uint32_t(storage_class));
  31. message_.set_initializer_id(initializer_id);
  32. }
  33. bool TransformationAddCopyMemory::IsApplicable(
  34. opt::IRContext* ir_context, const TransformationContext& /*unused*/) const {
  35. // Check that target id is fresh.
  36. if (!fuzzerutil::IsFreshId(ir_context, message_.fresh_id())) {
  37. return false;
  38. }
  39. // Check that instruction descriptor is valid. This also checks that
  40. // |message_.instruction_descriptor| is not a global instruction.
  41. auto* inst = FindInstruction(message_.instruction_descriptor(), ir_context);
  42. if (!inst) {
  43. return false;
  44. }
  45. // Check that we can insert OpCopyMemory before |instruction_descriptor|.
  46. auto iter = fuzzerutil::GetIteratorForInstruction(
  47. ir_context->get_instr_block(inst), inst);
  48. if (!fuzzerutil::CanInsertOpcodeBeforeInstruction(spv::Op::OpCopyMemory,
  49. iter)) {
  50. return false;
  51. }
  52. // Check that source instruction exists and is valid.
  53. auto* source_inst =
  54. ir_context->get_def_use_mgr()->GetDef(message_.source_id());
  55. if (!source_inst || !IsInstructionSupported(ir_context, source_inst)) {
  56. return false;
  57. }
  58. // |storage_class| is either Function or Private.
  59. if (spv::StorageClass(message_.storage_class()) !=
  60. spv::StorageClass::Function &&
  61. spv::StorageClass(message_.storage_class()) !=
  62. spv::StorageClass::Private) {
  63. return false;
  64. }
  65. auto pointee_type_id = fuzzerutil::GetPointeeTypeIdFromPointerType(
  66. ir_context, source_inst->type_id());
  67. // OpTypePointer with |message_.storage_class| exists.
  68. if (!fuzzerutil::MaybeGetPointerType(
  69. ir_context, pointee_type_id,
  70. static_cast<spv::StorageClass>(message_.storage_class()))) {
  71. return false;
  72. }
  73. // Check that |initializer_id| exists and has valid type.
  74. const auto* initializer_inst =
  75. ir_context->get_def_use_mgr()->GetDef(message_.initializer_id());
  76. if (!initializer_inst || initializer_inst->type_id() != pointee_type_id) {
  77. return false;
  78. }
  79. // Check that domination rules are satisfied.
  80. return fuzzerutil::IdIsAvailableBeforeInstruction(ir_context, inst,
  81. message_.source_id());
  82. }
  83. void TransformationAddCopyMemory::Apply(
  84. opt::IRContext* ir_context,
  85. TransformationContext* transformation_context) const {
  86. // Insert OpCopyMemory before |instruction_descriptor|.
  87. auto* insert_before_inst =
  88. FindInstruction(message_.instruction_descriptor(), ir_context);
  89. assert(insert_before_inst);
  90. opt::BasicBlock* enclosing_block =
  91. ir_context->get_instr_block(insert_before_inst);
  92. // Add global or local variable to copy memory into.
  93. auto storage_class = static_cast<spv::StorageClass>(message_.storage_class());
  94. auto type_id = fuzzerutil::MaybeGetPointerType(
  95. ir_context,
  96. fuzzerutil::GetPointeeTypeIdFromPointerType(
  97. ir_context, fuzzerutil::GetTypeId(ir_context, message_.source_id())),
  98. storage_class);
  99. if (storage_class == spv::StorageClass::Private) {
  100. opt::Instruction* new_global =
  101. fuzzerutil::AddGlobalVariable(ir_context, message_.fresh_id(), type_id,
  102. storage_class, message_.initializer_id());
  103. ir_context->get_def_use_mgr()->AnalyzeInstDefUse(new_global);
  104. } else {
  105. assert(storage_class == spv::StorageClass::Function &&
  106. "Storage class can be either Private or Function");
  107. opt::Function* enclosing_function = enclosing_block->GetParent();
  108. opt::Instruction* new_local = fuzzerutil::AddLocalVariable(
  109. ir_context, message_.fresh_id(), type_id,
  110. enclosing_function->result_id(), message_.initializer_id());
  111. ir_context->get_def_use_mgr()->AnalyzeInstDefUse(new_local);
  112. ir_context->set_instr_block(new_local, &*enclosing_function->entry());
  113. }
  114. auto insert_before_iter = fuzzerutil::GetIteratorForInstruction(
  115. enclosing_block, insert_before_inst);
  116. auto new_instruction = MakeUnique<opt::Instruction>(
  117. ir_context, spv::Op::OpCopyMemory, 0, 0,
  118. opt::Instruction::OperandList{
  119. {SPV_OPERAND_TYPE_ID, {message_.fresh_id()}},
  120. {SPV_OPERAND_TYPE_ID, {message_.source_id()}}});
  121. auto new_instruction_ptr = new_instruction.get();
  122. insert_before_iter.InsertBefore(std::move(new_instruction));
  123. ir_context->get_def_use_mgr()->AnalyzeInstDefUse(new_instruction_ptr);
  124. ir_context->set_instr_block(new_instruction_ptr, enclosing_block);
  125. fuzzerutil::UpdateModuleIdBound(ir_context, message_.fresh_id());
  126. // Even though the copy memory instruction will - at least temporarily - lead
  127. // to the destination and source pointers referring to identical values, this
  128. // fact is not guaranteed to hold throughout execution of the SPIR-V code
  129. // since the source pointer could be over-written. We thus assume nothing
  130. // about the destination pointer, and record this fact so that the destination
  131. // pointer can be used freely by other fuzzer passes.
  132. transformation_context->GetFactManager()->AddFactValueOfPointeeIsIrrelevant(
  133. message_.fresh_id());
  134. }
  135. protobufs::Transformation TransformationAddCopyMemory::ToMessage() const {
  136. protobufs::Transformation result;
  137. *result.mutable_add_copy_memory() = message_;
  138. return result;
  139. }
  140. bool TransformationAddCopyMemory::IsInstructionSupported(
  141. opt::IRContext* ir_context, opt::Instruction* inst) {
  142. if (!inst->result_id() || !inst->type_id() ||
  143. inst->opcode() == spv::Op::OpConstantNull ||
  144. inst->opcode() == spv::Op::OpUndef) {
  145. return false;
  146. }
  147. const auto* type = ir_context->get_type_mgr()->GetType(inst->type_id());
  148. assert(type && "Instruction must have a valid type");
  149. if (!type->AsPointer()) {
  150. return false;
  151. }
  152. // We do not support copying memory from a pointer to a block-/buffer
  153. // block-decorated struct.
  154. auto pointee_type_inst = ir_context->get_def_use_mgr()
  155. ->GetDef(inst->type_id())
  156. ->GetSingleWordInOperand(1);
  157. if (fuzzerutil::HasBlockOrBufferBlockDecoration(ir_context,
  158. pointee_type_inst)) {
  159. return false;
  160. }
  161. return CanUsePointeeWithCopyMemory(*type->AsPointer()->pointee_type());
  162. }
  163. bool TransformationAddCopyMemory::CanUsePointeeWithCopyMemory(
  164. const opt::analysis::Type& type) {
  165. switch (type.kind()) {
  166. case opt::analysis::Type::kBool:
  167. case opt::analysis::Type::kInteger:
  168. case opt::analysis::Type::kFloat:
  169. case opt::analysis::Type::kArray:
  170. return true;
  171. case opt::analysis::Type::kVector:
  172. return CanUsePointeeWithCopyMemory(*type.AsVector()->element_type());
  173. case opt::analysis::Type::kMatrix:
  174. return CanUsePointeeWithCopyMemory(*type.AsMatrix()->element_type());
  175. case opt::analysis::Type::kStruct:
  176. return std::all_of(type.AsStruct()->element_types().begin(),
  177. type.AsStruct()->element_types().end(),
  178. [](const opt::analysis::Type* element) {
  179. return CanUsePointeeWithCopyMemory(*element);
  180. });
  181. default:
  182. return false;
  183. }
  184. }
  185. std::unordered_set<uint32_t> TransformationAddCopyMemory::GetFreshIds() const {
  186. return {message_.fresh_id()};
  187. }
  188. } // namespace fuzz
  189. } // namespace spvtools