fuzzer_pass_push_ids_through_variables.cpp 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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. : FuzzerPass(ir_context, transformation_context, fuzzer_context,
  25. transformations) {}
  26. FuzzerPassPushIdsThroughVariables::~FuzzerPassPushIdsThroughVariables() =
  27. default;
  28. void FuzzerPassPushIdsThroughVariables::Apply() {
  29. ForEachInstructionWithInstructionDescriptor(
  30. [this](opt::Function* function, opt::BasicBlock* block,
  31. opt::BasicBlock::iterator instruction_iterator,
  32. const protobufs::InstructionDescriptor& instruction_descriptor)
  33. -> void {
  34. assert(instruction_iterator->opcode() ==
  35. 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 (!fuzzerutil::BlockIsReachableInItsFunction(GetIRContext(), 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. SpvOpStore, instruction_iterator) ||
  52. !fuzzerutil::CanInsertOpcodeBeforeInstruction(
  53. SpvOpLoad, 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. ? SpvStorageClassPrivate
  59. : SpvStorageClassFunction;
  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. uint32_t basic_type_id =
  65. basic_types[GetFuzzerContext()->RandomIndex(basic_types)];
  66. // Looks for ids that we might wish to consider pushing through a
  67. // variable.
  68. std::vector<opt::Instruction*> value_instructions =
  69. FindAvailableInstructions(
  70. function, block, instruction_iterator,
  71. [basic_type_id, instruction_descriptor](
  72. opt::IRContext* ir_context,
  73. opt::Instruction* instruction) -> bool {
  74. if (!instruction->result_id() || !instruction->type_id()) {
  75. return false;
  76. }
  77. if (instruction->type_id() != basic_type_id) {
  78. return false;
  79. }
  80. return fuzzerutil::IdIsAvailableBeforeInstruction(
  81. ir_context,
  82. FindInstruction(instruction_descriptor, ir_context),
  83. instruction->result_id());
  84. });
  85. if (value_instructions.empty()) {
  86. return;
  87. }
  88. // If the pointer type does not exist, then create it.
  89. FindOrCreatePointerType(basic_type_id, variable_storage_class);
  90. // TODO(https://github.com/KhronosGroup/SPIRV-Tools/issues/3403):
  91. // type support here is limited by the FindOrCreateZeroConstant
  92. // function.
  93. const auto* type_inst =
  94. GetIRContext()->get_def_use_mgr()->GetDef(basic_type_id);
  95. assert(type_inst);
  96. switch (type_inst->opcode()) {
  97. case SpvOpTypeBool:
  98. case SpvOpTypeFloat:
  99. case SpvOpTypeInt:
  100. case SpvOpTypeArray:
  101. case SpvOpTypeMatrix:
  102. case SpvOpTypeVector:
  103. case SpvOpTypeStruct:
  104. break;
  105. default:
  106. return;
  107. }
  108. // Create a constant to initialize the variable from. This might update
  109. // module's id bound so it must be done before any fresh ids are
  110. // computed.
  111. auto initializer_id = FindOrCreateZeroConstant(basic_type_id);
  112. // Applies the push id through variable transformation.
  113. ApplyTransformation(TransformationPushIdThroughVariable(
  114. value_instructions[GetFuzzerContext()->RandomIndex(
  115. value_instructions)]
  116. ->result_id(),
  117. GetFuzzerContext()->GetFreshId(), GetFuzzerContext()->GetFreshId(),
  118. variable_storage_class, initializer_id, instruction_descriptor));
  119. });
  120. }
  121. } // namespace fuzz
  122. } // namespace spvtools