2020-02-05 21:07:44 +00:00
|
|
|
// 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_add_local_variables.h"
|
|
|
|
|
|
|
|
#include "source/fuzz/fuzzer_util.h"
|
|
|
|
#include "source/fuzz/transformation_add_local_variable.h"
|
|
|
|
#include "source/fuzz/transformation_add_type_pointer.h"
|
|
|
|
|
|
|
|
namespace spvtools {
|
|
|
|
namespace fuzz {
|
|
|
|
|
|
|
|
FuzzerPassAddLocalVariables::FuzzerPassAddLocalVariables(
|
2020-04-02 14:54:46 +00:00
|
|
|
opt::IRContext* ir_context, TransformationContext* transformation_context,
|
2020-02-05 21:07:44 +00:00
|
|
|
FuzzerContext* fuzzer_context,
|
2021-07-28 21:59:37 +00:00
|
|
|
protobufs::TransformationSequence* transformations,
|
|
|
|
bool ignore_inapplicable_transformations)
|
2020-04-02 14:54:46 +00:00
|
|
|
: FuzzerPass(ir_context, transformation_context, fuzzer_context,
|
2021-07-28 21:59:37 +00:00
|
|
|
transformations, ignore_inapplicable_transformations) {}
|
2020-02-05 21:07:44 +00:00
|
|
|
|
|
|
|
void FuzzerPassAddLocalVariables::Apply() {
|
2020-04-02 16:35:18 +00:00
|
|
|
auto basic_type_ids_and_pointers =
|
2022-11-04 21:27:10 +00:00
|
|
|
GetAvailableBasicTypesAndPointers(spv::StorageClass::Function);
|
2020-02-05 21:07:44 +00:00
|
|
|
|
2020-04-02 16:35:18 +00:00
|
|
|
// These are the basic types that are available to this fuzzer pass.
|
|
|
|
auto& basic_types = basic_type_ids_and_pointers.first;
|
2021-07-22 22:44:03 +00:00
|
|
|
if (basic_types.empty()) {
|
|
|
|
// The pass cannot do anything if there are no basic types.
|
|
|
|
return;
|
|
|
|
}
|
2020-02-05 21:07:44 +00:00
|
|
|
|
2020-04-02 16:35:18 +00:00
|
|
|
// These are the pointers to those basic types that are *initially* available
|
2020-02-05 21:07:44 +00:00
|
|
|
// to the fuzzer pass. The fuzzer pass might add pointer types in cases where
|
2020-04-02 16:35:18 +00:00
|
|
|
// none are available for a given basic type.
|
|
|
|
auto& basic_type_to_pointers = basic_type_ids_and_pointers.second;
|
2020-02-05 21:07:44 +00:00
|
|
|
|
|
|
|
// Consider every function in the module.
|
|
|
|
for (auto& function : *GetIRContext()->module()) {
|
|
|
|
// Probabilistically keep adding random variables to this function.
|
|
|
|
while (GetFuzzerContext()->ChoosePercentage(
|
|
|
|
GetFuzzerContext()->GetChanceOfAddingLocalVariable())) {
|
2020-04-02 16:35:18 +00:00
|
|
|
// Choose a random basic type; the new variable's type will be a pointer
|
|
|
|
// to this basic type.
|
|
|
|
uint32_t basic_type =
|
|
|
|
basic_types[GetFuzzerContext()->RandomIndex(basic_types)];
|
2020-02-05 21:07:44 +00:00
|
|
|
uint32_t pointer_type;
|
2020-04-02 16:35:18 +00:00
|
|
|
std::vector<uint32_t>& available_pointers_to_basic_type =
|
|
|
|
basic_type_to_pointers.at(basic_type);
|
|
|
|
// Determine whether there is at least one pointer to this basic type.
|
|
|
|
if (available_pointers_to_basic_type.empty()) {
|
2020-02-05 21:07:44 +00:00
|
|
|
// There is not. Make one, to use here, and add it to the available
|
2020-04-02 16:35:18 +00:00
|
|
|
// pointers for the basic type so that future variables can potentially
|
2020-02-05 21:07:44 +00:00
|
|
|
// use it.
|
|
|
|
pointer_type = GetFuzzerContext()->GetFreshId();
|
|
|
|
ApplyTransformation(TransformationAddTypePointer(
|
2022-11-04 21:27:10 +00:00
|
|
|
pointer_type, spv::StorageClass::Function, basic_type));
|
2020-04-02 16:35:18 +00:00
|
|
|
available_pointers_to_basic_type.push_back(pointer_type);
|
2020-02-05 21:07:44 +00:00
|
|
|
} else {
|
|
|
|
// There is - grab one.
|
|
|
|
pointer_type =
|
2020-04-02 16:35:18 +00:00
|
|
|
available_pointers_to_basic_type[GetFuzzerContext()->RandomIndex(
|
|
|
|
available_pointers_to_basic_type)];
|
2020-02-05 21:07:44 +00:00
|
|
|
}
|
|
|
|
ApplyTransformation(TransformationAddLocalVariable(
|
|
|
|
GetFuzzerContext()->GetFreshId(), pointer_type, function.result_id(),
|
2020-07-24 06:57:49 +00:00
|
|
|
FindOrCreateZeroConstant(basic_type, false), true));
|
2020-02-05 21:07:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace fuzz
|
|
|
|
} // namespace spvtools
|