Description
Make it so that unit tests can ask for fuzz input:
test "foo" {
const input_bytes = std.testing.fuzzInput(.{});
try std.testing.expect(!std.mem.eql(u8, "canyoufindme", input_bytes));
}
Introduce flags to the compiler: -ffuzz
, -fno-fuzz
. These end up passing -fsanitize=fuzzer-no-link
to Clang for C/C++ files. Introduce build system equivalent API.
However, neither the CLI interface nor the build system interface is needed in order to enable fuzzing. The only thing that is needed is to ask for fuzz input in unit tests, as in the above example.
When the build runner interacts with the test runner, it learns which tests, if any, are fuzz tests. Then when unit tests pass, it moves on to fuzz testing, by providing our own implementation of the genetic algorithms that drive the input bytes (similar to libFuzzer or AFL), and re-compiling the unit test binary with -ffuzz
enabled.
Fuzz testing is level-driven so we will need some CLI to operate those options. For example, zig build --fuzz
might start fuzzing indefinitely, while zig build --fuzz=300s
declares success after fuzzing for five minutes. When fuzz testing is not requested, it defaults to a small number of iterations just to smoke test that it's all working.
Some sort of UI would be nice. For starters this could just be std.Progress
. In the future perhaps there could be a live-updating HTML page to visualize progress and code coverage in realtime. How cool would it be to watch source code turn from red to green live as the fuzzer finds new branches?
I think there's value in being able to fuzz test a mix of Zig and C/C++ source code, so let's start with evaluating LLVM's instrumentation and perhaps being compatible with it, or at least supporting it. First step is to implement the support library in Zig.
-ffuzz
will be made available as a comptime flag in @import("builtin")
so that it can be used, for example, to choose the naive implementation of std.mem.eql
which helps the fuzzer to find interesting branches.
Comments are welcome. Note this is an enhancement not a proposal. The question is not "whether?" but "how?".
Related: