| 1 |
// Copyright (c) 2020 André Perez Maselco |
| 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_vector_shuffle_instructions.h" |
| 16 |
|
| 17 |
#include "source/fuzz/fuzzer_util.h" |
| 18 |
#include "source/fuzz/instruction_descriptor.h" |
| 19 |
#include "source/fuzz/transformation_vector_shuffle.h" |
| 20 |
|
| 21 |
namespace spvtools { |
| 22 |
namespace fuzz { |
| 23 |
|
| 24 |
FuzzerPassAddVectorShuffleInstructions::FuzzerPassAddVectorShuffleInstructions( |
| 25 |
opt::IRContext* ir_context, TransformationContext* transformation_context, |
| 26 |
FuzzerContext* fuzzer_context, |
| 27 |
protobufs::TransformationSequence* transformations) |
| 28 |
: FuzzerPass(ir_context, transformation_context, fuzzer_context, |
| 29 |
transformations) {} |
| 30 |
|
| 31 |
FuzzerPassAddVectorShuffleInstructions:: |
| 32 |
~FuzzerPassAddVectorShuffleInstructions() = default; |
| 33 |
|
| 34 |
void FuzzerPassAddVectorShuffleInstructions::Apply() { |
| 35 |
ForEachInstructionWithInstructionDescriptor( |
| 36 |
[this](opt::Function* function, opt::BasicBlock* block, |
| 37 |
opt::BasicBlock::iterator instruction_iterator, |
| 38 |
const protobufs::InstructionDescriptor& instruction_descriptor) |
| 39 |
-> void { |
| 40 |
assert(instruction_iterator->opcode() == |
| 41 |
instruction_descriptor.target_instruction_opcode() && |
| 42 |
"The opcode of the instruction we might insert before must be " |
| 43 |
"the same as the opcode in the descriptor for the instruction"); |
| 44 |
|
| 45 |
// Randomly decide whether to try adding an OpVectorShuffle instruction. |
| 46 |
if (!GetFuzzerContext()->ChoosePercentage( |
| 47 |
GetFuzzerContext()->GetChanceOfAddingVectorShuffle())) { |
| 48 |
return; |
| 49 |
} |
| 50 |
|
| 51 |
// It must be valid to insert an OpVectorShuffle instruction |
| 52 |
// before |instruction_iterator|. |
| 53 |
if (!fuzzerutil::CanInsertOpcodeBeforeInstruction( |
| 54 |
SpvOpVectorShuffle, instruction_iterator)) { |
| 55 |
return; |
| 56 |
} |
| 57 |
|
| 58 |
// Looks for vectors that we might consider to use as OpVectorShuffle |
| 59 |
// operands. |
| 60 |
std::vector<opt::Instruction*> vector_instructions = |
| 61 |
FindAvailableInstructions( |
| 62 |
function, block, instruction_iterator, |
| 63 |
[this, instruction_descriptor]( |
| 64 |
opt::IRContext* ir_context, |
| 65 |
opt::Instruction* instruction) -> bool { |
| 66 |
if (!instruction->result_id() || !instruction->type_id()) { |
| 67 |
return false; |
| 68 |
} |
| 69 |
|
| 70 |
if (!ir_context->get_type_mgr() |
| 71 |
->GetType(instruction->type_id()) |
| 72 |
->AsVector()) { |
| 73 |
return false; |
| 74 |
} |
| 75 |
|
| 76 |
if (!GetTransformationContext() |
| 77 |
->GetFactManager() |
| 78 |
->IdIsIrrelevant(instruction->result_id()) && |
| 79 |
!fuzzerutil::CanMakeSynonymOf(ir_context, |
| 80 |
*GetTransformationContext(), |
| 81 |
instruction)) { |
| 82 |
// If the id is irrelevant, we can use it since it will not |
| 83 |
// participate in DataSynonym fact. Otherwise, we should be |
| 84 |
// able to produce a synonym out of the id. |
| 85 |
return false; |
| 86 |
} |
| 87 |
|
| 88 |
return fuzzerutil::IdIsAvailableBeforeInstruction( |
| 89 |
ir_context, |
| 90 |
FindInstruction(instruction_descriptor, ir_context), |
| 91 |
instruction->result_id()); |
| 92 |
}); |
| 93 |
|
| 94 |
// If there are no vector instructions, then return. |
| 95 |
if (vector_instructions.empty()) { |
| 96 |
return; |
| 97 |
} |
| 98 |
|
| 99 |
auto vector_1_instruction = |
| 100 |
vector_instructions[GetFuzzerContext()->RandomIndex( |
| 101 |
vector_instructions)]; |
| 102 |
auto vector_1_type = GetIRContext() |
| 103 |
->get_type_mgr() |
| 104 |
->GetType(vector_1_instruction->type_id()) |
| 105 |
->AsVector(); |
| 106 |
|
| 107 |
auto vector_2_instruction = |
| 108 |
GetFuzzerContext()->RemoveAtRandomIndex(&vector_instructions); |
| 109 |
auto vector_2_type = GetIRContext() |
| 110 |
->get_type_mgr() |
| 111 |
->GetType(vector_2_instruction->type_id()) |
| 112 |
->AsVector(); |
| 113 |
|
| 114 |
// |vector_1| and |vector_2| must have the same element type as each |
| 115 |
// other. The loop is guaranteed to terminate because each iteration |
| 116 |
// removes on possible choice for |vector_2|, and there is at least one |
| 117 |
// choice that will cause the loop to exit - namely |vector_1|. |
| 118 |
while (vector_1_type->element_type() != vector_2_type->element_type()) { |
| 119 |
vector_2_instruction = |
| 120 |
GetFuzzerContext()->RemoveAtRandomIndex(&vector_instructions); |
| 121 |
vector_2_type = GetIRContext() |
| 122 |
->get_type_mgr() |
| 123 |
->GetType(vector_2_instruction->type_id()) |
| 124 |
->AsVector(); |
| 125 |
} |
| 126 |
|
| 127 |
// Gets components and creates the appropriate result vector type. |
| 128 |
std::vector<uint32_t> components = |
| 129 |
GetFuzzerContext()->GetRandomComponentsForVectorShuffle( |
| 130 |
vector_1_type->element_count() + |
| 131 |
vector_2_type->element_count()); |
| 132 |
FindOrCreateVectorType(GetIRContext()->get_type_mgr()->GetId( |
| 133 |
vector_1_type->element_type()), |
| 134 |
static_cast<uint32_t>(components.size())); |
| 135 |
|
| 136 |
// Applies the vector shuffle transformation. |
| 137 |
ApplyTransformation(TransformationVectorShuffle( |
| 138 |
instruction_descriptor, GetFuzzerContext()->GetFreshId(), |
| 139 |
vector_1_instruction->result_id(), |
| 140 |
vector_2_instruction->result_id(), components)); |
| 141 |
}); |
| 142 |
} |
| 143 |
|
| 144 |
} // namespace fuzz |
| 145 |
} // namespace spvtools |