Tighter Clusters, Safer Code? Improving Vulnerability Detection with Enhanced Contrastive Loss

Pranav M. Kapparad, Biju R. MohanStudent Research Workshop @ NAACL 2025

Introduction

Detecting software vulnerabilities remains a critical challenge in cybersecurity. Deep learning models, while powerful, often struggle with the nuanced nature of programming languages where a single character change can transform a secure function into a vulnerable one. This paper presents CESCL (Cluster-Enhanced Supervised Contrastive Loss), a novel loss function designed to improve vulnerability detection by enforcing tighter internal data representations. By optimizing how models group similar code snippets, the researchers achieved significant performance gains, including a 4.1% improvement in detection accuracy for state-of-the-art architectures.

The Challenge: High Inter-Class Similarity

A primary hurdle in automated security is the extreme similarity between "safe" and "vulnerable" code. In natural language processing (NLP), opposing sentiments like "I love this" and "I hate this" are semantically distinct. However, in source code, a vulnerability often hinges on a minor logical error within an otherwise identical block of text.

Comparison of inter-class similarity between general text datasets and code datasets.

As shown in Figure 1, the researchers quantified this "flatness" in the data. While general text datasets exhibit a cosine similarity of approximately 0.83 across different labels, code datasets reach a staggering 0.98. This high inter-class similarity means that standard training objectives often produce "loose" clusters that overlap, leading to high rates of false positives and negatives.

Methodology: Introducing CESCL

To address this, the authors developed CESCL, which builds upon Supervised Contrastive Loss (SCL) by adding a distance-based regularization term. The approach follows a three-step process:

  1. Feature Extraction: Pre-trained transformer models, specifically CodeBERT and GraphCodeBERT, serve as the backbone to generate high-dimensional code embeddings.
  2. Distance-Based Regularization: Unlike standard SCL, which focuses on pushing different classes apart, CESCL introduces a term that explicitly penalizes large Euclidean distances between embeddings of the same class. This forces the model to minimize the "spread" of its internal clusters.
  3. Hybrid Optimization: The final training objective is a weighted combination of Binary Cross Entropy (BCE) for classification and the CESCL for embedding space optimization.

By balancing these goals, the model learns to construct a well-structured embedding space where vulnerable and safe code occupy distinct, compact neighborhoods.

Performance Evaluation

The researchers evaluated CESCL using the Devign dataset, a benchmark containing over 21,000 C/C++ code snippets from real-world open-source projects like FFmpeg and QEMU. The results demonstrate that enforcing cluster tightness directly translates to better classification reliability.

F1 score comparison across different models and loss functions.

As detailed in Figure 3, CESCL consistently outperformed both the standard BCE baseline and the standard SCL approach. For CodeBERT, F1 scores rose from 0.597 to 0.625. The impact was even more pronounced for GraphCodeBERT, which saw a significant jump from 0.594 to 0.633—a 4.1% improvement over the baseline.

Furthermore, the researchers used Silhouette Scores to measure clustering quality. CESCL achieved higher scores (e.g., 0.056 for CodeBERT) compared to standard SCL (0.043), proving that the resulting clusters were indeed more cohesive and better separated.

Conclusion and Future Work

The CESCL framework demonstrates that for high-similarity tasks like vulnerability detection, separation alone is insufficient; internal consistency and cluster tightness are paramount. While the embedding space for code remains crowded, CESCL provides a robust path forward for making AI-driven security tools more reliable. Future research will explore applying this loss function to other programming languages and high-precision tasks such as code clone detection and automated translation.

Summary updated

BibTeX
@inproceedings{kapparad2025tighter,
  author = {Pranav M. Kapparad and Biju R. Mohan},
  title = {{Tighter Clusters, Safer Code? Improving Vulnerability Detection with Enhanced Contrastive Loss}},
  booktitle = {Student Research Workshop @ NAACL},
  year = {2025},
  doi = {10.18653/v1/2025.naacl-srw.24},
}
Figure