2019-06-05 07:02:16 +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_break.h"
|
|
|
|
|
|
|
|
#include "source/fuzz/fact_manager.h"
|
2019-07-25 12:50:33 +00:00
|
|
|
#include "source/fuzz/fuzzer_util.h"
|
2019-06-05 07:02:16 +00:00
|
|
|
#include "source/opt/basic_block.h"
|
|
|
|
#include "source/opt/ir_context.h"
|
|
|
|
#include "source/opt/struct_cfg_analysis.h"
|
|
|
|
|
|
|
|
namespace spvtools {
|
|
|
|
namespace fuzz {
|
|
|
|
|
2019-06-25 19:49:46 +00:00
|
|
|
TransformationAddDeadBreak::TransformationAddDeadBreak(
|
|
|
|
const spvtools::fuzz::protobufs::TransformationAddDeadBreak& message)
|
|
|
|
: message_(message) {}
|
2019-06-05 07:02:16 +00:00
|
|
|
|
2019-06-25 19:49:46 +00:00
|
|
|
TransformationAddDeadBreak::TransformationAddDeadBreak(
|
|
|
|
uint32_t from_block, uint32_t to_block, bool break_condition_value,
|
|
|
|
std::vector<uint32_t> phi_id) {
|
|
|
|
message_.set_from_block(from_block);
|
|
|
|
message_.set_to_block(to_block);
|
|
|
|
message_.set_break_condition_value(break_condition_value);
|
|
|
|
for (auto id : phi_id) {
|
|
|
|
message_.add_phi_id(id);
|
|
|
|
}
|
|
|
|
}
|
2019-06-05 07:02:16 +00:00
|
|
|
|
2019-06-25 19:49:46 +00:00
|
|
|
bool TransformationAddDeadBreak::AddingBreakRespectsStructuredControlFlow(
|
|
|
|
opt::IRContext* context, opt::BasicBlock* bb_from) const {
|
2019-06-05 07:02:16 +00:00
|
|
|
// Look at the structured control flow associated with |from_block| and
|
|
|
|
// check whether it is contained in an appropriate construct with merge id
|
|
|
|
// |to_block| such that a break from |from_block| to |to_block| is legal.
|
|
|
|
|
|
|
|
// There are three legal cases to consider:
|
|
|
|
// (1) |from_block| is a loop header and |to_block| is its merge
|
|
|
|
// (2) |from_block| is a non-header node of a construct, and |to_block|
|
|
|
|
// is the merge for that construct
|
|
|
|
// (3) |from_block| is a non-header node of a selection construct, and
|
|
|
|
// |to_block| is the merge for the innermost loop containing
|
|
|
|
// |from_block|
|
|
|
|
//
|
|
|
|
// TODO(https://github.com/KhronosGroup/SPIRV-Tools/issues/2653) It may be
|
|
|
|
// possible to be more aggressive in breaking from switch constructs.
|
|
|
|
//
|
|
|
|
// The reason we need to distinguish between cases (1) and (2) is that the
|
|
|
|
// structured CFG analysis does not deem a header to be part of the construct
|
|
|
|
// that it heads.
|
|
|
|
|
|
|
|
// Consider case (1)
|
|
|
|
if (bb_from->IsLoopHeader()) {
|
|
|
|
// Case (1) holds if |to_block| is the merge block for the loop;
|
|
|
|
// otherwise no case holds
|
2019-06-25 19:49:46 +00:00
|
|
|
return bb_from->MergeBlockId() == message_.to_block();
|
2019-06-05 07:02:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Both cases (2) and (3) require that |from_block| is inside some
|
|
|
|
// structured control flow construct.
|
|
|
|
|
|
|
|
auto containing_construct =
|
|
|
|
context->GetStructuredCFGAnalysis()->ContainingConstruct(
|
2019-06-25 19:49:46 +00:00
|
|
|
message_.from_block());
|
2019-06-05 07:02:16 +00:00
|
|
|
if (!containing_construct) {
|
|
|
|
// |from_block| is not in a construct from which we can break.
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Consider case (2)
|
2019-06-25 19:49:46 +00:00
|
|
|
if (message_.to_block() ==
|
2019-06-05 07:02:16 +00:00
|
|
|
context->cfg()->block(containing_construct)->MergeBlockId()) {
|
|
|
|
// This looks like an instance of case (2).
|
|
|
|
// However, the structured CFG analysis regards the continue construct of a
|
|
|
|
// loop as part of the loop, but it is not legal to jump from a loop's
|
|
|
|
// continue construct to the loop's merge (except from the back-edge block),
|
|
|
|
// so we need to check for this case.
|
|
|
|
//
|
|
|
|
// TODO(https://github.com/KhronosGroup/SPIRV-Tools/issues/2577): We do not
|
|
|
|
// currently allow a dead break from a back edge block, but we could and
|
|
|
|
// ultimately should.
|
2019-07-25 12:50:33 +00:00
|
|
|
return !fuzzerutil::BlockIsInLoopContinueConstruct(
|
|
|
|
context, message_.from_block(), containing_construct);
|
2019-06-05 07:02:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Case (3) holds if and only if |to_block| is the merge block for this
|
|
|
|
// innermost loop that contains |from_block|
|
|
|
|
auto containing_loop_header =
|
2019-06-25 19:49:46 +00:00
|
|
|
context->GetStructuredCFGAnalysis()->ContainingLoop(
|
|
|
|
message_.from_block());
|
2019-06-05 07:02:16 +00:00
|
|
|
if (containing_loop_header &&
|
2019-06-25 19:49:46 +00:00
|
|
|
message_.to_block() ==
|
2019-06-05 07:02:16 +00:00
|
|
|
context->cfg()->block(containing_loop_header)->MergeBlockId()) {
|
2019-07-25 12:50:33 +00:00
|
|
|
return !fuzzerutil::BlockIsInLoopContinueConstruct(
|
|
|
|
context, message_.from_block(), containing_loop_header);
|
2019-06-05 07:02:16 +00:00
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-06-25 19:49:46 +00:00
|
|
|
bool TransformationAddDeadBreak::IsApplicable(
|
|
|
|
opt::IRContext* context, const FactManager& /*unused*/) const {
|
2019-06-05 07:02:16 +00:00
|
|
|
// First, we check that a constant with the same value as
|
2019-07-25 12:50:33 +00:00
|
|
|
// |message_.break_condition_value| is present.
|
2020-01-13 22:04:01 +00:00
|
|
|
if (!fuzzerutil::MaybeGetBoolConstantId(context,
|
|
|
|
message_.break_condition_value())) {
|
2019-06-05 07:02:16 +00:00
|
|
|
// The required constant is not present, so the transformation cannot be
|
|
|
|
// applied.
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-07-25 12:50:33 +00:00
|
|
|
// Check that |message_.from_block| and |message_.to_block| really are block
|
|
|
|
// ids
|
|
|
|
opt::BasicBlock* bb_from =
|
|
|
|
fuzzerutil::MaybeFindBlock(context, message_.from_block());
|
2019-06-05 07:02:16 +00:00
|
|
|
if (bb_from == nullptr) {
|
|
|
|
return false;
|
|
|
|
}
|
2019-07-25 12:50:33 +00:00
|
|
|
opt::BasicBlock* bb_to =
|
|
|
|
fuzzerutil::MaybeFindBlock(context, message_.to_block());
|
2019-06-05 07:02:16 +00:00
|
|
|
if (bb_to == nullptr) {
|
|
|
|
return false;
|
2019-09-26 09:57:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!fuzzerutil::BlockIsReachableInItsFunction(context, bb_to)) {
|
|
|
|
// If the target of the break is unreachable, we conservatively do not
|
|
|
|
// allow adding a dead break, to avoid the compilations that arise due to
|
|
|
|
// the lack of sensible dominance information for unreachable blocks.
|
|
|
|
return false;
|
2019-06-05 07:02:16 +00:00
|
|
|
}
|
|
|
|
|
2019-07-25 12:50:33 +00:00
|
|
|
// Check that |message_.from_block| ends with an unconditional branch.
|
2019-06-05 07:02:16 +00:00
|
|
|
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(
|
2019-06-25 19:49:46 +00:00
|
|
|
bb_from->id() == message_.from_block() &&
|
2019-06-05 07:02:16 +00:00
|
|
|
"The id of the block we found should match the source id for the break.");
|
|
|
|
assert(bb_to != nullptr &&
|
|
|
|
"We should have found a block if this line of code is reached.");
|
|
|
|
assert(
|
2019-06-25 19:49:46 +00:00
|
|
|
bb_to->id() == message_.to_block() &&
|
2019-06-05 07:02:16 +00:00
|
|
|
"The id of the block we found should match the target id for the break.");
|
|
|
|
|
|
|
|
// Check whether the data passed to extend OpPhi instructions is appropriate.
|
2019-07-25 12:50:33 +00:00
|
|
|
if (!fuzzerutil::PhiIdsOkForNewEdge(context, bb_from, bb_to,
|
|
|
|
message_.phi_id())) {
|
2019-06-05 07:02:16 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-09-10 13:48:27 +00:00
|
|
|
// Check that adding the break would respect the rules of structured
|
2019-06-05 07:02:16 +00:00
|
|
|
// control flow.
|
2019-09-10 13:48:27 +00:00
|
|
|
if (!AddingBreakRespectsStructuredControlFlow(context, bb_from)) {
|
|
|
|
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
|
|
|
|
// being places on the validator. This should be revisited if we are sure
|
|
|
|
// the validator is complete with respect to checking structured control flow
|
|
|
|
// rules.
|
|
|
|
auto cloned_context = fuzzerutil::CloneIRContext(context);
|
|
|
|
ApplyImpl(cloned_context.get());
|
|
|
|
return fuzzerutil::IsValid(cloned_context.get());
|
2019-06-05 07:02:16 +00:00
|
|
|
}
|
|
|
|
|
2019-06-25 19:49:46 +00:00
|
|
|
void TransformationAddDeadBreak::Apply(opt::IRContext* context,
|
|
|
|
FactManager* /*unused*/) const {
|
2019-12-06 16:38:17 +00:00
|
|
|
ApplyImpl(context);
|
2019-06-05 07:02:16 +00:00
|
|
|
// Invalidate all analyses
|
2019-06-25 19:49:46 +00:00
|
|
|
context->InvalidateAnalysesExceptFor(opt::IRContext::Analysis::kAnalysisNone);
|
2019-06-05 07:02:16 +00:00
|
|
|
}
|
|
|
|
|
2019-06-25 19:49:46 +00:00
|
|
|
protobufs::Transformation TransformationAddDeadBreak::ToMessage() const {
|
|
|
|
protobufs::Transformation result;
|
|
|
|
*result.mutable_add_dead_break() = message_;
|
2019-06-05 07:02:16 +00:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2019-12-06 16:38:17 +00:00
|
|
|
void TransformationAddDeadBreak::ApplyImpl(
|
|
|
|
spvtools::opt::IRContext* context) const {
|
|
|
|
fuzzerutil::AddUnreachableEdgeAndUpdateOpPhis(
|
|
|
|
context, context->cfg()->block(message_.from_block()),
|
|
|
|
context->cfg()->block(message_.to_block()),
|
|
|
|
message_.break_condition_value(), message_.phi_id());
|
|
|
|
}
|
|
|
|
|
2019-06-05 07:02:16 +00:00
|
|
|
} // namespace fuzz
|
|
|
|
} // namespace spvtools
|