transformation_mutate_pointer.cpp 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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. const protobufs::TransformationMutatePointer& message)
  21. : message_(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(SpvOpLoad,
  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. auto pointee_type_id = fuzzerutil::GetPointeeTypeIdFromPointerType(
  81. ir_context, fuzzerutil::GetTypeId(ir_context, message_.pointer_id()));
  82. // Back up the original value.
  83. insert_before_inst->InsertBefore(MakeUnique<opt::Instruction>(
  84. ir_context, SpvOpLoad, pointee_type_id, message_.fresh_id(),
  85. opt::Instruction::OperandList{
  86. {SPV_OPERAND_TYPE_ID, {message_.pointer_id()}}}));
  87. // Insert a new value.
  88. insert_before_inst->InsertBefore(MakeUnique<opt::Instruction>(
  89. ir_context, SpvOpStore, 0, 0,
  90. opt::Instruction::OperandList{
  91. {SPV_OPERAND_TYPE_ID, {message_.pointer_id()}},
  92. {SPV_OPERAND_TYPE_ID,
  93. {fuzzerutil::MaybeGetZeroConstant(
  94. ir_context, *transformation_context, pointee_type_id, true)}}}));
  95. // Restore the original value.
  96. insert_before_inst->InsertBefore(MakeUnique<opt::Instruction>(
  97. ir_context, SpvOpStore, 0, 0,
  98. opt::Instruction::OperandList{
  99. {SPV_OPERAND_TYPE_ID, {message_.pointer_id()}},
  100. {SPV_OPERAND_TYPE_ID, {message_.fresh_id()}}}));
  101. fuzzerutil::UpdateModuleIdBound(ir_context, message_.fresh_id());
  102. // Make sure analyses represent the correct state of the module.
  103. ir_context->InvalidateAnalysesExceptFor(opt::IRContext::kAnalysisNone);
  104. }
  105. protobufs::Transformation TransformationMutatePointer::ToMessage() const {
  106. protobufs::Transformation result;
  107. *result.mutable_mutate_pointer() = message_;
  108. return result;
  109. }
  110. bool TransformationMutatePointer::IsValidPointerInstruction(
  111. opt::IRContext* ir_context, const opt::Instruction& inst) {
  112. // |inst| must have both result id and type id and it may not cause undefined
  113. // behaviour.
  114. if (!inst.result_id() || !inst.type_id() || inst.opcode() == SpvOpUndef ||
  115. inst.opcode() == SpvOpConstantNull) {
  116. return false;
  117. }
  118. opt::Instruction* type_inst =
  119. ir_context->get_def_use_mgr()->GetDef(inst.type_id());
  120. assert(type_inst != nullptr && "|inst| has invalid type id");
  121. // |inst| must be a pointer.
  122. if (type_inst->opcode() != SpvOpTypePointer) {
  123. return false;
  124. }
  125. // |inst| must have a supported storage class.
  126. switch (static_cast<SpvStorageClass>(type_inst->GetSingleWordInOperand(0))) {
  127. case SpvStorageClassFunction:
  128. case SpvStorageClassPrivate:
  129. case SpvStorageClassWorkgroup:
  130. break;
  131. default:
  132. return false;
  133. }
  134. // |inst|'s pointee must consist of scalars and/or composites.
  135. return fuzzerutil::CanCreateConstant(ir_context,
  136. type_inst->GetSingleWordInOperand(1));
  137. }
  138. std::unordered_set<uint32_t> TransformationMutatePointer::GetFreshIds() const {
  139. return {message_.fresh_id()};
  140. }
  141. } // namespace fuzz
  142. } // namespace spvtools