fuzzer_pass_add_dead_continues.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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_add_dead_continues.h"
  15. #include "source/fuzz/fuzzer_util.h"
  16. #include "source/fuzz/transformation_add_dead_continue.h"
  17. #include "source/opt/ir_context.h"
  18. namespace spvtools {
  19. namespace fuzz {
  20. FuzzerPassAddDeadContinues::FuzzerPassAddDeadContinues(
  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 FuzzerPassAddDeadContinues::Apply() {
  28. // Consider every block in every function.
  29. for (auto& function : *GetIRContext()->module()) {
  30. for (auto& block : function) {
  31. // Get the label id of the continue target of the innermost loop.
  32. auto continue_block_id =
  33. block.IsLoopHeader()
  34. ? block.ContinueBlockId()
  35. : GetIRContext()->GetStructuredCFGAnalysis()->LoopContinueBlock(
  36. block.id());
  37. // This transformation is not applicable if current block is not inside a
  38. // loop.
  39. if (continue_block_id == 0) {
  40. continue;
  41. }
  42. auto* continue_block =
  43. fuzzerutil::MaybeFindBlock(GetIRContext(), continue_block_id);
  44. assert(continue_block && "Continue block is null");
  45. // Analyze return type of each OpPhi instruction in the continue target
  46. // and provide an id for the transformation if needed.
  47. std::vector<uint32_t> phi_ids;
  48. // Check whether current block has an edge to the continue target.
  49. // If this is the case, we don't need to do anything.
  50. if (!block.IsSuccessor(continue_block)) {
  51. continue_block->ForEachPhiInst([this, &phi_ids](opt::Instruction* phi) {
  52. // Add an additional operand for OpPhi instruction. Use a constant
  53. // if possible, and an undef otherwise.
  54. if (fuzzerutil::CanCreateConstant(GetIRContext(), phi->type_id())) {
  55. // We mark the constant as irrelevant so that we can replace it with
  56. // a more interesting value later.
  57. phi_ids.push_back(FindOrCreateZeroConstant(phi->type_id(), true));
  58. } else {
  59. phi_ids.push_back(FindOrCreateGlobalUndef(phi->type_id()));
  60. }
  61. });
  62. }
  63. // Make sure the module contains a boolean constant equal to
  64. // |condition_value|.
  65. bool condition_value = GetFuzzerContext()->ChooseEven();
  66. FindOrCreateBoolConstant(condition_value, false);
  67. // Make a transformation to add a dead continue from this node; if the
  68. // node turns out to be inappropriate (e.g. by not being in a loop) the
  69. // precondition for the transformation will fail and it will be ignored.
  70. auto candidate_transformation = TransformationAddDeadContinue(
  71. block.id(), condition_value, std::move(phi_ids));
  72. // Probabilistically decide whether to apply the transformation in the
  73. // case that it is applicable.
  74. if (GetFuzzerContext()->ChoosePercentage(
  75. GetFuzzerContext()->GetChanceOfAddingDeadContinue())) {
  76. MaybeApplyTransformation(candidate_transformation);
  77. }
  78. }
  79. }
  80. }
  81. } // namespace fuzz
  82. } // namespace spvtools