| 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_add_global_undef.h" |
| 16 |
|
| 17 |
#include "source/fuzz/fuzzer_util.h" |
| 18 |
|
| 19 |
namespace spvtools { |
| 20 |
namespace fuzz { |
| 21 |
|
| 22 |
TransformationAddGlobalUndef::TransformationAddGlobalUndef( |
| 23 |
const spvtools::fuzz::protobufs::TransformationAddGlobalUndef& message) |
| 24 |
: message_(message) {} |
| 25 |
|
| 26 |
TransformationAddGlobalUndef::TransformationAddGlobalUndef(uint32_t fresh_id, |
| 27 |
uint32_t type_id) { |
| 28 |
message_.set_fresh_id(fresh_id); |
| 29 |
message_.set_type_id(type_id); |
| 30 |
} |
| 31 |
|
| 32 |
bool TransformationAddGlobalUndef::IsApplicable( |
| 33 |
opt::IRContext* ir_context, const TransformationContext& /*unused*/) const { |
| 34 |
// A fresh id is required. |
| 35 |
if (!fuzzerutil::IsFreshId(ir_context, message_.fresh_id())) { |
| 36 |
return false; |
| 37 |
} |
| 38 |
auto type = ir_context->get_type_mgr()->GetType(message_.type_id()); |
| 39 |
// The type must exist, and must not be a function type. |
| 40 |
return type && !type->AsFunction(); |
| 41 |
} |
| 42 |
|
| 43 |
void TransformationAddGlobalUndef::Apply( |
| 44 |
opt::IRContext* ir_context, TransformationContext* /*unused*/) const { |
| 45 |
ir_context->module()->AddGlobalValue(MakeUnique<opt::Instruction>( |
| 46 |
ir_context, SpvOpUndef, message_.type_id(), message_.fresh_id(), |
| 47 |
opt::Instruction::OperandList())); |
| 48 |
fuzzerutil::UpdateModuleIdBound(ir_context, message_.fresh_id()); |
| 49 |
// We have added an instruction to the module, so need to be careful about the |
| 50 |
// validity of existing analyses. |
| 51 |
ir_context->InvalidateAnalysesExceptFor( |
| 52 |
opt::IRContext::Analysis::kAnalysisNone); |
| 53 |
} |
| 54 |
|
| 55 |
protobufs::Transformation TransformationAddGlobalUndef::ToMessage() const { |
| 56 |
protobufs::Transformation result; |
| 57 |
*result.mutable_add_global_undef() = message_; |
| 58 |
return result; |
| 59 |
} |
| 60 |
|
| 61 |
std::unordered_set<uint32_t> TransformationAddGlobalUndef::GetFreshIds() const { |
| 62 |
return {message_.fresh_id()}; |
| 63 |
} |
| 64 |
|
| 65 |
} // namespace fuzz |
| 66 |
} // namespace spvtools |