2019-12-16 23:54:13 +00:00
|
|
|
// Copyright (c) 2019 Google LLC
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
#include "source/fuzz/transformation_add_global_variable.h"
|
|
|
|
|
|
|
|
#include "source/fuzz/fuzzer_util.h"
|
|
|
|
|
|
|
|
namespace spvtools {
|
|
|
|
namespace fuzz {
|
|
|
|
|
|
|
|
TransformationAddGlobalVariable::TransformationAddGlobalVariable(
|
2021-03-20 22:48:02 +00:00
|
|
|
spvtools::fuzz::protobufs::TransformationAddGlobalVariable message)
|
|
|
|
: message_(std::move(message)) {}
|
2019-12-16 23:54:13 +00:00
|
|
|
|
|
|
|
TransformationAddGlobalVariable::TransformationAddGlobalVariable(
|
2020-04-06 15:08:14 +00:00
|
|
|
uint32_t fresh_id, uint32_t type_id, SpvStorageClass storage_class,
|
|
|
|
uint32_t initializer_id, bool value_is_irrelevant) {
|
2019-12-16 23:54:13 +00:00
|
|
|
message_.set_fresh_id(fresh_id);
|
|
|
|
message_.set_type_id(type_id);
|
2020-04-06 15:08:14 +00:00
|
|
|
message_.set_storage_class(storage_class);
|
2019-12-16 23:54:13 +00:00
|
|
|
message_.set_initializer_id(initializer_id);
|
2020-02-06 16:54:34 +00:00
|
|
|
message_.set_value_is_irrelevant(value_is_irrelevant);
|
2019-12-16 23:54:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool TransformationAddGlobalVariable::IsApplicable(
|
2020-04-02 14:54:46 +00:00
|
|
|
opt::IRContext* ir_context, const TransformationContext& /*unused*/) const {
|
2019-12-16 23:54:13 +00:00
|
|
|
// The result id must be fresh.
|
2020-04-02 14:54:46 +00:00
|
|
|
if (!fuzzerutil::IsFreshId(ir_context, message_.fresh_id())) {
|
2019-12-16 23:54:13 +00:00
|
|
|
return false;
|
|
|
|
}
|
2020-04-06 15:08:14 +00:00
|
|
|
|
|
|
|
// The storage class must be Private or Workgroup.
|
|
|
|
auto storage_class = static_cast<SpvStorageClass>(message_.storage_class());
|
|
|
|
switch (storage_class) {
|
|
|
|
case SpvStorageClassPrivate:
|
|
|
|
case SpvStorageClassWorkgroup:
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
assert(false && "Unsupported storage class.");
|
|
|
|
return false;
|
|
|
|
}
|
2019-12-16 23:54:13 +00:00
|
|
|
// The type id must correspond to a type.
|
2020-04-02 14:54:46 +00:00
|
|
|
auto type = ir_context->get_type_mgr()->GetType(message_.type_id());
|
2019-12-16 23:54:13 +00:00
|
|
|
if (!type) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
// That type must be a pointer type ...
|
|
|
|
auto pointer_type = type->AsPointer();
|
|
|
|
if (!pointer_type) {
|
|
|
|
return false;
|
|
|
|
}
|
2020-04-06 15:08:14 +00:00
|
|
|
// ... with the right storage class.
|
|
|
|
if (pointer_type->storage_class() != storage_class) {
|
2019-12-16 23:54:13 +00:00
|
|
|
return false;
|
|
|
|
}
|
2020-04-06 15:08:14 +00:00
|
|
|
if (message_.initializer_id()) {
|
|
|
|
// An initializer is not allowed if the storage class is Workgroup.
|
|
|
|
if (storage_class == SpvStorageClassWorkgroup) {
|
|
|
|
assert(false &&
|
|
|
|
"By construction this transformation should not have an "
|
|
|
|
"initializer when Workgroup storage class is used.");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
// The initializer id must be the id of a constant. Check this with the
|
|
|
|
// constant manager.
|
|
|
|
auto constant_id = ir_context->get_constant_mgr()->GetConstantsFromIds(
|
|
|
|
{message_.initializer_id()});
|
|
|
|
if (constant_id.empty()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
assert(constant_id.size() == 1 &&
|
|
|
|
"We asked for the constant associated with a single id; we should "
|
|
|
|
"get a single constant.");
|
|
|
|
// The type of the constant must match the pointee type of the pointer.
|
|
|
|
if (pointer_type->pointee_type() != constant_id[0]->type()) {
|
|
|
|
return false;
|
|
|
|
}
|
2019-12-16 23:54:13 +00:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void TransformationAddGlobalVariable::Apply(
|
2020-04-02 14:54:46 +00:00
|
|
|
opt::IRContext* ir_context,
|
|
|
|
TransformationContext* transformation_context) const {
|
2021-03-20 22:48:02 +00:00
|
|
|
opt::Instruction* new_instruction = fuzzerutil::AddGlobalVariable(
|
2020-06-19 15:40:18 +00:00
|
|
|
ir_context, message_.fresh_id(), message_.type_id(),
|
|
|
|
static_cast<SpvStorageClass>(message_.storage_class()),
|
|
|
|
message_.initializer_id());
|
2019-12-19 21:16:09 +00:00
|
|
|
|
2021-03-20 22:48:02 +00:00
|
|
|
// Inform the def-use manager about the new instruction.
|
|
|
|
ir_context->get_def_use_mgr()->AnalyzeInstDefUse(new_instruction);
|
2020-09-15 12:27:14 +00:00
|
|
|
|
|
|
|
if (message_.value_is_irrelevant()) {
|
|
|
|
transformation_context->GetFactManager()->AddFactValueOfPointeeIsIrrelevant(
|
2020-09-24 11:27:59 +00:00
|
|
|
message_.fresh_id());
|
2020-09-15 12:27:14 +00:00
|
|
|
}
|
2019-12-16 23:54:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
protobufs::Transformation TransformationAddGlobalVariable::ToMessage() const {
|
|
|
|
protobufs::Transformation result;
|
|
|
|
*result.mutable_add_global_variable() = message_;
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2020-09-29 21:12:49 +00:00
|
|
|
std::unordered_set<uint32_t> TransformationAddGlobalVariable::GetFreshIds()
|
|
|
|
const {
|
|
|
|
return {message_.fresh_id()};
|
|
|
|
}
|
|
|
|
|
2019-12-16 23:54:13 +00:00
|
|
|
} // namespace fuzz
|
|
|
|
} // namespace spvtools
|