transformation_add_copy_memory.cpp 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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. const protobufs::TransformationAddCopyMemory& message)
  22. : message_(message) {}
  23. TransformationAddCopyMemory::TransformationAddCopyMemory(
  24. const protobufs::InstructionDescriptor& instruction_descriptor,
  25. uint32_t fresh_id, uint32_t source_id, SpvStorageClass 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(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(SpvOpCopyMemory, iter)) {
  49. return false;
  50. }
  51. // Check that source instruction exists and is valid.
  52. auto* source_inst =
  53. ir_context->get_def_use_mgr()->GetDef(message_.source_id());
  54. if (!source_inst || !IsInstructionSupported(ir_context, source_inst)) {
  55. return false;
  56. }
  57. // |storage_class| is either Function or Private.
  58. if (message_.storage_class() != SpvStorageClassFunction &&
  59. message_.storage_class() != SpvStorageClassPrivate) {
  60. return false;
  61. }
  62. auto pointee_type_id = fuzzerutil::GetPointeeTypeIdFromPointerType(
  63. ir_context, source_inst->type_id());
  64. // OpTypePointer with |message_.storage_class| exists.
  65. if (!fuzzerutil::MaybeGetPointerType(
  66. ir_context, pointee_type_id,
  67. static_cast<SpvStorageClass>(message_.storage_class()))) {
  68. return false;
  69. }
  70. // Check that |initializer_id| exists and has valid type.
  71. const auto* initializer_inst =
  72. ir_context->get_def_use_mgr()->GetDef(message_.initializer_id());
  73. if (!initializer_inst || initializer_inst->type_id() != pointee_type_id) {
  74. return false;
  75. }
  76. // Check that domination rules are satisfied.
  77. return fuzzerutil::IdIsAvailableBeforeInstruction(ir_context, inst,
  78. message_.source_id());
  79. }
  80. void TransformationAddCopyMemory::Apply(
  81. opt::IRContext* ir_context,
  82. TransformationContext* transformation_context) const {
  83. // Insert OpCopyMemory before |instruction_descriptor|.
  84. auto* insert_before_inst =
  85. FindInstruction(message_.instruction_descriptor(), ir_context);
  86. assert(insert_before_inst);
  87. auto insert_before_iter = fuzzerutil::GetIteratorForInstruction(
  88. ir_context->get_instr_block(insert_before_inst), insert_before_inst);
  89. insert_before_iter.InsertBefore(MakeUnique<opt::Instruction>(
  90. ir_context, SpvOpCopyMemory, 0, 0,
  91. opt::Instruction::OperandList{
  92. {SPV_OPERAND_TYPE_ID, {message_.fresh_id()}},
  93. {SPV_OPERAND_TYPE_ID, {message_.source_id()}}}));
  94. // Add global or local variable to copy memory into.
  95. auto storage_class = static_cast<SpvStorageClass>(message_.storage_class());
  96. auto type_id = fuzzerutil::MaybeGetPointerType(
  97. ir_context,
  98. fuzzerutil::GetPointeeTypeIdFromPointerType(
  99. ir_context, fuzzerutil::GetTypeId(ir_context, message_.source_id())),
  100. storage_class);
  101. if (storage_class == SpvStorageClassPrivate) {
  102. fuzzerutil::AddGlobalVariable(ir_context, message_.fresh_id(), type_id,
  103. storage_class, message_.initializer_id());
  104. } else {
  105. assert(storage_class == SpvStorageClassFunction &&
  106. "Storage class can be either Private or Function");
  107. fuzzerutil::AddLocalVariable(ir_context, message_.fresh_id(), type_id,
  108. ir_context->get_instr_block(insert_before_inst)
  109. ->GetParent()
  110. ->result_id(),
  111. message_.initializer_id());
  112. }
  113. fuzzerutil::UpdateModuleIdBound(ir_context, message_.fresh_id());
  114. // Even though the copy memory instruction will - at least temporarily - lead
  115. // to the destination and source pointers referring to identical values, this
  116. // fact is not guaranteed to hold throughout execution of the SPIR-V code
  117. // since the source pointer could be over-written. We thus assume nothing
  118. // about the destination pointer, and record this fact so that the destination
  119. // pointer can be used freely by other fuzzer passes.
  120. transformation_context->GetFactManager()->AddFactValueOfPointeeIsIrrelevant(
  121. message_.fresh_id());
  122. // Make sure our changes are analyzed
  123. ir_context->InvalidateAnalysesExceptFor(
  124. opt::IRContext::Analysis::kAnalysisNone);
  125. }
  126. protobufs::Transformation TransformationAddCopyMemory::ToMessage() const {
  127. protobufs::Transformation result;
  128. *result.mutable_add_copy_memory() = message_;
  129. return result;
  130. }
  131. bool TransformationAddCopyMemory::IsInstructionSupported(
  132. opt::IRContext* ir_context, opt::Instruction* inst) {
  133. if (!inst->result_id() || !inst->type_id() ||
  134. inst->opcode() == SpvOpConstantNull || inst->opcode() == SpvOpUndef) {
  135. return false;
  136. }
  137. const auto* type = ir_context->get_type_mgr()->GetType(inst->type_id());
  138. assert(type && "Instruction must have a valid type");
  139. return type->AsPointer() &&
  140. CanUsePointeeWithCopyMemory(*type->AsPointer()->pointee_type());
  141. }
  142. bool TransformationAddCopyMemory::CanUsePointeeWithCopyMemory(
  143. const opt::analysis::Type& type) {
  144. switch (type.kind()) {
  145. case opt::analysis::Type::kBool:
  146. case opt::analysis::Type::kInteger:
  147. case opt::analysis::Type::kFloat:
  148. case opt::analysis::Type::kArray:
  149. return true;
  150. case opt::analysis::Type::kVector:
  151. return CanUsePointeeWithCopyMemory(*type.AsVector()->element_type());
  152. case opt::analysis::Type::kMatrix:
  153. return CanUsePointeeWithCopyMemory(*type.AsMatrix()->element_type());
  154. case opt::analysis::Type::kStruct:
  155. return std::all_of(type.AsStruct()->element_types().begin(),
  156. type.AsStruct()->element_types().end(),
  157. [](const opt::analysis::Type* element) {
  158. return CanUsePointeeWithCopyMemory(*element);
  159. });
  160. default:
  161. return false;
  162. }
  163. }
  164. } // namespace fuzz
  165. } // namespace spvtools