2019-07-25 12:50:33 +00:00
|
|
|
// Copyright (c) 2019 Google LLC
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
#include "source/fuzz/transformation_add_dead_continue.h"
|
|
|
|
|
|
|
|
#include "source/fuzz/fuzzer_util.h"
|
|
|
|
|
|
|
|
namespace spvtools {
|
|
|
|
namespace fuzz {
|
|
|
|
|
|
|
|
TransformationAddDeadContinue::TransformationAddDeadContinue(
|
|
|
|
const spvtools::fuzz::protobufs::TransformationAddDeadContinue& message)
|
|
|
|
: message_(message) {}
|
|
|
|
|
|
|
|
TransformationAddDeadContinue::TransformationAddDeadContinue(
|
|
|
|
uint32_t from_block, bool continue_condition_value,
|
|
|
|
std::vector<uint32_t> phi_id) {
|
|
|
|
message_.set_from_block(from_block);
|
|
|
|
message_.set_continue_condition_value(continue_condition_value);
|
|
|
|
for (auto id : phi_id) {
|
|
|
|
message_.add_phi_id(id);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool TransformationAddDeadContinue::IsApplicable(
|
2020-04-02 14:54:46 +00:00
|
|
|
opt::IRContext* ir_context,
|
|
|
|
const TransformationContext& transformation_context) const {
|
2019-07-25 12:50:33 +00:00
|
|
|
// First, we check that a constant with the same value as
|
|
|
|
// |message_.continue_condition_value| is present.
|
2020-07-07 09:46:04 +00:00
|
|
|
if (!fuzzerutil::MaybeGetBoolConstant(ir_context,
|
|
|
|
message_.continue_condition_value())) {
|
2019-07-25 12:50:33 +00:00
|
|
|
// The required constant is not present, so the transformation cannot be
|
|
|
|
// applied.
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check that |message_.from_block| really is a block id.
|
|
|
|
opt::BasicBlock* bb_from =
|
2020-04-02 14:54:46 +00:00
|
|
|
fuzzerutil::MaybeFindBlock(ir_context, message_.from_block());
|
2019-07-25 12:50:33 +00:00
|
|
|
if (bb_from == nullptr) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check that |message_.from_block| ends with an unconditional branch.
|
|
|
|
if (bb_from->terminator()->opcode() != SpvOpBranch) {
|
|
|
|
// The block associated with the id does not end with an unconditional
|
|
|
|
// branch.
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
assert(bb_from != nullptr &&
|
|
|
|
"We should have found a block if this line of code is reached.");
|
|
|
|
assert(
|
|
|
|
bb_from->id() == message_.from_block() &&
|
|
|
|
"The id of the block we found should match the source id for the break.");
|
|
|
|
|
|
|
|
// Get the header for the innermost loop containing |message_.from_block|.
|
|
|
|
// Because the structured CFG analysis does not regard a loop header as part
|
|
|
|
// of the loop it heads, we check first whether bb_from is a loop header
|
|
|
|
// before using the structured CFG analysis.
|
2020-04-02 14:54:46 +00:00
|
|
|
auto loop_header =
|
|
|
|
bb_from->IsLoopHeader()
|
|
|
|
? message_.from_block()
|
|
|
|
: ir_context->GetStructuredCFGAnalysis()->ContainingLoop(
|
|
|
|
message_.from_block());
|
2019-07-25 12:50:33 +00:00
|
|
|
if (!loop_header) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-04-02 14:54:46 +00:00
|
|
|
auto continue_block =
|
|
|
|
ir_context->cfg()->block(loop_header)->ContinueBlockId();
|
2019-09-25 15:51:41 +00:00
|
|
|
|
2019-09-26 09:57:05 +00:00
|
|
|
if (!fuzzerutil::BlockIsReachableInItsFunction(
|
2020-04-02 14:54:46 +00:00
|
|
|
ir_context, ir_context->cfg()->block(continue_block))) {
|
2019-09-26 09:57:05 +00:00
|
|
|
// If the loop's continue block is unreachable, we conservatively do not
|
2019-09-25 15:51:41 +00:00
|
|
|
// allow adding a dead continue, to avoid the compilations that arise due to
|
|
|
|
// the lack of sensible dominance information for unreachable blocks.
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-04-02 14:54:46 +00:00
|
|
|
if (fuzzerutil::BlockIsInLoopContinueConstruct(
|
|
|
|
ir_context, message_.from_block(), loop_header)) {
|
2019-07-25 12:50:33 +00:00
|
|
|
// We cannot jump to the continue target from the continue construct.
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-04-02 14:54:46 +00:00
|
|
|
if (ir_context->GetStructuredCFGAnalysis()->IsMergeBlock(continue_block)) {
|
2019-07-25 12:50:33 +00:00
|
|
|
// A branch straight to the continue target that is also a merge block might
|
|
|
|
// break the property that a construct header must dominate its merge block
|
|
|
|
// (if the merge block is reachable).
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-12-06 16:38:17 +00:00
|
|
|
// Check whether the data passed to extend OpPhi instructions is appropriate.
|
2020-04-02 14:54:46 +00:00
|
|
|
if (!fuzzerutil::PhiIdsOkForNewEdge(ir_context, bb_from,
|
|
|
|
ir_context->cfg()->block(continue_block),
|
2019-12-06 16:38:17 +00:00
|
|
|
message_.phi_id())) {
|
2019-09-10 13:48:27 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-12-06 16:38:17 +00:00
|
|
|
// Adding the dead break is only valid if SPIR-V rules related to dominance
|
|
|
|
// hold. Rather than checking these rules explicitly, we defer to the
|
|
|
|
// validator. We make a clone of the module, apply the transformation to the
|
|
|
|
// clone, and check whether the transformed clone is valid.
|
|
|
|
//
|
|
|
|
// In principle some of the above checks could be removed, with more reliance
|
2020-03-12 12:40:38 +00:00
|
|
|
// being placed on the validator. This should be revisited if we are sure
|
2019-12-06 16:38:17 +00:00
|
|
|
// the validator is complete with respect to checking structured control flow
|
|
|
|
// rules.
|
2020-04-02 14:54:46 +00:00
|
|
|
auto cloned_context = fuzzerutil::CloneIRContext(ir_context);
|
2019-12-06 16:38:17 +00:00
|
|
|
ApplyImpl(cloned_context.get());
|
2020-04-02 14:54:46 +00:00
|
|
|
return fuzzerutil::IsValid(cloned_context.get(),
|
|
|
|
transformation_context.GetValidatorOptions());
|
2019-07-25 12:50:33 +00:00
|
|
|
}
|
|
|
|
|
2020-04-02 14:54:46 +00:00
|
|
|
void TransformationAddDeadContinue::Apply(
|
|
|
|
opt::IRContext* ir_context, TransformationContext* /*unused*/) const {
|
|
|
|
ApplyImpl(ir_context);
|
2019-12-06 16:38:17 +00:00
|
|
|
// Invalidate all analyses
|
2020-04-02 14:54:46 +00:00
|
|
|
ir_context->InvalidateAnalysesExceptFor(
|
|
|
|
opt::IRContext::Analysis::kAnalysisNone);
|
2019-12-06 16:38:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
protobufs::Transformation TransformationAddDeadContinue::ToMessage() const {
|
|
|
|
protobufs::Transformation result;
|
|
|
|
*result.mutable_add_dead_continue() = message_;
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
void TransformationAddDeadContinue::ApplyImpl(
|
2020-04-02 14:54:46 +00:00
|
|
|
spvtools::opt::IRContext* ir_context) const {
|
|
|
|
auto bb_from = ir_context->cfg()->block(message_.from_block());
|
2019-07-25 12:50:33 +00:00
|
|
|
auto continue_block =
|
|
|
|
bb_from->IsLoopHeader()
|
|
|
|
? bb_from->ContinueBlockId()
|
2020-04-02 14:54:46 +00:00
|
|
|
: ir_context->GetStructuredCFGAnalysis()->LoopContinueBlock(
|
2019-07-25 12:50:33 +00:00
|
|
|
message_.from_block());
|
2019-12-06 16:38:17 +00:00
|
|
|
assert(continue_block && "message_.from_block must be in a loop.");
|
2019-07-25 12:50:33 +00:00
|
|
|
fuzzerutil::AddUnreachableEdgeAndUpdateOpPhis(
|
2020-04-02 14:54:46 +00:00
|
|
|
ir_context, bb_from, ir_context->cfg()->block(continue_block),
|
2019-07-25 12:50:33 +00:00
|
|
|
message_.continue_condition_value(), message_.phi_id());
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace fuzz
|
|
|
|
} // namespace spvtools
|