Sanitizing Large Language Models in Bug Detection with Data-Flow

Chengpeng Wang, Wuqi Zhang, Zian Su, Xiangzhe Xu, Xiangyu ZhangEMNLP 2024

Large Language Models (LLMs) have demonstrated remarkable capabilities in understanding and analyzing source code. However, their practical utility in software engineering is often hampered by "hallucinations"—the tendency to report non-existent bugs. These false positives create significant noise for developers and undermine the reliability of AI-driven security tools. To address this, researchers from Purdue and HKUST developed LLMSAN, a framework designed to sanitize LLM outputs by enforcing rigorous data-flow validation.

The Challenge: Hallucinations in Bug Detection

Traditional static analysis tools are often rigid, requiring complete, compilable code and complex rule definitions. While LLMs offer a flexible, "few-shot" alternative capable of analyzing partial code snippets, they lack the deterministic rigor of traditional tools. An LLM might correctly identify a potential vulnerability but fail to notice a crucial sanitization check or a control flow path that makes the bug unreachable. Because these models typically provide explanations in unstructured natural language, verifying their reasoning is difficult and time-consuming.

Methods: LLMSAN and the "Prover" Paradigm

LLMSAN shifts the role of the LLM from a simple classifier to a "prover." Instead of merely stating that a bug exists, the model must provide a structured "proof" in the form of a data-flow path. This path consists of a sequence of (variable, line number) pairs that trace the movement of a "faulty" value (e.g., a null pointer or an unvalidated input) from its source to a sink where it causes a failure.

The LLMSAN architecture illustrating the generation and multi-stage sanitization of bug reports.

The framework validates these paths through a two-tier sanitization process:

1. Data Sanitization

This stage ensures the start and end points of the reported path are semantically valid.

  • Type Sanitizer: Uses a deterministic parser to verify that the variables involved have the appropriate types for the reported bug (e.g., ensuring a divisor is a numeric type).
  • Functionality Sanitizer: Employs a targeted LLM prompt to check if specific functions can actually produce the problematic values claimed in the bug report.

2. Flow Sanitization

This stage verifies the integrity of the path itself.

  • Order Sanitizer: A parser checks that the path respects the program's actual control flow, ensuring variables are not used before they are defined.
  • Reachability Sanitizer: An LLM analyzes local code snippets to determine if a value can truly transition between two points in the path, accounting for conditional logic (e.g., if (x != null)) that might block the flow.

By decomposing the validation into small, localized tasks, LLMSAN minimizes the risk of the validator itself hallucinating, leveraging the LLM's strengths in semantic understanding while maintaining structural correctness through parsing.

Evaluation and Key Insights

The researchers evaluated LLMSAN using the Juliet Test Suite (synthetic vulnerabilities) and TaintBench (real-world Android malware).

Venn diagrams showing the distribution of bugs caught by different sanitizers (TS, FS, OS, RS) across various vulnerability types.

The evaluation yielded several critical insights:

  • Precision Gains: On synthetic benchmarks, LLMSAN achieved a precision of 91.03%, representing a 21.98% improvement over standard few-shot prompting.
  • Real-World Performance: In tests against Pinpoint, a sophisticated industrial static analyzer, LLMSAN demonstrated superior performance in both precision (+15.36%) and recall (+3.61%) when identifying data leaks in Android applications.
  • The Power of Structure: Forcing the model to output structured data-flow paths acts as a "rigorous certificate," making it significantly harder for the model to "fake" a bug compared to natural language explanations.

Conclusion

LLMSAN demonstrates that the reliability of LLMs in software engineering can be significantly enhanced by combining their semantic intuition with structured, deterministic validation. By requiring "proof" for every detected bug, the framework transforms LLMs into more trustworthy partners in the development lifecycle. Future work aims to incorporate self-reflection, allowing the model to automatically correct rejected paths and further improve recall.

LLMSAN is open-sourced at https://github.com/chengpeng-wang/LLMSAN.

Summary updated

BibTeX
@inproceedings{wang2024sanitizing,
  author = {Chengpeng Wang and Wuqi Zhang and Zian Su and Xiangzhe Xu and Xiangyu Zhang},
  title = {{Sanitizing Large Language Models in Bug Detection with Data-Flow}},
  booktitle = {Conference on Empirical Methods in Natural Language Processing},
  year = {2024},
  doi = {10.18653/v1/2024.findings-emnlp.217},
}
Figure