Tint currently has two fuzzer target executables: tint_wgsl_fuzzer which takes WGSL source code as input and tint_ir_fuzzer which takes a protobuf binary file as input.
Both fuzzers are implemented using libFuzzer, and are automatically and continuously run by Chromium's ClusterFuzz infrastructure. Fuzzer targets are automatically found via gn refs. See tint.gni for the core fuzzer target rules.
Tint's fuzzers are implemented as functions registered with the macros:
TINT_WGSL_PROGRAM_FUZZER() registers a fuzzer function that is handed a tint::Program.TINT_IR_MODULE_FUZZER() registers a fuzzer function that is handed a tint::core::ir::Module.The fuzzer targets can be build with either CMake or GN:
TINT_BUILD_FUZZERS=1 (pass -DTINT_BUILD_FUZZERS=1 to CMake, or set in CMakeCache.txt)use_libfuzzer = true in args.gn.The tint_wgsl_fuzzer and tint_ir_fuzzer executables accept the standard libFuzzer command line arguments with extended command line arguments described below.
There's also a helper tool to run the fuzzers locally:
To run the local fuzzers across the full number of CPU threads available on the system, seeded with the corpus in test/tint, run:
tools/run fuzz
To check that all the test files in test/tint pass the fuzzers without error and then exit, run:
tools/run fuzz --check
Note: This is run by Dawn‘s CQ presubmit to check that fuzzers aren’t accidentally broken.
tint::Program fuzzerCreate a new source file with a _fuzz.cc suffix.
#include "src/tint/cmd/fuzz/wgsl/fuzz.h"
Define a function in a (possibly nested) anonymous namespace with one of the signatures:
void MyFuzzer(const tint::Program& program /*, ...additional fuzzed parameters... */) {void MyFuzzer(const tint::Program& program, const tint::fuzz::wgsl::Context& context /*, ...additional fuzzed parameters... */) {The optional context parameter holds information about the Program and the environment used to run the fuzzers.
Note: Any number of additional fuzzer-populated parameters can be appended to the function signature.
Implement your fuzzer function, using TINT_ICE() to catch invalid state. Return early if the fuzzer cannot handle the input.
At the bottom of the file, in the global namespace, register the fuzzer with: TINT_WGSL_PROGRAM_FUZZER(MyFuzzer);
Use tools/run gen build to generate the build files for this new fuzzer.
Example:
#include "src/tint/cmd/fuzz/wgsl/fuzz.h" namespace tint::my_namespace { namespace { bool CanRun(const tint::Program& program) { if (program.AST().HasOverrides()) { return false; // Overrides are not supported. } return true; } void MyWGSLFuzzer(const tint::Program& program, bool a_fuzzer_provided_value) { if (!CanRun(program)) { return; } // Check something with program. } } // namespace } // namespace tint::my_namespace TINT_WGSL_PROGRAM_FUZZER(tint::my_namespace::MyWGSLFuzzer);
tint::core::ir::Module fuzzerCreate a new source file with a _fuzz.cc suffix.
#include "src/tint/cmd/fuzz/ir/fuzz.h"
Define a function in a (possibly nested) anonymous namespace with the signature:
void MyFuzzer(core::ir::Module& module /*, ...additional fuzzed parameters... */) {Implement your fuzzer function, using TINT_ICE() to catch invalid state. Return early if the fuzzer cannot handle the input.
At the bottom of the file, in the global namespace, register the fuzzer with: TINT_IR_MODULE_FUZZER(MyFuzzer);
Use tools/run gen build to generate the build files for this new fuzzer.
Example:
#include "src/tint/cmd/fuzz/ir/fuzz.h" namespace tint::my_namespace { namespace { void MyIRFuzzer(core::ir::Module& module) { // Do something interesting with module. } } // namespace } // namespace tint::my_namespace TINT_IR_MODULE_FUZZER(tint::my_namespace::MyIRFuzzer);
WGSL and IR fuzzer functions can also declare any number of additional parameters, which will be populated with fuzzer provided data. These additional parameters must come at the end of the signatures described above, and can be of the following types:
TINT_REFLECT. Note: It's recommended to use a const reference, for these to avoid pass-by-value overheads.TINT_REFLECT_ENUM_RANGE.Tint has two fuzzer executable targets:
tint_wgsl_fuzzertint_wgsl_fuzzer accepts WGSL textual input and parses line comments (//) as a base-64 binary encoded data stream for the additional fuzzer parameters.
The entry point for the fuzzer lives at src/tint/cmd/fuzz/wgsl/main_fuzz.cc.
--help: lists the command line arguments.--filter: only runs the fuzzer functions that contain the given string in its name.--concurrent: each of the fuzzer functions will be run on a separate, concurrent thread. This potentially offers performance improvements, and also tests for concurrent execution.--verbose : prints verbose information about what the fuzzer is doing.The tint_wgsl_fuzzer will do the following:
TINT_WGSL_PROGRAM_FUZZER()Program to an IR module and run the function for each function registered with TINT_IR_MODULE_FUZZER(). Note: The Program is converted to an IR module for each registered IR fuzzer as the module is mutable.tint_ir_fuzzerTODO: Document when landed.