| 1 |
// Copyright (c) 2020 Vasyl Teliman |
| 2 |
// |
| 3 |
// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 |
// you may not use this file except in compliance with the License. |
| 5 |
// You may obtain a copy of the License at |
| 6 |
// |
| 7 |
// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 |
// |
| 9 |
// Unless required by applicable law or agreed to in writing, software |
| 10 |
// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 |
// See the License for the specific language governing permissions and |
| 13 |
// limitations under the License. |
| 14 |
|
| 15 |
#include "source/fuzz/fuzzer_pass_add_copy_memory.h" |
| 16 |
|
| 17 |
#include "source/fuzz/fuzzer_context.h" |
| 18 |
#include "source/fuzz/fuzzer_util.h" |
| 19 |
#include "source/fuzz/instruction_descriptor.h" |
| 20 |
#include "source/fuzz/transformation_add_copy_memory.h" |
| 21 |
|
| 22 |
namespace spvtools { |
| 23 |
namespace fuzz { |
| 24 |
|
| 25 |
FuzzerPassAddCopyMemory::FuzzerPassAddCopyMemory( |
| 26 |
opt::IRContext* ir_context, TransformationContext* transformation_context, |
| 27 |
FuzzerContext* fuzzer_context, |
| 28 |
protobufs::TransformationSequence* transformations) |
| 29 |
: FuzzerPass(ir_context, transformation_context, fuzzer_context, |
| 30 |
transformations) {} |
| 31 |
|
| 32 |
FuzzerPassAddCopyMemory::~FuzzerPassAddCopyMemory() = default; |
| 33 |
|
| 34 |
void FuzzerPassAddCopyMemory::Apply() { |
| 35 |
ForEachInstructionWithInstructionDescriptor( |
| 36 |
[this](opt::Function* function, opt::BasicBlock* block, |
| 37 |
opt::BasicBlock::iterator inst_it, |
| 38 |
const protobufs::InstructionDescriptor& instruction_descriptor) { |
| 39 |
// Check that we can insert an OpCopyMemory before this instruction. |
| 40 |
if (!fuzzerutil::CanInsertOpcodeBeforeInstruction(SpvOpCopyMemory, |
| 41 |
inst_it)) { |
| 42 |
return; |
| 43 |
} |
| 44 |
|
| 45 |
if (!GetFuzzerContext()->ChoosePercentage( |
| 46 |
GetFuzzerContext()->GetChanceOfAddingCopyMemory())) { |
| 47 |
return; |
| 48 |
} |
| 49 |
|
| 50 |
// Get all instructions available before |inst_it| according to the |
| 51 |
// domination rules. |
| 52 |
auto instructions = FindAvailableInstructions( |
| 53 |
function, block, inst_it, |
| 54 |
TransformationAddCopyMemory::IsInstructionSupported); |
| 55 |
|
| 56 |
if (instructions.empty()) { |
| 57 |
return; |
| 58 |
} |
| 59 |
|
| 60 |
const auto* inst = |
| 61 |
instructions[GetFuzzerContext()->RandomIndex(instructions)]; |
| 62 |
|
| 63 |
// Decide whether to create global or local variable. |
| 64 |
auto storage_class = GetFuzzerContext()->ChooseEven() |
| 65 |
? SpvStorageClassPrivate |
| 66 |
: SpvStorageClassFunction; |
| 67 |
|
| 68 |
auto pointee_type_id = fuzzerutil::GetPointeeTypeIdFromPointerType( |
| 69 |
GetIRContext(), inst->type_id()); |
| 70 |
|
| 71 |
// Create a pointer type with |storage_class| if needed. |
| 72 |
FindOrCreatePointerType(pointee_type_id, storage_class); |
| 73 |
|
| 74 |
ApplyTransformation(TransformationAddCopyMemory( |
| 75 |
instruction_descriptor, GetFuzzerContext()->GetFreshId(), |
| 76 |
inst->result_id(), storage_class, |
| 77 |
FindOrCreateZeroConstant(pointee_type_id, false))); |
| 78 |
}); |
| 79 |
} |
| 80 |
|
| 81 |
} // namespace fuzz |
| 82 |
} // namespace spvtools |