| 1 |
// Copyright (c) 2019 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_apply_id_synonyms.h" |
| 16 |
|
| 17 |
#include "source/fuzz/data_descriptor.h" |
| 18 |
#include "source/fuzz/fuzzer_util.h" |
| 19 |
#include "source/fuzz/id_use_descriptor.h" |
| 20 |
#include "source/fuzz/instruction_descriptor.h" |
| 21 |
#include "source/fuzz/transformation_composite_extract.h" |
| 22 |
#include "source/fuzz/transformation_compute_data_synonym_fact_closure.h" |
| 23 |
#include "source/fuzz/transformation_replace_id_with_synonym.h" |
| 24 |
|
| 25 |
namespace spvtools { |
| 26 |
namespace fuzz { |
| 27 |
|
| 28 |
FuzzerPassApplyIdSynonyms::FuzzerPassApplyIdSynonyms( |
| 29 |
opt::IRContext* ir_context, TransformationContext* transformation_context, |
| 30 |
FuzzerContext* fuzzer_context, |
| 31 |
protobufs::TransformationSequence* transformations) |
| 32 |
: FuzzerPass(ir_context, transformation_context, fuzzer_context, |
| 33 |
transformations) {} |
| 34 |
|
| 35 |
FuzzerPassApplyIdSynonyms::~FuzzerPassApplyIdSynonyms() = default; |
| 36 |
|
| 37 |
void FuzzerPassApplyIdSynonyms::Apply() { |
| 38 |
// Compute a closure of data synonym facts, to enrich the pool of synonyms |
| 39 |
// that are available. |
| 40 |
ApplyTransformation(TransformationComputeDataSynonymFactClosure( |
| 41 |
GetFuzzerContext() |
| 42 |
->GetMaximumEquivalenceClassSizeForDataSynonymFactClosure())); |
| 43 |
|
| 44 |
for (auto id_with_known_synonyms : GetTransformationContext() |
| 45 |
->GetFactManager() |
| 46 |
->GetIdsForWhichSynonymsAreKnown()) { |
| 47 |
// Gather up all uses of |id_with_known_synonym| as a regular id, and |
| 48 |
// subsequently iterate over these uses. We use this separation because, |
| 49 |
// when considering a given use, we might apply a transformation that will |
| 50 |
// invalidate the def-use manager. |
| 51 |
std::vector<std::pair<opt::Instruction*, uint32_t>> uses; |
| 52 |
GetIRContext()->get_def_use_mgr()->ForEachUse( |
| 53 |
id_with_known_synonyms, |
| 54 |
[&uses](opt::Instruction* use_inst, uint32_t use_index) -> void { |
| 55 |
// We only gather up regular id uses; e.g. we do not include a use of |
| 56 |
// the id as the scope for an atomic operation. |
| 57 |
if (use_inst->GetOperand(use_index).type == SPV_OPERAND_TYPE_ID) { |
| 58 |
uses.emplace_back( |
| 59 |
std::pair<opt::Instruction*, uint32_t>(use_inst, use_index)); |
| 60 |
} |
| 61 |
}); |
| 62 |
|
| 63 |
for (const auto& use : uses) { |
| 64 |
auto use_inst = use.first; |
| 65 |
auto use_index = use.second; |
| 66 |
auto block_containing_use = GetIRContext()->get_instr_block(use_inst); |
| 67 |
// The use might not be in a block; e.g. it could be a decoration. |
| 68 |
if (!block_containing_use) { |
| 69 |
continue; |
| 70 |
} |
| 71 |
if (!GetFuzzerContext()->ChoosePercentage( |
| 72 |
GetFuzzerContext()->GetChanceOfReplacingIdWithSynonym())) { |
| 73 |
continue; |
| 74 |
} |
| 75 |
// |use_index| is the absolute index of the operand. We require |
| 76 |
// the index of the operand restricted to input operands only. |
| 77 |
uint32_t use_in_operand_index = |
| 78 |
fuzzerutil::InOperandIndexFromOperandIndex(*use_inst, use_index); |
| 79 |
if (!fuzzerutil::IdUseCanBeReplaced(GetIRContext(), |
| 80 |
*GetTransformationContext(), use_inst, |
| 81 |
use_in_operand_index)) { |
| 82 |
continue; |
| 83 |
} |
| 84 |
|
| 85 |
std::vector<const protobufs::DataDescriptor*> synonyms_to_try; |
| 86 |
for (const auto* data_descriptor : |
| 87 |
GetTransformationContext()->GetFactManager()->GetSynonymsForId( |
| 88 |
id_with_known_synonyms)) { |
| 89 |
protobufs::DataDescriptor descriptor_for_this_id = |
| 90 |
MakeDataDescriptor(id_with_known_synonyms, {}); |
| 91 |
if (DataDescriptorEquals()(data_descriptor, &descriptor_for_this_id)) { |
| 92 |
// Exclude the fact that the id is synonymous with itself. |
| 93 |
continue; |
| 94 |
} |
| 95 |
|
| 96 |
if (DataDescriptorsHaveCompatibleTypes( |
| 97 |
use_inst->opcode(), use_in_operand_index, |
| 98 |
descriptor_for_this_id, *data_descriptor)) { |
| 99 |
synonyms_to_try.push_back(data_descriptor); |
| 100 |
} |
| 101 |
} |
| 102 |
while (!synonyms_to_try.empty()) { |
| 103 |
auto synonym_to_try = |
| 104 |
GetFuzzerContext()->RemoveAtRandomIndex(&synonyms_to_try); |
| 105 |
|
| 106 |
// If the synonym's |index_size| is zero, the synonym represents an id. |
| 107 |
// Otherwise it represents some element of a composite structure, in |
| 108 |
// which case we need to be able to add an extract instruction to get |
| 109 |
// that element out. |
| 110 |
if (synonym_to_try->index_size() > 0 && |
| 111 |
!fuzzerutil::CanInsertOpcodeBeforeInstruction(SpvOpCompositeExtract, |
| 112 |
use_inst) && |
| 113 |
use_inst->opcode() != SpvOpPhi) { |
| 114 |
// We cannot insert an extract before this instruction, so this |
| 115 |
// synonym is no good. |
| 116 |
continue; |
| 117 |
} |
| 118 |
|
| 119 |
if (!fuzzerutil::IdIsAvailableAtUse(GetIRContext(), use_inst, |
| 120 |
use_in_operand_index, |
| 121 |
synonym_to_try->object())) { |
| 122 |
continue; |
| 123 |
} |
| 124 |
|
| 125 |
// We either replace the use with an id known to be synonymous (when |
| 126 |
// the synonym's |index_size| is 0), or an id that will hold the result |
| 127 |
// of extracting a synonym from a composite (when the synonym's |
| 128 |
// |index_size| is > 0). |
| 129 |
uint32_t id_with_which_to_replace_use; |
| 130 |
if (synonym_to_try->index_size() == 0) { |
| 131 |
id_with_which_to_replace_use = synonym_to_try->object(); |
| 132 |
} else { |
| 133 |
id_with_which_to_replace_use = GetFuzzerContext()->GetFreshId(); |
| 134 |
opt::Instruction* instruction_to_insert_before = nullptr; |
| 135 |
|
| 136 |
if (use_inst->opcode() != SpvOpPhi) { |
| 137 |
instruction_to_insert_before = use_inst; |
| 138 |
} else { |
| 139 |
auto parent_block_id = |
| 140 |
use_inst->GetSingleWordInOperand(use_in_operand_index + 1); |
| 141 |
auto parent_block_instruction = |
| 142 |
GetIRContext()->get_def_use_mgr()->GetDef(parent_block_id); |
| 143 |
auto parent_block = |
| 144 |
GetIRContext()->get_instr_block(parent_block_instruction); |
| 145 |
|
| 146 |
instruction_to_insert_before = parent_block->GetMergeInst() |
| 147 |
? parent_block->GetMergeInst() |
| 148 |
: parent_block->terminator(); |
| 149 |
} |
| 150 |
|
| 151 |
if (GetTransformationContext()->GetFactManager()->BlockIsDead( |
| 152 |
GetIRContext() |
| 153 |
->get_instr_block(instruction_to_insert_before) |
| 154 |
->id())) { |
| 155 |
// We cannot create a synonym via a composite extraction in a dead |
| 156 |
// block, as the resulting id is irrelevant. |
| 157 |
continue; |
| 158 |
} |
| 159 |
|
| 160 |
assert(!GetTransformationContext()->GetFactManager()->IdIsIrrelevant( |
| 161 |
synonym_to_try->object()) && |
| 162 |
"Irrelevant ids can't participate in DataSynonym facts"); |
| 163 |
ApplyTransformation(TransformationCompositeExtract( |
| 164 |
MakeInstructionDescriptor(GetIRContext(), |
| 165 |
instruction_to_insert_before), |
| 166 |
id_with_which_to_replace_use, synonym_to_try->object(), |
| 167 |
fuzzerutil::RepeatedFieldToVector(synonym_to_try->index()))); |
| 168 |
assert(GetTransformationContext()->GetFactManager()->IsSynonymous( |
| 169 |
MakeDataDescriptor(id_with_which_to_replace_use, {}), |
| 170 |
*synonym_to_try) && |
| 171 |
"The extracted id must be synonymous with the component from " |
| 172 |
"which it was extracted."); |
| 173 |
} |
| 174 |
|
| 175 |
ApplyTransformation(TransformationReplaceIdWithSynonym( |
| 176 |
MakeIdUseDescriptorFromUse(GetIRContext(), use_inst, |
| 177 |
use_in_operand_index), |
| 178 |
id_with_which_to_replace_use)); |
| 179 |
break; |
| 180 |
} |
| 181 |
} |
| 182 |
} |
| 183 |
} |
| 184 |
|
| 185 |
bool FuzzerPassApplyIdSynonyms::DataDescriptorsHaveCompatibleTypes( |
| 186 |
SpvOp opcode, uint32_t use_in_operand_index, |
| 187 |
const protobufs::DataDescriptor& dd1, |
| 188 |
const protobufs::DataDescriptor& dd2) { |
| 189 |
auto base_object_type_id_1 = |
| 190 |
fuzzerutil::GetTypeId(GetIRContext(), dd1.object()); |
| 191 |
auto base_object_type_id_2 = |
| 192 |
fuzzerutil::GetTypeId(GetIRContext(), dd2.object()); |
| 193 |
assert(base_object_type_id_1 && base_object_type_id_2 && |
| 194 |
"Data descriptors are invalid"); |
| 195 |
|
| 196 |
auto type_id_1 = fuzzerutil::WalkCompositeTypeIndices( |
| 197 |
GetIRContext(), base_object_type_id_1, dd1.index()); |
| 198 |
auto type_id_2 = fuzzerutil::WalkCompositeTypeIndices( |
| 199 |
GetIRContext(), base_object_type_id_2, dd2.index()); |
| 200 |
assert(type_id_1 && type_id_2 && "Data descriptors have invalid types"); |
| 201 |
|
| 202 |
return TransformationReplaceIdWithSynonym::TypesAreCompatible( |
| 203 |
GetIRContext(), opcode, use_in_operand_index, type_id_1, type_id_2); |
| 204 |
} |
| 205 |
|
| 206 |
} // namespace fuzz |
| 207 |
} // namespace spvtools |