Table of Contents
Fetching ...

Evaluating Line-level Localization Ability of Learning-based Code Vulnerability Detection Models

Marco Pintore, Giorgio Piras, Angelo Sotgiu, Maura Pintor, Battista Biggio

TL;DR

ML vulnerability detectors frequently localize at the function level rather than pinpointing exact vulnerable lines. The authors introduce Detection Alignment (DA), an explainability-based evaluation that uses line-level attributions and a Jaccard-style metric to quantify alignment between influential lines and ground-truth vulnerable lines. DA is model-agnostic and applied to three transformer-based detectors across three datasets (BigVul, Devign, PrimeVul), revealing consistent misalignment due to spurious correlations even when function-level metrics look strong. The approach provides a practical trust signal for debugging and can be extended as a regularizer during training, broadening evaluation of code vulnerability detectors beyond standard performance metrics.

Abstract

To address the extremely concerning problem of software vulnerability, system security is often entrusted to Machine Learning (ML) algorithms. Despite their now established detection capabilities, such models are limited by design to flagging the entire input source code function as vulnerable, rather than precisely localizing the concerned code lines. However, the detection granularity is crucial to support human operators during software development, ensuring that such predictions reflect the true code semantics to help debug, evaluate, and fix the detected vulnerabilities. To address this issue, recent work made progress toward improving the detector's localization ability, thus narrowing down the vulnerability detection "window" and providing more fine-grained predictions. Such approaches, however, implicitly disregard the presence of spurious correlations and biases in the data, which often predominantly influence the performance of ML algorithms. In this work, we investigate how detectors comply with this requirement by proposing an explainability-based evaluation procedure. Our approach, defined as Detection Alignment (DA), quantifies the agreement between the input source code lines that most influence the prediction and the actual localization of the vulnerability as per the ground truth. Through DA, which is model-agnostic and adaptable to different detection tasks, not limited to our use case, we analyze multiple learning-based vulnerability detectors and datasets. As a result, we show how the predictions of such models are consistently biased by non-vulnerable lines, ultimately highlighting the high impact of biases and spurious correlations. The code is available at https://github.com/pralab/vuln-localization-eval.

Evaluating Line-level Localization Ability of Learning-based Code Vulnerability Detection Models

TL;DR

ML vulnerability detectors frequently localize at the function level rather than pinpointing exact vulnerable lines. The authors introduce Detection Alignment (DA), an explainability-based evaluation that uses line-level attributions and a Jaccard-style metric to quantify alignment between influential lines and ground-truth vulnerable lines. DA is model-agnostic and applied to three transformer-based detectors across three datasets (BigVul, Devign, PrimeVul), revealing consistent misalignment due to spurious correlations even when function-level metrics look strong. The approach provides a practical trust signal for debugging and can be extended as a regularizer during training, broadening evaluation of code vulnerability detectors beyond standard performance metrics.

Abstract

To address the extremely concerning problem of software vulnerability, system security is often entrusted to Machine Learning (ML) algorithms. Despite their now established detection capabilities, such models are limited by design to flagging the entire input source code function as vulnerable, rather than precisely localizing the concerned code lines. However, the detection granularity is crucial to support human operators during software development, ensuring that such predictions reflect the true code semantics to help debug, evaluate, and fix the detected vulnerabilities. To address this issue, recent work made progress toward improving the detector's localization ability, thus narrowing down the vulnerability detection "window" and providing more fine-grained predictions. Such approaches, however, implicitly disregard the presence of spurious correlations and biases in the data, which often predominantly influence the performance of ML algorithms. In this work, we investigate how detectors comply with this requirement by proposing an explainability-based evaluation procedure. Our approach, defined as Detection Alignment (DA), quantifies the agreement between the input source code lines that most influence the prediction and the actual localization of the vulnerability as per the ground truth. Through DA, which is model-agnostic and adaptable to different detection tasks, not limited to our use case, we analyze multiple learning-based vulnerability detectors and datasets. As a result, we show how the predictions of such models are consistently biased by non-vulnerable lines, ultimately highlighting the high impact of biases and spurious correlations. The code is available at https://github.com/pralab/vuln-localization-eval.

Paper Structure

This paper contains 15 sections, 7 equations, 5 figures, 4 tables.

Figures (5)

  • Figure 1: Ground truth vulnerable lines against the lines indicated by the detector. We show on a sample from the BigVul dataset fan2020bigvul, correctly predicted as vulnerable by a finetuned CodeBERT detector from fu2022linevul, how the localized vulnerable lines driving the prediction (3,5) are not aligned with the ground truth vulnerable line (4).
  • Figure 2: A code vulnerability detector. Given an input code function, we tokenize the input and use, in this use case, a transformer-based vulnerability detector to predict the presence of a vulnerability in the source code. We then compute, with an explainability-based approach (via attention scores, AttnLRP or integrated gradients), a relevance score $r_{l,i}$ for each $i$-th token of the $l$-th line.
  • Figure 3: A $DA$ approach overview. We start from the token relevance scores $r_{l,:}$, which we aggregate into line relevance scores $r_l$. Then, we design the two sets: $\mathcal{G}^\prime$, derived from the ground truth $\mathcal{G}$; and $\mathcal{R}$, derived from the line relevance scores $r_l$. We finally compute $DA$ through the Jaccard's index computed between the two sets.
  • Figure 4: A vulnerable sample from the BigVul dataset. Figure (a) depicts the ground truth vulnerable line (14), while Figure (b) shows the relevant lines ($DA$-$A^{(1)}$) as conceived by a CodeBERT model trained on the Devign dataset. The detector mostly assigns high relevance to non-vulnerable code lines (1, 15, 17, 18, and 19).
  • Figure 5: A vulnerable sample from the BigVul dataset. Figure (a) depicts the ground truth vulnerable line (4), while Figure (b) shows the relevant lines ($DA$-$A^{(1)}$) as conceived by a CodeBERT model trained on the Devign dataset. The detector assigns high relevance to non-vulnerable code line (8).