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"
|
|
|
|
|
2019-07-25 12:50:33 +00:00
|
|
|
#include "source/fuzz/fuzzer_util.h"
|
2020-04-02 14:54:46 +00:00
|
|
|
#include "source/fuzz/transformation_context.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(
|
2021-03-23 13:31:44 +00:00
|
|
|
protobufs::TransformationAddDeadBreak message)
|
|
|
|
: message_(std::move(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(
|
2020-04-02 14:54:46 +00:00
|
|
|
opt::IRContext* ir_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 =
|
2020-04-02 14:54:46 +00:00
|
|
|
ir_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() ==
|
2020-04-02 14:54:46 +00:00
|
|
|
ir_context->cfg()->block(containing_construct)->MergeBlockId()) {
|
2019-06-05 07:02:16 +00:00
|
|
|
// 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.
|
2019-07-25 12:50:33 +00:00
|
|
|
return !fuzzerutil::BlockIsInLoopContinueConstruct(
|
2020-07-14 11:32:16 +00:00
|
|
|
ir_context, message_.from_block(), containing_construct) ||
|
|
|
|
fuzzerutil::BlockIsBackEdge(ir_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 =
|
2020-04-02 14:54:46 +00:00
|
|
|
ir_context->GetStructuredCFGAnalysis()->ContainingLoop(
|
2019-06-25 19:49:46 +00:00
|
|
|
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() ==
|
2020-04-02 14:54:46 +00:00
|
|
|
ir_context->cfg()->block(containing_loop_header)->MergeBlockId()) {
|
2019-07-25 12:50:33 +00:00
|
|
|
return !fuzzerutil::BlockIsInLoopContinueConstruct(
|
2020-07-14 11:32:16 +00:00
|
|
|
ir_context, message_.from_block(), containing_loop_header) ||
|
|
|
|
fuzzerutil::BlockIsBackEdge(ir_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(
|
2020-04-02 14:54:46 +00:00
|
|
|
opt::IRContext* ir_context,
|
|
|
|
const TransformationContext& transformation_context) 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.
|
2021-03-05 14:27:37 +00:00
|
|
|
const auto bool_id =
|
|
|
|
fuzzerutil::MaybeGetBoolConstant(ir_context, transformation_context,
|
|
|
|
message_.break_condition_value(), false);
|
|
|
|
if (!bool_id) {
|
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 =
|
2020-04-02 14:54:46 +00:00
|
|
|
fuzzerutil::MaybeFindBlock(ir_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 =
|
2020-04-02 14:54:46 +00:00
|
|
|
fuzzerutil::MaybeFindBlock(ir_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
|
|
|
}
|
|
|
|
|
2021-06-28 19:00:14 +00:00
|
|
|
if (!ir_context->IsReachable(*bb_to)) {
|
2019-09-26 09:57:05 +00:00
|
|
|
// 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.
|
2022-11-04 21:27:10 +00:00
|
|
|
if (bb_from->terminator()->opcode() != spv::Op::OpBranch) {
|
2019-06-05 07:02:16 +00:00
|
|
|
// 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.
|
2020-04-02 14:54:46 +00:00
|
|
|
if (!fuzzerutil::PhiIdsOkForNewEdge(ir_context, bb_from, bb_to,
|
2019-07-25 12:50:33 +00:00
|
|
|
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.
|
2020-04-02 14:54:46 +00:00
|
|
|
if (!AddingBreakRespectsStructuredControlFlow(ir_context, bb_from)) {
|
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
|
2021-03-05 14:27:37 +00:00
|
|
|
// hold.
|
|
|
|
return fuzzerutil::NewTerminatorPreservesDominationRules(
|
|
|
|
ir_context, message_.from_block(),
|
|
|
|
fuzzerutil::CreateUnreachableEdgeInstruction(
|
|
|
|
ir_context, message_.from_block(), message_.to_block(), bool_id));
|
2019-06-05 07:02:16 +00:00
|
|
|
}
|
|
|
|
|
2020-04-02 14:54:46 +00:00
|
|
|
void TransformationAddDeadBreak::Apply(
|
2020-07-21 07:59:13 +00:00
|
|
|
opt::IRContext* ir_context,
|
|
|
|
TransformationContext* transformation_context) const {
|
2021-03-05 14:27:37 +00:00
|
|
|
fuzzerutil::AddUnreachableEdgeAndUpdateOpPhis(
|
|
|
|
ir_context, ir_context->cfg()->block(message_.from_block()),
|
|
|
|
ir_context->cfg()->block(message_.to_block()),
|
|
|
|
fuzzerutil::MaybeGetBoolConstant(ir_context, *transformation_context,
|
|
|
|
message_.break_condition_value(), false),
|
|
|
|
message_.phi_id());
|
|
|
|
|
2019-06-05 07:02:16 +00:00
|
|
|
// Invalidate all analyses
|
2020-04-02 14:54:46 +00:00
|
|
|
ir_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;
|
|
|
|
}
|
|
|
|
|
2020-09-29 21:12:49 +00:00
|
|
|
std::unordered_set<uint32_t> TransformationAddDeadBreak::GetFreshIds() const {
|
|
|
|
return std::unordered_set<uint32_t>();
|
|
|
|
}
|
|
|
|
|
2019-06-05 07:02:16 +00:00
|
|
|
} // namespace fuzz
|
|
|
|
} // namespace spvtools
|