| 1 |
// Copyright (c) 2020 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_add_local_variable.h" |
| 16 |
|
| 17 |
#include "source/fuzz/fuzzer_util.h" |
| 18 |
|
| 19 |
namespace spvtools { |
| 20 |
namespace fuzz { |
| 21 |
|
| 22 |
TransformationAddLocalVariable::TransformationAddLocalVariable( |
| 23 |
const spvtools::fuzz::protobufs::TransformationAddLocalVariable& message) |
| 24 |
: message_(message) {} |
| 25 |
|
| 26 |
TransformationAddLocalVariable::TransformationAddLocalVariable( |
| 27 |
uint32_t fresh_id, uint32_t type_id, uint32_t function_id, |
| 28 |
uint32_t initializer_id, bool value_is_irrelevant) { |
| 29 |
message_.set_fresh_id(fresh_id); |
| 30 |
message_.set_type_id(type_id); |
| 31 |
message_.set_function_id(function_id); |
| 32 |
message_.set_initializer_id(initializer_id); |
| 33 |
message_.set_value_is_irrelevant(value_is_irrelevant); |
| 34 |
} |
| 35 |
|
| 36 |
bool TransformationAddLocalVariable::IsApplicable( |
| 37 |
opt::IRContext* ir_context, const TransformationContext& /*unused*/) const { |
| 38 |
// The provided id must be fresh. |
| 39 |
if (!fuzzerutil::IsFreshId(ir_context, message_.fresh_id())) { |
| 40 |
return false; |
| 41 |
} |
| 42 |
// The pointer type id must indeed correspond to a pointer, and it must have |
| 43 |
// function storage class. |
| 44 |
auto type_instruction = |
| 45 |
ir_context->get_def_use_mgr()->GetDef(message_.type_id()); |
| 46 |
if (!type_instruction || type_instruction->opcode() != SpvOpTypePointer || |
| 47 |
type_instruction->GetSingleWordInOperand(0) != SpvStorageClassFunction) { |
| 48 |
return false; |
| 49 |
} |
| 50 |
// The initializer must... |
| 51 |
auto initializer_instruction = |
| 52 |
ir_context->get_def_use_mgr()->GetDef(message_.initializer_id()); |
| 53 |
// ... exist, ... |
| 54 |
if (!initializer_instruction) { |
| 55 |
return false; |
| 56 |
} |
| 57 |
// ... be a constant, ... |
| 58 |
if (!spvOpcodeIsConstant(initializer_instruction->opcode())) { |
| 59 |
return false; |
| 60 |
} |
| 61 |
// ... and have the same type as the pointee type. |
| 62 |
if (initializer_instruction->type_id() != |
| 63 |
type_instruction->GetSingleWordInOperand(1)) { |
| 64 |
return false; |
| 65 |
} |
| 66 |
// The function to which the local variable is to be added must exist. |
| 67 |
return fuzzerutil::FindFunction(ir_context, message_.function_id()); |
| 68 |
} |
| 69 |
|
| 70 |
void TransformationAddLocalVariable::Apply( |
| 71 |
opt::IRContext* ir_context, |
| 72 |
TransformationContext* transformation_context) const { |
| 73 |
fuzzerutil::AddLocalVariable(ir_context, message_.fresh_id(), |
| 74 |
message_.type_id(), message_.function_id(), |
| 75 |
message_.initializer_id()); |
| 76 |
|
| 77 |
ir_context->InvalidateAnalysesExceptFor(opt::IRContext::kAnalysisNone); |
| 78 |
|
| 79 |
if (message_.value_is_irrelevant()) { |
| 80 |
transformation_context->GetFactManager()->AddFactValueOfPointeeIsIrrelevant( |
| 81 |
message_.fresh_id()); |
| 82 |
} |
| 83 |
} |
| 84 |
|
| 85 |
protobufs::Transformation TransformationAddLocalVariable::ToMessage() const { |
| 86 |
protobufs::Transformation result; |
| 87 |
*result.mutable_add_local_variable() = message_; |
| 88 |
return result; |
| 89 |
} |
| 90 |
|
| 91 |
std::unordered_set<uint32_t> TransformationAddLocalVariable::GetFreshIds() |
| 92 |
const { |
| 93 |
return {message_.fresh_id()}; |
| 94 |
} |
| 95 |
|
| 96 |
} // namespace fuzz |
| 97 |
} // namespace spvtools |