LLMDFA: Analyzing Dataflow in Code with Large Language Models

Chengpeng Wang, Wuqi Zhang, Zian Su, Xiangzhe Xu, Xiaoheng Xie, Xiangyu Zhang

TL;DR

LLMDFA is a novel framework that enables precise, compilation-free dataflow analysis by combining the code-comprehension strengths of Large Language Models (LLMs) with the rigor of formal tools like SMT solvers. By decomposing analysis into sub-tasks and using LLMs to synthesize executable scripts rather than guessing results, it achieves an average precision of 87.10% and significantly outperforms traditional static analyzers on both synthetic and real-world code.

What Problem Does This Paper Address?

Dataflow analysis (DFA) is the "bread and butter" of program analysis, used to track how values move from a source (like user input) to a sink (like a sensitive API or a divisor). However, traditional DFA tools face two major hurdles:

Traditional DFA Limitations
  1. The Compilation Wall: Most tools require code to be fully compilable to generate Intermediate Representations (IRs). This makes them useless for the "incomplete" code snippets often found in IDEs or during early development.
  2. The Expertise Gap: Customizing an analyzer for a new bug type usually requires deep expertise in compiler theory and IRs, which most developers lack.

While LLMs seem like a natural fit for "reading" code, they suffer from hallucinations—they might imagine a dataflow path that doesn't exist or fail to understand complex logical constraints (path feasibility). LLMDFA aims to provide a compilation-free, customizable analysis framework that mitigates these hallucinations.

Methods

The authors introduce a three-phase workflow that "outsources" delicate reasoning from the LLM to external expert tools:

LLMDFA Framework Architecture
  1. Phase I: Source/Sink Extraction: Instead of asking the LLM "where are the sources?", LLMDFA asks the LLM to synthesize a Python script using the tree-sitter parsing library. This script traverses the Abstract Syntax Tree (AST) to find points of interest deterministically. If the script fails, the LLM enters an iterative "fix-it" loop based on error messages.
  2. Phase II: Dataflow Summarization: To handle large codebases that exceed context windows, LLMDFA summarizes dataflow facts within individual functions. It uses few-shot Chain-of-Thought (CoT) prompting to align the LLM with program semantics, asking it to explain how a value moves from an input to an output step-by-step.
  3. Phase III: Path Feasibility Validation: This is the "aha" moment. LLMs are notoriously bad at formal logic. LLMDFA solves this by having the LLM write a Z3 SMT solver script to validate if a specific code path is actually reachable. By decoupling constraint collection from constraint solving, the framework ensures mathematical correctness.

Datasets

The researchers evaluated LLMDFA across several diverse benchmarks:

  • Juliet Test Suite: A massive synthetic benchmark for C/C++ and Java. They focused on three bug types: Divide-by-Zero (DBZ), Cross-Site Scripting (XSS), and OS Command Injection (OSCI).
  • TaintBench: A collection of 39 real-world Android malware applications characterized by complex, customized dataflow needs.
  • SecBench.js: A real-world JavaScript benchmark used to test the framework's multi-linguistic capabilities.

Key Insights and Ideas

  • Synthesis over Direct Inference: The core philosophy is: Don't ask the LLM for the answer; ask it for the tool that finds the answer. By generating scripts for tree-sitter and Z3, the system moves from probabilistic guessing to deterministic execution.
  • Modular Summarization: By breaking the analysis into function-level summaries, the authors prevent the "hallucination accumulation" that occurs when an LLM tries to reason about a massive, multi-file execution trace all at once.
  • The "Compilation-Free" Advantage: Because it operates on ASTs rather than compiled IRs, LLMDFA can analyze code that is broken, missing dependencies, or written in multiple languages (Java, C++, JS) with minimal configuration changes (often <100 lines of code).

Key Results

LLMDFA was tested using models like GPT-4, Gemini 1.0 Pro, and Claude 3 Opus, yielding impressive metrics:

  • High Accuracy: On average, it achieved 87.10% precision and 80.77% recall.
  • Outperforming Baselines: It surpassed industrial tools like CodeFuseQuery and Pinpoint, improving the F1 score by up to 0.35.
  • Real-World Utility: On the TaintBench Android dataset, it achieved a 75.38% precision, proving it can handle the "messy" nature of real-world malware analysis where traditional tools often fail due to build errors.
  • Cost Efficiency: The average cost for detecting a bug was roughly $0.04 to $0.14, making it a viable option for continuous integration pipelines.

Limitations and Future Work

Despite its success, the authors note several areas for improvement:

  • Prompt Length & Token Cost: For extremely large functions, the CoT prompts can become long and expensive.
  • Complex Pointers: While it handles basic pointers, highly sophisticated pointer arithmetic or deep nested global variables can still lead to imprecision.
  • Library Semantics: The system sometimes struggles with the "hidden" logic of external library functions unless they are explicitly modeled in the prompt.
  • Future Directions: The authors suggest fine-tuning LLMs specifically on dataflow facts and exploring parallelization to speed up the analysis of large-scale software projects.

Reference: Wang, C., et al. (2024). LLMDFA: Analyzing Dataflow in Code with Large Language Models. 38th Conference on Neural Information Processing Systems (NeurIPS 2024). GitHub Repository

Summary updated

BibTeX
@inproceedings{wang2024llmdfa,
  author = {Chengpeng Wang and Wuqi Zhang and Zian Su and Xiangzhe Xu and Xiaoheng Xie and Xiangyu Zhang},
  title = {{LLMDFA: Analyzing Dataflow in Code with Large Language Models}},
  booktitle = {Advances in Neural Information Processing Systems},
  year = {2024},
  url = {https://openreview.net/forum?id=QZ2d8E8Whu},
}
Figure