| 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/transformation_push_id_through_variable.h" |
| 16 |
|
| 17 |
#include "source/fuzz/fuzzer_util.h" |
| 18 |
#include "source/fuzz/instruction_descriptor.h" |
| 19 |
|
| 20 |
namespace spvtools { |
| 21 |
namespace fuzz { |
| 22 |
|
| 23 |
TransformationPushIdThroughVariable::TransformationPushIdThroughVariable( |
| 24 |
const spvtools::fuzz::protobufs::TransformationPushIdThroughVariable& |
| 25 |
message) |
| 26 |
: message_(message) {} |
| 27 |
|
| 28 |
TransformationPushIdThroughVariable::TransformationPushIdThroughVariable( |
| 29 |
uint32_t value_id, uint32_t value_synonym_id, uint32_t variable_id, |
| 30 |
uint32_t variable_storage_class, uint32_t initializer_id, |
| 31 |
const protobufs::InstructionDescriptor& instruction_descriptor) { |
| 32 |
message_.set_value_id(value_id); |
| 33 |
message_.set_value_synonym_id(value_synonym_id); |
| 34 |
message_.set_variable_id(variable_id); |
| 35 |
message_.set_variable_storage_class(variable_storage_class); |
| 36 |
message_.set_initializer_id(initializer_id); |
| 37 |
*message_.mutable_instruction_descriptor() = instruction_descriptor; |
| 38 |
} |
| 39 |
|
| 40 |
bool TransformationPushIdThroughVariable::IsApplicable( |
| 41 |
opt::IRContext* ir_context, const TransformationContext& /*unused*/) const { |
| 42 |
// |message_.value_synonym_id| and |message_.variable_id| must be fresh. |
| 43 |
if (!fuzzerutil::IsFreshId(ir_context, message_.value_synonym_id()) || |
| 44 |
!fuzzerutil::IsFreshId(ir_context, message_.variable_id())) { |
| 45 |
return false; |
| 46 |
} |
| 47 |
|
| 48 |
// The instruction to insert before must be defined. |
| 49 |
auto instruction_to_insert_before = |
| 50 |
FindInstruction(message_.instruction_descriptor(), ir_context); |
| 51 |
if (!instruction_to_insert_before) { |
| 52 |
return false; |
| 53 |
} |
| 54 |
|
| 55 |
// It must be valid to insert the OpStore and OpLoad instruction before it. |
| 56 |
if (!fuzzerutil::CanInsertOpcodeBeforeInstruction( |
| 57 |
SpvOpStore, instruction_to_insert_before) || |
| 58 |
!fuzzerutil::CanInsertOpcodeBeforeInstruction( |
| 59 |
SpvOpLoad, instruction_to_insert_before)) { |
| 60 |
return false; |
| 61 |
} |
| 62 |
|
| 63 |
// The instruction to insert before must belong to a reachable block. |
| 64 |
auto basic_block = ir_context->get_instr_block(instruction_to_insert_before); |
| 65 |
if (!fuzzerutil::BlockIsReachableInItsFunction(ir_context, basic_block)) { |
| 66 |
return false; |
| 67 |
} |
| 68 |
|
| 69 |
// The value instruction must be defined and have a type. |
| 70 |
auto value_instruction = |
| 71 |
ir_context->get_def_use_mgr()->GetDef(message_.value_id()); |
| 72 |
if (!value_instruction || !value_instruction->type_id()) { |
| 73 |
return false; |
| 74 |
} |
| 75 |
|
| 76 |
// A pointer type instruction pointing to the value type must be defined. |
| 77 |
auto pointer_type_id = fuzzerutil::MaybeGetPointerType( |
| 78 |
ir_context, value_instruction->type_id(), |
| 79 |
static_cast<SpvStorageClass>(message_.variable_storage_class())); |
| 80 |
if (!pointer_type_id) { |
| 81 |
return false; |
| 82 |
} |
| 83 |
|
| 84 |
// |message_.variable_storage_class| must be private or function. |
| 85 |
assert((message_.variable_storage_class() == SpvStorageClassPrivate || |
| 86 |
message_.variable_storage_class() == SpvStorageClassFunction) && |
| 87 |
"The variable storage class must be private or function."); |
| 88 |
|
| 89 |
// Check that initializer is valid. |
| 90 |
const auto* constant_inst = |
| 91 |
ir_context->get_def_use_mgr()->GetDef(message_.initializer_id()); |
| 92 |
if (!constant_inst || !spvOpcodeIsConstant(constant_inst->opcode()) || |
| 93 |
value_instruction->type_id() != constant_inst->type_id()) { |
| 94 |
return false; |
| 95 |
} |
| 96 |
|
| 97 |
// |message_.value_id| must be available at the insertion point. |
| 98 |
return fuzzerutil::IdIsAvailableBeforeInstruction( |
| 99 |
ir_context, instruction_to_insert_before, message_.value_id()); |
| 100 |
} |
| 101 |
|
| 102 |
void TransformationPushIdThroughVariable::Apply( |
| 103 |
opt::IRContext* ir_context, |
| 104 |
TransformationContext* transformation_context) const { |
| 105 |
auto value_instruction = |
| 106 |
ir_context->get_def_use_mgr()->GetDef(message_.value_id()); |
| 107 |
|
| 108 |
// A pointer type instruction pointing to the value type must be defined. |
| 109 |
auto pointer_type_id = fuzzerutil::MaybeGetPointerType( |
| 110 |
ir_context, value_instruction->type_id(), |
| 111 |
static_cast<SpvStorageClass>(message_.variable_storage_class())); |
| 112 |
assert(pointer_type_id && "The required pointer type must be available."); |
| 113 |
|
| 114 |
// Adds whether a global or local variable. |
| 115 |
if (message_.variable_storage_class() == SpvStorageClassPrivate) { |
| 116 |
fuzzerutil::AddGlobalVariable(ir_context, message_.variable_id(), |
| 117 |
pointer_type_id, SpvStorageClassPrivate, |
| 118 |
message_.initializer_id()); |
| 119 |
} else { |
| 120 |
auto function_id = ir_context |
| 121 |
->get_instr_block(FindInstruction( |
| 122 |
message_.instruction_descriptor(), ir_context)) |
| 123 |
->GetParent() |
| 124 |
->result_id(); |
| 125 |
fuzzerutil::AddLocalVariable(ir_context, message_.variable_id(), |
| 126 |
pointer_type_id, function_id, |
| 127 |
message_.initializer_id()); |
| 128 |
} |
| 129 |
|
| 130 |
// First, insert the OpLoad instruction before |instruction_descriptor| and |
| 131 |
// then insert the OpStore instruction before the OpLoad instruction. |
| 132 |
fuzzerutil::UpdateModuleIdBound(ir_context, message_.value_synonym_id()); |
| 133 |
FindInstruction(message_.instruction_descriptor(), ir_context) |
| 134 |
->InsertBefore(MakeUnique<opt::Instruction>( |
| 135 |
ir_context, SpvOpLoad, value_instruction->type_id(), |
| 136 |
message_.value_synonym_id(), |
| 137 |
opt::Instruction::OperandList( |
| 138 |
{{SPV_OPERAND_TYPE_ID, {message_.variable_id()}}}))) |
| 139 |
->InsertBefore(MakeUnique<opt::Instruction>( |
| 140 |
ir_context, SpvOpStore, 0, 0, |
| 141 |
opt::Instruction::OperandList( |
| 142 |
{{SPV_OPERAND_TYPE_ID, {message_.variable_id()}}, |
| 143 |
{SPV_OPERAND_TYPE_ID, {message_.value_id()}}}))); |
| 144 |
|
| 145 |
ir_context->InvalidateAnalysesExceptFor(opt::IRContext::kAnalysisNone); |
| 146 |
|
| 147 |
// We should be able to create a synonym of |value_id| if it's not irrelevant. |
| 148 |
if (fuzzerutil::CanMakeSynonymOf(ir_context, *transformation_context, |
| 149 |
value_instruction) && |
| 150 |
!transformation_context->GetFactManager()->IdIsIrrelevant( |
| 151 |
message_.value_synonym_id())) { |
| 152 |
// Adds the fact that |message_.value_synonym_id| |
| 153 |
// and |message_.value_id| are synonymous. |
| 154 |
transformation_context->GetFactManager()->AddFactDataSynonym( |
| 155 |
MakeDataDescriptor(message_.value_synonym_id(), {}), |
| 156 |
MakeDataDescriptor(message_.value_id(), {})); |
| 157 |
} |
| 158 |
} |
| 159 |
|
| 160 |
protobufs::Transformation TransformationPushIdThroughVariable::ToMessage() |
| 161 |
const { |
| 162 |
protobufs::Transformation result; |
| 163 |
*result.mutable_push_id_through_variable() = message_; |
| 164 |
return result; |
| 165 |
} |
| 166 |
|
| 167 |
std::unordered_set<uint32_t> TransformationPushIdThroughVariable::GetFreshIds() |
| 168 |
const { |
| 169 |
return {message_.value_synonym_id(), message_.variable_id()}; |
| 170 |
} |
| 171 |
|
| 172 |
} // namespace fuzz |
| 173 |
} // namespace spvtools |