Large Language Models Are Zero-Shot Fuzzers: Fuzzing Deep-Learning Libraries via Large Language Models

Yinlin Deng, Chunqiu Steven Xia, Haoran Peng, Chenyuan Yang, Lingming Zhang

TL;DR

This paper introduces TITANFUZZ, a novel approach that leverages Large Language Models (LLMs) like Codex and InCoder to automatically generate and mutate programs for fuzzing deep learning (DL) libraries. By treating LLMs as zero-shot fuzzers, TITANFUZZ significantly improves code coverage and discovers previously unknown bugs in popular DL frameworks like TensorFlow and PyTorch, overcoming limitations of traditional fuzzing techniques.

What Problem Does This Paper Address?

Deep learning (DL) systems are increasingly prevalent, relying heavily on complex libraries like TensorFlow and PyTorch. Ensuring the reliability and safety of these DL libraries is crucial. However, traditional fuzzing techniques struggle with DL libraries because they require inputs that adhere to both Python's syntax and semantics, as well as the intricate shape and type constraints of DL APIs for tensor computations. Existing fuzzers often fail to generate diverse and valid DL programs, missing bugs that arise from complex API sequences or subtle constraint violations.

Methods

TITANFUZZ employs a two-pronged LLM-based strategy:

  1. Initial Seed Generation: A generative LLM (Codex) is used with carefully crafted "step-by-step" prompts to generate high-quality seed programs. These prompts guide the LLM to import the target library, generate input data, and then invoke the target DL API, effectively learning API usage patterns from its training data.
  2. Evolutionary Fuzzing with Infilling LLMs: An evolutionary algorithm iteratively generates new test programs. This involves:
  3. Mutation Operators: Various operators (argument replacement, keyword insertion, prefix/suffix manipulation, method replacement) are used to mask parts of existing seed programs.
  4. Infilling LLM (InCoder): The masked programs are fed to an infilling LLM, which fills in the masked sections with contextually relevant code, creating diverse mutated programs.
  5. Mutation Operator Selection: A Beta-Bernoulli bandit algorithm (Thompson Sampling) adaptively learns which mutation operators are most effective for a given DL API, balancing exploration and exploitation.
  6. Fitness Function: A custom fitness function, combining dataflow graph depth, unique API calls, and penalizing repeated calls, guides the selection of seeds for mutation, prioritizing complex and diverse API sequences.
  7. Differential Testing Oracle: Generated programs are executed on both CPU and GPU, and differences in intermediate values or crashes are used to detect bugs.

Datasets

The paper evaluates TITANFUZZ on two major DL libraries:

  • PyTorch (v1.12)
  • TensorFlow (v2.10)

The LLMs (Codex and InCoder) are pre-trained on massive datasets of code from the internet, including a significant amount of DL library code, which allows them to implicitly learn DL API constraints. The evaluation uses existing DL library APIs and does not introduce new benchmarks.

Key Insights and Ideas

  • LLMs as Zero-Shot Fuzzers: The core insight is that modern, large-scale LLMs, trained on vast amounts of code, can directly generate and mutate valid DL programs for fuzzing without explicit fine-tuning for this specific task. They implicitly learn DL API syntax, semantics, and constraints.
  • Generative + Infilling LLM Synergy: Combining a generative LLM for initial seed creation with an infilling LLM for mutation and diversification is highly effective. Generative LLMs provide a strong starting point, while infilling LLMs allow for more nuanced and targeted program modifications.
  • Prompt Engineering is Crucial: The effectiveness of LLM-based generation is significantly boosted by structured, step-by-step prompts that guide the LLM through the process of importing libraries, generating data, and calling APIs.
  • Evolutionary Fuzzing Enhances LLM Capabilities: An evolutionary approach, guided by a sophisticated fitness function and adaptive mutation operator selection, further amplifies the LLMs' ability to explore the DL library API space and uncover complex bugs.
  • Beyond Simple API Calls: TITANFUZZ's ability to generate complex API sequences and Python-specific constructs (like loops) allows it to find bugs missed by traditional API-level fuzzers that focus on single API calls or model-level fuzzers with restrictive generation rules.

Key Results

  • Improved API Coverage: TITANFUZZ covers significantly more APIs than state-of-the-art fuzzers: 2215 APIs in TensorFlow and 1329 in PyTorch. This represents an increase of up to 91.11% over baselines.
  • Higher Code Coverage: TITANFUZZ achieves 20.98% line coverage on PyTorch and 39.97% on TensorFlow, outperforming baselines by up to 50.84% and 30.38% respectively.
  • Bug Detection: TITANFUZZ detected 65 bugs in total, with 53 confirmed. Notably, 41 of these confirmed bugs were previously unknown, with 8 already fixed by developers.
  • Effectiveness against Prior Work: Only 9 of the confirmed bugs could be found by prior API-level fuzzers, and none by model-level fuzzers.
  • Efficiency: While LLM inference can be slower, TITANFUZZ with seed generation alone (targeting APIs covered by baselines) can outperform them with less time.

Limitations and Future Work

  • Threats to Validity:
    • Internal: The authors acknowledge the potential for implementation errors and have addressed this through careful testing and code review.
  • External: The evaluation is limited to PyTorch and TensorFlow. While these are popular, future work could extend to other DL libraries.
  • Future Work:
    • The authors suggest that TITANFUZZ's approach is generalizable and can be extended to fuzzing/testing software systems from other application domains (e.g., compilers, interpreters, database systems).
  • The paper opens a new dimension for fuzzing by demonstrating LLMs' potential, encouraging further research in this direction.

Summary updated

BibTeX
@inproceedings{deng2023large,
  author = {Yinlin Deng and Chunqiu Steven Xia and Haoran Peng and Chenyuan Yang and Lingming Zhang},
  title = {{Large Language Models Are Zero-Shot Fuzzers: Fuzzing Deep-Learning Libraries via Large Language Models}},
  booktitle = {ISSTA},
  year = {2023},
  doi = {10.1145/3597926.3598067},
}
Figure