2020-07-15 11:58:29 +00:00
|
|
|
// Copyright (c) 2020 Stefano Milizia
|
|
|
|
// Copyright (c) 2020 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/fuzzer_pass_interchange_zero_like_constants.h"
|
2020-07-22 18:03:58 +00:00
|
|
|
|
2020-07-15 11:58:29 +00:00
|
|
|
#include "source/fuzz/fuzzer_util.h"
|
|
|
|
#include "source/fuzz/id_use_descriptor.h"
|
|
|
|
#include "source/fuzz/transformation_record_synonymous_constants.h"
|
|
|
|
#include "source/fuzz/transformation_replace_id_with_synonym.h"
|
|
|
|
|
|
|
|
namespace spvtools {
|
|
|
|
namespace fuzz {
|
|
|
|
FuzzerPassInterchangeZeroLikeConstants::FuzzerPassInterchangeZeroLikeConstants(
|
|
|
|
opt::IRContext* ir_context, TransformationContext* transformation_context,
|
|
|
|
FuzzerContext* fuzzer_context,
|
2021-07-28 21:59:37 +00:00
|
|
|
protobufs::TransformationSequence* transformations,
|
|
|
|
bool ignore_inapplicable_transformations)
|
2020-07-15 11:58:29 +00:00
|
|
|
: FuzzerPass(ir_context, transformation_context, fuzzer_context,
|
2021-07-28 21:59:37 +00:00
|
|
|
transformations, ignore_inapplicable_transformations) {}
|
2020-07-15 11:58:29 +00:00
|
|
|
|
|
|
|
uint32_t FuzzerPassInterchangeZeroLikeConstants::FindOrCreateToggledConstant(
|
|
|
|
opt::Instruction* declaration) {
|
2020-08-12 17:59:47 +00:00
|
|
|
// |declaration| must not be a specialization constant because we do not know
|
|
|
|
// the value of specialization constants.
|
|
|
|
if (opt::IsSpecConstantInst(declaration->opcode())) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-07-15 11:58:29 +00:00
|
|
|
auto constant = GetIRContext()->get_constant_mgr()->FindDeclaredConstant(
|
|
|
|
declaration->result_id());
|
|
|
|
|
|
|
|
// This pass only toggles zero-like constants
|
|
|
|
if (!constant->IsZero()) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (constant->AsScalarConstant()) {
|
|
|
|
return FindOrCreateNullConstant(declaration->type_id());
|
|
|
|
} else if (constant->AsNullConstant()) {
|
|
|
|
// Add declaration of equivalent scalar constant
|
|
|
|
auto kind = constant->type()->kind();
|
|
|
|
if (kind == opt::analysis::Type::kBool ||
|
|
|
|
kind == opt::analysis::Type::kInteger ||
|
|
|
|
kind == opt::analysis::Type::kFloat) {
|
2020-07-24 06:57:49 +00:00
|
|
|
return FindOrCreateZeroConstant(declaration->type_id(), false);
|
2020-07-15 11:58:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void FuzzerPassInterchangeZeroLikeConstants::Apply() {
|
|
|
|
// Make vector keeping track of all the uses we want to replace.
|
|
|
|
// This is a vector of pairs, where the first element is an id use descriptor
|
|
|
|
// identifying the use of a constant id and the second is the id that should
|
|
|
|
// be used to replace it.
|
|
|
|
std::vector<std::pair<protobufs::IdUseDescriptor, uint32_t>> uses_to_replace;
|
|
|
|
|
|
|
|
for (auto constant : GetIRContext()->GetConstants()) {
|
|
|
|
uint32_t constant_id = constant->result_id();
|
2020-07-22 18:03:58 +00:00
|
|
|
if (GetTransformationContext()->GetFactManager()->IdIsIrrelevant(
|
2020-09-24 11:27:59 +00:00
|
|
|
constant_id)) {
|
2020-07-22 18:03:58 +00:00
|
|
|
continue;
|
|
|
|
}
|
2020-07-15 11:58:29 +00:00
|
|
|
|
2020-07-22 18:03:58 +00:00
|
|
|
uint32_t toggled_id = FindOrCreateToggledConstant(constant);
|
2020-07-15 11:58:29 +00:00
|
|
|
if (!toggled_id) {
|
|
|
|
// Not a zero-like constant
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2020-07-22 18:03:58 +00:00
|
|
|
assert(!GetTransformationContext()->GetFactManager()->IdIsIrrelevant(
|
2020-09-24 11:27:59 +00:00
|
|
|
toggled_id) &&
|
2020-07-22 18:03:58 +00:00
|
|
|
"FindOrCreateToggledConstant can't produce an irrelevant id");
|
|
|
|
|
2020-07-15 11:58:29 +00:00
|
|
|
// Record synonymous constants
|
|
|
|
ApplyTransformation(
|
|
|
|
TransformationRecordSynonymousConstants(constant_id, toggled_id));
|
|
|
|
|
|
|
|
// Find all the uses of the constant and, for each, probabilistically
|
|
|
|
// decide whether to replace it.
|
|
|
|
GetIRContext()->get_def_use_mgr()->ForEachUse(
|
|
|
|
constant_id,
|
|
|
|
[this, toggled_id, &uses_to_replace](opt::Instruction* use_inst,
|
|
|
|
uint32_t use_index) -> void {
|
|
|
|
if (GetFuzzerContext()->ChoosePercentage(
|
|
|
|
GetFuzzerContext()
|
|
|
|
->GetChanceOfInterchangingZeroLikeConstants())) {
|
|
|
|
MaybeAddUseToReplace(use_inst, use_index, toggled_id,
|
|
|
|
&uses_to_replace);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-07-30 18:48:29 +00:00
|
|
|
// Replace the ids if it is allowed.
|
2020-07-15 11:58:29 +00:00
|
|
|
for (auto use_to_replace : uses_to_replace) {
|
2020-07-17 12:01:35 +00:00
|
|
|
MaybeApplyTransformation(TransformationReplaceIdWithSynonym(
|
|
|
|
use_to_replace.first, use_to_replace.second));
|
2020-07-15 11:58:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} // namespace fuzz
|
2020-08-12 17:59:47 +00:00
|
|
|
} // namespace spvtools
|