2020-10-23 13:49:50 +00:00
|
|
|
// Copyright (c) 2020 Vasyl Teliman
|
|
|
|
//
|
|
|
|
// 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_add_composite_extract.h"
|
|
|
|
|
2021-03-20 18:51:18 +00:00
|
|
|
#include "source/fuzz/available_instructions.h"
|
2020-10-23 13:49:50 +00:00
|
|
|
#include "source/fuzz/fuzzer_context.h"
|
|
|
|
#include "source/fuzz/fuzzer_util.h"
|
|
|
|
#include "source/fuzz/instruction_descriptor.h"
|
|
|
|
#include "source/fuzz/transformation_composite_extract.h"
|
|
|
|
|
|
|
|
namespace spvtools {
|
|
|
|
namespace fuzz {
|
|
|
|
|
|
|
|
FuzzerPassAddCompositeExtract::FuzzerPassAddCompositeExtract(
|
|
|
|
opt::IRContext* ir_context, TransformationContext* transformation_context,
|
|
|
|
FuzzerContext* fuzzer_context,
|
2021-07-28 21:59:37 +00:00
|
|
|
protobufs::TransformationSequence* transformations,
|
|
|
|
bool ignore_inapplicable_transformations)
|
2020-10-23 13:49:50 +00:00
|
|
|
: FuzzerPass(ir_context, transformation_context, fuzzer_context,
|
2021-07-28 21:59:37 +00:00
|
|
|
transformations, ignore_inapplicable_transformations) {}
|
2020-10-23 13:49:50 +00:00
|
|
|
|
|
|
|
void FuzzerPassAddCompositeExtract::Apply() {
|
|
|
|
std::vector<const protobufs::DataDescriptor*> composite_synonyms;
|
|
|
|
for (const auto* dd :
|
|
|
|
GetTransformationContext()->GetFactManager()->GetAllSynonyms()) {
|
|
|
|
// |dd| must describe a component of a composite.
|
|
|
|
if (!dd->index().empty()) {
|
|
|
|
composite_synonyms.push_back(dd);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-20 18:51:18 +00:00
|
|
|
AvailableInstructions available_composites(
|
|
|
|
GetIRContext(), [](opt::IRContext* ir_context, opt::Instruction* inst) {
|
|
|
|
return inst->type_id() && inst->result_id() &&
|
|
|
|
fuzzerutil::IsCompositeType(
|
|
|
|
ir_context->get_type_mgr()->GetType(inst->type_id()));
|
|
|
|
});
|
2020-10-23 13:49:50 +00:00
|
|
|
|
|
|
|
ForEachInstructionWithInstructionDescriptor(
|
2021-03-20 18:51:18 +00:00
|
|
|
[this, &available_composites, &composite_synonyms](
|
|
|
|
opt::Function* /*unused*/, opt::BasicBlock* /*unused*/,
|
2020-10-23 13:49:50 +00:00
|
|
|
opt::BasicBlock::iterator inst_it,
|
|
|
|
const protobufs::InstructionDescriptor& instruction_descriptor) {
|
2022-11-04 21:27:10 +00:00
|
|
|
if (!fuzzerutil::CanInsertOpcodeBeforeInstruction(
|
|
|
|
spv::Op::OpCompositeExtract, inst_it)) {
|
2020-10-23 13:49:50 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!GetFuzzerContext()->ChoosePercentage(
|
|
|
|
GetFuzzerContext()->GetChanceOfAddingCompositeExtract())) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<const protobufs::DataDescriptor*> available_synonyms;
|
|
|
|
for (const auto* dd : composite_synonyms) {
|
|
|
|
if (fuzzerutil::IdIsAvailableBeforeInstruction(
|
|
|
|
GetIRContext(), &*inst_it, dd->object())) {
|
|
|
|
available_synonyms.push_back(dd);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-20 18:51:18 +00:00
|
|
|
auto candidate_composites =
|
|
|
|
available_composites.GetAvailableBeforeInstruction(&*inst_it);
|
|
|
|
|
|
|
|
if (available_synonyms.empty() && candidate_composites.empty()) {
|
2020-10-23 13:49:50 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t composite_id = 0;
|
|
|
|
std::vector<uint32_t> indices;
|
|
|
|
|
2021-03-20 18:51:18 +00:00
|
|
|
if (available_synonyms.empty() || (!candidate_composites.empty() &&
|
2020-10-23 13:49:50 +00:00
|
|
|
GetFuzzerContext()->ChooseEven())) {
|
|
|
|
const auto* inst =
|
2021-03-20 18:51:18 +00:00
|
|
|
candidate_composites[GetFuzzerContext()->RandomIndex(
|
|
|
|
candidate_composites)];
|
2020-10-23 13:49:50 +00:00
|
|
|
composite_id = inst->result_id();
|
|
|
|
|
2020-11-03 15:09:36 +00:00
|
|
|
auto type_id = inst->type_id();
|
2020-10-23 13:49:50 +00:00
|
|
|
do {
|
|
|
|
uint32_t number_of_members = 0;
|
|
|
|
|
2020-11-03 15:09:36 +00:00
|
|
|
const auto* type_inst =
|
|
|
|
GetIRContext()->get_def_use_mgr()->GetDef(type_id);
|
|
|
|
assert(type_inst && "Composite instruction has invalid type id");
|
|
|
|
|
|
|
|
switch (type_inst->opcode()) {
|
2022-11-04 21:27:10 +00:00
|
|
|
case spv::Op::OpTypeArray:
|
2020-11-03 15:09:36 +00:00
|
|
|
number_of_members =
|
|
|
|
fuzzerutil::GetArraySize(*type_inst, GetIRContext());
|
|
|
|
break;
|
2022-11-04 21:27:10 +00:00
|
|
|
case spv::Op::OpTypeVector:
|
|
|
|
case spv::Op::OpTypeMatrix:
|
2020-11-03 15:09:36 +00:00
|
|
|
number_of_members = type_inst->GetSingleWordInOperand(1);
|
|
|
|
break;
|
2022-11-04 21:27:10 +00:00
|
|
|
case spv::Op::OpTypeStruct:
|
2020-11-03 15:09:36 +00:00
|
|
|
number_of_members = type_inst->NumInOperands();
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
assert(false && "|type_inst| is not a composite");
|
|
|
|
return;
|
2020-10-23 13:49:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (number_of_members == 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
indices.push_back(
|
|
|
|
GetFuzzerContext()->GetRandomCompositeExtractIndex(
|
|
|
|
number_of_members));
|
|
|
|
|
2020-11-03 15:09:36 +00:00
|
|
|
switch (type_inst->opcode()) {
|
2022-11-04 21:27:10 +00:00
|
|
|
case spv::Op::OpTypeArray:
|
|
|
|
case spv::Op::OpTypeVector:
|
|
|
|
case spv::Op::OpTypeMatrix:
|
2020-11-03 15:09:36 +00:00
|
|
|
type_id = type_inst->GetSingleWordInOperand(0);
|
|
|
|
break;
|
2022-11-04 21:27:10 +00:00
|
|
|
case spv::Op::OpTypeStruct:
|
2020-11-03 15:09:36 +00:00
|
|
|
type_id = type_inst->GetSingleWordInOperand(indices.back());
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
assert(false && "|type_inst| is not a composite");
|
|
|
|
return;
|
2020-10-23 13:49:50 +00:00
|
|
|
}
|
2020-11-03 15:09:36 +00:00
|
|
|
} while (fuzzerutil::IsCompositeType(
|
|
|
|
GetIRContext()->get_type_mgr()->GetType(type_id)) &&
|
2020-10-23 13:49:50 +00:00
|
|
|
GetFuzzerContext()->ChoosePercentage(
|
|
|
|
GetFuzzerContext()
|
|
|
|
->GetChanceOfGoingDeeperToExtractComposite()));
|
|
|
|
} else {
|
|
|
|
const auto* dd = available_synonyms[GetFuzzerContext()->RandomIndex(
|
|
|
|
available_synonyms)];
|
|
|
|
|
|
|
|
composite_id = dd->object();
|
|
|
|
indices.assign(dd->index().begin(), dd->index().end());
|
|
|
|
}
|
|
|
|
|
|
|
|
assert(composite_id != 0 && !indices.empty() &&
|
|
|
|
"Composite object should have been chosen correctly");
|
|
|
|
|
2021-03-20 18:51:18 +00:00
|
|
|
ApplyTransformation(TransformationCompositeExtract(
|
|
|
|
instruction_descriptor, GetFuzzerContext()->GetFreshId(),
|
|
|
|
composite_id, indices));
|
2020-10-23 13:49:50 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace fuzz
|
|
|
|
} // namespace spvtools
|