| 1 |
// Copyright (c) 2020 Google LLC |
| 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_stores.h" |
| 16 |
|
| 17 |
#include "source/fuzz/fuzzer_util.h" |
| 18 |
#include "source/fuzz/transformation_store.h" |
| 19 |
|
| 20 |
namespace spvtools { |
| 21 |
namespace fuzz { |
| 22 |
|
| 23 |
FuzzerPassAddStores::FuzzerPassAddStores( |
| 24 |
opt::IRContext* ir_context, TransformationContext* transformation_context, |
| 25 |
FuzzerContext* fuzzer_context, |
| 26 |
protobufs::TransformationSequence* transformations) |
| 27 |
: FuzzerPass(ir_context, transformation_context, fuzzer_context, |
| 28 |
transformations) {} |
| 29 |
|
| 30 |
FuzzerPassAddStores::~FuzzerPassAddStores() = default; |
| 31 |
|
| 32 |
void FuzzerPassAddStores::Apply() { |
| 33 |
ForEachInstructionWithInstructionDescriptor( |
| 34 |
[this](opt::Function* function, opt::BasicBlock* block, |
| 35 |
opt::BasicBlock::iterator inst_it, |
| 36 |
const protobufs::InstructionDescriptor& instruction_descriptor) |
| 37 |
-> void { |
| 38 |
assert(inst_it->opcode() == |
| 39 |
instruction_descriptor.target_instruction_opcode() && |
| 40 |
"The opcode of the instruction we might insert before must be " |
| 41 |
"the same as the opcode in the descriptor for the instruction"); |
| 42 |
|
| 43 |
// Check whether it is legitimate to insert a store before this |
| 44 |
// instruction. |
| 45 |
if (!fuzzerutil::CanInsertOpcodeBeforeInstruction(SpvOpStore, |
| 46 |
inst_it)) { |
| 47 |
return; |
| 48 |
} |
| 49 |
|
| 50 |
// Randomly decide whether to try inserting a store here. |
| 51 |
if (!GetFuzzerContext()->ChoosePercentage( |
| 52 |
GetFuzzerContext()->GetChanceOfAddingStore())) { |
| 53 |
return; |
| 54 |
} |
| 55 |
|
| 56 |
// Look for pointers we might consider storing to. |
| 57 |
std::vector<opt::Instruction*> relevant_pointers = |
| 58 |
FindAvailableInstructions( |
| 59 |
function, block, inst_it, |
| 60 |
[this, block](opt::IRContext* context, |
| 61 |
opt::Instruction* instruction) -> bool { |
| 62 |
if (!instruction->result_id() || !instruction->type_id()) { |
| 63 |
return false; |
| 64 |
} |
| 65 |
auto type_inst = context->get_def_use_mgr()->GetDef( |
| 66 |
instruction->type_id()); |
| 67 |
if (type_inst->opcode() != SpvOpTypePointer) { |
| 68 |
// Not a pointer. |
| 69 |
return false; |
| 70 |
} |
| 71 |
if (instruction->IsReadOnlyPointer()) { |
| 72 |
// Read only: cannot store to it. |
| 73 |
return false; |
| 74 |
} |
| 75 |
switch (instruction->opcode()) { |
| 76 |
case SpvOpConstantNull: |
| 77 |
case SpvOpUndef: |
| 78 |
// Do not allow storing to a null or undefined pointer; |
| 79 |
// this might be OK if the block is dead, but for now we |
| 80 |
// conservatively avoid it. |
| 81 |
return false; |
| 82 |
default: |
| 83 |
break; |
| 84 |
} |
| 85 |
return GetTransformationContext() |
| 86 |
->GetFactManager() |
| 87 |
->BlockIsDead(block->id()) || |
| 88 |
GetTransformationContext() |
| 89 |
->GetFactManager() |
| 90 |
->PointeeValueIsIrrelevant( |
| 91 |
instruction->result_id()); |
| 92 |
}); |
| 93 |
|
| 94 |
// At this point, |relevant_pointers| contains all the pointers we might |
| 95 |
// think of storing to. |
| 96 |
if (relevant_pointers.empty()) { |
| 97 |
return; |
| 98 |
} |
| 99 |
|
| 100 |
auto pointer = relevant_pointers[GetFuzzerContext()->RandomIndex( |
| 101 |
relevant_pointers)]; |
| 102 |
|
| 103 |
std::vector<opt::Instruction*> relevant_values = |
| 104 |
FindAvailableInstructions( |
| 105 |
function, block, inst_it, |
| 106 |
[pointer](opt::IRContext* context, |
| 107 |
opt::Instruction* instruction) -> bool { |
| 108 |
if (!instruction->result_id() || !instruction->type_id()) { |
| 109 |
return false; |
| 110 |
} |
| 111 |
return instruction->type_id() == |
| 112 |
context->get_def_use_mgr() |
| 113 |
->GetDef(pointer->type_id()) |
| 114 |
->GetSingleWordInOperand(1); |
| 115 |
}); |
| 116 |
|
| 117 |
if (relevant_values.empty()) { |
| 118 |
return; |
| 119 |
} |
| 120 |
|
| 121 |
// Choose a value at random, and create and apply a storing |
| 122 |
// transformation based on it and the pointer. |
| 123 |
ApplyTransformation(TransformationStore( |
| 124 |
pointer->result_id(), |
| 125 |
relevant_values[GetFuzzerContext()->RandomIndex(relevant_values)] |
| 126 |
->result_id(), |
| 127 |
instruction_descriptor)); |
| 128 |
}); |
| 129 |
} |
| 130 |
|
| 131 |
} // namespace fuzz |
| 132 |
} // namespace spvtools |