2019-06-13 13:08:33 +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/replayer.h"
|
|
|
|
|
2020-09-29 21:12:49 +00:00
|
|
|
#include <algorithm>
|
2020-08-26 06:49:42 +00:00
|
|
|
#include <memory>
|
2019-06-13 13:08:33 +00:00
|
|
|
#include <utility>
|
|
|
|
|
2020-08-26 06:49:42 +00:00
|
|
|
#include "source/fuzz/counter_overflow_id_source.h"
|
2020-08-25 17:11:37 +00:00
|
|
|
#include "source/fuzz/fact_manager/fact_manager.h"
|
2019-06-13 13:08:33 +00:00
|
|
|
#include "source/fuzz/protobufs/spirvfuzz_protobufs.h"
|
2019-06-25 19:49:46 +00:00
|
|
|
#include "source/fuzz/transformation.h"
|
2020-04-02 14:54:46 +00:00
|
|
|
#include "source/fuzz/transformation_context.h"
|
2019-06-13 13:08:33 +00:00
|
|
|
#include "source/opt/build_module.h"
|
|
|
|
#include "source/util/make_unique.h"
|
|
|
|
|
|
|
|
namespace spvtools {
|
|
|
|
namespace fuzz {
|
|
|
|
|
2020-09-22 09:07:58 +00:00
|
|
|
Replayer::Replayer(
|
|
|
|
spv_target_env target_env, MessageConsumer consumer,
|
|
|
|
const std::vector<uint32_t>& binary_in,
|
|
|
|
const protobufs::FactSequence& initial_facts,
|
|
|
|
const protobufs::TransformationSequence& transformation_sequence_in,
|
2020-09-29 21:12:49 +00:00
|
|
|
uint32_t num_transformations_to_apply, bool validate_during_replay,
|
|
|
|
spv_validator_options validator_options)
|
2020-08-28 12:30:06 +00:00
|
|
|
: target_env_(target_env),
|
2020-09-22 09:07:58 +00:00
|
|
|
consumer_(std::move(consumer)),
|
|
|
|
binary_in_(binary_in),
|
|
|
|
initial_facts_(initial_facts),
|
|
|
|
transformation_sequence_in_(transformation_sequence_in),
|
|
|
|
num_transformations_to_apply_(num_transformations_to_apply),
|
2020-08-28 12:30:06 +00:00
|
|
|
validate_during_replay_(validate_during_replay),
|
|
|
|
validator_options_(validator_options) {}
|
2019-06-13 13:08:33 +00:00
|
|
|
|
|
|
|
Replayer::~Replayer() = default;
|
|
|
|
|
2020-09-22 09:07:58 +00:00
|
|
|
Replayer::ReplayerResult Replayer::Run() {
|
2019-06-13 13:08:33 +00:00
|
|
|
// Check compatibility between the library version being linked with and the
|
|
|
|
// header files being used.
|
|
|
|
GOOGLE_PROTOBUF_VERIFY_VERSION;
|
|
|
|
|
2020-09-22 09:07:58 +00:00
|
|
|
if (num_transformations_to_apply_ >
|
|
|
|
static_cast<uint32_t>(
|
|
|
|
transformation_sequence_in_.transformation_size())) {
|
2020-08-28 12:30:06 +00:00
|
|
|
consumer_(SPV_MSG_ERROR, nullptr, {},
|
|
|
|
"The number of transformations to be replayed must not "
|
|
|
|
"exceed the size of the transformation sequence.");
|
2020-09-22 09:07:58 +00:00
|
|
|
return {Replayer::ReplayerResultStatus::kTooManyTransformationsRequested,
|
2020-09-25 08:58:10 +00:00
|
|
|
nullptr, nullptr, protobufs::TransformationSequence()};
|
2020-07-15 11:13:23 +00:00
|
|
|
}
|
|
|
|
|
2020-08-28 12:30:06 +00:00
|
|
|
spvtools::SpirvTools tools(target_env_);
|
2019-06-13 13:08:33 +00:00
|
|
|
if (!tools.IsValid()) {
|
2020-08-28 12:30:06 +00:00
|
|
|
consumer_(SPV_MSG_ERROR, nullptr, {},
|
|
|
|
"Failed to create SPIRV-Tools interface; stopping.");
|
2020-09-22 09:07:58 +00:00
|
|
|
return {Replayer::ReplayerResultStatus::kFailedToCreateSpirvToolsInterface,
|
2020-09-25 08:58:10 +00:00
|
|
|
nullptr, nullptr, protobufs::TransformationSequence()};
|
2019-06-13 13:08:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Initial binary should be valid.
|
2020-09-22 09:07:58 +00:00
|
|
|
if (!tools.Validate(&binary_in_[0], binary_in_.size(), validator_options_)) {
|
2020-08-28 12:30:06 +00:00
|
|
|
consumer_(SPV_MSG_INFO, nullptr, {},
|
|
|
|
"Initial binary is invalid; stopping.");
|
2020-09-25 08:58:10 +00:00
|
|
|
return {Replayer::ReplayerResultStatus::kInitialBinaryInvalid, nullptr,
|
|
|
|
nullptr, protobufs::TransformationSequence()};
|
2019-06-13 13:08:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Build the module from the input binary.
|
2020-08-28 12:30:06 +00:00
|
|
|
std::unique_ptr<opt::IRContext> ir_context =
|
2020-09-22 09:07:58 +00:00
|
|
|
BuildModule(target_env_, consumer_, binary_in_.data(), binary_in_.size());
|
2019-06-13 13:08:33 +00:00
|
|
|
assert(ir_context);
|
|
|
|
|
2019-09-20 09:54:09 +00:00
|
|
|
// For replay validation, we track the last valid SPIR-V binary that was
|
|
|
|
// observed. Initially this is the input binary.
|
|
|
|
std::vector<uint32_t> last_valid_binary;
|
2020-08-28 12:30:06 +00:00
|
|
|
if (validate_during_replay_) {
|
2020-09-22 09:07:58 +00:00
|
|
|
last_valid_binary = binary_in_;
|
2019-09-20 09:54:09 +00:00
|
|
|
}
|
|
|
|
|
2020-09-29 21:12:49 +00:00
|
|
|
// We find the smallest id that is (a) not in use by the original module, and
|
|
|
|
// (b) not used by any transformation in the sequence to be replayed. This
|
|
|
|
// serves as a starting id from which to issue overflow ids if they are
|
|
|
|
// required during replay.
|
|
|
|
uint32_t first_overflow_id = ir_context->module()->id_bound();
|
|
|
|
for (auto& transformation : transformation_sequence_in_.transformation()) {
|
|
|
|
auto fresh_ids = Transformation::FromMessage(transformation)->GetFreshIds();
|
|
|
|
if (!fresh_ids.empty()) {
|
|
|
|
first_overflow_id =
|
|
|
|
std::max(first_overflow_id,
|
2020-10-22 19:07:09 +00:00
|
|
|
*std::max_element(fresh_ids.begin(), fresh_ids.end()) + 1);
|
2020-09-29 21:12:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-26 06:49:42 +00:00
|
|
|
std::unique_ptr<TransformationContext> transformation_context =
|
2020-09-29 21:12:49 +00:00
|
|
|
MakeUnique<TransformationContext>(
|
|
|
|
MakeUnique<FactManager>(ir_context.get()), validator_options_,
|
|
|
|
MakeUnique<CounterOverflowIdSource>(first_overflow_id));
|
2020-10-01 10:48:47 +00:00
|
|
|
transformation_context->GetFactManager()->AddInitialFacts(consumer_,
|
|
|
|
initial_facts_);
|
2020-08-26 06:49:42 +00:00
|
|
|
|
|
|
|
// We track the largest id bound observed, to ensure that it only increases
|
|
|
|
// as transformations are applied.
|
|
|
|
uint32_t max_observed_id_bound = ir_context->module()->id_bound();
|
|
|
|
(void)(max_observed_id_bound); // Keep release-mode compilers happy.
|
2019-06-13 13:08:33 +00:00
|
|
|
|
2020-09-22 09:07:58 +00:00
|
|
|
protobufs::TransformationSequence transformation_sequence_out;
|
|
|
|
|
2019-06-13 13:08:33 +00:00
|
|
|
// Consider the transformation proto messages in turn.
|
2020-07-15 11:13:23 +00:00
|
|
|
uint32_t counter = 0;
|
2020-09-22 09:07:58 +00:00
|
|
|
for (auto& message : transformation_sequence_in_.transformation()) {
|
|
|
|
if (counter >= num_transformations_to_apply_) {
|
2020-07-15 11:13:23 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
counter++;
|
|
|
|
|
2019-06-25 19:49:46 +00:00
|
|
|
auto transformation = Transformation::FromMessage(message);
|
|
|
|
|
2019-06-13 13:08:33 +00:00
|
|
|
// Check whether the transformation can be applied.
|
2020-04-02 14:54:46 +00:00
|
|
|
if (transformation->IsApplicable(ir_context.get(),
|
2020-08-26 06:49:42 +00:00
|
|
|
*transformation_context)) {
|
2019-06-13 13:08:33 +00:00
|
|
|
// The transformation is applicable, so apply it, and copy it to the
|
|
|
|
// sequence of transformations that were applied.
|
2020-08-26 06:49:42 +00:00
|
|
|
transformation->Apply(ir_context.get(), transformation_context.get());
|
2020-09-22 09:07:58 +00:00
|
|
|
*transformation_sequence_out.add_transformation() = message;
|
2019-09-20 09:54:09 +00:00
|
|
|
|
2020-08-26 06:49:42 +00:00
|
|
|
assert(ir_context->module()->id_bound() >= max_observed_id_bound &&
|
|
|
|
"The module's id bound should only increase due to applying "
|
|
|
|
"transformations.");
|
|
|
|
max_observed_id_bound = ir_context->module()->id_bound();
|
|
|
|
|
2020-08-28 12:30:06 +00:00
|
|
|
if (validate_during_replay_) {
|
2019-09-20 09:54:09 +00:00
|
|
|
std::vector<uint32_t> binary_to_validate;
|
|
|
|
ir_context->module()->ToBinary(&binary_to_validate, false);
|
|
|
|
|
|
|
|
// Check whether the latest transformation led to a valid binary.
|
2020-04-02 13:37:59 +00:00
|
|
|
if (!tools.Validate(&binary_to_validate[0], binary_to_validate.size(),
|
2020-08-28 12:30:06 +00:00
|
|
|
validator_options_)) {
|
|
|
|
consumer_(SPV_MSG_INFO, nullptr, {},
|
|
|
|
"Binary became invalid during replay (set a "
|
|
|
|
"breakpoint to inspect); stopping.");
|
2020-09-22 09:07:58 +00:00
|
|
|
return {Replayer::ReplayerResultStatus::kReplayValidationFailure,
|
2020-09-25 08:58:10 +00:00
|
|
|
nullptr, nullptr, protobufs::TransformationSequence()};
|
2019-09-20 09:54:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// The binary was valid, so it becomes the latest valid binary.
|
|
|
|
last_valid_binary = std::move(binary_to_validate);
|
|
|
|
}
|
2019-06-13 13:08:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-25 08:58:10 +00:00
|
|
|
return {Replayer::ReplayerResultStatus::kComplete, std::move(ir_context),
|
|
|
|
std::move(transformation_context),
|
2020-09-22 09:07:58 +00:00
|
|
|
std::move(transformation_sequence_out)};
|
2019-06-13 13:08:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace fuzz
|
|
|
|
} // namespace spvtools
|