transformation_push_id_through_variable.cpp 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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. const spvtools::fuzz::protobufs::TransformationPushIdThroughVariable&
  21. message)
  22. : message_(message) {}
  23. TransformationPushIdThroughVariable::TransformationPushIdThroughVariable(
  24. uint32_t value_id, uint32_t value_synonym_id, uint32_t variable_id,
  25. uint32_t variable_storage_class, uint32_t initializer_id,
  26. const protobufs::InstructionDescriptor& instruction_descriptor) {
  27. message_.set_value_id(value_id);
  28. message_.set_value_synonym_id(value_synonym_id);
  29. message_.set_variable_id(variable_id);
  30. message_.set_variable_storage_class(variable_storage_class);
  31. message_.set_initializer_id(initializer_id);
  32. *message_.mutable_instruction_descriptor() = instruction_descriptor;
  33. }
  34. bool TransformationPushIdThroughVariable::IsApplicable(
  35. opt::IRContext* ir_context,
  36. const TransformationContext& transformation_context) const {
  37. // |message_.value_synonym_id| and |message_.variable_id| must be fresh.
  38. if (!fuzzerutil::IsFreshId(ir_context, message_.value_synonym_id()) ||
  39. !fuzzerutil::IsFreshId(ir_context, message_.variable_id())) {
  40. return false;
  41. }
  42. // The instruction to insert before must be defined.
  43. auto instruction_to_insert_before =
  44. FindInstruction(message_.instruction_descriptor(), ir_context);
  45. if (!instruction_to_insert_before) {
  46. return false;
  47. }
  48. // It must be valid to insert the OpStore and OpLoad instruction before it.
  49. if (!fuzzerutil::CanInsertOpcodeBeforeInstruction(
  50. SpvOpStore, instruction_to_insert_before) ||
  51. !fuzzerutil::CanInsertOpcodeBeforeInstruction(
  52. SpvOpLoad, instruction_to_insert_before)) {
  53. return false;
  54. }
  55. // The instruction to insert before must belong to a reachable block.
  56. auto basic_block = ir_context->get_instr_block(instruction_to_insert_before);
  57. if (!fuzzerutil::BlockIsReachableInItsFunction(ir_context, basic_block)) {
  58. return false;
  59. }
  60. // The value instruction must be defined and have a type.
  61. auto value_instruction =
  62. ir_context->get_def_use_mgr()->GetDef(message_.value_id());
  63. if (!value_instruction || !value_instruction->type_id()) {
  64. return false;
  65. }
  66. // We should be able to create a synonym of |value_id| if it's not irrelevant.
  67. if (!transformation_context.GetFactManager()->IdIsIrrelevant(
  68. message_.value_id()) &&
  69. !fuzzerutil::CanMakeSynonymOf(ir_context, transformation_context,
  70. value_instruction)) {
  71. return false;
  72. }
  73. // A pointer type instruction pointing to the value type must be defined.
  74. auto pointer_type_id = fuzzerutil::MaybeGetPointerType(
  75. ir_context, value_instruction->type_id(),
  76. static_cast<SpvStorageClass>(message_.variable_storage_class()));
  77. if (!pointer_type_id) {
  78. return false;
  79. }
  80. // |message_.variable_storage_class| must be private or function.
  81. assert((message_.variable_storage_class() == SpvStorageClassPrivate ||
  82. message_.variable_storage_class() == SpvStorageClassFunction) &&
  83. "The variable storage class must be private or function.");
  84. // Check that initializer is valid.
  85. const auto* constant_inst =
  86. ir_context->get_def_use_mgr()->GetDef(message_.initializer_id());
  87. if (!constant_inst || !spvOpcodeIsConstant(constant_inst->opcode()) ||
  88. value_instruction->type_id() != constant_inst->type_id()) {
  89. return false;
  90. }
  91. // |message_.value_id| must be available at the insertion point.
  92. return fuzzerutil::IdIsAvailableBeforeInstruction(
  93. ir_context, instruction_to_insert_before, message_.value_id());
  94. }
  95. void TransformationPushIdThroughVariable::Apply(
  96. opt::IRContext* ir_context,
  97. TransformationContext* transformation_context) const {
  98. auto value_instruction =
  99. ir_context->get_def_use_mgr()->GetDef(message_.value_id());
  100. // A pointer type instruction pointing to the value type must be defined.
  101. auto pointer_type_id = fuzzerutil::MaybeGetPointerType(
  102. ir_context, value_instruction->type_id(),
  103. static_cast<SpvStorageClass>(message_.variable_storage_class()));
  104. assert(pointer_type_id && "The required pointer type must be available.");
  105. // Adds whether a global or local variable.
  106. if (message_.variable_storage_class() == SpvStorageClassPrivate) {
  107. fuzzerutil::AddGlobalVariable(ir_context, message_.variable_id(),
  108. pointer_type_id, SpvStorageClassPrivate,
  109. message_.initializer_id());
  110. } else {
  111. auto function_id = ir_context
  112. ->get_instr_block(FindInstruction(
  113. message_.instruction_descriptor(), ir_context))
  114. ->GetParent()
  115. ->result_id();
  116. fuzzerutil::AddLocalVariable(ir_context, message_.variable_id(),
  117. pointer_type_id, function_id,
  118. message_.initializer_id());
  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. FindInstruction(message_.instruction_descriptor(), ir_context)
  124. ->InsertBefore(MakeUnique<opt::Instruction>(
  125. ir_context, SpvOpLoad, value_instruction->type_id(),
  126. message_.value_synonym_id(),
  127. opt::Instruction::OperandList(
  128. {{SPV_OPERAND_TYPE_ID, {message_.variable_id()}}})))
  129. ->InsertBefore(MakeUnique<opt::Instruction>(
  130. ir_context, SpvOpStore, 0, 0,
  131. opt::Instruction::OperandList(
  132. {{SPV_OPERAND_TYPE_ID, {message_.variable_id()}},
  133. {SPV_OPERAND_TYPE_ID, {message_.value_id()}}})));
  134. ir_context->InvalidateAnalysesExceptFor(opt::IRContext::kAnalysisNone);
  135. if (!transformation_context->GetFactManager()->IdIsIrrelevant(
  136. message_.value_id())) {
  137. // Adds the fact that |message_.value_synonym_id|
  138. // and |message_.value_id| are synonymous.
  139. transformation_context->GetFactManager()->AddFactDataSynonym(
  140. MakeDataDescriptor(message_.value_synonym_id(), {}),
  141. MakeDataDescriptor(message_.value_id(), {}), ir_context);
  142. }
  143. }
  144. protobufs::Transformation TransformationPushIdThroughVariable::ToMessage()
  145. const {
  146. protobufs::Transformation result;
  147. *result.mutable_push_id_through_variable() = message_;
  148. return result;
  149. }
  150. } // namespace fuzz
  151. } // namespace spvtools