2019-08-05 17:00:13 +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_copy_object.h"
|
|
|
|
|
2019-10-25 16:37:55 +00:00
|
|
|
#include "source/fuzz/data_descriptor.h"
|
2019-08-05 17:00:13 +00:00
|
|
|
#include "source/fuzz/fuzzer_util.h"
|
2019-10-15 19:00:17 +00:00
|
|
|
#include "source/fuzz/instruction_descriptor.h"
|
2019-08-05 17:00:13 +00:00
|
|
|
#include "source/opt/instruction.h"
|
|
|
|
#include "source/util/make_unique.h"
|
|
|
|
|
|
|
|
namespace spvtools {
|
|
|
|
namespace fuzz {
|
|
|
|
|
|
|
|
TransformationCopyObject::TransformationCopyObject(
|
|
|
|
const protobufs::TransformationCopyObject& message)
|
|
|
|
: message_(message) {}
|
|
|
|
|
2019-10-15 19:00:17 +00:00
|
|
|
TransformationCopyObject::TransformationCopyObject(
|
|
|
|
uint32_t object,
|
|
|
|
const protobufs::InstructionDescriptor& instruction_to_insert_before,
|
|
|
|
uint32_t fresh_id) {
|
2019-08-05 17:00:13 +00:00
|
|
|
message_.set_object(object);
|
2019-10-15 19:00:17 +00:00
|
|
|
*message_.mutable_instruction_to_insert_before() =
|
|
|
|
instruction_to_insert_before;
|
2019-08-05 17:00:13 +00:00
|
|
|
message_.set_fresh_id(fresh_id);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool TransformationCopyObject::IsApplicable(
|
|
|
|
opt::IRContext* context, const FactManager& /*fact_manager*/) const {
|
|
|
|
if (!fuzzerutil::IsFreshId(context, message_.fresh_id())) {
|
|
|
|
// We require the id for the object copy to be unused.
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
// The id of the object to be copied must exist
|
|
|
|
auto object_inst = context->get_def_use_mgr()->GetDef(message_.object());
|
|
|
|
if (!object_inst) {
|
|
|
|
return false;
|
|
|
|
}
|
2019-10-08 13:04:10 +00:00
|
|
|
if (!fuzzerutil::CanMakeSynonymOf(context, object_inst)) {
|
2019-08-05 17:00:13 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-10-15 19:00:17 +00:00
|
|
|
auto insert_before =
|
|
|
|
FindInstruction(message_.instruction_to_insert_before(), context);
|
|
|
|
if (!insert_before) {
|
|
|
|
// The instruction before which the copy should be inserted was not found.
|
2019-08-05 17:00:13 +00:00
|
|
|
return false;
|
|
|
|
}
|
2019-09-11 22:45:20 +00:00
|
|
|
|
2019-10-08 13:04:10 +00:00
|
|
|
if (!fuzzerutil::CanInsertOpcodeBeforeInstruction(SpvOpCopyObject,
|
|
|
|
insert_before)) {
|
2019-08-05 17:00:13 +00:00
|
|
|
return false;
|
|
|
|
}
|
2019-09-11 22:45:20 +00:00
|
|
|
|
2019-08-05 17:00:13 +00:00
|
|
|
// |message_object| must be available at the point where we want to add the
|
|
|
|
// copy. It is available if it is at global scope (in which case it has no
|
|
|
|
// block), or if it dominates the point of insertion but is different from the
|
|
|
|
// point of insertion.
|
|
|
|
//
|
|
|
|
// The reason why the object needs to be different from the insertion point is
|
|
|
|
// that the copy will be added *before* this point, and we do not want to
|
|
|
|
// insert it before the object's defining instruction.
|
|
|
|
return !context->get_instr_block(object_inst) ||
|
|
|
|
(object_inst != &*insert_before &&
|
2019-10-15 19:00:17 +00:00
|
|
|
context
|
|
|
|
->GetDominatorAnalysis(
|
|
|
|
context->get_instr_block(insert_before)->GetParent())
|
2019-08-05 17:00:13 +00:00
|
|
|
->Dominates(object_inst, &*insert_before));
|
|
|
|
}
|
|
|
|
|
|
|
|
void TransformationCopyObject::Apply(opt::IRContext* context,
|
|
|
|
FactManager* fact_manager) const {
|
|
|
|
auto object_inst = context->get_def_use_mgr()->GetDef(message_.object());
|
|
|
|
assert(object_inst && "The object to be copied must exist.");
|
2019-10-15 19:00:17 +00:00
|
|
|
auto insert_before_inst =
|
|
|
|
FindInstruction(message_.instruction_to_insert_before(), context);
|
|
|
|
auto destination_block = context->get_instr_block(insert_before_inst);
|
2019-08-05 17:00:13 +00:00
|
|
|
assert(destination_block && "The base instruction must be in a block.");
|
2019-10-15 19:00:17 +00:00
|
|
|
auto insert_before = fuzzerutil::GetIteratorForInstruction(
|
|
|
|
destination_block, insert_before_inst);
|
2019-08-05 17:00:13 +00:00
|
|
|
assert(insert_before != destination_block->end() &&
|
|
|
|
"There must be an instruction before which the copy can be inserted.");
|
|
|
|
|
|
|
|
opt::Instruction::OperandList operands = {
|
|
|
|
{SPV_OPERAND_TYPE_ID, {message_.object()}}};
|
|
|
|
insert_before->InsertBefore(MakeUnique<opt::Instruction>(
|
|
|
|
context, SpvOp::SpvOpCopyObject, object_inst->type_id(),
|
|
|
|
message_.fresh_id(), operands));
|
|
|
|
|
|
|
|
fuzzerutil::UpdateModuleIdBound(context, message_.fresh_id());
|
|
|
|
context->InvalidateAnalysesExceptFor(opt::IRContext::Analysis::kAnalysisNone);
|
|
|
|
|
2019-11-01 17:50:01 +00:00
|
|
|
fact_manager->AddFactDataSynonym(MakeDataDescriptor(message_.object(), {}),
|
|
|
|
MakeDataDescriptor(message_.fresh_id(), {}),
|
|
|
|
context);
|
2019-08-05 17:00:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
protobufs::Transformation TransformationCopyObject::ToMessage() const {
|
|
|
|
protobufs::Transformation result;
|
|
|
|
*result.mutable_copy_object() = message_;
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace fuzz
|
|
|
|
} // namespace spvtools
|