Repository navigation
Proposal: Improvements to the AFL++ fuzzing setup (test/fuzz) #4348
Replies: 2 comments 7 replies
|
Amazing @stevenjohnstone this is going to be great. I'm very eager to have this in Would it make sense to upstream a better grammar-mutator? Should we maybe use https://github.com/ruby/lrama and/or https://github.com/kddnewton/lrama-fuzz to make a good set? |
|
I have added a differential fuzzer to https://github.com/stevenjohnstone/prism/commits/sjj/fuzz-improvements/. The differential fuzzer can find inputs which are parsed differently by prism and parse.y so that they end up with material AST differences. The fuzzer will produce thousands of crashes as it finds minor variations which arrive at the same problem; a triage script will put these into tens of buckets and then they are clustered by a heuristic measure of closeness for backtraces, edge coverage etc. This (and some back and forth with gemini) got me down to about 60 (or so) ways that prism and parse.y diverge. There are also parse.y bugs and crashes in the ruby VM which I'll report on the ruby bugtracker. Should I make an issue on this repo for each finding or put them all into one mega-issue? |
Uh oh!
There was an error while loading. Please reload this page.
Summary
The fuzzing framework I contributed in
test/fuzzis a bit janky; mea culpa! I've been aware of several limitations in the currenttest/fuzzsetup that limit its throughput, coverage, and usability and prevented it from catching a number of parser and lexer bugs (such as AST location underflows, uninitialized fields, and state-dependent heredoc bugs). I've been running various hacked together things to make it better and decided it's time to bring them together in one (hopefully) easy to use package.I'd like to propose a set of improvements to
test/fuzzand get maintainer feedback on what would be welcome upstream.Limitations of the Current Setup
1. Control, Observability, and Workflow
screensession management intest/fuzz/parse.sh:parse.shstarts two separatescreensessions (grammar_fuzzerin the background with-d -m, andmain_fuzzerin the foreground) and then drops intoexec /bin/bash. There is no single session to navigate between workers, and interruptingmain_fuzzerwithCtrl+Cleavesgrammar_fuzzerrunning in the background while dropping into a shell inside the container instead of shutting down cleanly.llvm-cov) to see which parts ofsrc/the fuzzer is reaching.Makefile:fuzz-run-%runsbundle exec rake templateson the host before starting the container (requiring a host Ruby/bundler setup despitedocs/fuzzing.mdnoting that only Docker is needed), andfuzz-docker-buildhas no stamp file sodocker buildruns on everymake fuzz-run-parse.make fuzz-cleanis unguarded: Runningmake fuzz-cleanimmediately deletestest/fuzz/outputwithout confirmation, which can accidentally wipe out untriaged crashes.2. Crash Triage and Reproduction
test/fuzz/tools/minimize.shandtest/fuzz/tools/backtrace.shinside the container:crashes/, and each one has to be minimized and inspected manually.backtrace.shonly catches ASAN reports: It sets a breakpoint on__asan::ReportGenericError, which missesassert()/abort()failures, UBSAN errors, MSAN uninitialized reads, and memory leaks.halfemptyis unused in automation:test/fuzz/docker/Dockerfileclones and compiles Google Project Zero'shalfemptyminimizer, but nothing intest/fuzzuses it; I use it heavily in manual triaging but this should be automatedgdb/lldboutside the fuzzer has to be done by hand.3. Execution Speed and Instrumentation Overhead
-O0: InMakefile,build/fuzz.%andbuild/fuzz.%.cmplogare compiled with-O0 -fsanitize=fuzzer,address, which significantly reduces execution speed compared to-O2/-O3or using native-speed workers alongside sanitizer workers (such as AFL++'s SAND mode).parse.shonly launches 1 main worker and 1 grammar worker regardless of how many CPU cores are available.pm_serialize_parseruns on every iteration: Running full serialization on every fuzz input slows down parser exploration and mixes serializer control-flow edges into the parser's coverage map..treefiles are written directly to the bind-mounted volume (-v $(CURDIR):/prism -v $(FUZZ_OUTPUT_DIR):/fuzz_output), which becomes a heavy I/O bottleneck on macOS and Windows.pm_strncasecmp: Because Prism uses its ownpm_strncasecmpinsrc/strncasecmp.cinstead of libcstrncasecmp, AFL++'s CmpLog hooks cannot intercept case-insensitive comparisons to solve magic comments and encoding names.afl-clang-lto, ASAN andassert()branches are injected before LTO coverage instrumentation runs, causing AFL++ to instrument compiler-generated sanitizer checks alongside Prism's own control flow.4. Initial Corpus Seeding
test/prism/fixturesare skipped:parse.shpasses-i test/fuzz/corpus/parse(a symlink to../../prism/fixtures) directly toafl-fuzz. Becauseafl-fuzz -ionly reads top-level files and ignores subdirectories, nested fixture directories (whitequark/,unparser/,seismograph/, etc.) are never loaded.afl-cmin) or encoding variety: All top-level fixture files are fed directly intoafl-fuzzon every startup withoutafl-cmindeduplication, and there are no initial seeds exercising non-UTF-8# encoding:magic comments.-i test/fuzz/corpus/parsetogrammar_fuzzercauses Grammar-Mutator's ANTLR4 parser to spend startup time trying to reverse-parse every fixture file.5. Grammar-Mutator's Default
grammars/ruby.jsontest/fuzz/docker/Dockerfilebuilds Grammar-Mutator with its bundledgrammars/ruby.json, which is a tiny toy grammar that:id) and a single constant (MyConstant).#{...}), regular expressions, percent literals (%w,%i,%q,%r,%x), pattern matching (case ... in), endless methods, parameter forwarding, numbered/itparameters, character escapes (\C-,\M-,\u{...}), and magic comments.6. Missing Parser Options, AST Invariants, and Sanitizer Coverage
pm_options_tis never exercised:test/fuzz/parse.calways passesNULLfor options (pm_serialize_parse(buffer, input, size, NULL)), so version-specific syntax (3.3,3.4,3.5,4.1), command-line flags (-a,-e,-l,-n,-p,-x),frozen_string_literal,encoding_locked,partial_script,freeze, surrounding local scopes, parameter forwarding, andstart_lineare never fuzzed.test/fuzz/parse.cdoes not check the resulting AST or parser state:location.start <= location.endand lies within[input, input + size].parser->line_offsetsis monotonically increasing.pm_prettyprint,pm_errors_format,pm_dump_json) are not tested.src/arena.csub-allocates AST nodes out of 8 KB blocks by default, intra-arena buffer overflows and uninitialized reads don't crossmallocredzones unlessPM_ARENA_INITIAL_SIZEis set to0on sanitizer builds.PM_INTEGER_DIVIDE_THRESHOLD = 224chunks / 2,017 digits) andPM_ERR_NESTING_TOO_DEEP(PRISM_DEPTH_MAXIMUM = 10000) are unreachable within the-G 500input size limit unless lowered for fuzz builds.Proposed Direction
I have been working on a set of updates to
test/fuzzto address these points, including:screenwith atmux-based runner supporting interactive and daemon modes, a live fleet summary dashboard, HTML telemetry plots, and anllvm-covcoverage target.halfempty+afl-tmin, and generates self-containedtestcase.creproducers.test/fuzz/parse.c) to decodepm_options_tfrom a small input header, assert AST location bounds andline_offsetsmonotonicity, and exercisepm_prettyprint,pm_errors_format, andpm_dump_jsonalongsidepm_serialize.-DSLOWMSAN/LSAN workers), usingtmpfsfor scratch/tree I/O, and tuning compile-time flags (PM_ARENA_INITIAL_SIZE=0,PM_INTEGER_DIVIDE_THRESHOLD,PRISM_DEPTH_MAXIMUM, andstrncasecmpfor CmpLog).afl-cmin.Proof of Concept
Before opening PRs (either as a single PR or broken into smaller steps), I wanted to check if this direction sounds good to the maintainers and how you'd prefer to see it structured. I have changes on a branch which can be tried out pretty easily: https://github.com/stevenjohnstone/prism/commits/sjj/fuzz-improvements/.
I've been running the new fuzzer and it's already finding issues e.g. #4345, #4346, and #4347. I probably have another half-dozen to report.
All reactions