2021-07-13 12:01:25 +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 <cstddef>
|
|
|
|
#include <cstdint>
|
|
|
|
|
2021-09-03 00:59:35 +00:00
|
|
|
#include "fuzzers/random_generator.h"
|
2021-07-13 12:01:25 +00:00
|
|
|
#include "fuzzers/tint_ast_fuzzer/cli.h"
|
|
|
|
#include "fuzzers/tint_ast_fuzzer/mutator.h"
|
2021-08-31 22:07:17 +00:00
|
|
|
#include "fuzzers/tint_ast_fuzzer/override_cli_params.h"
|
2021-07-13 12:01:25 +00:00
|
|
|
#include "fuzzers/tint_common_fuzzer.h"
|
2021-10-05 19:19:26 +00:00
|
|
|
#include "fuzzers/transform_builder.h"
|
2021-07-13 12:01:25 +00:00
|
|
|
|
|
|
|
#include "src/reader/wgsl/parser.h"
|
|
|
|
#include "src/writer/wgsl/generator.h"
|
|
|
|
|
|
|
|
namespace tint {
|
|
|
|
namespace fuzzers {
|
|
|
|
namespace ast_fuzzer {
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
CliParams cli_params{};
|
|
|
|
|
|
|
|
extern "C" int LLVMFuzzerInitialize(int* argc, char*** argv) {
|
|
|
|
// Parse CLI parameters. `ParseCliParams` will call `exit` if some parameter
|
|
|
|
// is invalid.
|
2021-07-16 10:26:34 +00:00
|
|
|
cli_params = ParseCliParams(argc, *argv);
|
2021-08-31 22:07:17 +00:00
|
|
|
// For some fuzz targets it is desirable to force the values of certain CLI
|
|
|
|
// parameters after parsing.
|
|
|
|
OverrideCliParams(cli_params);
|
2021-07-13 12:01:25 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
extern "C" size_t LLVMFuzzerCustomMutator(uint8_t* data,
|
|
|
|
size_t size,
|
|
|
|
size_t max_size,
|
|
|
|
unsigned seed) {
|
2021-07-16 17:50:04 +00:00
|
|
|
Source::File file("test.wgsl", {reinterpret_cast<char*>(data), size});
|
2021-07-13 12:01:25 +00:00
|
|
|
auto program = reader::wgsl::Parse(&file);
|
2021-07-16 17:50:04 +00:00
|
|
|
if (!program.IsValid()) {
|
|
|
|
std::cout << "Trying to mutate an invalid program:" << std::endl
|
|
|
|
<< program.Diagnostics().str() << std::endl;
|
|
|
|
return 0;
|
2021-07-13 12:01:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Run the mutator.
|
2021-09-03 00:59:35 +00:00
|
|
|
RandomGenerator generator(seed);
|
|
|
|
ProbabilityContext probability_context(&generator);
|
2021-07-13 12:01:25 +00:00
|
|
|
program = Mutate(std::move(program), &probability_context,
|
|
|
|
cli_params.enable_all_mutations,
|
2021-07-16 17:50:04 +00:00
|
|
|
cli_params.mutation_batch_size, nullptr);
|
2021-07-13 12:01:25 +00:00
|
|
|
|
|
|
|
if (!program.IsValid()) {
|
|
|
|
std::cout << "Mutator produced invalid WGSL:" << std::endl
|
|
|
|
<< " seed: " << seed << std::endl
|
|
|
|
<< program.Diagnostics().str() << std::endl;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2021-07-16 17:50:04 +00:00
|
|
|
auto result = writer::wgsl::Generate(&program, writer::wgsl::Options());
|
|
|
|
if (!result.success) {
|
|
|
|
std::cout << "Can't generate WGSL for a valid tint::Program:" << std::endl
|
|
|
|
<< result.error << std::endl;
|
|
|
|
return 0;
|
2021-07-13 12:01:25 +00:00
|
|
|
}
|
|
|
|
|
2021-07-26 11:25:17 +00:00
|
|
|
if (result.wgsl.size() > max_size) {
|
2021-07-13 12:01:25 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2021-07-26 11:25:17 +00:00
|
|
|
// No need to worry about the \0 here. The reason is that if \0 is included by
|
|
|
|
// developer by mistake, it will be considered a part of the string and will
|
|
|
|
// cause all sorts of strange bugs. Thus, unless `data` below is used as a raw
|
|
|
|
// C string, the \0 symbol should be ignored.
|
|
|
|
std::memcpy( // NOLINT - clang-tidy warns about lack of null termination.
|
|
|
|
data, result.wgsl.data(), result.wgsl.size());
|
|
|
|
return result.wgsl.size();
|
2021-07-13 12:01:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) {
|
|
|
|
if (size == 0) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2021-07-16 17:50:04 +00:00
|
|
|
struct Target {
|
|
|
|
FuzzingTarget fuzzing_target;
|
|
|
|
OutputFormat output_format;
|
|
|
|
const char* name;
|
|
|
|
};
|
2021-07-13 12:01:25 +00:00
|
|
|
|
2021-07-16 17:50:04 +00:00
|
|
|
Target targets[] = {{FuzzingTarget::kWgsl, OutputFormat::kWGSL, "WGSL"},
|
|
|
|
{FuzzingTarget::kHlsl, OutputFormat::kHLSL, "HLSL"},
|
|
|
|
{FuzzingTarget::kMsl, OutputFormat::kMSL, "MSL"},
|
|
|
|
{FuzzingTarget::kSpv, OutputFormat::kSpv, "SPV"}};
|
2021-07-13 12:01:25 +00:00
|
|
|
|
|
|
|
for (auto target : targets) {
|
2021-07-16 17:50:04 +00:00
|
|
|
if ((target.fuzzing_target & cli_params.fuzzing_target) !=
|
|
|
|
target.fuzzing_target) {
|
2021-07-13 12:01:25 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2021-10-05 19:19:26 +00:00
|
|
|
TransformBuilder tb(data, size);
|
|
|
|
tb.AddTransform<tint::transform::Robustness>();
|
2021-08-11 13:02:45 +00:00
|
|
|
|
2021-07-16 17:50:04 +00:00
|
|
|
CommonFuzzer fuzzer(InputFormat::kWGSL, target.output_format);
|
2021-07-13 12:01:25 +00:00
|
|
|
fuzzer.EnableInspector();
|
2021-10-05 19:19:26 +00:00
|
|
|
fuzzer.SetTransformManager(tb.manager(), tb.data_map());
|
2021-08-11 13:02:45 +00:00
|
|
|
|
2021-07-16 17:50:04 +00:00
|
|
|
fuzzer.Run(data, size);
|
|
|
|
if (fuzzer.HasErrors()) {
|
2021-07-20 14:39:50 +00:00
|
|
|
std::cout << "Fuzzing " << target.name << " produced an error"
|
|
|
|
<< std::endl;
|
|
|
|
auto printer = tint::diag::Printer::create(stderr, true);
|
|
|
|
tint::diag::Formatter{}.format(fuzzer.Diagnostics(), printer.get());
|
2021-07-16 17:50:04 +00:00
|
|
|
}
|
2021-07-13 12:01:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
} // namespace ast_fuzzer
|
|
|
|
} // namespace fuzzers
|
|
|
|
} // namespace tint
|