transformation_add_dead_continue.cpp 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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. auto continue_block = context->cfg()->block(loop_header)->ContinueBlockId();
  76. if (!fuzzerutil::BlockIsReachableInItsFunction(
  77. context, context->cfg()->block(continue_block))) {
  78. // If the loop's continue block is unreachable, we conservatively do not
  79. // allow adding a dead continue, to avoid the compilations that arise due to
  80. // the lack of sensible dominance information for unreachable blocks.
  81. return false;
  82. }
  83. if (fuzzerutil::BlockIsInLoopContinueConstruct(context, message_.from_block(),
  84. loop_header)) {
  85. // We cannot jump to the continue target from the continue construct.
  86. return false;
  87. }
  88. if (context->GetStructuredCFGAnalysis()->IsMergeBlock(continue_block)) {
  89. // A branch straight to the continue target that is also a merge block might
  90. // break the property that a construct header must dominate its merge block
  91. // (if the merge block is reachable).
  92. return false;
  93. }
  94. // Check whether the data passed to extend OpPhi instructions is appropriate.
  95. if (!fuzzerutil::PhiIdsOkForNewEdge(context, bb_from,
  96. context->cfg()->block(continue_block),
  97. message_.phi_id())) {
  98. return false;
  99. }
  100. // Adding the dead break is only valid if SPIR-V rules related to dominance
  101. // hold. Rather than checking these rules explicitly, we defer to the
  102. // validator. We make a clone of the module, apply the transformation to the
  103. // clone, and check whether the transformed clone is valid.
  104. //
  105. // In principle some of the above checks could be removed, with more reliance
  106. // being places on the validator. This should be revisited if we are sure
  107. // the validator is complete with respect to checking structured control flow
  108. // rules.
  109. auto cloned_context = fuzzerutil::CloneIRContext(context);
  110. ApplyImpl(cloned_context.get());
  111. return fuzzerutil::IsValid(cloned_context.get());
  112. }
  113. void TransformationAddDeadContinue::Apply(opt::IRContext* context,
  114. FactManager* /*unused*/) const {
  115. ApplyImpl(context);
  116. // Invalidate all analyses
  117. context->InvalidateAnalysesExceptFor(opt::IRContext::Analysis::kAnalysisNone);
  118. }
  119. protobufs::Transformation TransformationAddDeadContinue::ToMessage() const {
  120. protobufs::Transformation result;
  121. *result.mutable_add_dead_continue() = message_;
  122. return result;
  123. }
  124. void TransformationAddDeadContinue::ApplyImpl(
  125. spvtools::opt::IRContext* context) const {
  126. auto bb_from = context->cfg()->block(message_.from_block());
  127. auto continue_block =
  128. bb_from->IsLoopHeader()
  129. ? bb_from->ContinueBlockId()
  130. : context->GetStructuredCFGAnalysis()->LoopContinueBlock(
  131. message_.from_block());
  132. assert(continue_block && "message_.from_block must be in a loop.");
  133. fuzzerutil::AddUnreachableEdgeAndUpdateOpPhis(
  134. context, bb_from, context->cfg()->block(continue_block),
  135. message_.continue_condition_value(), message_.phi_id());
  136. }
  137. } // namespace fuzz
  138. } // namespace spvtools