2020-02-06 16:54:34 +00:00
|
|
|
// Copyright (c) 2020 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_add_stores.h"
|
|
|
|
|
|
|
|
#include "source/fuzz/fuzzer_util.h"
|
|
|
|
#include "source/fuzz/transformation_store.h"
|
|
|
|
|
|
|
|
namespace spvtools {
|
|
|
|
namespace fuzz {
|
|
|
|
|
|
|
|
FuzzerPassAddStores::FuzzerPassAddStores(
|
2020-04-02 14:54:46 +00:00
|
|
|
opt::IRContext* ir_context, TransformationContext* transformation_context,
|
2020-02-06 16:54:34 +00:00
|
|
|
FuzzerContext* fuzzer_context,
|
2021-07-28 21:59:37 +00:00
|
|
|
protobufs::TransformationSequence* transformations,
|
|
|
|
bool ignore_inapplicable_transformations)
|
2020-04-02 14:54:46 +00:00
|
|
|
: FuzzerPass(ir_context, transformation_context, fuzzer_context,
|
2021-07-28 21:59:37 +00:00
|
|
|
transformations, ignore_inapplicable_transformations) {}
|
2020-02-06 16:54:34 +00:00
|
|
|
|
|
|
|
void FuzzerPassAddStores::Apply() {
|
2020-03-06 12:25:57 +00:00
|
|
|
ForEachInstructionWithInstructionDescriptor(
|
2020-02-10 23:22:34 +00:00
|
|
|
[this](opt::Function* function, opt::BasicBlock* block,
|
2020-02-06 16:54:34 +00:00
|
|
|
opt::BasicBlock::iterator inst_it,
|
|
|
|
const protobufs::InstructionDescriptor& instruction_descriptor)
|
|
|
|
-> void {
|
2022-11-04 21:27:10 +00:00
|
|
|
assert(
|
|
|
|
inst_it->opcode() ==
|
|
|
|
spv::Op(instruction_descriptor.target_instruction_opcode()) &&
|
|
|
|
"The opcode of the instruction we might insert before must be "
|
|
|
|
"the same as the opcode in the descriptor for the instruction");
|
2020-02-06 16:54:34 +00:00
|
|
|
|
2021-08-05 13:08:44 +00:00
|
|
|
// Randomly decide whether to try inserting a store here.
|
|
|
|
if (!GetFuzzerContext()->ChoosePercentage(
|
|
|
|
GetFuzzerContext()->GetChanceOfAddingStore())) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-02-06 16:54:34 +00:00
|
|
|
// Check whether it is legitimate to insert a store before this
|
|
|
|
// instruction.
|
2022-11-04 21:27:10 +00:00
|
|
|
if (!fuzzerutil::CanInsertOpcodeBeforeInstruction(spv::Op::OpStore,
|
2020-02-06 16:54:34 +00:00
|
|
|
inst_it)) {
|
|
|
|
return;
|
|
|
|
}
|
2022-11-04 21:27:10 +00:00
|
|
|
if (!fuzzerutil::CanInsertOpcodeBeforeInstruction(
|
|
|
|
spv::Op::OpAtomicStore, inst_it)) {
|
2020-02-06 16:54:34 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Look for pointers we might consider storing to.
|
|
|
|
std::vector<opt::Instruction*> relevant_pointers =
|
|
|
|
FindAvailableInstructions(
|
|
|
|
function, block, inst_it,
|
|
|
|
[this, block](opt::IRContext* context,
|
|
|
|
opt::Instruction* instruction) -> bool {
|
|
|
|
if (!instruction->result_id() || !instruction->type_id()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
auto type_inst = context->get_def_use_mgr()->GetDef(
|
|
|
|
instruction->type_id());
|
2022-11-04 21:27:10 +00:00
|
|
|
if (type_inst->opcode() != spv::Op::OpTypePointer) {
|
2020-02-06 16:54:34 +00:00
|
|
|
// Not a pointer.
|
|
|
|
return false;
|
|
|
|
}
|
2020-05-01 01:50:44 +00:00
|
|
|
if (instruction->IsReadOnlyPointer()) {
|
|
|
|
// Read only: cannot store to it.
|
2020-02-06 16:54:34 +00:00
|
|
|
return false;
|
|
|
|
}
|
2020-05-26 11:28:02 +00:00
|
|
|
switch (instruction->opcode()) {
|
2022-11-04 21:27:10 +00:00
|
|
|
case spv::Op::OpConstantNull:
|
|
|
|
case spv::Op::OpUndef:
|
2020-02-06 16:54:34 +00:00
|
|
|
// Do not allow storing to a null or undefined pointer;
|
|
|
|
// this might be OK if the block is dead, but for now we
|
|
|
|
// conservatively avoid it.
|
|
|
|
return false;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
2020-04-02 14:54:46 +00:00
|
|
|
return GetTransformationContext()
|
|
|
|
->GetFactManager()
|
|
|
|
->BlockIsDead(block->id()) ||
|
|
|
|
GetTransformationContext()
|
|
|
|
->GetFactManager()
|
|
|
|
->PointeeValueIsIrrelevant(
|
|
|
|
instruction->result_id());
|
2020-02-06 16:54:34 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
// At this point, |relevant_pointers| contains all the pointers we might
|
|
|
|
// think of storing to.
|
|
|
|
if (relevant_pointers.empty()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto pointer = relevant_pointers[GetFuzzerContext()->RandomIndex(
|
|
|
|
relevant_pointers)];
|
|
|
|
|
|
|
|
std::vector<opt::Instruction*> relevant_values =
|
|
|
|
FindAvailableInstructions(
|
|
|
|
function, block, inst_it,
|
|
|
|
[pointer](opt::IRContext* context,
|
|
|
|
opt::Instruction* instruction) -> bool {
|
|
|
|
if (!instruction->result_id() || !instruction->type_id()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return instruction->type_id() ==
|
|
|
|
context->get_def_use_mgr()
|
|
|
|
->GetDef(pointer->type_id())
|
|
|
|
->GetSingleWordInOperand(1);
|
|
|
|
});
|
|
|
|
|
|
|
|
if (relevant_values.empty()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-08-05 13:08:44 +00:00
|
|
|
bool is_atomic_store = false;
|
|
|
|
uint32_t memory_scope_id = 0;
|
|
|
|
uint32_t memory_semantics_id = 0;
|
|
|
|
|
|
|
|
auto storage_class =
|
2022-11-04 21:27:10 +00:00
|
|
|
static_cast<spv::StorageClass>(GetIRContext()
|
|
|
|
->get_def_use_mgr()
|
|
|
|
->GetDef(pointer->type_id())
|
|
|
|
->GetSingleWordInOperand(0));
|
2021-08-05 13:08:44 +00:00
|
|
|
|
|
|
|
switch (storage_class) {
|
2022-11-04 21:27:10 +00:00
|
|
|
case spv::StorageClass::StorageBuffer:
|
|
|
|
case spv::StorageClass::PhysicalStorageBuffer:
|
|
|
|
case spv::StorageClass::Workgroup:
|
|
|
|
case spv::StorageClass::CrossWorkgroup:
|
|
|
|
case spv::StorageClass::AtomicCounter:
|
|
|
|
case spv::StorageClass::Image:
|
2021-08-05 13:08:44 +00:00
|
|
|
if (GetFuzzerContext()->ChoosePercentage(
|
|
|
|
GetFuzzerContext()->GetChanceOfAddingAtomicStore())) {
|
|
|
|
is_atomic_store = true;
|
|
|
|
|
|
|
|
memory_scope_id = FindOrCreateConstant(
|
2022-11-04 21:27:10 +00:00
|
|
|
{uint32_t(spv::Scope::Invocation)},
|
2021-08-05 13:08:44 +00:00
|
|
|
FindOrCreateIntegerType(32, GetFuzzerContext()->ChooseEven()),
|
|
|
|
false);
|
|
|
|
|
|
|
|
memory_semantics_id = FindOrCreateConstant(
|
|
|
|
{static_cast<uint32_t>(
|
|
|
|
fuzzerutil::GetMemorySemanticsForStorageClass(
|
|
|
|
storage_class))},
|
|
|
|
FindOrCreateIntegerType(32, GetFuzzerContext()->ChooseEven()),
|
|
|
|
false);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create and apply the transformation.
|
2020-02-06 16:54:34 +00:00
|
|
|
ApplyTransformation(TransformationStore(
|
2021-08-05 13:08:44 +00:00
|
|
|
pointer->result_id(), is_atomic_store, memory_scope_id,
|
|
|
|
memory_semantics_id,
|
2020-02-06 16:54:34 +00:00
|
|
|
relevant_values[GetFuzzerContext()->RandomIndex(relevant_values)]
|
|
|
|
->result_id(),
|
|
|
|
instruction_descriptor));
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace fuzz
|
|
|
|
} // namespace spvtools
|