DeepFuzz: Automatic Generation of Syntax Valid C Programs for Fuzz Testing

Xiao Liu, Xiaoting Li, Rupesh Prajapati, Dinghao WuAAAI 2019

Introduction

Compilers such as GCC and Clang are fundamental to software development, yet verifying their correctness remains a significant challenge. Fuzz testing—the process of providing random or mutated inputs to trigger crashes—is a primary method for discovering compiler bugs. However, compilers are highly sensitive to input syntax. Randomly generated strings are typically rejected by the parser, preventing the fuzzer from reaching deeper logic like optimization and code generation.

Traditional "grammar-based fuzzing" relies on manually defined rules to produce valid code, a process that is labor-intensive and often misses edge cases. DeepFuzz addresses this by using a generative Sequence-to-Sequence (Seq2Seq) model to learn the implicit grammar of C from existing test suites, automatically generating syntactically valid programs without manual rule-writing.

Methodology

DeepFuzz replaces manual grammar rules with a neural network architecture designed to predict code continuations.

The DeepFuzz Framework

The system operates in two main phases: model training and program generation. The model is a character-level Seq2Seq architecture utilizing Long Short-Term Memory (LSTM) units. It is trained to "translate" a prefix of C code into a valid continuation.

DeepFuzz system architecture showing the Seq2Seq model and the fuzz testing pipeline.

Data Preprocessing and Training

The model was trained on the GCC test suite, which contains approximately 10,000 small, well-formed C programs covering a wide range of C11 features. To improve learning efficiency, the authors preprocessed the data by removing comments, unifying whitespace, and expanding macros.

Generation and Mutation Strategies

DeepFuzz employs several strategies to generate new test cases:

  • Sampling Variants: The researchers compared NoSample (greedy selection), Sample (probabilistic selection), and SampleSpace (probabilistic sampling only after whitespace).
  • Mutation Strategies: Rather than generating entire programs from scratch, DeepFuzz takes "seed" programs and injects new code using three methods: G1 (single-location insertion), G2 (multi-location random insertion), and G3 (line replacement).

Experimental Results

The effectiveness of DeepFuzz was evaluated based on the syntactic validity of generated programs and the resulting compiler coverage.

Syntactic Validity

A key finding was that character-level RNNs can successfully learn complex language structures like nested braces and semicolons. As shown in the training progress, the model's ability to produce valid code improves significantly over time.

Pass rate of generated programs across different sampling methods over 50 training epochs.

At its peak (30 epochs), DeepFuzz achieved a pass rate of 82.63%, indicating that the vast majority of generated programs were syntactically valid. While greedy sampling (NoSample) yielded the highest validity, probabilistic sampling (Sample) proved more effective at exploring complex compiler states.

Coverage and Bug Discovery

DeepFuzz significantly outperformed the industry-standard random generator, Csmith. In testing GCC, DeepFuzz improved line coverage by 7.14%, compared to just 0.45% for Csmith. Most importantly, the tool successfully uncovered eight previously unknown bugs in GCC, specifically "internal compiler errors" (ICE) where the compiler crashed during optimization.

Discussion and Future Work

While DeepFuzz demonstrates the power of neural-based grammar learning, it faces certain limitations. Because it operates at the character level, the model occasionally loses global context, leading to semantic errors such as using undeclared variables. Furthermore, while the generated code is syntactically correct, it is often semantically nonsensical, which limits its ability to find "wrong code" bugs. Future work may involve moving to token-level modeling to better capture long-range dependencies and semantic relationships.

Summary updated

BibTeX
@inproceedings{liu2019deepfuzz,
  author = {Xiao Liu and Xiaoting Li and Rupesh Prajapati and Dinghao Wu},
  title = {{DeepFuzz: Automatic Generation of Syntax Valid C Programs for Fuzz Testing}},
  booktitle = {AAAI Conference on Artificial Intelligence},
  year = {2019},
  doi = {10.1609/aaai.v33i01.33011044},
}
Figure