| 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_constant_null.h" |
| 16 |
|
| 17 |
#include "source/fuzz/fuzzer_util.h" |
| 18 |
|
| 19 |
namespace spvtools { |
| 20 |
namespace fuzz { |
| 21 |
|
| 22 |
TransformationAddConstantNull::TransformationAddConstantNull( |
| 23 |
const spvtools::fuzz::protobufs::TransformationAddConstantNull& message) |
| 24 |
: message_(message) {} |
| 25 |
|
| 26 |
TransformationAddConstantNull::TransformationAddConstantNull(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 TransformationAddConstantNull::IsApplicable( |
| 33 |
opt::IRContext* context, const TransformationContext& /*unused*/) const { |
| 34 |
// A fresh id is required. |
| 35 |
if (!fuzzerutil::IsFreshId(context, message_.fresh_id())) { |
| 36 |
return false; |
| 37 |
} |
| 38 |
auto type = context->get_type_mgr()->GetType(message_.type_id()); |
| 39 |
// The type must exist. |
| 40 |
if (!type) { |
| 41 |
return false; |
| 42 |
} |
| 43 |
// The type must be one of the types for which null constants are allowed, |
| 44 |
// according to the SPIR-V spec. |
| 45 |
return fuzzerutil::IsNullConstantSupported(*type); |
| 46 |
} |
| 47 |
|
| 48 |
void TransformationAddConstantNull::Apply( |
| 49 |
opt::IRContext* context, TransformationContext* /*unused*/) const { |
| 50 |
context->module()->AddGlobalValue(MakeUnique<opt::Instruction>( |
| 51 |
context, SpvOpConstantNull, message_.type_id(), message_.fresh_id(), |
| 52 |
opt::Instruction::OperandList())); |
| 53 |
fuzzerutil::UpdateModuleIdBound(context, message_.fresh_id()); |
| 54 |
// We have added an instruction to the module, so need to be careful about the |
| 55 |
// validity of existing analyses. |
| 56 |
context->InvalidateAnalysesExceptFor(opt::IRContext::Analysis::kAnalysisNone); |
| 57 |
} |
| 58 |
|
| 59 |
protobufs::Transformation TransformationAddConstantNull::ToMessage() const { |
| 60 |
protobufs::Transformation result; |
| 61 |
*result.mutable_add_constant_null() = message_; |
| 62 |
return result; |
| 63 |
} |
| 64 |
|
| 65 |
std::unordered_set<uint32_t> TransformationAddConstantNull::GetFreshIds() |
| 66 |
const { |
| 67 |
return {message_.fresh_id()}; |
| 68 |
} |
| 69 |
|
| 70 |
} // namespace fuzz |
| 71 |
} // namespace spvtools |