| 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_add_synonym.h" |
| 16 |
|
| 17 |
#include <utility> |
| 18 |
|
| 19 |
#include "source/fuzz/fuzzer_util.h" |
| 20 |
#include "source/fuzz/instruction_descriptor.h" |
| 21 |
|
| 22 |
namespace spvtools { |
| 23 |
namespace fuzz { |
| 24 |
|
| 25 |
TransformationAddSynonym::TransformationAddSynonym( |
| 26 |
protobufs::TransformationAddSynonym message) |
| 27 |
: message_(std::move(message)) {} |
| 28 |
|
| 29 |
TransformationAddSynonym::TransformationAddSynonym( |
| 30 |
uint32_t result_id, |
| 31 |
protobufs::TransformationAddSynonym::SynonymType synonym_type, |
| 32 |
uint32_t synonym_fresh_id, |
| 33 |
const protobufs::InstructionDescriptor& insert_before) { |
| 34 |
message_.set_result_id(result_id); |
| 35 |
message_.set_synonym_type(synonym_type); |
| 36 |
message_.set_synonym_fresh_id(synonym_fresh_id); |
| 37 |
*message_.mutable_insert_before() = insert_before; |
| 38 |
} |
| 39 |
|
| 40 |
bool TransformationAddSynonym::IsApplicable( |
| 41 |
opt::IRContext* ir_context, |
| 42 |
const TransformationContext& transformation_context) const { |
| 43 |
assert(protobufs::TransformationAddSynonym::SynonymType_IsValid( |
| 44 |
message_.synonym_type()) && |
| 45 |
"Synonym type is invalid"); |
| 46 |
|
| 47 |
// |synonym_fresh_id| must be fresh. |
| 48 |
if (!fuzzerutil::IsFreshId(ir_context, message_.synonym_fresh_id())) { |
| 49 |
return false; |
| 50 |
} |
| 51 |
|
| 52 |
// Check that |message_.result_id| is valid. |
| 53 |
auto* synonym = ir_context->get_def_use_mgr()->GetDef(message_.result_id()); |
| 54 |
if (!synonym) { |
| 55 |
return false; |
| 56 |
} |
| 57 |
|
| 58 |
// Check that we can apply |synonym_type| to |result_id|. |
| 59 |
if (!IsInstructionValid(ir_context, transformation_context, synonym, |
| 60 |
message_.synonym_type())) { |
| 61 |
return false; |
| 62 |
} |
| 63 |
|
| 64 |
// Check that |insert_before| is valid. |
| 65 |
auto* insert_before_inst = |
| 66 |
FindInstruction(message_.insert_before(), ir_context); |
| 67 |
if (!insert_before_inst) { |
| 68 |
return false; |
| 69 |
} |
| 70 |
|
| 71 |
const auto* insert_before_inst_block = |
| 72 |
ir_context->get_instr_block(insert_before_inst); |
| 73 |
assert(insert_before_inst_block && |
| 74 |
"|insert_before_inst| must be in some block"); |
| 75 |
|
| 76 |
if (transformation_context.GetFactManager()->BlockIsDead( |
| 77 |
insert_before_inst_block->id())) { |
| 78 |
// We don't create synonyms in dead blocks. |
| 79 |
return false; |
| 80 |
} |
| 81 |
|
| 82 |
// Check that we can insert |message._synonymous_instruction| before |
| 83 |
// |message_.insert_before| instruction. We use OpIAdd to represent some |
| 84 |
// instruction that can produce a synonym. |
| 85 |
if (!fuzzerutil::CanInsertOpcodeBeforeInstruction(SpvOpIAdd, |
| 86 |
insert_before_inst)) { |
| 87 |
return false; |
| 88 |
} |
| 89 |
|
| 90 |
// A constant instruction must be present in the module if required. |
| 91 |
if (IsAdditionalConstantRequired(message_.synonym_type()) && |
| 92 |
MaybeGetConstantId(ir_context, transformation_context) == 0) { |
| 93 |
return false; |
| 94 |
} |
| 95 |
|
| 96 |
// Domination rules must be satisfied. |
| 97 |
return fuzzerutil::IdIsAvailableBeforeInstruction( |
| 98 |
ir_context, insert_before_inst, message_.result_id()); |
| 99 |
} |
| 100 |
|
| 101 |
void TransformationAddSynonym::Apply( |
| 102 |
opt::IRContext* ir_context, |
| 103 |
TransformationContext* transformation_context) const { |
| 104 |
// Add a synonymous instruction. |
| 105 |
FindInstruction(message_.insert_before(), ir_context) |
| 106 |
->InsertBefore( |
| 107 |
MakeSynonymousInstruction(ir_context, *transformation_context)); |
| 108 |
|
| 109 |
fuzzerutil::UpdateModuleIdBound(ir_context, message_.synonym_fresh_id()); |
| 110 |
|
| 111 |
ir_context->InvalidateAnalysesExceptFor( |
| 112 |
opt::IRContext::Analysis::kAnalysisNone); |
| 113 |
|
| 114 |
// Propagate PointeeValueIsIrrelevant fact. |
| 115 |
const auto* new_synonym_type = ir_context->get_type_mgr()->GetType( |
| 116 |
fuzzerutil::GetTypeId(ir_context, message_.synonym_fresh_id())); |
| 117 |
assert(new_synonym_type && "New synonym should have a valid type"); |
| 118 |
|
| 119 |
if (transformation_context->GetFactManager()->PointeeValueIsIrrelevant( |
| 120 |
message_.result_id()) && |
| 121 |
new_synonym_type->AsPointer()) { |
| 122 |
transformation_context->GetFactManager()->AddFactValueOfPointeeIsIrrelevant( |
| 123 |
message_.synonym_fresh_id()); |
| 124 |
} |
| 125 |
|
| 126 |
// Mark two ids as synonymous. |
| 127 |
transformation_context->GetFactManager()->AddFactDataSynonym( |
| 128 |
MakeDataDescriptor(message_.result_id(), {}), |
| 129 |
MakeDataDescriptor(message_.synonym_fresh_id(), {})); |
| 130 |
} |
| 131 |
|
| 132 |
protobufs::Transformation TransformationAddSynonym::ToMessage() const { |
| 133 |
protobufs::Transformation result; |
| 134 |
*result.mutable_add_synonym() = message_; |
| 135 |
return result; |
| 136 |
} |
| 137 |
|
| 138 |
bool TransformationAddSynonym::IsInstructionValid( |
| 139 |
opt::IRContext* ir_context, |
| 140 |
const TransformationContext& transformation_context, opt::Instruction* inst, |
| 141 |
protobufs::TransformationAddSynonym::SynonymType synonym_type) { |
| 142 |
// Instruction must have a result id, type id. We skip OpUndef and |
| 143 |
// OpConstantNull. |
| 144 |
if (!inst || !inst->result_id() || !inst->type_id() || |
| 145 |
inst->opcode() == SpvOpUndef || inst->opcode() == SpvOpConstantNull) { |
| 146 |
return false; |
| 147 |
} |
| 148 |
|
| 149 |
if (!fuzzerutil::CanMakeSynonymOf(ir_context, transformation_context, inst)) { |
| 150 |
return false; |
| 151 |
} |
| 152 |
|
| 153 |
switch (synonym_type) { |
| 154 |
case protobufs::TransformationAddSynonym::ADD_ZERO: |
| 155 |
case protobufs::TransformationAddSynonym::SUB_ZERO: |
| 156 |
case protobufs::TransformationAddSynonym::MUL_ONE: { |
| 157 |
// The instruction must be either scalar or vector of integers or floats. |
| 158 |
const auto* type = ir_context->get_type_mgr()->GetType(inst->type_id()); |
| 159 |
assert(type && "Instruction's result id is invalid"); |
| 160 |
|
| 161 |
if (const auto* vector = type->AsVector()) { |
| 162 |
return vector->element_type()->AsInteger() || |
| 163 |
vector->element_type()->AsFloat(); |
| 164 |
} |
| 165 |
|
| 166 |
return type->AsInteger() || type->AsFloat(); |
| 167 |
} |
| 168 |
case protobufs::TransformationAddSynonym::COPY_OBJECT: |
| 169 |
// All checks for OpCopyObject are handled by |
| 170 |
// fuzzerutil::CanMakeSynonymOf. |
| 171 |
return true; |
| 172 |
case protobufs::TransformationAddSynonym::LOGICAL_AND: |
| 173 |
case protobufs::TransformationAddSynonym::LOGICAL_OR: { |
| 174 |
// The instruction must be either a scalar or a vector of booleans. |
| 175 |
const auto* type = ir_context->get_type_mgr()->GetType(inst->type_id()); |
| 176 |
assert(type && "Instruction's result id is invalid"); |
| 177 |
return (type->AsVector() && type->AsVector()->element_type()->AsBool()) || |
| 178 |
type->AsBool(); |
| 179 |
} |
| 180 |
default: |
| 181 |
assert(false && "Synonym type is not supported"); |
| 182 |
return false; |
| 183 |
} |
| 184 |
} |
| 185 |
|
| 186 |
std::unique_ptr<opt::Instruction> |
| 187 |
TransformationAddSynonym::MakeSynonymousInstruction( |
| 188 |
opt::IRContext* ir_context, |
| 189 |
const TransformationContext& transformation_context) const { |
| 190 |
auto synonym_type_id = |
| 191 |
fuzzerutil::GetTypeId(ir_context, message_.result_id()); |
| 192 |
assert(synonym_type_id && "Synonym has invalid type id"); |
| 193 |
|
| 194 |
switch (message_.synonym_type()) { |
| 195 |
case protobufs::TransformationAddSynonym::SUB_ZERO: |
| 196 |
case protobufs::TransformationAddSynonym::MUL_ONE: |
| 197 |
case protobufs::TransformationAddSynonym::ADD_ZERO: { |
| 198 |
const auto* synonym_type = |
| 199 |
ir_context->get_type_mgr()->GetType(synonym_type_id); |
| 200 |
assert(synonym_type && "Synonym has invalid type"); |
| 201 |
|
| 202 |
// Compute instruction's opcode based on the type of the operand. |
| 203 |
// We have already checked that the operand is either a scalar or a vector |
| 204 |
// of either integers or floats. |
| 205 |
auto is_integral = |
| 206 |
(synonym_type->AsVector() && |
| 207 |
synonym_type->AsVector()->element_type()->AsInteger()) || |
| 208 |
synonym_type->AsInteger(); |
| 209 |
auto opcode = SpvOpNop; |
| 210 |
switch (message_.synonym_type()) { |
| 211 |
case protobufs::TransformationAddSynonym::SUB_ZERO: |
| 212 |
opcode = is_integral ? SpvOpISub : SpvOpFSub; |
| 213 |
break; |
| 214 |
case protobufs::TransformationAddSynonym::MUL_ONE: |
| 215 |
opcode = is_integral ? SpvOpIMul : SpvOpFMul; |
| 216 |
break; |
| 217 |
case protobufs::TransformationAddSynonym::ADD_ZERO: |
| 218 |
opcode = is_integral ? SpvOpIAdd : SpvOpFAdd; |
| 219 |
break; |
| 220 |
default: |
| 221 |
assert(false && "Unreachable"); |
| 222 |
break; |
| 223 |
} |
| 224 |
|
| 225 |
return MakeUnique<opt::Instruction>( |
| 226 |
ir_context, opcode, synonym_type_id, message_.synonym_fresh_id(), |
| 227 |
opt::Instruction::OperandList{ |
| 228 |
{SPV_OPERAND_TYPE_ID, {message_.result_id()}}, |
| 229 |
{SPV_OPERAND_TYPE_ID, |
| 230 |
{MaybeGetConstantId(ir_context, transformation_context)}}}); |
| 231 |
} |
| 232 |
case protobufs::TransformationAddSynonym::COPY_OBJECT: |
| 233 |
return MakeUnique<opt::Instruction>( |
| 234 |
ir_context, SpvOpCopyObject, synonym_type_id, |
| 235 |
message_.synonym_fresh_id(), |
| 236 |
opt::Instruction::OperandList{ |
| 237 |
{SPV_OPERAND_TYPE_ID, {message_.result_id()}}}); |
| 238 |
case protobufs::TransformationAddSynonym::LOGICAL_OR: |
| 239 |
case protobufs::TransformationAddSynonym::LOGICAL_AND: { |
| 240 |
auto opcode = message_.synonym_type() == |
| 241 |
protobufs::TransformationAddSynonym::LOGICAL_OR |
| 242 |
? SpvOpLogicalOr |
| 243 |
: SpvOpLogicalAnd; |
| 244 |
return MakeUnique<opt::Instruction>( |
| 245 |
ir_context, opcode, synonym_type_id, message_.synonym_fresh_id(), |
| 246 |
opt::Instruction::OperandList{ |
| 247 |
{SPV_OPERAND_TYPE_ID, {message_.result_id()}}, |
| 248 |
{SPV_OPERAND_TYPE_ID, |
| 249 |
{MaybeGetConstantId(ir_context, transformation_context)}}}); |
| 250 |
} |
| 251 |
default: |
| 252 |
assert(false && "Unhandled synonym type"); |
| 253 |
return nullptr; |
| 254 |
} |
| 255 |
} |
| 256 |
|
| 257 |
uint32_t TransformationAddSynonym::MaybeGetConstantId( |
| 258 |
opt::IRContext* ir_context, |
| 259 |
const TransformationContext& transformation_context) const { |
| 260 |
assert(IsAdditionalConstantRequired(message_.synonym_type()) && |
| 261 |
"Synonym type doesn't require an additional constant"); |
| 262 |
|
| 263 |
auto synonym_type_id = |
| 264 |
fuzzerutil::GetTypeId(ir_context, message_.result_id()); |
| 265 |
assert(synonym_type_id && "Synonym has invalid type id"); |
| 266 |
|
| 267 |
switch (message_.synonym_type()) { |
| 268 |
case protobufs::TransformationAddSynonym::ADD_ZERO: |
| 269 |
case protobufs::TransformationAddSynonym::SUB_ZERO: |
| 270 |
case protobufs::TransformationAddSynonym::LOGICAL_OR: |
| 271 |
return fuzzerutil::MaybeGetZeroConstant( |
| 272 |
ir_context, transformation_context, synonym_type_id, false); |
| 273 |
case protobufs::TransformationAddSynonym::MUL_ONE: |
| 274 |
case protobufs::TransformationAddSynonym::LOGICAL_AND: { |
| 275 |
auto synonym_type = ir_context->get_type_mgr()->GetType(synonym_type_id); |
| 276 |
assert(synonym_type && "Synonym has invalid type"); |
| 277 |
|
| 278 |
if (const auto* vector = synonym_type->AsVector()) { |
| 279 |
auto element_type_id = |
| 280 |
ir_context->get_type_mgr()->GetId(vector->element_type()); |
| 281 |
assert(element_type_id && "Vector's element type is invalid"); |
| 282 |
|
| 283 |
auto one_word = |
| 284 |
vector->element_type()->AsFloat() ? fuzzerutil::FloatToWord(1) : 1u; |
| 285 |
if (auto scalar_one_id = fuzzerutil::MaybeGetScalarConstant( |
| 286 |
ir_context, transformation_context, {one_word}, element_type_id, |
| 287 |
false)) { |
| 288 |
return fuzzerutil::MaybeGetCompositeConstant( |
| 289 |
ir_context, transformation_context, |
| 290 |
std::vector<uint32_t>(vector->element_count(), scalar_one_id), |
| 291 |
synonym_type_id, false); |
| 292 |
} |
| 293 |
|
| 294 |
return 0; |
| 295 |
} else { |
| 296 |
return fuzzerutil::MaybeGetScalarConstant( |
| 297 |
ir_context, transformation_context, |
| 298 |
{synonym_type->AsFloat() ? fuzzerutil::FloatToWord(1) : 1u}, |
| 299 |
synonym_type_id, false); |
| 300 |
} |
| 301 |
} |
| 302 |
default: |
| 303 |
// The assertion at the beginning of the function will fail in the debug |
| 304 |
// mode. |
| 305 |
return 0; |
| 306 |
} |
| 307 |
} |
| 308 |
|
| 309 |
bool TransformationAddSynonym::IsAdditionalConstantRequired( |
| 310 |
protobufs::TransformationAddSynonym::SynonymType synonym_type) { |
| 311 |
switch (synonym_type) { |
| 312 |
case protobufs::TransformationAddSynonym::ADD_ZERO: |
| 313 |
case protobufs::TransformationAddSynonym::SUB_ZERO: |
| 314 |
case protobufs::TransformationAddSynonym::LOGICAL_OR: |
| 315 |
case protobufs::TransformationAddSynonym::MUL_ONE: |
| 316 |
case protobufs::TransformationAddSynonym::LOGICAL_AND: |
| 317 |
return true; |
| 318 |
default: |
| 319 |
return false; |
| 320 |
} |
| 321 |
} |
| 322 |
|
| 323 |
std::unordered_set<uint32_t> TransformationAddSynonym::GetFreshIds() const { |
| 324 |
return {message_.synonym_fresh_id()}; |
| 325 |
} |
| 326 |
|
| 327 |
} // namespace fuzz |
| 328 |
} // namespace spvtools |