transformation_push_id_through_variable.cpp 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. // Copyright (c) 2020 André Perez Maselco
  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_push_id_through_variable.h"
  15. #include "source/fuzz/fuzzer_util.h"
  16. #include "source/fuzz/instruction_descriptor.h"
  17. namespace spvtools {
  18. namespace fuzz {
  19. TransformationPushIdThroughVariable::TransformationPushIdThroughVariable(
  20. protobufs::TransformationPushIdThroughVariable message)
  21. : message_(std::move(message)) {}
  22. TransformationPushIdThroughVariable::TransformationPushIdThroughVariable(
  23. uint32_t value_id, uint32_t value_synonym_id, uint32_t variable_id,
  24. uint32_t variable_storage_class, uint32_t initializer_id,
  25. const protobufs::InstructionDescriptor& instruction_descriptor) {
  26. message_.set_value_id(value_id);
  27. message_.set_value_synonym_id(value_synonym_id);
  28. message_.set_variable_id(variable_id);
  29. message_.set_variable_storage_class(variable_storage_class);
  30. message_.set_initializer_id(initializer_id);
  31. *message_.mutable_instruction_descriptor() = instruction_descriptor;
  32. }
  33. bool TransformationPushIdThroughVariable::IsApplicable(
  34. opt::IRContext* ir_context, const TransformationContext& /*unused*/) const {
  35. // |message_.value_synonym_id| and |message_.variable_id| must be fresh.
  36. if (!fuzzerutil::IsFreshId(ir_context, message_.value_synonym_id()) ||
  37. !fuzzerutil::IsFreshId(ir_context, message_.variable_id())) {
  38. return false;
  39. }
  40. // The instruction to insert before must be defined.
  41. auto instruction_to_insert_before =
  42. FindInstruction(message_.instruction_descriptor(), ir_context);
  43. if (!instruction_to_insert_before) {
  44. return false;
  45. }
  46. // It must be valid to insert the OpStore and OpLoad instruction before it.
  47. if (!fuzzerutil::CanInsertOpcodeBeforeInstruction(
  48. spv::Op::OpStore, instruction_to_insert_before) ||
  49. !fuzzerutil::CanInsertOpcodeBeforeInstruction(
  50. spv::Op::OpLoad, instruction_to_insert_before)) {
  51. return false;
  52. }
  53. // The instruction to insert before must belong to a reachable block.
  54. auto basic_block = ir_context->get_instr_block(instruction_to_insert_before);
  55. if (!ir_context->IsReachable(*basic_block)) {
  56. return false;
  57. }
  58. // The value instruction must be defined and have a type.
  59. auto value_instruction =
  60. ir_context->get_def_use_mgr()->GetDef(message_.value_id());
  61. if (!value_instruction || !value_instruction->type_id()) {
  62. return false;
  63. }
  64. // A pointer type instruction pointing to the value type must be defined.
  65. auto pointer_type_id = fuzzerutil::MaybeGetPointerType(
  66. ir_context, value_instruction->type_id(),
  67. static_cast<spv::StorageClass>(message_.variable_storage_class()));
  68. if (!pointer_type_id) {
  69. return false;
  70. }
  71. // |message_.variable_storage_class| must be private or function.
  72. assert((message_.variable_storage_class() ==
  73. (uint32_t)spv::StorageClass::Private ||
  74. message_.variable_storage_class() ==
  75. (uint32_t)spv::StorageClass::Function) &&
  76. "The variable storage class must be private or function.");
  77. // Check that initializer is valid.
  78. const auto* constant_inst =
  79. ir_context->get_def_use_mgr()->GetDef(message_.initializer_id());
  80. if (!constant_inst || !spvOpcodeIsConstant(constant_inst->opcode()) ||
  81. value_instruction->type_id() != constant_inst->type_id()) {
  82. return false;
  83. }
  84. // |message_.value_id| must be available at the insertion point.
  85. return fuzzerutil::IdIsAvailableBeforeInstruction(
  86. ir_context, instruction_to_insert_before, message_.value_id());
  87. }
  88. void TransformationPushIdThroughVariable::Apply(
  89. opt::IRContext* ir_context,
  90. TransformationContext* transformation_context) const {
  91. auto value_instruction =
  92. ir_context->get_def_use_mgr()->GetDef(message_.value_id());
  93. opt::Instruction* insert_before =
  94. FindInstruction(message_.instruction_descriptor(), ir_context);
  95. opt::BasicBlock* enclosing_block = ir_context->get_instr_block(insert_before);
  96. // A pointer type instruction pointing to the value type must be defined.
  97. auto pointer_type_id = fuzzerutil::MaybeGetPointerType(
  98. ir_context, value_instruction->type_id(),
  99. static_cast<spv::StorageClass>(message_.variable_storage_class()));
  100. assert(pointer_type_id && "The required pointer type must be available.");
  101. // Adds whether a global or local variable.
  102. if (spv::StorageClass(message_.variable_storage_class()) ==
  103. spv::StorageClass::Private) {
  104. opt::Instruction* global_variable = fuzzerutil::AddGlobalVariable(
  105. ir_context, message_.variable_id(), pointer_type_id,
  106. spv::StorageClass::Private, message_.initializer_id());
  107. ir_context->get_def_use_mgr()->AnalyzeInstDefUse(global_variable);
  108. } else {
  109. opt::Function* function =
  110. ir_context
  111. ->get_instr_block(
  112. FindInstruction(message_.instruction_descriptor(), ir_context))
  113. ->GetParent();
  114. opt::Instruction* local_variable = fuzzerutil::AddLocalVariable(
  115. ir_context, message_.variable_id(), pointer_type_id,
  116. function->result_id(), message_.initializer_id());
  117. ir_context->get_def_use_mgr()->AnalyzeInstDefUse(local_variable);
  118. ir_context->set_instr_block(local_variable, &*function->entry());
  119. }
  120. // First, insert the OpLoad instruction before |instruction_descriptor| and
  121. // then insert the OpStore instruction before the OpLoad instruction.
  122. fuzzerutil::UpdateModuleIdBound(ir_context, message_.value_synonym_id());
  123. opt::Instruction* load_instruction =
  124. insert_before->InsertBefore(MakeUnique<opt::Instruction>(
  125. ir_context, spv::Op::OpLoad, value_instruction->type_id(),
  126. message_.value_synonym_id(),
  127. opt::Instruction::OperandList(
  128. {{SPV_OPERAND_TYPE_ID, {message_.variable_id()}}})));
  129. opt::Instruction* store_instruction =
  130. load_instruction->InsertBefore(MakeUnique<opt::Instruction>(
  131. ir_context, spv::Op::OpStore, 0, 0,
  132. opt::Instruction::OperandList(
  133. {{SPV_OPERAND_TYPE_ID, {message_.variable_id()}},
  134. {SPV_OPERAND_TYPE_ID, {message_.value_id()}}})));
  135. ir_context->get_def_use_mgr()->AnalyzeInstDefUse(store_instruction);
  136. ir_context->set_instr_block(store_instruction, enclosing_block);
  137. ir_context->get_def_use_mgr()->AnalyzeInstDefUse(load_instruction);
  138. ir_context->set_instr_block(load_instruction, enclosing_block);
  139. // We should be able to create a synonym of |value_id| if it's not irrelevant.
  140. if (fuzzerutil::CanMakeSynonymOf(ir_context, *transformation_context,
  141. *value_instruction) &&
  142. !transformation_context->GetFactManager()->IdIsIrrelevant(
  143. message_.value_synonym_id())) {
  144. // Adds the fact that |message_.value_synonym_id|
  145. // and |message_.value_id| are synonymous.
  146. transformation_context->GetFactManager()->AddFactDataSynonym(
  147. MakeDataDescriptor(message_.value_synonym_id(), {}),
  148. MakeDataDescriptor(message_.value_id(), {}));
  149. }
  150. }
  151. protobufs::Transformation TransformationPushIdThroughVariable::ToMessage()
  152. const {
  153. protobufs::Transformation result;
  154. *result.mutable_push_id_through_variable() = message_;
  155. return result;
  156. }
  157. std::unordered_set<uint32_t> TransformationPushIdThroughVariable::GetFreshIds()
  158. const {
  159. return {message_.value_synonym_id(), message_.variable_id()};
  160. }
  161. } // namespace fuzz
  162. } // namespace spvtools