fuzzer_pass_copy_objects.cpp 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. // Copyright (c) 2019 Google LLC
  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_copy_objects.h"
  15. #include "source/fuzz/transformation_copy_object.h"
  16. namespace spvtools {
  17. namespace fuzz {
  18. FuzzerPassCopyObjects::FuzzerPassCopyObjects(
  19. opt::IRContext* ir_context, FactManager* fact_manager,
  20. FuzzerContext* fuzzer_context,
  21. protobufs::TransformationSequence* transformations)
  22. : FuzzerPass(ir_context, fact_manager, fuzzer_context, transformations) {}
  23. FuzzerPassCopyObjects::~FuzzerPassCopyObjects() = default;
  24. void FuzzerPassCopyObjects::Apply() {
  25. // Consider every block in every function.
  26. for (auto& function : *GetIRContext()->module()) {
  27. for (auto& block : function) {
  28. // We now consider every instruction in the block, randomly deciding
  29. // whether to add an object copy before the instruction.
  30. // In order to insert an object copy instruction, we need to be able to
  31. // identify the instruction a copy should be inserted before. We do this
  32. // by tracking a base instruction, which must generate a result id, and an
  33. // offset (to allow us to identify instructions that do not generate
  34. // result ids).
  35. // The initial base instruction is the block label.
  36. uint32_t base = block.id();
  37. uint32_t offset = 0;
  38. // Consider every instruction in the block.
  39. for (auto inst_it = block.begin(); inst_it != block.end(); ++inst_it) {
  40. if (inst_it->HasResultId()) {
  41. // In the case that the instruction has a result id, we use the
  42. // instruction as its own base, with zero offset.
  43. base = inst_it->result_id();
  44. offset = 0;
  45. } else {
  46. // The instruction does not have a result id, so we need to identify
  47. // it via the latest instruction that did have a result id (base), and
  48. // an incremented offset.
  49. offset++;
  50. }
  51. // Check whether it is legitimate to insert a copy before this
  52. // instruction.
  53. if (!TransformationCopyObject::CanInsertCopyBefore(inst_it)) {
  54. continue;
  55. }
  56. // Randomly decide whether to try inserting an object copy here.
  57. if (!GetFuzzerContext()->ChoosePercentage(
  58. GetFuzzerContext()->GetChanceOfCopyingObject())) {
  59. continue;
  60. }
  61. // Populate list of potential instructions that can be copied.
  62. // TODO(afd) The following is (relatively) simple, but may end up being
  63. // prohibitively inefficient, as it walks the whole dominator tree for
  64. // every copy that is added.
  65. std::vector<opt::Instruction*> copyable_instructions;
  66. // Consider all global declarations
  67. for (auto& global : GetIRContext()->module()->types_values()) {
  68. if (TransformationCopyObject::IsCopyable(GetIRContext(), &global)) {
  69. copyable_instructions.push_back(&global);
  70. }
  71. }
  72. // Consider all previous instructions in this block
  73. for (auto prev_inst_it = block.begin(); prev_inst_it != inst_it;
  74. ++prev_inst_it) {
  75. if (TransformationCopyObject::IsCopyable(GetIRContext(),
  76. &*prev_inst_it)) {
  77. copyable_instructions.push_back(&*prev_inst_it);
  78. }
  79. }
  80. // Walk the dominator tree to consider all instructions from dominating
  81. // blocks
  82. auto dominator_analysis =
  83. GetIRContext()->GetDominatorAnalysis(&function);
  84. for (auto next_dominator =
  85. dominator_analysis->ImmediateDominator(&block);
  86. next_dominator != nullptr;
  87. next_dominator =
  88. dominator_analysis->ImmediateDominator(next_dominator)) {
  89. for (auto& dominating_inst : *next_dominator) {
  90. if (TransformationCopyObject::IsCopyable(GetIRContext(),
  91. &dominating_inst)) {
  92. copyable_instructions.push_back(&dominating_inst);
  93. }
  94. }
  95. }
  96. // At this point, |copyable_instructions| contains all the instructions
  97. // we might think of copying.
  98. if (!copyable_instructions.empty()) {
  99. // Choose a copyable instruction at random, and create and apply an
  100. // object copying transformation based on it.
  101. uint32_t index =
  102. GetFuzzerContext()->RandomIndex(copyable_instructions);
  103. TransformationCopyObject transformation(
  104. copyable_instructions[index]->result_id(), base, offset,
  105. GetFuzzerContext()->GetFreshId());
  106. assert(
  107. transformation.IsApplicable(GetIRContext(), *GetFactManager()) &&
  108. "This transformation should be applicable by construction.");
  109. transformation.Apply(GetIRContext(), GetFactManager());
  110. *GetTransformations()->add_transformation() =
  111. transformation.ToMessage();
  112. if (!inst_it->HasResultId()) {
  113. // We have inserted a new instruction before the current
  114. // instruction, and we are tracking the current id-less instruction
  115. // via an offset (offset) from a previous instruction (base) that
  116. // has an id. We increment |offset| to reflect the newly-inserted
  117. // instruction.
  118. //
  119. // This is slightly preferable to the alternative of setting |base|
  120. // to be the result id of the new instruction, since on replay we
  121. // might end up eliminating this copy but keeping a subsequent copy.
  122. offset++;
  123. }
  124. }
  125. }
  126. }
  127. }
  128. }
  129. } // namespace fuzz
  130. } // namespace spvtools