2019-06-04 13:55:00 +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_constant_boolean.h"
|
|
|
|
|
|
|
|
#include "source/fuzz/fuzzer_util.h"
|
|
|
|
#include "source/opt/types.h"
|
|
|
|
|
|
|
|
namespace spvtools {
|
|
|
|
namespace fuzz {
|
|
|
|
|
2019-06-25 19:49:46 +00:00
|
|
|
TransformationAddConstantBoolean::TransformationAddConstantBoolean(
|
|
|
|
const protobufs::TransformationAddConstantBoolean& message)
|
|
|
|
: message_(message) {}
|
2019-06-04 13:55:00 +00:00
|
|
|
|
2019-06-25 19:49:46 +00:00
|
|
|
TransformationAddConstantBoolean::TransformationAddConstantBoolean(
|
|
|
|
uint32_t fresh_id, bool is_true) {
|
|
|
|
message_.set_fresh_id(fresh_id);
|
|
|
|
message_.set_is_true(is_true);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool TransformationAddConstantBoolean::IsApplicable(
|
2020-04-02 14:54:46 +00:00
|
|
|
opt::IRContext* ir_context, const TransformationContext& /*unused*/) const {
|
2019-06-04 13:55:00 +00:00
|
|
|
opt::analysis::Bool bool_type;
|
2020-04-02 14:54:46 +00:00
|
|
|
if (!ir_context->get_type_mgr()->GetId(&bool_type)) {
|
2019-06-04 13:55:00 +00:00
|
|
|
// No OpTypeBool is present.
|
|
|
|
return false;
|
|
|
|
}
|
2020-04-02 14:54:46 +00:00
|
|
|
return fuzzerutil::IsFreshId(ir_context, message_.fresh_id());
|
2019-06-04 13:55:00 +00:00
|
|
|
}
|
|
|
|
|
2020-04-02 14:54:46 +00:00
|
|
|
void TransformationAddConstantBoolean::Apply(
|
|
|
|
opt::IRContext* ir_context, TransformationContext* /*unused*/) const {
|
2019-06-04 13:55:00 +00:00
|
|
|
opt::analysis::Bool bool_type;
|
|
|
|
// Add the boolean constant to the module, ensuring the module's id bound is
|
|
|
|
// high enough.
|
2020-04-02 14:54:46 +00:00
|
|
|
fuzzerutil::UpdateModuleIdBound(ir_context, message_.fresh_id());
|
|
|
|
ir_context->module()->AddGlobalValue(
|
2019-06-25 19:49:46 +00:00
|
|
|
message_.is_true() ? SpvOpConstantTrue : SpvOpConstantFalse,
|
2020-04-02 14:54:46 +00:00
|
|
|
message_.fresh_id(), ir_context->get_type_mgr()->GetId(&bool_type));
|
2019-06-04 13:55:00 +00:00
|
|
|
// We have added an instruction to the module, so need to be careful about the
|
|
|
|
// validity of existing analyses.
|
2020-04-02 14:54:46 +00:00
|
|
|
ir_context->InvalidateAnalysesExceptFor(
|
|
|
|
opt::IRContext::Analysis::kAnalysisNone);
|
2019-06-04 13:55:00 +00:00
|
|
|
}
|
|
|
|
|
2019-06-25 19:49:46 +00:00
|
|
|
protobufs::Transformation TransformationAddConstantBoolean::ToMessage() const {
|
|
|
|
protobufs::Transformation result;
|
|
|
|
*result.mutable_add_constant_boolean() = message_;
|
2019-06-04 13:55:00 +00:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace fuzz
|
|
|
|
} // namespace spvtools
|