Abstract
Software vulnerabilities existing in a program or function of computer systems are a serious and crucial concern. Typically, in a program or function consisting of hundreds or thousands of source code statements, there are only few statements causing the corresponding vulnerabilities. Vulnerability labeling is currently done on a function or program level by experts with the assistance of machine learning tools. Extending this approach to the code statement level is much more costly and time-consuming and remains an open problem. In this paper we propose a novel end-to-end deep learning-based approach to identify the vulnerability-relevant code statements of a specific function. Inspired by the specific structures observed in real world vulnerable code, we first leverage mutual information for learning a set of latent variables representing the relevance of the source code statements to the corresponding function's vulnerability. We then propose novel clustered spatial contrastive learning in order to further improve the representation learning and the robust selection process of vulnerability-relevant code statements. Experimental results on real-world datasets of 200k+ C/C++ functions show the superiority of our method over other state-of-the-art baselines. In general, our method obtains a higher performance in VCP, VCA, and Top-10 ACC measures of between 3% to 14% over the baselines when running on real-world datasets in an unsupervised setting.
Introduction
Software Vulnerability Detection (SVD) is a critical task in maintaining secure computing environments. While deep learning models have achieved significant success in classifying entire functions as vulnerable or benign, identifying the specific lines of code responsible for the vulnerability remains a challenge. This "needle in a haystack" problem is exacerbated by the lack of statement-level ground truth labels, as manual labeling by experts is prohibitively expensive and slow. Most existing automated tools stop at the function level, leaving developers to manually hunt through hundreds of lines of code for the actual bug.
Methodology: The LEAP Framework
To bridge the gap between function-level classification and statement-level identification, we introduce LEAP. This framework treats statement selection as a latent variable problem, identifying vulnerability-relevant code without requiring line-by-line training data.
The LEAP architecture consists of several key components:
- Learnable Selection Process: For each statement in a function, the model learns Bernoulli latent variables (0 or 1). A value of 1 indicates the statement is considered "vulnerability-relevant."
- Mutual Information (MI) Maximization: The objective function maximizes the MI between the selected subset of code and the overall function label. This ensures the chosen lines are those that provide the most information for predicting if a function is vulnerable.
- Clustered Spatial Contrastive Learning (CSCL): This novel technique groups functions with similar vulnerability patterns into clusters using k-means. By applying contrastive learning, the model forces representations of functions within the same cluster to be similar, helping it learn the underlying semantics of bugs regardless of their location in the code.
- Differentiable Selection: Since selecting discrete lines is non-differentiable, the Gumbel-Softmax trick is used to make the selection process continuous and trainable via backpropagation.
Key Insights: Patterns and Sparsity
A fundamental observation driving LEAP is that vulnerabilities are sparse—often residing in just one or two lines—and follow global patterns. Logic flaws like buffer overflows or use-after-free errors look similar across different programs, even if they appear at different "spatial" locations (line numbers) within the source code.
By forcing the model to select a limited number of statements (e.g., the top 10), LEAP effectively filters out "boilerplate" code. Furthermore, by clustering these patterns, the model learns the semantics of the bug rather than just memorizing specific line features. This information-theoretic objective naturally pushes the model to find the "guilty" statements even in an unsupervised setting.
Experimental Results
LEAP was evaluated on three large-scale C/C++ datasets: CWE-119 (buffer errors), CWE-399 (resource management), and Big-Vul (over 188,000 functions).
In unsupervised settings, LEAP outperformed state-of-the-art baselines (such as L2X and LineVul) by 3% to 14% across metrics like Vulnerability Coverage Proportion (VCP) and Top-10 Accuracy. Notably, on the CWE-399 dataset, LEAP achieved a 96.6% VCP, successfully capturing nearly all vulnerable statements within its top selections. When provided with just 10% of statement-level ground truth in a semi-supervised setup, performance reached near-perfect scores on several CWE benchmarks.
Conclusion
LEAP represents a significant advancement in automated vulnerability localization. By combining information theory with contrastive learning, it provides a scalable solution for identifying the specific lines of code that compromise software security without the need for exhaustive manual labeling. Future work will focus on dynamic selection thresholds to accommodate bugs of varying lengths and extending the framework to memory-safe languages.
Summary updated