transformation_add_dead_continue.cpp 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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/transformation_add_dead_continue.h"
  15. #include "source/fuzz/fuzzer_util.h"
  16. namespace spvtools {
  17. namespace fuzz {
  18. TransformationAddDeadContinue::TransformationAddDeadContinue(
  19. const spvtools::fuzz::protobufs::TransformationAddDeadContinue& message)
  20. : message_(message) {}
  21. TransformationAddDeadContinue::TransformationAddDeadContinue(
  22. uint32_t from_block, bool continue_condition_value,
  23. std::vector<uint32_t> phi_id) {
  24. message_.set_from_block(from_block);
  25. message_.set_continue_condition_value(continue_condition_value);
  26. for (auto id : phi_id) {
  27. message_.add_phi_id(id);
  28. }
  29. }
  30. bool TransformationAddDeadContinue::IsApplicable(
  31. opt::IRContext* context, const FactManager& /*unused*/) const {
  32. // First, we check that a constant with the same value as
  33. // |message_.continue_condition_value| is present.
  34. opt::analysis::Bool bool_type;
  35. auto registered_bool_type =
  36. context->get_type_mgr()->GetRegisteredType(&bool_type);
  37. if (!registered_bool_type) {
  38. return false;
  39. }
  40. opt::analysis::BoolConstant bool_constant(
  41. registered_bool_type->AsBool(), message_.continue_condition_value());
  42. if (!context->get_constant_mgr()->FindConstant(&bool_constant)) {
  43. // The required constant is not present, so the transformation cannot be
  44. // applied.
  45. return false;
  46. }
  47. // Check that |message_.from_block| really is a block id.
  48. opt::BasicBlock* bb_from =
  49. fuzzerutil::MaybeFindBlock(context, message_.from_block());
  50. if (bb_from == nullptr) {
  51. return false;
  52. }
  53. // Check that |message_.from_block| ends with an unconditional branch.
  54. if (bb_from->terminator()->opcode() != SpvOpBranch) {
  55. // The block associated with the id does not end with an unconditional
  56. // branch.
  57. return false;
  58. }
  59. assert(bb_from != nullptr &&
  60. "We should have found a block if this line of code is reached.");
  61. assert(
  62. bb_from->id() == message_.from_block() &&
  63. "The id of the block we found should match the source id for the break.");
  64. // Get the header for the innermost loop containing |message_.from_block|.
  65. // Because the structured CFG analysis does not regard a loop header as part
  66. // of the loop it heads, we check first whether bb_from is a loop header
  67. // before using the structured CFG analysis.
  68. auto loop_header = bb_from->IsLoopHeader()
  69. ? message_.from_block()
  70. : context->GetStructuredCFGAnalysis()->ContainingLoop(
  71. message_.from_block());
  72. if (!loop_header) {
  73. return false;
  74. }
  75. if (fuzzerutil::BlockIsInLoopContinueConstruct(context, message_.from_block(),
  76. loop_header)) {
  77. // We cannot jump to the continue target from the continue construct.
  78. return false;
  79. }
  80. auto continue_block = context->cfg()->block(loop_header)->ContinueBlockId();
  81. if (context->GetStructuredCFGAnalysis()->IsMergeBlock(continue_block)) {
  82. // A branch straight to the continue target that is also a merge block might
  83. // break the property that a construct header must dominate its merge block
  84. // (if the merge block is reachable).
  85. return false;
  86. }
  87. // The transformation is good if and only if the given phi ids are sufficient
  88. // to extend relevant OpPhi instructions in the continue block.
  89. return fuzzerutil::PhiIdsOkForNewEdge(context, bb_from,
  90. context->cfg()->block(continue_block),
  91. message_.phi_id());
  92. }
  93. void TransformationAddDeadContinue::Apply(opt::IRContext* context,
  94. FactManager* /*unused*/) const {
  95. auto bb_from = context->cfg()->block(message_.from_block());
  96. auto continue_block =
  97. bb_from->IsLoopHeader()
  98. ? bb_from->ContinueBlockId()
  99. : context->GetStructuredCFGAnalysis()->LoopContinueBlock(
  100. message_.from_block());
  101. assert(continue_block &&
  102. "Precondition for this transformation requires that "
  103. "message_.from_block must be in a loop.");
  104. fuzzerutil::AddUnreachableEdgeAndUpdateOpPhis(
  105. context, bb_from, context->cfg()->block(continue_block),
  106. message_.continue_condition_value(), message_.phi_id());
  107. // Invalidate all analyses
  108. context->InvalidateAnalysesExceptFor(opt::IRContext::Analysis::kAnalysisNone);
  109. }
  110. protobufs::Transformation TransformationAddDeadContinue::ToMessage() const {
  111. protobufs::Transformation result;
  112. *result.mutable_add_dead_continue() = message_;
  113. return result;
  114. }
  115. } // namespace fuzz
  116. } // namespace spvtools