transformation_mutate_pointer.cpp 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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_mutate_pointer.h"
  15. #include "source/fuzz/fuzzer_util.h"
  16. #include "source/fuzz/instruction_descriptor.h"
  17. namespace spvtools {
  18. namespace fuzz {
  19. TransformationMutatePointer::TransformationMutatePointer(
  20. protobufs::TransformationMutatePointer message)
  21. : message_(std::move(message)) {}
  22. TransformationMutatePointer::TransformationMutatePointer(
  23. uint32_t pointer_id, uint32_t fresh_id,
  24. const protobufs::InstructionDescriptor& insert_before) {
  25. message_.set_pointer_id(pointer_id);
  26. message_.set_fresh_id(fresh_id);
  27. *message_.mutable_insert_before() = insert_before;
  28. }
  29. bool TransformationMutatePointer::IsApplicable(
  30. opt::IRContext* ir_context,
  31. const TransformationContext& transformation_context) const {
  32. // Check that |fresh_id| is fresh.
  33. if (!fuzzerutil::IsFreshId(ir_context, message_.fresh_id())) {
  34. return false;
  35. }
  36. auto* insert_before_inst =
  37. FindInstruction(message_.insert_before(), ir_context);
  38. // Check that |insert_before| is a valid instruction descriptor.
  39. if (!insert_before_inst) {
  40. return false;
  41. }
  42. // Check that it is possible to insert OpLoad and OpStore before
  43. // |insert_before_inst|. We are only using OpLoad here since the result does
  44. // not depend on the opcode.
  45. if (!fuzzerutil::CanInsertOpcodeBeforeInstruction(spv::Op::OpLoad,
  46. insert_before_inst)) {
  47. return false;
  48. }
  49. const auto* pointer_inst =
  50. ir_context->get_def_use_mgr()->GetDef(message_.pointer_id());
  51. // Check that |pointer_id| is a result id of a valid pointer instruction.
  52. if (!pointer_inst || !IsValidPointerInstruction(ir_context, *pointer_inst)) {
  53. return false;
  54. }
  55. // Check that the module contains an irrelevant constant that will be used to
  56. // mutate |pointer_inst|. The constant is irrelevant so that the latter
  57. // transformation can change its value to something more interesting.
  58. auto constant_id = fuzzerutil::MaybeGetZeroConstant(
  59. ir_context, transformation_context,
  60. fuzzerutil::GetPointeeTypeIdFromPointerType(ir_context,
  61. pointer_inst->type_id()),
  62. true);
  63. if (!constant_id) {
  64. return false;
  65. }
  66. assert(fuzzerutil::IdIsAvailableBeforeInstruction(
  67. ir_context, insert_before_inst, constant_id) &&
  68. "Global constant instruction is not available before "
  69. "|insert_before_inst|");
  70. // Check that |pointer_inst| is available before |insert_before_inst|.
  71. return fuzzerutil::IdIsAvailableBeforeInstruction(
  72. ir_context, insert_before_inst, pointer_inst->result_id());
  73. }
  74. void TransformationMutatePointer::Apply(
  75. opt::IRContext* ir_context,
  76. TransformationContext* transformation_context) const {
  77. auto* insert_before_inst =
  78. FindInstruction(message_.insert_before(), ir_context);
  79. assert(insert_before_inst && "|insert_before| descriptor is invalid");
  80. opt::BasicBlock* enclosing_block =
  81. ir_context->get_instr_block(insert_before_inst);
  82. auto pointee_type_id = fuzzerutil::GetPointeeTypeIdFromPointerType(
  83. ir_context, fuzzerutil::GetTypeId(ir_context, message_.pointer_id()));
  84. // Back up the original value.
  85. auto backup_instruction = MakeUnique<opt::Instruction>(
  86. ir_context, spv::Op::OpLoad, pointee_type_id, message_.fresh_id(),
  87. opt::Instruction::OperandList{
  88. {SPV_OPERAND_TYPE_ID, {message_.pointer_id()}}});
  89. auto backup_instruction_ptr = backup_instruction.get();
  90. insert_before_inst->InsertBefore(std::move(backup_instruction));
  91. ir_context->get_def_use_mgr()->AnalyzeInstDefUse(backup_instruction_ptr);
  92. ir_context->set_instr_block(backup_instruction_ptr, enclosing_block);
  93. // Insert a new value.
  94. auto new_value_instruction = MakeUnique<opt::Instruction>(
  95. ir_context, spv::Op::OpStore, 0, 0,
  96. opt::Instruction::OperandList{
  97. {SPV_OPERAND_TYPE_ID, {message_.pointer_id()}},
  98. {SPV_OPERAND_TYPE_ID,
  99. {fuzzerutil::MaybeGetZeroConstant(
  100. ir_context, *transformation_context, pointee_type_id, true)}}});
  101. auto new_value_instruction_ptr = new_value_instruction.get();
  102. insert_before_inst->InsertBefore(std::move(new_value_instruction));
  103. ir_context->get_def_use_mgr()->AnalyzeInstDefUse(new_value_instruction_ptr);
  104. ir_context->set_instr_block(new_value_instruction_ptr, enclosing_block);
  105. // Restore the original value.
  106. auto restore_instruction = MakeUnique<opt::Instruction>(
  107. ir_context, spv::Op::OpStore, 0, 0,
  108. opt::Instruction::OperandList{
  109. {SPV_OPERAND_TYPE_ID, {message_.pointer_id()}},
  110. {SPV_OPERAND_TYPE_ID, {message_.fresh_id()}}});
  111. auto restore_instruction_ptr = restore_instruction.get();
  112. insert_before_inst->InsertBefore(std::move(restore_instruction));
  113. ir_context->get_def_use_mgr()->AnalyzeInstDefUse(restore_instruction_ptr);
  114. ir_context->set_instr_block(restore_instruction_ptr, enclosing_block);
  115. fuzzerutil::UpdateModuleIdBound(ir_context, message_.fresh_id());
  116. }
  117. protobufs::Transformation TransformationMutatePointer::ToMessage() const {
  118. protobufs::Transformation result;
  119. *result.mutable_mutate_pointer() = message_;
  120. return result;
  121. }
  122. bool TransformationMutatePointer::IsValidPointerInstruction(
  123. opt::IRContext* ir_context, const opt::Instruction& inst) {
  124. // |inst| must have both result id and type id and it may not cause undefined
  125. // behaviour.
  126. if (!inst.result_id() || !inst.type_id() ||
  127. inst.opcode() == spv::Op::OpUndef ||
  128. inst.opcode() == spv::Op::OpConstantNull) {
  129. return false;
  130. }
  131. opt::Instruction* type_inst =
  132. ir_context->get_def_use_mgr()->GetDef(inst.type_id());
  133. assert(type_inst != nullptr && "|inst| has invalid type id");
  134. // |inst| must be a pointer.
  135. if (type_inst->opcode() != spv::Op::OpTypePointer) {
  136. return false;
  137. }
  138. // |inst| must have a supported storage class.
  139. switch (
  140. static_cast<spv::StorageClass>(type_inst->GetSingleWordInOperand(0))) {
  141. case spv::StorageClass::Function:
  142. case spv::StorageClass::Private:
  143. case spv::StorageClass::Workgroup:
  144. break;
  145. default:
  146. return false;
  147. }
  148. // |inst|'s pointee must consist of scalars and/or composites.
  149. return fuzzerutil::CanCreateConstant(ir_context,
  150. type_inst->GetSingleWordInOperand(1));
  151. }
  152. std::unordered_set<uint32_t> TransformationMutatePointer::GetFreshIds() const {
  153. return {message_.fresh_id()};
  154. }
  155. } // namespace fuzz
  156. } // namespace spvtools