FLAG: Finding Line Anomalies (in code) with Generative AI

Baleegh Ahmad, Benjamin Tan, Ramesh Karri, Hammond PearcearXiv 2023 · DOI

Abstract

FLAG (Finding Line Anomalies with Generative AI) is a novel framework designed to assist developers in identifying software defects by treating Large Language Models (LLMs) as "intent mirrors." By comparing a developer's actual code against an LLM's prediction of what should be there, FLAG identifies statistical outliers that frequently correspond to bugs. Evaluated on 121 benchmarks across C, Python, and Verilog, FLAG achieved an 83.5% recall rate while reducing the human manual review search space by roughly 85%.

Introduction

Traditional bug detection typically relies on static analysis or dynamic testing. Static analysis often requires rigid, language-specific rules and a compilable codebase, while dynamic testing necessitates extensive, labor-intensive test suites. FLAG addresses the need for a lightweight, language-agnostic tool that can identify potential bugs during the development process, even before the code is ready for compilation. The core hypothesis is that since bugs are rare anomalies, a buggy line will appear as a statistical outlier to an LLM trained on vast repositories of correct code.

The FLAG Framework

The FLAG pipeline operates on a line-by-line basis through a four-stage process as illustrated in the architecture below:

  1. Prompt Formation: For a target line $x$, FLAG constructs a prompt using the preceding 50 lines (prefix) and the following code (suffix).
  2. Line Generation: An LLM (e.g., GPT-3.5-turbo) generates its prediction for line $x$.
  3. Feature Extraction: The original line is compared against the generated line using metrics such as Levenshtein Distance (ld), BLEU scores for comments, and "Distance from Comment" (dfc).
  4. Classification: Threshold-based criteria are applied to these features to determine if a line should be "flagged" for human review.
The FLAG pipeline: from source code to anomaly classification.

Experimental Evaluation

The authors evaluated FLAG on 121 benchmarks spanning C, Python, and Verilog. These benchmarks included security vulnerabilities (CVEs/CWEs) like buffer overflows and cross-site scripting, as well as functional logic errors. The inclusion of Verilog highlights FLAG's utility in hardware design, demonstrating its language-agnostic nature.

Key Results

FLAG successfully identified 101 out of 121 defects. Crucially, it reduced the amount of code requiring human inspection to just 12–17% of the original file size. The study found that gpt-3.5-turbo generally outperformed older models in recall but exhibited a higher false-positive rate.

Example of FLAG's feature extraction and flagging process on a C code snippet.

A significant finding was the role of comments. FLAG's performance improved when code was proximal to comments, as they provide explicit "intent" context that helps the LLM recognize implementation failures. This is shown in the feature extraction table, where distance from comment (dfc) and Levenshtein distance (ld) serve as primary indicators for flagging anomalies.

Limitations and Future Directions

Despite its high recall, FLAG faces challenges regarding computational overhead; line-by-line analysis of large files can be time-consuming. Additionally, while the search space is significantly reduced, a 15% false-positive rate remains. Future work aims to optimize speed through "chunk-based" analysis and improve accuracy by using machine learning classifiers to tune anomaly thresholds dynamically.

Summary updated

BibTeX
@misc{ahmad2023flag,
  author = {Baleegh Ahmad and Benjamin Tan and Ramesh Karri and Hammond Pearce},
  title = {{FLAG: Finding Line Anomalies (in code) with Generative AI}},
  year = {2023},
  eprint = {2306.12643},
  archivePrefix = {arXiv},
  url = {https://arxiv.org/abs/2306.12643},
}
Figure