transformation_push_id_through_variable.cpp 6.7 KB

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