transformation_add_dead_continue.cpp 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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. if (!fuzzerutil::MaybeGetBoolConstantId(
  35. context, message_.continue_condition_value())) {
  36. // The required constant is not present, so the transformation cannot be
  37. // applied.
  38. return false;
  39. }
  40. // Check that |message_.from_block| really is a block id.
  41. opt::BasicBlock* bb_from =
  42. fuzzerutil::MaybeFindBlock(context, message_.from_block());
  43. if (bb_from == nullptr) {
  44. return false;
  45. }
  46. // Check that |message_.from_block| ends with an unconditional branch.
  47. if (bb_from->terminator()->opcode() != SpvOpBranch) {
  48. // The block associated with the id does not end with an unconditional
  49. // branch.
  50. return false;
  51. }
  52. assert(bb_from != nullptr &&
  53. "We should have found a block if this line of code is reached.");
  54. assert(
  55. bb_from->id() == message_.from_block() &&
  56. "The id of the block we found should match the source id for the break.");
  57. // Get the header for the innermost loop containing |message_.from_block|.
  58. // Because the structured CFG analysis does not regard a loop header as part
  59. // of the loop it heads, we check first whether bb_from is a loop header
  60. // before using the structured CFG analysis.
  61. auto loop_header = bb_from->IsLoopHeader()
  62. ? message_.from_block()
  63. : context->GetStructuredCFGAnalysis()->ContainingLoop(
  64. message_.from_block());
  65. if (!loop_header) {
  66. return false;
  67. }
  68. auto continue_block = context->cfg()->block(loop_header)->ContinueBlockId();
  69. if (!fuzzerutil::BlockIsReachableInItsFunction(
  70. context, context->cfg()->block(continue_block))) {
  71. // If the loop's continue block is unreachable, we conservatively do not
  72. // allow adding a dead continue, to avoid the compilations that arise due to
  73. // the lack of sensible dominance information for unreachable blocks.
  74. return false;
  75. }
  76. if (fuzzerutil::BlockIsInLoopContinueConstruct(context, message_.from_block(),
  77. loop_header)) {
  78. // We cannot jump to the continue target from the continue construct.
  79. return false;
  80. }
  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. // Check whether the data passed to extend OpPhi instructions is appropriate.
  88. if (!fuzzerutil::PhiIdsOkForNewEdge(context, bb_from,
  89. context->cfg()->block(continue_block),
  90. message_.phi_id())) {
  91. return false;
  92. }
  93. // Adding the dead break is only valid if SPIR-V rules related to dominance
  94. // hold. Rather than checking these rules explicitly, we defer to the
  95. // validator. We make a clone of the module, apply the transformation to the
  96. // clone, and check whether the transformed clone is valid.
  97. //
  98. // In principle some of the above checks could be removed, with more reliance
  99. // being placed on the validator. This should be revisited if we are sure
  100. // the validator is complete with respect to checking structured control flow
  101. // rules.
  102. auto cloned_context = fuzzerutil::CloneIRContext(context);
  103. ApplyImpl(cloned_context.get());
  104. return fuzzerutil::IsValid(cloned_context.get());
  105. }
  106. void TransformationAddDeadContinue::Apply(opt::IRContext* context,
  107. FactManager* /*unused*/) const {
  108. ApplyImpl(context);
  109. // Invalidate all analyses
  110. context->InvalidateAnalysesExceptFor(opt::IRContext::Analysis::kAnalysisNone);
  111. }
  112. protobufs::Transformation TransformationAddDeadContinue::ToMessage() const {
  113. protobufs::Transformation result;
  114. *result.mutable_add_dead_continue() = message_;
  115. return result;
  116. }
  117. void TransformationAddDeadContinue::ApplyImpl(
  118. spvtools::opt::IRContext* context) const {
  119. auto bb_from = context->cfg()->block(message_.from_block());
  120. auto continue_block =
  121. bb_from->IsLoopHeader()
  122. ? bb_from->ContinueBlockId()
  123. : context->GetStructuredCFGAnalysis()->LoopContinueBlock(
  124. message_.from_block());
  125. assert(continue_block && "message_.from_block must be in a loop.");
  126. fuzzerutil::AddUnreachableEdgeAndUpdateOpPhis(
  127. context, bb_from, context->cfg()->block(continue_block),
  128. message_.continue_condition_value(), message_.phi_id());
  129. }
  130. } // namespace fuzz
  131. } // namespace spvtools