2019-09-18 19:47:08 +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/fuzzer_pass_apply_id_synonyms.h"
|
|
|
|
|
2019-11-07 16:19:06 +00:00
|
|
|
#include "source/fuzz/data_descriptor.h"
|
|
|
|
#include "source/fuzz/fuzzer_util.h"
|
2019-09-18 19:47:08 +00:00
|
|
|
#include "source/fuzz/id_use_descriptor.h"
|
2019-11-07 16:19:06 +00:00
|
|
|
#include "source/fuzz/instruction_descriptor.h"
|
|
|
|
#include "source/fuzz/transformation_composite_extract.h"
|
2020-04-20 18:02:49 +00:00
|
|
|
#include "source/fuzz/transformation_compute_data_synonym_fact_closure.h"
|
2019-09-18 19:47:08 +00:00
|
|
|
#include "source/fuzz/transformation_replace_id_with_synonym.h"
|
|
|
|
|
|
|
|
namespace spvtools {
|
|
|
|
namespace fuzz {
|
|
|
|
|
|
|
|
FuzzerPassApplyIdSynonyms::FuzzerPassApplyIdSynonyms(
|
2020-04-02 14:54:46 +00:00
|
|
|
opt::IRContext* ir_context, TransformationContext* transformation_context,
|
2019-09-18 19:47:08 +00:00
|
|
|
FuzzerContext* fuzzer_context,
|
|
|
|
protobufs::TransformationSequence* transformations)
|
2020-04-02 14:54:46 +00:00
|
|
|
: FuzzerPass(ir_context, transformation_context, fuzzer_context,
|
|
|
|
transformations) {}
|
2019-09-18 19:47:08 +00:00
|
|
|
|
|
|
|
FuzzerPassApplyIdSynonyms::~FuzzerPassApplyIdSynonyms() = default;
|
|
|
|
|
|
|
|
void FuzzerPassApplyIdSynonyms::Apply() {
|
2020-04-20 18:02:49 +00:00
|
|
|
// Compute a closure of data synonym facts, to enrich the pool of synonyms
|
|
|
|
// that are available.
|
|
|
|
ApplyTransformation(TransformationComputeDataSynonymFactClosure(
|
|
|
|
GetFuzzerContext()
|
|
|
|
->GetMaximumEquivalenceClassSizeForDataSynonymFactClosure()));
|
|
|
|
|
|
|
|
for (auto id_with_known_synonyms : GetTransformationContext()
|
|
|
|
->GetFactManager()
|
|
|
|
->GetIdsForWhichSynonymsAreKnown()) {
|
2020-04-02 16:35:00 +00:00
|
|
|
// Gather up all uses of |id_with_known_synonym| as a regular id, and
|
|
|
|
// subsequently iterate over these uses. We use this separation because,
|
|
|
|
// when considering a given use, we might apply a transformation that will
|
2019-11-07 16:19:06 +00:00
|
|
|
// invalidate the def-use manager.
|
|
|
|
std::vector<std::pair<opt::Instruction*, uint32_t>> uses;
|
2019-09-18 19:47:08 +00:00
|
|
|
GetIRContext()->get_def_use_mgr()->ForEachUse(
|
|
|
|
id_with_known_synonyms,
|
2019-11-07 16:19:06 +00:00
|
|
|
[&uses](opt::Instruction* use_inst, uint32_t use_index) -> void {
|
2020-04-02 16:35:00 +00:00
|
|
|
// We only gather up regular id uses; e.g. we do not include a use of
|
|
|
|
// the id as the scope for an atomic operation.
|
|
|
|
if (use_inst->GetOperand(use_index).type == SPV_OPERAND_TYPE_ID) {
|
|
|
|
uses.emplace_back(
|
|
|
|
std::pair<opt::Instruction*, uint32_t>(use_inst, use_index));
|
|
|
|
}
|
2019-09-18 19:47:08 +00:00
|
|
|
});
|
|
|
|
|
2019-11-07 16:19:06 +00:00
|
|
|
for (auto& use : uses) {
|
|
|
|
auto use_inst = use.first;
|
|
|
|
auto use_index = use.second;
|
|
|
|
auto block_containing_use = GetIRContext()->get_instr_block(use_inst);
|
|
|
|
// The use might not be in a block; e.g. it could be a decoration.
|
|
|
|
if (!block_containing_use) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (!GetFuzzerContext()->ChoosePercentage(
|
|
|
|
GetFuzzerContext()->GetChanceOfReplacingIdWithSynonym())) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
// |use_index| is the absolute index of the operand. We require
|
|
|
|
// the index of the operand restricted to input operands only, so
|
|
|
|
// we subtract the number of non-input operands from |use_index|.
|
|
|
|
uint32_t use_in_operand_index =
|
|
|
|
use_index - use_inst->NumOperands() + use_inst->NumInOperands();
|
|
|
|
if (!TransformationReplaceIdWithSynonym::UseCanBeReplacedWithSynonym(
|
|
|
|
GetIRContext(), use_inst, use_in_operand_index)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<const protobufs::DataDescriptor*> synonyms_to_try;
|
2020-04-02 14:54:46 +00:00
|
|
|
for (auto& data_descriptor :
|
|
|
|
GetTransformationContext()->GetFactManager()->GetSynonymsForId(
|
2020-04-20 18:02:49 +00:00
|
|
|
id_with_known_synonyms)) {
|
2019-11-07 16:19:06 +00:00
|
|
|
protobufs::DataDescriptor descriptor_for_this_id =
|
|
|
|
MakeDataDescriptor(id_with_known_synonyms, {});
|
|
|
|
if (DataDescriptorEquals()(data_descriptor, &descriptor_for_this_id)) {
|
|
|
|
// Exclude the fact that the id is synonymous with itself.
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
synonyms_to_try.push_back(data_descriptor);
|
|
|
|
}
|
|
|
|
while (!synonyms_to_try.empty()) {
|
2020-03-09 16:16:18 +00:00
|
|
|
auto synonym_to_try =
|
|
|
|
GetFuzzerContext()->RemoveAtRandomIndex(&synonyms_to_try);
|
2019-11-07 16:19:06 +00:00
|
|
|
|
2020-03-04 14:54:08 +00:00
|
|
|
// If the synonym's |index_size| is zero, the synonym represents an id.
|
|
|
|
// Otherwise it represents some element of a composite structure, in
|
|
|
|
// which case we need to be able to add an extract instruction to get
|
|
|
|
// that element out.
|
2019-11-07 16:19:06 +00:00
|
|
|
if (synonym_to_try->index_size() > 0 &&
|
2020-03-04 14:54:08 +00:00
|
|
|
!fuzzerutil::CanInsertOpcodeBeforeInstruction(SpvOpCompositeExtract,
|
2020-03-09 16:16:18 +00:00
|
|
|
use_inst) &&
|
|
|
|
use_inst->opcode() != SpvOpPhi) {
|
2020-03-04 14:54:08 +00:00
|
|
|
// We cannot insert an extract before this instruction, so this
|
|
|
|
// synonym is no good.
|
2019-11-07 16:19:06 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2020-02-06 16:54:34 +00:00
|
|
|
if (!fuzzerutil::IdIsAvailableAtUse(GetIRContext(), use_inst,
|
|
|
|
use_in_operand_index,
|
|
|
|
synonym_to_try->object())) {
|
2019-11-07 16:19:06 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2020-03-04 14:54:08 +00:00
|
|
|
// We either replace the use with an id known to be synonymous (when
|
|
|
|
// the synonym's |index_size| is 0), or an id that will hold the result
|
|
|
|
// of extracting a synonym from a composite (when the synonym's
|
|
|
|
// |index_size| is > 0).
|
2019-11-07 16:19:06 +00:00
|
|
|
uint32_t id_with_which_to_replace_use;
|
|
|
|
if (synonym_to_try->index_size() == 0) {
|
|
|
|
id_with_which_to_replace_use = synonym_to_try->object();
|
|
|
|
} else {
|
|
|
|
id_with_which_to_replace_use = GetFuzzerContext()->GetFreshId();
|
2020-03-09 16:16:18 +00:00
|
|
|
opt::Instruction* instruction_to_insert_before = nullptr;
|
|
|
|
|
|
|
|
if (use_inst->opcode() != SpvOpPhi) {
|
|
|
|
instruction_to_insert_before = use_inst;
|
|
|
|
} else {
|
|
|
|
auto parent_block_id =
|
|
|
|
use_inst->GetSingleWordInOperand(use_in_operand_index + 1);
|
|
|
|
auto parent_block_instruction =
|
|
|
|
GetIRContext()->get_def_use_mgr()->GetDef(parent_block_id);
|
|
|
|
auto parent_block =
|
|
|
|
GetIRContext()->get_instr_block(parent_block_instruction);
|
|
|
|
|
|
|
|
instruction_to_insert_before = parent_block->GetMergeInst()
|
|
|
|
? parent_block->GetMergeInst()
|
|
|
|
: parent_block->terminator();
|
|
|
|
}
|
|
|
|
|
2020-03-04 14:54:08 +00:00
|
|
|
ApplyTransformation(TransformationCompositeExtract(
|
2020-03-09 16:16:18 +00:00
|
|
|
MakeInstructionDescriptor(GetIRContext(),
|
|
|
|
instruction_to_insert_before),
|
2020-03-04 14:54:08 +00:00
|
|
|
id_with_which_to_replace_use, synonym_to_try->object(),
|
|
|
|
fuzzerutil::RepeatedFieldToVector(synonym_to_try->index())));
|
2019-11-07 16:19:06 +00:00
|
|
|
}
|
|
|
|
|
2020-03-04 14:54:08 +00:00
|
|
|
ApplyTransformation(TransformationReplaceIdWithSynonym(
|
2019-11-07 16:19:06 +00:00
|
|
|
MakeIdUseDescriptorFromUse(GetIRContext(), use_inst,
|
|
|
|
use_in_operand_index),
|
2020-03-04 14:54:08 +00:00
|
|
|
id_with_which_to_replace_use));
|
2019-11-07 16:19:06 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2019-09-18 19:47:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace fuzz
|
|
|
|
} // namespace spvtools
|