| 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/transformation_composite_extract.h" |
| 16 |
|
| 17 |
#include <vector> |
| 18 |
|
| 19 |
#include "source/fuzz/data_descriptor.h" |
| 20 |
#include "source/fuzz/fuzzer_util.h" |
| 21 |
#include "source/fuzz/instruction_descriptor.h" |
| 22 |
|
| 23 |
namespace spvtools { |
| 24 |
namespace fuzz { |
| 25 |
|
| 26 |
TransformationCompositeExtract::TransformationCompositeExtract( |
| 27 |
const spvtools::fuzz::protobufs::TransformationCompositeExtract& message) |
| 28 |
: message_(message) {} |
| 29 |
|
| 30 |
TransformationCompositeExtract::TransformationCompositeExtract( |
| 31 |
const protobufs::InstructionDescriptor& instruction_to_insert_before, |
| 32 |
uint32_t fresh_id, uint32_t composite_id, |
| 33 |
const std::vector<uint32_t>& index) { |
| 34 |
*message_.mutable_instruction_to_insert_before() = |
| 35 |
instruction_to_insert_before; |
| 36 |
message_.set_fresh_id(fresh_id); |
| 37 |
message_.set_composite_id(composite_id); |
| 38 |
for (auto an_index : index) { |
| 39 |
message_.add_index(an_index); |
| 40 |
} |
| 41 |
} |
| 42 |
|
| 43 |
bool TransformationCompositeExtract::IsApplicable( |
| 44 |
opt::IRContext* ir_context, const TransformationContext& /*unused*/) const { |
| 45 |
if (!fuzzerutil::IsFreshId(ir_context, message_.fresh_id())) { |
| 46 |
return false; |
| 47 |
} |
| 48 |
auto instruction_to_insert_before = |
| 49 |
FindInstruction(message_.instruction_to_insert_before(), ir_context); |
| 50 |
if (!instruction_to_insert_before) { |
| 51 |
return false; |
| 52 |
} |
| 53 |
auto composite_instruction = |
| 54 |
ir_context->get_def_use_mgr()->GetDef(message_.composite_id()); |
| 55 |
if (!composite_instruction) { |
| 56 |
return false; |
| 57 |
} |
| 58 |
if (!fuzzerutil::IdIsAvailableBeforeInstruction( |
| 59 |
ir_context, instruction_to_insert_before, message_.composite_id())) { |
| 60 |
return false; |
| 61 |
} |
| 62 |
|
| 63 |
auto composite_type = |
| 64 |
ir_context->get_type_mgr()->GetType(composite_instruction->type_id()); |
| 65 |
if (!fuzzerutil::IsCompositeType(composite_type)) { |
| 66 |
return false; |
| 67 |
} |
| 68 |
|
| 69 |
if (!fuzzerutil::CanInsertOpcodeBeforeInstruction( |
| 70 |
SpvOpCompositeExtract, instruction_to_insert_before)) { |
| 71 |
return false; |
| 72 |
} |
| 73 |
|
| 74 |
return fuzzerutil::WalkCompositeTypeIndices(ir_context, |
| 75 |
composite_instruction->type_id(), |
| 76 |
message_.index()) != 0; |
| 77 |
} |
| 78 |
|
| 79 |
void TransformationCompositeExtract::Apply( |
| 80 |
opt::IRContext* ir_context, |
| 81 |
TransformationContext* transformation_context) const { |
| 82 |
opt::Instruction::OperandList extract_operands; |
| 83 |
extract_operands.push_back({SPV_OPERAND_TYPE_ID, {message_.composite_id()}}); |
| 84 |
for (auto an_index : message_.index()) { |
| 85 |
extract_operands.push_back({SPV_OPERAND_TYPE_LITERAL_INTEGER, {an_index}}); |
| 86 |
} |
| 87 |
auto composite_instruction = |
| 88 |
ir_context->get_def_use_mgr()->GetDef(message_.composite_id()); |
| 89 |
auto extracted_type = fuzzerutil::WalkCompositeTypeIndices( |
| 90 |
ir_context, composite_instruction->type_id(), message_.index()); |
| 91 |
|
| 92 |
FindInstruction(message_.instruction_to_insert_before(), ir_context) |
| 93 |
->InsertBefore(MakeUnique<opt::Instruction>( |
| 94 |
ir_context, SpvOpCompositeExtract, extracted_type, |
| 95 |
message_.fresh_id(), extract_operands)); |
| 96 |
|
| 97 |
fuzzerutil::UpdateModuleIdBound(ir_context, message_.fresh_id()); |
| 98 |
|
| 99 |
ir_context->InvalidateAnalysesExceptFor( |
| 100 |
opt::IRContext::Analysis::kAnalysisNone); |
| 101 |
|
| 102 |
AddDataSynonymFacts(ir_context, transformation_context); |
| 103 |
} |
| 104 |
|
| 105 |
protobufs::Transformation TransformationCompositeExtract::ToMessage() const { |
| 106 |
protobufs::Transformation result; |
| 107 |
*result.mutable_composite_extract() = message_; |
| 108 |
return result; |
| 109 |
} |
| 110 |
|
| 111 |
std::unordered_set<uint32_t> TransformationCompositeExtract::GetFreshIds() |
| 112 |
const { |
| 113 |
return {message_.fresh_id()}; |
| 114 |
} |
| 115 |
|
| 116 |
void TransformationCompositeExtract::AddDataSynonymFacts( |
| 117 |
opt::IRContext* ir_context, |
| 118 |
TransformationContext* transformation_context) const { |
| 119 |
// Don't add synonyms if the composite being extracted from is not suitable, |
| 120 |
// or if the result id into which we are extracting is irrelevant. |
| 121 |
if (!fuzzerutil::CanMakeSynonymOf( |
| 122 |
ir_context, *transformation_context, |
| 123 |
ir_context->get_def_use_mgr()->GetDef(message_.composite_id())) || |
| 124 |
transformation_context->GetFactManager()->IdIsIrrelevant( |
| 125 |
message_.fresh_id())) { |
| 126 |
return; |
| 127 |
} |
| 128 |
|
| 129 |
// Add the fact that the id storing the extracted element is synonymous with |
| 130 |
// the index into the structure. |
| 131 |
std::vector<uint32_t> indices(message_.index().begin(), |
| 132 |
message_.index().end()); |
| 133 |
auto data_descriptor_for_extracted_element = |
| 134 |
MakeDataDescriptor(message_.composite_id(), indices); |
| 135 |
auto data_descriptor_for_result_id = |
| 136 |
MakeDataDescriptor(message_.fresh_id(), {}); |
| 137 |
transformation_context->GetFactManager()->AddFactDataSynonym( |
| 138 |
data_descriptor_for_extracted_element, data_descriptor_for_result_id); |
| 139 |
} |
| 140 |
|
| 141 |
} // namespace fuzz |
| 142 |
} // namespace spvtools |