| 1 |
// Copyright (c) 2020 Vasyl Teliman |
| 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_mutate_pointer.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 |
TransformationMutatePointer::TransformationMutatePointer( |
| 24 |
const protobufs::TransformationMutatePointer& message) |
| 25 |
: message_(message) {} |
| 26 |
|
| 27 |
TransformationMutatePointer::TransformationMutatePointer( |
| 28 |
uint32_t pointer_id, uint32_t fresh_id, |
| 29 |
const protobufs::InstructionDescriptor& insert_before) { |
| 30 |
message_.set_pointer_id(pointer_id); |
| 31 |
message_.set_fresh_id(fresh_id); |
| 32 |
*message_.mutable_insert_before() = insert_before; |
| 33 |
} |
| 34 |
|
| 35 |
bool TransformationMutatePointer::IsApplicable( |
| 36 |
opt::IRContext* ir_context, |
| 37 |
const TransformationContext& transformation_context) const { |
| 38 |
// Check that |fresh_id| is fresh. |
| 39 |
if (!fuzzerutil::IsFreshId(ir_context, message_.fresh_id())) { |
| 40 |
return false; |
| 41 |
} |
| 42 |
|
| 43 |
auto* insert_before_inst = |
| 44 |
FindInstruction(message_.insert_before(), ir_context); |
| 45 |
|
| 46 |
// Check that |insert_before| is a valid instruction descriptor. |
| 47 |
if (!insert_before_inst) { |
| 48 |
return false; |
| 49 |
} |
| 50 |
|
| 51 |
// Check that it is possible to insert OpLoad and OpStore before |
| 52 |
// |insert_before_inst|. We are only using OpLoad here since the result does |
| 53 |
// not depend on the opcode. |
| 54 |
if (!fuzzerutil::CanInsertOpcodeBeforeInstruction(SpvOpLoad, |
| 55 |
insert_before_inst)) { |
| 56 |
return false; |
| 57 |
} |
| 58 |
|
| 59 |
const auto* pointer_inst = |
| 60 |
ir_context->get_def_use_mgr()->GetDef(message_.pointer_id()); |
| 61 |
|
| 62 |
// Check that |pointer_id| is a result id of a valid pointer instruction. |
| 63 |
if (!pointer_inst || !IsValidPointerInstruction(ir_context, *pointer_inst)) { |
| 64 |
return false; |
| 65 |
} |
| 66 |
|
| 67 |
// Check that the module contains an irrelevant constant that will be used to |
| 68 |
// mutate |pointer_inst|. The constant is irrelevant so that the latter |
| 69 |
// transformation can change its value to something more interesting. |
| 70 |
auto constant_id = fuzzerutil::MaybeGetZeroConstant( |
| 71 |
ir_context, transformation_context, |
| 72 |
fuzzerutil::GetPointeeTypeIdFromPointerType(ir_context, |
| 73 |
pointer_inst->type_id()), |
| 74 |
true); |
| 75 |
if (!constant_id) { |
| 76 |
return false; |
| 77 |
} |
| 78 |
|
| 79 |
assert(fuzzerutil::IdIsAvailableBeforeInstruction( |
| 80 |
ir_context, insert_before_inst, constant_id) && |
| 81 |
"Global constant instruction is not available before " |
| 82 |
"|insert_before_inst|"); |
| 83 |
|
| 84 |
// Check that |pointer_inst| is available before |insert_before_inst|. |
| 85 |
return fuzzerutil::IdIsAvailableBeforeInstruction( |
| 86 |
ir_context, insert_before_inst, pointer_inst->result_id()); |
| 87 |
} |
| 88 |
|
| 89 |
void TransformationMutatePointer::Apply( |
| 90 |
opt::IRContext* ir_context, |
| 91 |
TransformationContext* transformation_context) const { |
| 92 |
auto* insert_before_inst = |
| 93 |
FindInstruction(message_.insert_before(), ir_context); |
| 94 |
assert(insert_before_inst && "|insert_before| descriptor is invalid"); |
| 95 |
|
| 96 |
auto pointee_type_id = fuzzerutil::GetPointeeTypeIdFromPointerType( |
| 97 |
ir_context, fuzzerutil::GetTypeId(ir_context, message_.pointer_id())); |
| 98 |
|
| 99 |
// Back up the original value. |
| 100 |
insert_before_inst->InsertBefore(MakeUnique<opt::Instruction>( |
| 101 |
ir_context, SpvOpLoad, pointee_type_id, message_.fresh_id(), |
| 102 |
opt::Instruction::OperandList{ |
| 103 |
{SPV_OPERAND_TYPE_ID, {message_.pointer_id()}}})); |
| 104 |
|
| 105 |
// Insert a new value. |
| 106 |
insert_before_inst->InsertBefore(MakeUnique<opt::Instruction>( |
| 107 |
ir_context, SpvOpStore, 0, 0, |
| 108 |
opt::Instruction::OperandList{ |
| 109 |
{SPV_OPERAND_TYPE_ID, {message_.pointer_id()}}, |
| 110 |
{SPV_OPERAND_TYPE_ID, |
| 111 |
{fuzzerutil::MaybeGetZeroConstant( |
| 112 |
ir_context, *transformation_context, pointee_type_id, true)}}})); |
| 113 |
|
| 114 |
// Restore the original value. |
| 115 |
insert_before_inst->InsertBefore(MakeUnique<opt::Instruction>( |
| 116 |
ir_context, SpvOpStore, 0, 0, |
| 117 |
opt::Instruction::OperandList{ |
| 118 |
{SPV_OPERAND_TYPE_ID, {message_.pointer_id()}}, |
| 119 |
{SPV_OPERAND_TYPE_ID, {message_.fresh_id()}}})); |
| 120 |
|
| 121 |
fuzzerutil::UpdateModuleIdBound(ir_context, message_.fresh_id()); |
| 122 |
|
| 123 |
// Make sure analyses represent the correct state of the module. |
| 124 |
ir_context->InvalidateAnalysesExceptFor(opt::IRContext::kAnalysisNone); |
| 125 |
} |
| 126 |
|
| 127 |
protobufs::Transformation TransformationMutatePointer::ToMessage() const { |
| 128 |
protobufs::Transformation result; |
| 129 |
*result.mutable_mutate_pointer() = message_; |
| 130 |
return result; |
| 131 |
} |
| 132 |
|
| 133 |
bool TransformationMutatePointer::IsValidPointerInstruction( |
| 134 |
opt::IRContext* ir_context, const opt::Instruction& inst) { |
| 135 |
// |inst| must have both result id and type id and it may not cause undefined |
| 136 |
// behaviour. |
| 137 |
if (!inst.result_id() || !inst.type_id() || inst.opcode() == SpvOpUndef || |
| 138 |
inst.opcode() == SpvOpConstantNull) { |
| 139 |
return false; |
| 140 |
} |
| 141 |
|
| 142 |
opt::Instruction* type_inst = |
| 143 |
ir_context->get_def_use_mgr()->GetDef(inst.type_id()); |
| 144 |
assert(type_inst != nullptr && "|inst| has invalid type id"); |
| 145 |
|
| 146 |
// |inst| must be a pointer. |
| 147 |
if (type_inst->opcode() != SpvOpTypePointer) { |
| 148 |
return false; |
| 149 |
} |
| 150 |
|
| 151 |
// |inst| must have a supported storage class. |
| 152 |
switch (static_cast<SpvStorageClass>(type_inst->GetSingleWordInOperand(0))) { |
| 153 |
case SpvStorageClassFunction: |
| 154 |
case SpvStorageClassPrivate: |
| 155 |
case SpvStorageClassWorkgroup: |
| 156 |
break; |
| 157 |
default: |
| 158 |
return false; |
| 159 |
} |
| 160 |
|
| 161 |
// |inst|'s pointee must consist of scalars and/or composites. |
| 162 |
return fuzzerutil::CanCreateConstant(ir_context, |
| 163 |
type_inst->GetSingleWordInOperand(1)); |
| 164 |
} |
| 165 |
|
| 166 |
std::unordered_set<uint32_t> TransformationMutatePointer::GetFreshIds() const { |
| 167 |
return {message_.fresh_id()}; |
| 168 |
} |
| 169 |
|
| 170 |
} // namespace fuzz |
| 171 |
} // namespace spvtools |