NeurIPS 2026Coming to NeurIPS?Explore papers, orals & directions

ANVIL: Anomaly-based Vulnerability Identification without Labelled Training Data

Ilya Grishchenko, Weizhou Wang, Eric Liu, Xiangyu Guo, David LiearXiv 2024 · DOI

Introduction

Vulnerability detection has traditionally been a supervised learning challenge, requiring vast, high-quality datasets of labeled "buggy" and "clean" code. However, such datasets are notoriously difficult to curate, often noisy, and prone to overfitting, which limits a model's ability to generalize to novel vulnerabilities or new codebases. ANVIL (Anomaly-based Vulnerability Identification) proposes a paradigm shift: reframing vulnerability detection as an anomaly detection task. This approach leverages the inherent "naturalness" of benign code captured by Large Language Models (LLMs) during pre-training. By identifying code that deviates from the model's expectations, ANVIL can pinpoint vulnerabilities without any labeled training data.

The ANVIL Methodology

At its core, ANVIL utilizes a Masked Code Reconstruction task. It employs the Fill-In-the-Middle (FIM) capabilities of modern code LLMs to predict missing segments of code.

The ANVIL pipeline: from masking code to generating an anomaly score.

The process involves several key steps:

  1. Masking and Adaptive Context (AC): Instead of using a fixed-size window, ANVIL identifies the largest "compound statement" (e.g., a function body or a loop block) surrounding a masked line. This ensures the LLM receives semantically complete context, which is more effective than raw text windows.
  2. Reconstruction: The LLM attempts to reconstruct the masked line based on this context.
  3. Hybrid Anomaly Scoring: ANVIL computes a composite score from four distinct signals:
  4. Cross-entropy loss: Measures the statistical improbability of the original line.
  5. Negative Exact Match: A binary penalty applied if the LLM's top prediction does not perfectly match the original code.
  6. First-token variance: Captures the model's uncertainty; vulnerable lines often lead to "hedged" predictions.
  7. AST Complexity: Weights the score based on the line's Abstract Syntax Tree complexity, as more complex logic is statistically more error-prone.

Key Insights: The "Tolstoy" Hypothesis for Code

The researchers hypothesize that "all happy (benign) lines are alike; each unhappy (vulnerable) line is unhappy in its own way." Because vulnerabilities represent a tiny fraction of the code in an LLM's training set, they effectively function as out-of-distribution samples. While benign code is predictable and generates high-confidence outputs, vulnerable code induces a specific type of confusion or low-confidence clustering in the LLM's predictions. ANVIL found that even if the statistical "loss" is low, a failure to achieve an exact match is a powerful indicator of an anomaly.

Evaluation and Results

ANVIL was evaluated against several benchmarks, including PrimeVul and a newly curated dataset of 2024 CVEs (CVEFixes) to ensure results weren't due to "data leakage" from the model's training set.

Performance metrics of ANVIL components across various context sizes.

The results demonstrate significant improvements over supervised state-of-the-art tools:

  • Superior Accuracy: On the PrimeVul dataset, ANVIL achieved up to 2x higher Top-3 accuracy and a 75% improvement in Normalized Mean First Ranking (N-MFR) compared to models like LineVul and LLMAO.
  • Zero-Day Generalization: ANVIL maintained high performance on the 2024 CVEFixes dataset, proving its ability to detect vulnerabilities disclosed after its primary model's training cutoff.
  • Real-World Impact: By integrating ANVIL's anomaly scores into a fuzzer (LibFuzzer) to prioritize "unusual" code paths, the researchers discovered two previously unknown vulnerabilities (a heap-use-after-free and a heap-buffer-overflow) in a popular multimedia library with over 11,000 GitHub stars.

Discussion and Future Work

While powerful, ANVIL is not without limitations. As an anomaly detector, it may flag highly creative or idiosyncratic (but safe) code as suspicious, leading to false positives. Additionally, the computational cost of running an LLM on every line of a large codebase is high. Future work aims to optimize this by using ANVIL as a targeted guide for other security tools like fuzzers. While currently focused on C/C++, the methodology is generalizable to other languages where logic bugs are the primary concern.

Summary updated

BibTeX
@misc{grishchenko2024anvil,
  author = {Ilya Grishchenko and Weizhou Wang and Eric Liu and Xiangyu Guo and David Lie},
  title = {{ANVIL: Anomaly-based Vulnerability Identification without Labelled Training Data}},
  year = {2024},
  eprint = {2408.16028},
  archivePrefix = {arXiv},
  url = {https://arxiv.org/abs/2408.16028},
}
Figure