Transformer-Based Approaches for Source Code Vulnerability Detection

Koushik Ahamed Tonmoy, Afridi Hassan, Shahriyar Zaman RidoyMusIML @ ICML 2026

TL;DR

Researchers systematically benchmarked transformer-based models for detecting C/C++ vulnerabilities, finding that end-to-end fine-tuning is essential for performance. By ensembling models with different "inductive biases"—specifically UniXcoder and GraphCodeBERT—they achieved a state-of-the-art accuracy of 82.9% on the Draper VDISC dataset.

What Problem Does This Paper Address?

Software written in memory-unsafe languages like C and C++ remains the backbone of modern infrastructure, yet it is notoriously prone to vulnerabilities like buffer overflows and null pointer dereferences. Traditionally, we’ve relied on static analysis tools to find these bugs. However, these tools are often "brittle": they rely on hand-written rules and pattern matching that struggle with obfuscated code and generate a frustratingly high number of false positives.

The authors investigate whether modern Transformer models (the same architecture behind LLMs) can serve as a more robust, data-driven alternative. Specifically, they want to know:

  1. How much does task-specific fine-tuning actually help compared to using "frozen" pre-trained models?
  2. Which specific transformer architectures (CodeBERT, GraphCodeBERT, or UniXcoder) are best suited for security auditing?
  3. Can we combine these models to catch rare, hard-to-detect vulnerability types?

Methods

The researchers designed a multi-stage pipeline to evaluate four progressively more complex modeling strategies:

  1. Classical ML on Static Embeddings: Using a pre-trained CodeBERT model as a "feature extractor" to feed 768-dimensional vectors into traditional classifiers like XGBoost and LightGBM.
  2. Lightweight Neural Heads: Attaching a simple two-layer MLP to frozen transformer encoders.
  3. End-to-End Fine-Tuning: Updating all parameters of the transformer (CodeBERT, GraphCodeBERT, and UniXcoder) to specialize them for vulnerability detection.
  4. Stacked Ensembles: Taking the "logits" (raw predictions) from multiple fine-tuned models and feeding them into a logistic regression meta-classifier to get a final consensus.

The models chosen represent different "inductive biases": CodeBERT focuses on natural language/code relationships, GraphCodeBERT incorporates data-flow structures, and UniXcoder uses cross-modal pre-training (including Abstract Syntax Trees).

Datasets

The study uses the Draper VDISC dataset, a massive corpus of over 1.27 million C/C++ functions. The functions are labeled into five categories based on Common Weakness Enumeration (CWE) types:

  • CWE-119: Memory corruption.
  • CWE-120: Buffer overflows.
  • CWE-469: Pointer subtraction (an extremely rare class in this dataset).
  • CWE-476: NULL pointer dereference.
  • CWE-other: A residual category for other flaws.

To handle the massive size and class imbalance, the authors downsampled the data to a balanced split of approximately 20,000 training examples.

Key Insights and Ideas

  • The "UniXcoder Paradox": In a surprising twist, UniXcoder was the worst performing model when its parameters were frozen, but became the best single model once fine-tuned. This suggests a "representation-objective mismatch"—its pre-training is optimized for generation and retrieval, making its raw embeddings less linearly separable for classification until it is adapted to the specific task.
  • Fine-Tuning is Non-Negotiable: There is a massive ~24-point accuracy gap between classical baselines and fine-tuned models. Static representations simply aren't enough to capture the nuance of a security vulnerability; the model must be allowed to "specialize" its internal weights.
  • Architectural Diversity Wins: The best results came from ensembling UniXcoder with GraphCodeBERT. Because one focuses on semantic relationships and the other on structural data flow, they provide complementary "opinions" that a single model lacks.
  • The Rare Class Bottleneck: Most models completely failed to detect the rare CWE-469 class (0.00 F1 score). Only fine-tuned UniXcoder and the ensembles were able to recover this class, highlighting that model architecture choice is more critical than ensembling for catching "needle-in-a-haystack" bugs.

Key Results

  • Top Performance: The UniXcoder + GraphCodeBERT ensemble achieved the highest accuracy of 82.9% and a weighted F1-score of 0.828.
  • Single Model Leader: UniXcoder (fine-tuned) was the strongest individual performer at 81.8% accuracy.
  • Baseline Comparison: Classical learners (LightGBM) peaked at only 58.7% accuracy, proving that deep learning provides a significant leap over traditional feature-based ML for this task.
  • Rare Class Recovery: While most models ignored the rare CWE-469 class entirely, the best ensemble managed to reach an F1-score of 0.400 for it.

Limitations and Future Work

The authors acknowledge that class imbalance remains the "dominant bottleneck." Even with sophisticated transformers, the model's ability to detect rare vulnerabilities is significantly lower than its ability to find common ones.

Furthermore, because the dataset labels were generated by existing static analysis tools, the models may be "inheriting" the biases and blind spots of those tools. Future work should focus on imbalance-aware training (like targeted resampling or specialized loss functions) and using human-verified datasets to ensure the models are learning genuine security flaws rather than just mimicking the quirks of older software.

Summary updated

BibTeX
@inproceedings{tonmoy2026transformer,
  author = {Koushik Ahamed Tonmoy and Afridi Hassan and Shahriyar Zaman Ridoy},
  title = {{Transformer-Based Approaches for Source Code Vulnerability Detection}},
  booktitle = {MusIML @ ICML},
  year = {2026},
  url = {https://openreview.net/forum?id=AW3jPDKxsw},
}
Figure