transformation_add_dead_continue.cpp 6.0 KB

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