2021-01-12 16:23:48 +00:00
|
|
|
// Copyright 2021 The Tint Authors.
|
|
|
|
//
|
|
|
|
// 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 "fuzzers/tint_common_fuzzer.h"
|
|
|
|
|
2021-06-07 08:07:11 +00:00
|
|
|
#include <cstring>
|
2021-01-12 16:23:48 +00:00
|
|
|
#include <memory>
|
|
|
|
#include <string>
|
|
|
|
#include <utility>
|
|
|
|
#include <vector>
|
|
|
|
|
2021-01-26 16:57:10 +00:00
|
|
|
#include "src/ast/module.h"
|
2021-06-24 18:10:46 +00:00
|
|
|
#include "src/diagnostic/formatter.h"
|
2021-01-26 16:57:10 +00:00
|
|
|
#include "src/program.h"
|
|
|
|
|
2021-01-12 16:23:48 +00:00
|
|
|
namespace tint {
|
|
|
|
namespace fuzzers {
|
|
|
|
|
2021-05-06 15:43:33 +00:00
|
|
|
namespace {
|
|
|
|
|
2021-03-25 11:40:07 +00:00
|
|
|
[[noreturn]] void TintInternalCompilerErrorReporter(
|
|
|
|
const tint::diag::List& diagnostics) {
|
|
|
|
auto printer = tint::diag::Printer::create(stderr, true);
|
|
|
|
tint::diag::Formatter{}.format(diagnostics, printer.get());
|
|
|
|
__builtin_trap();
|
|
|
|
}
|
|
|
|
|
2021-06-24 11:27:36 +00:00
|
|
|
[[noreturn]] void ValidityErrorReporter(const tint::diag::List& diags) {
|
2021-05-05 21:12:22 +00:00
|
|
|
auto printer = tint::diag::Printer::create(stderr, true);
|
|
|
|
printer->write(
|
|
|
|
"Fuzzing detected valid input program being transformed into an invalid "
|
2021-05-06 15:43:33 +00:00
|
|
|
"output progam\n",
|
2021-05-05 21:12:22 +00:00
|
|
|
{diag::Color::kRed, true});
|
2021-06-24 11:27:36 +00:00
|
|
|
tint::diag::Formatter().format(diags, printer.get());
|
2021-05-05 21:12:22 +00:00
|
|
|
__builtin_trap();
|
|
|
|
}
|
|
|
|
|
2021-05-06 15:43:33 +00:00
|
|
|
transform::VertexAttributeDescriptor ExtractVertexAttributeDescriptor(
|
|
|
|
Reader* r) {
|
|
|
|
transform::VertexAttributeDescriptor desc;
|
|
|
|
desc.format = r->enum_class<transform::VertexFormat>(
|
|
|
|
static_cast<uint8_t>(transform::VertexFormat::kLastEntry) + 1);
|
|
|
|
desc.offset = r->read<uint64_t>();
|
|
|
|
desc.shader_location = r->read<uint32_t>();
|
|
|
|
return desc;
|
|
|
|
}
|
|
|
|
|
|
|
|
transform::VertexBufferLayoutDescriptor ExtractVertexBufferLayoutDescriptor(
|
|
|
|
Reader* r) {
|
|
|
|
transform::VertexBufferLayoutDescriptor desc;
|
|
|
|
desc.array_stride = r->read<uint64_t>();
|
|
|
|
desc.step_mode = r->enum_class<transform::InputStepMode>(
|
|
|
|
static_cast<uint8_t>(transform::InputStepMode::kLastEntry) + 1);
|
|
|
|
desc.attributes = r->vector(ExtractVertexAttributeDescriptor);
|
|
|
|
return desc;
|
|
|
|
}
|
2021-04-28 15:35:43 +00:00
|
|
|
|
2021-05-06 15:43:33 +00:00
|
|
|
} // namespace
|
2021-04-28 15:35:43 +00:00
|
|
|
|
2021-05-06 15:43:33 +00:00
|
|
|
Reader::Reader(const uint8_t* data, size_t size) : data_(data), size_(size) {}
|
2021-04-28 15:35:43 +00:00
|
|
|
|
2021-05-06 15:43:33 +00:00
|
|
|
std::string Reader::string() {
|
|
|
|
auto count = read<uint8_t>();
|
|
|
|
if (failed_ || size_ < count) {
|
|
|
|
mark_failed();
|
|
|
|
return "";
|
2021-04-28 15:35:43 +00:00
|
|
|
}
|
2021-05-06 15:43:33 +00:00
|
|
|
std::string out(data_, data_ + count);
|
|
|
|
data_ += count;
|
|
|
|
size_ -= count;
|
|
|
|
return out;
|
|
|
|
}
|
2021-04-28 15:35:43 +00:00
|
|
|
|
2021-05-06 15:43:33 +00:00
|
|
|
void Reader::mark_failed() {
|
|
|
|
size_ = 0;
|
|
|
|
failed_ = true;
|
|
|
|
}
|
2021-04-28 15:35:43 +00:00
|
|
|
|
2021-05-06 15:43:33 +00:00
|
|
|
void Reader::read(void* out, size_t n) {
|
|
|
|
if (n > size_) {
|
|
|
|
mark_failed();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
memcpy(&out, data_, n);
|
|
|
|
data_ += n;
|
|
|
|
size_ -= n;
|
|
|
|
}
|
2021-04-28 15:35:43 +00:00
|
|
|
|
2021-05-06 15:43:33 +00:00
|
|
|
void ExtractBindingRemapperInputs(Reader* r, tint::transform::DataMap* inputs) {
|
|
|
|
struct Config {
|
|
|
|
uint8_t old_group;
|
|
|
|
uint8_t old_binding;
|
|
|
|
uint8_t new_group;
|
|
|
|
uint8_t new_binding;
|
2021-06-04 20:41:47 +00:00
|
|
|
ast::Access new_access;
|
2021-05-06 15:43:33 +00:00
|
|
|
};
|
2021-04-28 15:35:43 +00:00
|
|
|
|
2021-05-06 15:43:33 +00:00
|
|
|
std::vector<Config> configs = r->vector<Config>();
|
2021-04-28 15:35:43 +00:00
|
|
|
transform::BindingRemapper::BindingPoints binding_points;
|
2021-06-07 08:07:11 +00:00
|
|
|
transform::BindingRemapper::AccessControls accesses;
|
2021-04-28 15:35:43 +00:00
|
|
|
for (const auto& config : configs) {
|
|
|
|
binding_points[{config.old_binding, config.old_group}] = {
|
|
|
|
config.new_binding, config.new_group};
|
2021-06-07 08:07:11 +00:00
|
|
|
accesses[{config.old_binding, config.old_group}] = config.new_access;
|
2021-04-28 15:35:43 +00:00
|
|
|
}
|
|
|
|
|
2021-06-07 08:07:11 +00:00
|
|
|
inputs->Add<transform::BindingRemapper::Remappings>(binding_points, accesses);
|
2021-04-28 15:35:43 +00:00
|
|
|
}
|
|
|
|
|
2021-05-06 15:43:33 +00:00
|
|
|
void ExtractFirstIndexOffsetInputs(Reader* r,
|
2021-04-28 15:35:43 +00:00
|
|
|
tint::transform::DataMap* inputs) {
|
|
|
|
struct Config {
|
|
|
|
uint32_t group;
|
|
|
|
uint32_t binding;
|
|
|
|
};
|
|
|
|
|
2021-05-06 15:43:33 +00:00
|
|
|
Config config = r->read<Config>();
|
2021-04-28 15:35:43 +00:00
|
|
|
inputs->Add<tint::transform::FirstIndexOffset::BindingPoint>(config.binding,
|
|
|
|
config.group);
|
|
|
|
}
|
|
|
|
|
2021-05-06 15:43:33 +00:00
|
|
|
void ExtractSingleEntryPointInputs(Reader* r,
|
2021-04-29 20:49:25 +00:00
|
|
|
tint::transform::DataMap* inputs) {
|
2021-05-06 15:43:33 +00:00
|
|
|
std::string input = r->string();
|
2021-04-29 20:49:25 +00:00
|
|
|
transform::SingleEntryPoint::Config cfg(input);
|
|
|
|
inputs->Add<transform::SingleEntryPoint::Config>(cfg);
|
2021-05-06 15:43:33 +00:00
|
|
|
}
|
2021-04-29 20:49:25 +00:00
|
|
|
|
2021-05-06 15:43:33 +00:00
|
|
|
void ExtractVertexPullingInputs(Reader* r, tint::transform::DataMap* inputs) {
|
|
|
|
transform::VertexPulling::Config cfg;
|
|
|
|
cfg.entry_point_name = r->string();
|
|
|
|
cfg.vertex_state = r->vector(ExtractVertexBufferLayoutDescriptor);
|
|
|
|
cfg.pulling_group = r->read<uint32_t>();
|
|
|
|
inputs->Add<transform::VertexPulling::Config>(cfg);
|
2021-04-29 20:49:25 +00:00
|
|
|
}
|
|
|
|
|
2021-01-12 16:23:48 +00:00
|
|
|
CommonFuzzer::CommonFuzzer(InputFormat input, OutputFormat output)
|
2021-01-18 19:48:54 +00:00
|
|
|
: input_(input),
|
|
|
|
output_(output),
|
|
|
|
transform_manager_(nullptr),
|
|
|
|
inspector_enabled_(false) {}
|
|
|
|
|
2021-01-12 16:23:48 +00:00
|
|
|
CommonFuzzer::~CommonFuzzer() = default;
|
|
|
|
|
|
|
|
int CommonFuzzer::Run(const uint8_t* data, size_t size) {
|
2021-03-25 11:40:07 +00:00
|
|
|
tint::SetInternalCompilerErrorReporter(&TintInternalCompilerErrorReporter);
|
|
|
|
|
2021-02-17 20:15:25 +00:00
|
|
|
Program program;
|
|
|
|
|
2021-01-12 16:23:48 +00:00
|
|
|
#if TINT_BUILD_WGSL_READER
|
2021-01-13 15:50:11 +00:00
|
|
|
std::unique_ptr<Source::File> file;
|
2021-01-12 16:23:48 +00:00
|
|
|
#endif // TINT_BUILD_WGSL_READER
|
|
|
|
|
|
|
|
switch (input_) {
|
|
|
|
#if TINT_BUILD_WGSL_READER
|
2021-02-19 19:04:18 +00:00
|
|
|
case InputFormat::kWGSL: {
|
2021-01-12 16:23:48 +00:00
|
|
|
std::string str(reinterpret_cast<const char*>(data), size);
|
2021-01-13 15:50:11 +00:00
|
|
|
file = std::make_unique<Source::File>("test.wgsl", str);
|
2021-02-17 20:15:25 +00:00
|
|
|
program = reader::wgsl::Parse(file.get());
|
2021-02-19 19:04:18 +00:00
|
|
|
break;
|
2021-01-12 16:23:48 +00:00
|
|
|
}
|
|
|
|
#endif // TINT_BUILD_WGSL_READER
|
|
|
|
#if TINT_BUILD_SPV_READER
|
2021-02-19 19:04:18 +00:00
|
|
|
case InputFormat::kSpv: {
|
2021-01-12 16:23:48 +00:00
|
|
|
size_t sizeInU32 = size / sizeof(uint32_t);
|
|
|
|
const uint32_t* u32Data = reinterpret_cast<const uint32_t*>(data);
|
|
|
|
std::vector<uint32_t> input(u32Data, u32Data + sizeInU32);
|
|
|
|
|
|
|
|
if (input.size() != 0) {
|
2021-02-17 20:15:25 +00:00
|
|
|
program = reader::spirv::Parse(input);
|
2021-01-12 16:23:48 +00:00
|
|
|
}
|
2021-02-19 19:04:18 +00:00
|
|
|
break;
|
2021-01-12 16:23:48 +00:00
|
|
|
}
|
|
|
|
#endif // TINT_BUILD_WGSL_READER
|
|
|
|
default:
|
2021-02-19 19:04:18 +00:00
|
|
|
return 0;
|
2021-01-12 16:23:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (output_ == OutputFormat::kNone) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2021-01-26 16:57:10 +00:00
|
|
|
if (!program.IsValid()) {
|
2021-06-24 18:10:46 +00:00
|
|
|
errors_ = diag::Formatter().format(program.Diagnostics());
|
2021-01-12 16:23:48 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2021-01-13 15:50:11 +00:00
|
|
|
if (inspector_enabled_) {
|
2021-01-26 16:57:10 +00:00
|
|
|
inspector::Inspector inspector(&program);
|
2021-01-13 15:50:11 +00:00
|
|
|
|
|
|
|
auto entry_points = inspector.GetEntryPoints();
|
|
|
|
if (inspector.has_error()) {
|
2021-06-24 18:10:46 +00:00
|
|
|
errors_ = inspector.error();
|
2021-01-13 15:50:11 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (auto& ep : entry_points) {
|
|
|
|
auto remapped_name = inspector.GetRemappedNameForEntryPoint(ep.name);
|
|
|
|
if (inspector.has_error()) {
|
2021-06-24 18:10:46 +00:00
|
|
|
errors_ = inspector.error();
|
2021-01-13 15:50:11 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto constant_ids = inspector.GetConstantIDs();
|
|
|
|
if (inspector.has_error()) {
|
2021-06-24 18:10:46 +00:00
|
|
|
errors_ = inspector.error();
|
2021-01-13 15:50:11 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto uniform_bindings =
|
|
|
|
inspector.GetUniformBufferResourceBindings(ep.name);
|
|
|
|
if (inspector.has_error()) {
|
2021-06-24 18:10:46 +00:00
|
|
|
errors_ = inspector.error();
|
2021-01-13 15:50:11 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto storage_bindings =
|
|
|
|
inspector.GetStorageBufferResourceBindings(ep.name);
|
|
|
|
if (inspector.has_error()) {
|
2021-06-24 18:10:46 +00:00
|
|
|
errors_ = inspector.error();
|
2021-01-13 15:50:11 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto readonly_bindings =
|
|
|
|
inspector.GetReadOnlyStorageBufferResourceBindings(ep.name);
|
|
|
|
if (inspector.has_error()) {
|
2021-06-24 18:10:46 +00:00
|
|
|
errors_ = inspector.error();
|
2021-01-13 15:50:11 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto sampler_bindings = inspector.GetSamplerResourceBindings(ep.name);
|
|
|
|
if (inspector.has_error()) {
|
2021-06-24 18:10:46 +00:00
|
|
|
errors_ = inspector.error();
|
2021-01-13 15:50:11 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto comparison_sampler_bindings =
|
|
|
|
inspector.GetComparisonSamplerResourceBindings(ep.name);
|
|
|
|
if (inspector.has_error()) {
|
2021-06-24 18:10:46 +00:00
|
|
|
errors_ = inspector.error();
|
2021-01-13 15:50:11 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto sampled_texture_bindings =
|
|
|
|
inspector.GetSampledTextureResourceBindings(ep.name);
|
|
|
|
if (inspector.has_error()) {
|
2021-06-24 18:10:46 +00:00
|
|
|
errors_ = inspector.error();
|
2021-01-13 15:50:11 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto multisampled_texture_bindings =
|
|
|
|
inspector.GetMultisampledTextureResourceBindings(ep.name);
|
|
|
|
if (inspector.has_error()) {
|
2021-06-24 18:10:46 +00:00
|
|
|
errors_ = inspector.error();
|
2021-01-13 15:50:11 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-12 16:23:48 +00:00
|
|
|
if (transform_manager_) {
|
2021-04-26 20:38:36 +00:00
|
|
|
auto out = transform_manager_->Run(&program, transform_inputs_);
|
2021-02-24 15:55:24 +00:00
|
|
|
if (!out.program.IsValid()) {
|
2021-06-24 11:27:36 +00:00
|
|
|
// Transforms can produce error messages for bad input.
|
|
|
|
// Catch ICEs and errors from non transform systems.
|
|
|
|
for (auto diag : out.program.Diagnostics()) {
|
|
|
|
if (diag.severity > diag::Severity::Error ||
|
|
|
|
diag.system != diag::System::Transform) {
|
|
|
|
ValidityErrorReporter(out.program.Diagnostics());
|
|
|
|
}
|
|
|
|
}
|
2021-01-12 16:23:48 +00:00
|
|
|
}
|
|
|
|
|
2021-01-26 16:57:10 +00:00
|
|
|
program = std::move(out.program);
|
2021-01-12 16:23:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
switch (output_) {
|
|
|
|
case OutputFormat::kWGSL:
|
|
|
|
#if TINT_BUILD_WGSL_WRITER
|
2021-06-24 18:10:46 +00:00
|
|
|
writer_ = std::make_unique<writer::wgsl::Generator>(&program);
|
2021-01-12 16:23:48 +00:00
|
|
|
#endif // TINT_BUILD_WGSL_WRITER
|
|
|
|
break;
|
|
|
|
case OutputFormat::kSpv:
|
|
|
|
#if TINT_BUILD_SPV_WRITER
|
2021-06-24 18:10:46 +00:00
|
|
|
writer_ = std::make_unique<writer::spirv::Generator>(&program);
|
2021-01-12 16:23:48 +00:00
|
|
|
#endif // TINT_BUILD_SPV_WRITER
|
|
|
|
break;
|
|
|
|
case OutputFormat::kHLSL:
|
|
|
|
#if TINT_BUILD_HLSL_WRITER
|
2021-06-24 18:10:46 +00:00
|
|
|
writer_ = std::make_unique<writer::hlsl::Generator>(&program);
|
2021-01-12 16:23:48 +00:00
|
|
|
#endif // TINT_BUILD_HLSL_WRITER
|
|
|
|
break;
|
|
|
|
case OutputFormat::kMSL:
|
|
|
|
#if TINT_BUILD_MSL_WRITER
|
2021-06-24 18:10:46 +00:00
|
|
|
writer_ = std::make_unique<writer::msl::Generator>(&program);
|
2021-01-12 16:23:48 +00:00
|
|
|
#endif // TINT_BUILD_MSL_WRITER
|
|
|
|
break;
|
|
|
|
case OutputFormat::kNone:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2021-06-24 18:10:46 +00:00
|
|
|
if (writer_) {
|
|
|
|
if (!writer_->Generate()) {
|
|
|
|
errors_ = writer_->error();
|
|
|
|
return 0;
|
|
|
|
}
|
2021-01-12 16:23:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2021-06-24 18:10:46 +00:00
|
|
|
const writer::Writer* CommonFuzzer::GetWriter() const {
|
|
|
|
return writer_.get();
|
|
|
|
}
|
|
|
|
|
2021-01-12 16:23:48 +00:00
|
|
|
} // namespace fuzzers
|
|
|
|
} // namespace tint
|