fuzzer_pass_push_ids_through_variables.cpp 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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/fuzzer_pass_push_ids_through_variables.h"
  15. #include "source/fuzz/fuzzer_util.h"
  16. #include "source/fuzz/instruction_descriptor.h"
  17. #include "source/fuzz/transformation_push_id_through_variable.h"
  18. namespace spvtools {
  19. namespace fuzz {
  20. FuzzerPassPushIdsThroughVariables::FuzzerPassPushIdsThroughVariables(
  21. opt::IRContext* ir_context, TransformationContext* transformation_context,
  22. FuzzerContext* fuzzer_context,
  23. protobufs::TransformationSequence* transformations,
  24. bool ignore_inapplicable_transformations)
  25. : FuzzerPass(ir_context, transformation_context, fuzzer_context,
  26. transformations, ignore_inapplicable_transformations) {}
  27. void FuzzerPassPushIdsThroughVariables::Apply() {
  28. ForEachInstructionWithInstructionDescriptor(
  29. [this](opt::Function* function, opt::BasicBlock* block,
  30. opt::BasicBlock::iterator instruction_iterator,
  31. const protobufs::InstructionDescriptor& instruction_descriptor)
  32. -> void {
  33. assert(
  34. instruction_iterator->opcode() ==
  35. spv::Op(instruction_descriptor.target_instruction_opcode()) &&
  36. "The opcode of the instruction we might insert before must be "
  37. "the same as the opcode in the descriptor for the instruction");
  38. // Randomly decide whether to try pushing an id through a variable.
  39. if (!GetFuzzerContext()->ChoosePercentage(
  40. GetFuzzerContext()->GetChanceOfPushingIdThroughVariable())) {
  41. return;
  42. }
  43. // The block containing the instruction we are going to insert before
  44. // must be reachable.
  45. if (!GetIRContext()->IsReachable(*block)) {
  46. return;
  47. }
  48. // It must be valid to insert OpStore and OpLoad instructions
  49. // before the instruction to insert before.
  50. if (!fuzzerutil::CanInsertOpcodeBeforeInstruction(
  51. spv::Op::OpStore, instruction_iterator) ||
  52. !fuzzerutil::CanInsertOpcodeBeforeInstruction(
  53. spv::Op::OpLoad, instruction_iterator)) {
  54. return;
  55. }
  56. // Randomly decides whether a global or local variable will be added.
  57. auto variable_storage_class = GetFuzzerContext()->ChooseEven()
  58. ? spv::StorageClass::Private
  59. : spv::StorageClass::Function;
  60. // Gets the available basic and pointer types.
  61. auto basic_type_ids_and_pointers =
  62. GetAvailableBasicTypesAndPointers(variable_storage_class);
  63. auto& basic_types = basic_type_ids_and_pointers.first;
  64. // There must be at least some basic types.
  65. if (basic_types.empty()) {
  66. return;
  67. }
  68. uint32_t basic_type_id =
  69. basic_types[GetFuzzerContext()->RandomIndex(basic_types)];
  70. // Looks for ids that we might wish to consider pushing through a
  71. // variable.
  72. std::vector<opt::Instruction*> value_instructions =
  73. FindAvailableInstructions(
  74. function, block, instruction_iterator,
  75. [this, basic_type_id, instruction_descriptor](
  76. opt::IRContext* ir_context,
  77. opt::Instruction* instruction) -> bool {
  78. if (!instruction->result_id() || !instruction->type_id()) {
  79. return false;
  80. }
  81. if (instruction->type_id() != basic_type_id) {
  82. return false;
  83. }
  84. // If the id is irrelevant, we can use it since it will not
  85. // participate in DataSynonym fact. Otherwise, we should be
  86. // able to produce a synonym out of the id.
  87. if (!GetTransformationContext()
  88. ->GetFactManager()
  89. ->IdIsIrrelevant(instruction->result_id()) &&
  90. !fuzzerutil::CanMakeSynonymOf(ir_context,
  91. *GetTransformationContext(),
  92. *instruction)) {
  93. return false;
  94. }
  95. return fuzzerutil::IdIsAvailableBeforeInstruction(
  96. ir_context,
  97. FindInstruction(instruction_descriptor, ir_context),
  98. instruction->result_id());
  99. });
  100. if (value_instructions.empty()) {
  101. return;
  102. }
  103. // If the pointer type does not exist, then create it.
  104. FindOrCreatePointerType(basic_type_id, variable_storage_class);
  105. // TODO(https://github.com/KhronosGroup/SPIRV-Tools/issues/3403):
  106. // type support here is limited by the FindOrCreateZeroConstant
  107. // function.
  108. const auto* type_inst =
  109. GetIRContext()->get_def_use_mgr()->GetDef(basic_type_id);
  110. assert(type_inst);
  111. switch (type_inst->opcode()) {
  112. case spv::Op::OpTypeBool:
  113. case spv::Op::OpTypeFloat:
  114. case spv::Op::OpTypeInt:
  115. case spv::Op::OpTypeArray:
  116. case spv::Op::OpTypeMatrix:
  117. case spv::Op::OpTypeVector:
  118. case spv::Op::OpTypeStruct:
  119. break;
  120. default:
  121. return;
  122. }
  123. // Create a constant to initialize the variable from. This might update
  124. // module's id bound so it must be done before any fresh ids are
  125. // computed.
  126. auto initializer_id = FindOrCreateZeroConstant(basic_type_id, false);
  127. // Applies the push id through variable transformation.
  128. ApplyTransformation(TransformationPushIdThroughVariable(
  129. value_instructions[GetFuzzerContext()->RandomIndex(
  130. value_instructions)]
  131. ->result_id(),
  132. GetFuzzerContext()->GetFreshId(), GetFuzzerContext()->GetFreshId(),
  133. uint32_t(variable_storage_class), initializer_id,
  134. instruction_descriptor));
  135. });
  136. }
  137. } // namespace fuzz
  138. } // namespace spvtools