Abstract
Automated vulnerability detection remains a critical challenge due to the difficulty of isolating security-specific semantics from general functional logic in source code. We present CLeVeR (Contrastive Learning for Vulnerability Code Representation), a framework that leverages CLIP-style contrastive learning to align source code with high-level natural language vulnerability descriptions. By utilizing descriptions as a "semantic filter," CLeVeR generates precise representations that significantly outperform state-of-the-art methods on real-world datasets and enables robust zero-shot classification.
Introduction
Identifying vulnerabilities in software is often described as a "needle in a haystack" problem. While contemporary deep learning models like CodeBERT effectively capture the functional semantics of code (e.g., identifying a JSON parser), they frequently fail to distinguish the subtle patterns that constitute a security flaw, such as a missing bounds check. Existing approaches often treat code as generic text or graphs, lacking a strong supervisory signal to highlight security-relevant features. CLeVeR addresses this by aligning the complex structure of source code with the precise, high-level insights found in natural language vulnerability reports.
Methodology
The CLeVeR framework employs a dual-encoder architecture designed for multi-modal alignment and representation refinement.
Multi-modal Alignment and Refinement
CLeVeR utilizes a Code Encoder (CodeBERT) and a Description Encoder (RoBERTa), projecting both into a shared latent space via an Adapter module. To address the semantic imbalance between long code sequences and concise descriptions, a cross-attention mechanism uses the vulnerability description as a "query" to highlight relevant code segments, effectively filtering out functional noise.
Description Simulator
A significant challenge in real-world deployment is the absence of descriptions for undiscovered bugs. CLeVeR introduces a Description Simulator trained to predict refined, vulnerability-aware code representations directly from raw code. This allows the model to maintain high performance during fine-tuning and inference stages without requiring manual descriptions.
Contrastive Learning
The model is optimized using an InfoNCE loss, which maximizes the similarity between matching code-description pairs while minimizing it for mismatched pairs, ensuring the latent space is structured around security-relevant features.
Experimental Evaluation
Datasets
The framework was evaluated on three primary datasets: VCLData (a new dataset of 280,034 annotated C/C++ functions), SynData (semi-synthetic for controlled testing), and RealData (complex real-world codebases like FFMpeg and Qemu).
Key Results
CLeVeR demonstrates superior performance across various metrics and scenarios compared to existing baselines.
- Detection and Classification: On real-world data, CLeVeR achieved an F1 score of 72.82%, an 11.85% improvement over the previous state-of-the-art (CasualVul). In CWE classification, it outperformed existing methods by 13.61%.
- Zero-Shot Capabilities: The shared latent space allows for zero-shot detection. By prompting the model (e.g., "This is a buffer overflow function"), CLeVeR can identify vulnerabilities in new codebases without explicit fine-tuning, often surpassing the performance of fully trained legacy models.
- Real-World Validation: In a deployment on Qemu v2.7.0, CLeVeR identified 5 of 9 known CVEs and discovered 2 previously unreported vulnerabilities that were subsequently patched.
Discussion and Conclusion
The success of CLeVeR highlights the power of using natural language as a "semantic filter" for code. By bridging the gap between linguistic descriptions and source code, the model captures underlying security patterns more effectively than functional-only models. Future work will focus on expanding data diversity, exploring vulnerability localization, and scaling the architecture to larger foundation models to enhance reasoning over complex logic flaws.
Summary updated