| 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_variable.h" |
| 16 |
|
| 17 |
#include "source/fuzz/fuzzer_util.h" |
| 18 |
|
| 19 |
namespace spvtools { |
| 20 |
namespace fuzz { |
| 21 |
|
| 22 |
TransformationAddGlobalVariable::TransformationAddGlobalVariable( |
| 23 |
const spvtools::fuzz::protobufs::TransformationAddGlobalVariable& message) |
| 24 |
: message_(message) {} |
| 25 |
|
| 26 |
TransformationAddGlobalVariable::TransformationAddGlobalVariable( |
| 27 |
uint32_t fresh_id, uint32_t type_id, SpvStorageClass storage_class, |
| 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_storage_class(storage_class); |
| 32 |
message_.set_initializer_id(initializer_id); |
| 33 |
message_.set_value_is_irrelevant(value_is_irrelevant); |
| 34 |
} |
| 35 |
|
| 36 |
bool TransformationAddGlobalVariable::IsApplicable( |
| 37 |
opt::IRContext* ir_context, const TransformationContext& /*unused*/) const { |
| 38 |
// The result id must be fresh. |
| 39 |
if (!fuzzerutil::IsFreshId(ir_context, message_.fresh_id())) { |
| 40 |
return false; |
| 41 |
} |
| 42 |
|
| 43 |
// The storage class must be Private or Workgroup. |
| 44 |
auto storage_class = static_cast<SpvStorageClass>(message_.storage_class()); |
| 45 |
switch (storage_class) { |
| 46 |
case SpvStorageClassPrivate: |
| 47 |
case SpvStorageClassWorkgroup: |
| 48 |
break; |
| 49 |
default: |
| 50 |
assert(false && "Unsupported storage class."); |
| 51 |
return false; |
| 52 |
} |
| 53 |
// The type id must correspond to a type. |
| 54 |
auto type = ir_context->get_type_mgr()->GetType(message_.type_id()); |
| 55 |
if (!type) { |
| 56 |
return false; |
| 57 |
} |
| 58 |
// That type must be a pointer type ... |
| 59 |
auto pointer_type = type->AsPointer(); |
| 60 |
if (!pointer_type) { |
| 61 |
return false; |
| 62 |
} |
| 63 |
// ... with the right storage class. |
| 64 |
if (pointer_type->storage_class() != storage_class) { |
| 65 |
return false; |
| 66 |
} |
| 67 |
if (message_.initializer_id()) { |
| 68 |
// An initializer is not allowed if the storage class is Workgroup. |
| 69 |
if (storage_class == SpvStorageClassWorkgroup) { |
| 70 |
assert(false && |
| 71 |
"By construction this transformation should not have an " |
| 72 |
"initializer when Workgroup storage class is used."); |
| 73 |
return false; |
| 74 |
} |
| 75 |
// The initializer id must be the id of a constant. Check this with the |
| 76 |
// constant manager. |
| 77 |
auto constant_id = ir_context->get_constant_mgr()->GetConstantsFromIds( |
| 78 |
{message_.initializer_id()}); |
| 79 |
if (constant_id.empty()) { |
| 80 |
return false; |
| 81 |
} |
| 82 |
assert(constant_id.size() == 1 && |
| 83 |
"We asked for the constant associated with a single id; we should " |
| 84 |
"get a single constant."); |
| 85 |
// The type of the constant must match the pointee type of the pointer. |
| 86 |
if (pointer_type->pointee_type() != constant_id[0]->type()) { |
| 87 |
return false; |
| 88 |
} |
| 89 |
} |
| 90 |
return true; |
| 91 |
} |
| 92 |
|
| 93 |
void TransformationAddGlobalVariable::Apply( |
| 94 |
opt::IRContext* ir_context, |
| 95 |
TransformationContext* transformation_context) const { |
| 96 |
fuzzerutil::AddGlobalVariable( |
| 97 |
ir_context, message_.fresh_id(), message_.type_id(), |
| 98 |
static_cast<SpvStorageClass>(message_.storage_class()), |
| 99 |
message_.initializer_id()); |
| 100 |
|
| 101 |
// We have added an instruction to the module, so need to be careful about the |
| 102 |
// validity of existing analyses. |
| 103 |
ir_context->InvalidateAnalysesExceptFor( |
| 104 |
opt::IRContext::Analysis::kAnalysisNone); |
| 105 |
|
| 106 |
if (message_.value_is_irrelevant()) { |
| 107 |
transformation_context->GetFactManager()->AddFactValueOfPointeeIsIrrelevant( |
| 108 |
message_.fresh_id()); |
| 109 |
} |
| 110 |
} |
| 111 |
|
| 112 |
protobufs::Transformation TransformationAddGlobalVariable::ToMessage() const { |
| 113 |
protobufs::Transformation result; |
| 114 |
*result.mutable_add_global_variable() = message_; |
| 115 |
return result; |
| 116 |
} |
| 117 |
|
| 118 |
std::unordered_set<uint32_t> TransformationAddGlobalVariable::GetFreshIds() |
| 119 |
const { |
| 120 |
return {message_.fresh_id()}; |
| 121 |
} |
| 122 |
|
| 123 |
} // namespace fuzz |
| 124 |
} // namespace spvtools |