Synthesizing program input grammars

Osbert Bastani, Rahul Sharma, Alex Aiken, Percy Liang

TL;DR

This paper introduces GLADE, a novel algorithm for automatically synthesizing context-free grammars that describe valid program inputs, using only example inputs and black-box access to the program. GLADE significantly outperforms existing grammar inference techniques in terms of speed and accuracy, and its synthesized grammars are effectively used to improve the coverage of grammar-based fuzzing tools.

What Problem Does This Paper Address?

Many software analysis tools, like fuzzers, rely on understanding the structure of program inputs. However, this information is often poorly documented or not available in a machine-readable format. Manually creating grammars for complex input formats is time-consuming and error-prone. Existing grammar inference algorithms struggle with this problem because they either overgeneralize (producing grammars that accept too many invalid inputs) or are too slow to be practical, especially when only black-box access to the program is available (meaning the code itself cannot be inspected). This paper tackles the challenge of automatically learning accurate and efficient grammars for program input languages in a black-box setting.

Methods

GLADE employs a two-phase approach to synthesize context-free grammars:

  1. Phase One: Regular Expression Synthesis: This phase starts with a small set of valid input examples and incrementally generalizes them into a regular expression. It achieves this by considering a rich set of potential generalizations (repetitions and alternations) and using carefully crafted "checks" (inputs generated on the fly) to reject overgeneralizations. This is a key innovation, as it actively generates negative examples to maintain precision, unlike traditional methods that rely on external oracles.
  2. Phase Two: Recursive Properties (Context-Free Grammar Synthesis): Once a regular expression is learned, this phase translates it into a context-free grammar. It then further generalizes this grammar by "merging" nonterminals that correspond to repeated subexpressions. This allows GLADE to learn recursive structures, such as nested parentheses or matching tags, which are beyond the capabilities of regular expressions.

A character generalization phase is also included, which can be performed at any point, to generalize specific terminal symbols. The algorithm is designed to be "greedy," making locally optimal choices at each step to balance precision and recall efficiently.

Datasets

The paper does not explicitly mention training datasets. For evaluation, they created four grammars encoding valid inputs for various programs:

  • A regular expression for matching URLs.
  • A grammar for the input accepted by GNU Grep.
  • A grammar for a simple Lisp parser.
  • A grammar for XML parsers (with a limited number of tags to keep it context-free).

For training and evaluation, they sampled inputs from these grammars. They also used real-world programs (sed, flex, grep, bison, xml, ruby, python, javascript) and their associated small test suites or documentation examples as seed inputs for fuzzing experiments.

Key Insights and Ideas

  • Active Generation of Negative Examples: GLADE's ability to generate its own "checks" (inputs that are likely invalid) on the fly is a crucial innovation. This allows it to maintain high precision without needing an external "equivalence oracle," which is often unavailable in practice.
  • Rich Generalization Space: By considering a wider range of potential generalizations (beyond simple repetitions and alternations), GLADE can more effectively capture complex input structures.
  • Two-Phase Synthesis for Expressiveness: The separation into learning regular expressions first and then learning context-free structures allows GLADE to handle both simpler and more complex, recursive grammars.
  • Greedy Approach for Efficiency: While a greedy strategy might miss the absolute optimal grammar, it makes the algorithm practical by efficiently searching a vast space of possible grammars. This is particularly important for real-world applications where perfect accuracy might be less critical than timely results.

Key Results

  • Superior Performance vs. Existing Algorithms: GLADE significantly outperformed L-Star and RPNI in terms of F1-score (a combined measure of precision and recall) and running time across four test grammars. L-Star and RPNI often failed to learn even simple grammars or timed out.
  • Effective Fuzzing: When used to synthesize grammars for fuzzing, GLADE substantially increased the normalized incremental coverage (new code paths discovered) by up to 6x compared to a naive fuzzer and also outperformed the state-of-the-art afl-fuzz.
  • Scalability: GLADE's performance scaled well with the number of seed inputs, and its running time was manageable even for complex grammars.
  • Learning Complex Structures: GLADE successfully learned grammars for formats like XML, which involve recursive structures and context-sensitive constraints (like unique attribute names), demonstrating its ability to go beyond regular languages.

Limitations and Future Work

  • Greedy Search Suboptimality: The paper acknowledges that the greedy search strategy might lead to suboptimal grammars in some cases, particularly when multiple incompatible candidates exist. This was illustrated with an example of extending the XML grammar.
  • Context-Free Approximation: GLADE synthesizes context-free grammars. However, some input languages might have context-sensitive properties (e.g., unique attribute names in XML), meaning the synthesized grammar might be a subset of the true language.
  • Future Work: The authors suggest that the character generalization phase could be interleaved with other phases. They also believe GLADE could be valuable beyond fuzzing, for tasks like generating input whitelists or reverse-engineering input formats. The potential for adapting Bayesian language learning algorithms to their active learning setting is also mentioned.

Summary updated

BibTeX
@inproceedings{bastani2017synthesizing,
  author = {Osbert Bastani and Rahul Sharma and Alex Aiken and Percy Liang},
  title = {{Synthesizing program input grammars}},
  booktitle = {PLDI},
  year = {2017},
  doi = {10.1145/3062341.3062349},
}
Figure