fuzzer_pass_add_synonyms.cpp 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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/fuzzer_pass_add_synonyms.h"
  15. #include "source/fuzz/fuzzer_context.h"
  16. #include "source/fuzz/fuzzer_util.h"
  17. #include "source/fuzz/instruction_descriptor.h"
  18. #include "source/fuzz/transformation_add_synonym.h"
  19. namespace spvtools {
  20. namespace fuzz {
  21. FuzzerPassAddSynonyms::FuzzerPassAddSynonyms(
  22. opt::IRContext* ir_context, TransformationContext* transformation_context,
  23. FuzzerContext* fuzzer_context,
  24. protobufs::TransformationSequence* transformations,
  25. bool ignore_inapplicable_transformations)
  26. : FuzzerPass(ir_context, transformation_context, fuzzer_context,
  27. transformations, ignore_inapplicable_transformations) {}
  28. void FuzzerPassAddSynonyms::Apply() {
  29. ForEachInstructionWithInstructionDescriptor(
  30. [this](opt::Function* function, opt::BasicBlock* block,
  31. opt::BasicBlock::iterator inst_it,
  32. const protobufs::InstructionDescriptor& instruction_descriptor) {
  33. if (GetTransformationContext()->GetFactManager()->BlockIsDead(
  34. block->id())) {
  35. // Don't create synonyms in dead blocks.
  36. return;
  37. }
  38. // Skip |inst_it| if we can't insert anything above it. OpIAdd is just
  39. // a representative of some instruction that might be produced by the
  40. // transformation.
  41. if (!fuzzerutil::CanInsertOpcodeBeforeInstruction(spv::Op::OpIAdd,
  42. inst_it)) {
  43. return;
  44. }
  45. if (!GetFuzzerContext()->ChoosePercentage(
  46. GetFuzzerContext()->GetChanceOfAddingSynonyms())) {
  47. return;
  48. }
  49. auto synonym_type = GetFuzzerContext()->GetRandomSynonymType();
  50. // Select all instructions that can be used to create a synonym to.
  51. auto available_instructions = FindAvailableInstructions(
  52. function, block, inst_it,
  53. [synonym_type, this](opt::IRContext* ir_context,
  54. opt::Instruction* inst) {
  55. // Check that we can create a synonym to |inst| as described by
  56. // the |synonym_type| and insert it before |inst_it|.
  57. return TransformationAddSynonym::IsInstructionValid(
  58. ir_context, *GetTransformationContext(), inst, synonym_type);
  59. });
  60. if (available_instructions.empty()) {
  61. return;
  62. }
  63. const auto* existing_synonym =
  64. available_instructions[GetFuzzerContext()->RandomIndex(
  65. available_instructions)];
  66. // Make sure the module contains all instructions required to apply the
  67. // transformation.
  68. switch (synonym_type) {
  69. case protobufs::TransformationAddSynonym::ADD_ZERO:
  70. case protobufs::TransformationAddSynonym::SUB_ZERO:
  71. case protobufs::TransformationAddSynonym::LOGICAL_OR:
  72. case protobufs::TransformationAddSynonym::BITWISE_OR:
  73. case protobufs::TransformationAddSynonym::BITWISE_XOR:
  74. // Create a zero constant to be used as an operand of the synonymous
  75. // instruction.
  76. FindOrCreateZeroConstant(existing_synonym->type_id(), false);
  77. break;
  78. case protobufs::TransformationAddSynonym::MUL_ONE:
  79. case protobufs::TransformationAddSynonym::LOGICAL_AND: {
  80. const auto* existing_synonym_type =
  81. GetIRContext()->get_type_mgr()->GetType(
  82. existing_synonym->type_id());
  83. assert(existing_synonym_type && "Instruction has invalid type");
  84. if (const auto* vector = existing_synonym_type->AsVector()) {
  85. auto element_type_id =
  86. GetIRContext()->get_type_mgr()->GetId(vector->element_type());
  87. assert(element_type_id && "Vector's element type is invalid");
  88. auto one_word = vector->element_type()->AsFloat()
  89. ? fuzzerutil::FloatToWord(1)
  90. : 1u;
  91. FindOrCreateCompositeConstant(
  92. std::vector<uint32_t>(
  93. vector->element_count(),
  94. FindOrCreateConstant({one_word}, element_type_id, false)),
  95. existing_synonym->type_id(), false);
  96. } else {
  97. FindOrCreateConstant(
  98. {existing_synonym_type->AsFloat() ? fuzzerutil::FloatToWord(1)
  99. : 1u},
  100. existing_synonym->type_id(), false);
  101. }
  102. } break;
  103. default:
  104. // This assertion will fail if some SynonymType is missing from the
  105. // switch statement.
  106. assert(
  107. !TransformationAddSynonym::IsAdditionalConstantRequired(
  108. synonym_type) &&
  109. "|synonym_type| requires an additional constant to be present "
  110. "in the module");
  111. break;
  112. }
  113. ApplyTransformation(TransformationAddSynonym(
  114. existing_synonym->result_id(), synonym_type,
  115. GetFuzzerContext()->GetFreshId(), instruction_descriptor));
  116. });
  117. }
  118. } // namespace fuzz
  119. } // namespace spvtools