arXiv:2611.06583cs.SENov 2026

Manuscript Offprint

arXiv:2611.06583

PatchCon: Patch-aware Weighted Supervised Contrastive Learning for Vulnerability Detection

Chaomeng Lu, Pooya Rostami Mazrae, Bert Lagaisse

Chaomeng Lu Ⓤ · Pooya Rostami

Mazrae Ⓤ · Bert Lagaisse Ⓤ

Abstract Automated vulnerability detection is critical for securing modern software systems, yet existing learning-based approaches often treat functions as independent samples and underuse the supervision provided by vulnerability-fixing patches. In this paper, we propose PatchCon, a CodeBERT-based patch-aware contrastive learning model for function-level vulnerability detection. PatchCon jointly optimizes vulnerability classification and supervised contrastive learning, and treats vulnerable-patched function pairs as hard negatives with larger contrastive weights to capture subtle security-relevant differences introduced by fixes. To evaluate patch-level discrimination, we use two pairwise metrics that measure whether a model can correctly distinguish vulnerable functions from their patched counterparts. We evaluate PatchCon on PairVul-Linux through extensive experiments, including state-of-the-art comparisons, ablation studies, hyperparameter sensitivity analysis, and statistical significance testing. We also conduct fine-grained analyses by Common Weakness Enumeration (CWE) type, function length, and patch size, and further evaluate PatchCon on Linux kernel vulnerabilities disclosed after the training data cutoff. On PairVul-Linux, PatchCon outperforms representative pretrained code models and contrastive learning baselines on both classification and pairwise metrics, improves pairwise accuracy over the strongest reproduced baseline from 0.2060 to 0.3264, and achieves pairwise accuracy comparable to that of a GPT-4o-based large language model (LLM) method at a substantially lower cost. Among the reproduced models, PatchCon also achieves the highest pairwise accuracy on test pairs from Common Vulnerabilities and Exposures (CVE) entries unseen during training and on newly disclosed vulnerabilities.

Chaomeng Lu (corresponding author) · Pooya Rostami Mazrae · Bert Lagaisse DistriNet Group T, KU Leuven, Leuven, Belgium E-mail: chaomeng.lu@kuleuven.be; pooya.rostamimazrae@kuleuven.be; bert.lagaisse@kuleuven.be

Keywords Software vulnerability detection · Supervised contrastive learning · Vulnerability-fixing patches · Hard negatives · Pre-trained code models · Code representation learning

1 Introduction

Software vulnerabilities remain a persistent threat to modern software ecosystems (Ohm et al. 2020). As large-scale open-source repositories are increasingly reused in safety-critical, financial, governmental, and cloud infrastructures, vulnerable code can propagate rapidly across projects and affect downstream users at scale (Ladisa et al. 2023). Automated vulnerability detection has therefore become an important research direction in software security (Le et al. 2022; Yamaguchi et al. 2014). Recent approaches based on deep learning and Large Language Models (LLMs) have shown promise in identifying vulnerable functions from source code, but reliably distinguishing vulnerable code from its patched counterpart remains challenging because vulnerability fixes often involve subtle yet security-critical changes (Risse and Böhme 2024; Steenhoek et al. 2023; Zhou et al. 2025).

Existing vulnerability detection approaches still face three major limitations.

Unreliable datasets. The datasets used in many previous studies are often not sufficiently reliable for realistic and fine-grained vulnerability detection evaluation. Some commonly used datasets suffer from low label accuracy, high duplication rates, and a large number of functions that are not directly related to vulnerabilities. In other cases, the data may not be constructed from real-world vulnerability reports and corresponding patches, making it difficult to assess whether a model can generalize to practical vulnerability detection scenarios (Chen et al. 2023; Croft et al. 2022; Ding et al. 2025).

Underused patch supervision. Many existing methods formulate vulnerability detection as an independent single-function classification task. However, real-world vulnerabilities are often best understood through their fixes. A vulnerable function and its patched counterpart usually share most lexical and syntactic structures, while differing only in small but security-critical edits. Such vulnerable-patched pairs provide strong supervision signals for learning security-sensitive representations, but this information has not been fully exploited in many existing pipelines (Tan et al. 2025). Although contrastive learning has recently been introduced into this field (Cao et al. 2025; Tian et al. 2026), existing designs either do not use vulnerable-patched pairs or treat them in the same way as ordinary negative samples.

Limited patch-level evaluation. Existing evaluation protocols provide limited insight into patch-level discrimination. Most detectors are still evaluated only with standard classification metrics, such as accuracy, precision, and F1-score. These metrics are useful for measuring overall detection performance, but they do not directly evaluate whether a model assigns higher vulnerability confidence to a vulnerable function than to its fixed version. This ability is critical in practice, because a reliable vulnerability detector should be able to distinguish a vulnerable implementation from a minimally changed but security-corrected patch.

To address these limitations, this paper proposes PatchCon, a Patch-aware Weighted Supervised Contrastive Learning framework for function-level vulnerability detection. To support reliable and fine-grained evaluation, we conduct experiments on PairVul-Linux (Du et al. 2026), which is constructed from real-world Linux kernel Common Vulnerabilities and Exposures (CVE) reports and their corresponding patches, removes functions that are not directly related to vulnerabilities, and applies strict filtering to improve label quality. In addition, we use the Linux kernel subset of VentiVul (Lu and Lagaisse 2026), a test set of vulnerabilities published more than a year after the most recent vulnerability in PairVul-Linux, to evaluate whether the learned discrimination generalizes to newly disclosed vulnerabilities.

Different from conventional single-function classifiers, PatchCon explicitly incorporates patch-pair information by constructing four types of function pairs. In particular, vulnerable-patched pairs are treated as hard negatives and assigned larger contrastive weights. This design encourages the model to learn compact intra-class representations while enlarging the representation gap between vulnerable functions and their patched counterparts. Beyond standard classification metrics, we use two pairwise evaluation metrics, Pairwise Accuracy (PACC), which follows the pairwise evaluation of prior work (Du et al. 2026), and Mean Confidence Gap (MCG), to directly evaluate whether a model ranks vulnerable functions as more vulnerable than their corresponding fixed versions. We further conduct a comprehensive evaluation around six research questions (RQs) and discuss the robustness of the results to CVE overlap and representative cases. The main contributions of this paper are summarized as follows:

  • We propose PatchCon, a supervised contrastive approach to function-level vulnerability detection that treats vulnerable-patched function pairs as hard negatives, weights them more heavily than ordinary negatives, and is trained jointly with classification.
  • We evaluate PatchCon on PairVul-Linux and VentiVul with standard and pairwise metrics. PatchCon improves PACC over the strongest reproduced baseline from 0.2060 to 0.3264, with statistically significant gains over all evaluated baselines, and achieves pairwise accuracy comparable to a GPT-4o-based method at a much lower cost.
  • We show that the advantage of PatchCon in vulnerable-patched discrimination persists without CVE overlap: it retains the highest PACC among the reproduced models on test pairs from CVEs unseen during training and on VentiVul.

This paper is structured as follows. Section 2 reviews related work on vulnerability detection, contrastive learning, and patch-aware vulnerability analysis. Section 3 presents the design of PatchCon. Section 4 describes the datasets, baselines, implementation details, and evaluation metrics. Section 5 reports the results for the six RQs. Section 6 discusses the robustness of the results to CVE overlap, a case study, limitations, and threats to validity. Section 7 concludes the paper.

References (40)