Applying Contrastive Learning to Code Vulnerability Type Classification

Chen Ji, Su Yang, Hongyu Sun, Yuqing ZhangEMNLP 2024

Abstract

Identifying the specific type of a software vulnerability is critical for effective remediation. However, Common Weakness Enumeration (CWE) classification is hindered by long-tail data distributions, class isolation, and input length constraints of transformer models. This paper introduces a hierarchical contrastive learning (HCL) framework that leverages the inherent structure of the CWE hierarchy. By combining supervised and self-supervised contrastive losses with a max-pooling strategy for long code sequences, the proposed method significantly outperforms existing state-of-the-art models and general-purpose large language models like GPT-4o.

Introduction

While vulnerability detection—determining if code is buggy—has seen significant progress, vulnerability classification remains a complex challenge. Security teams require precise CWE identification to prioritize fixes and understand root causes. Existing deep learning approaches often fail to account for the "long-tail" nature of vulnerability data, where a few types dominate and hundreds are rare. Furthermore, most models treat CWE labels as independent categories, ignoring the semantic relationships defined in the MITRE CWE hierarchy.

The Hierarchical Contrastive Learning Framework

To address these limitations, the authors propose a framework centered on Hierarchical Contrastive Learning (HCL). The core idea is to map code snippets into a high-dimensional vector space where vulnerabilities with similar root causes are clustered together, regardless of their specific variant.

The HCL framework architecture, featuring code chunking, max-pooling, and hierarchical label expansion for contrastive learning.

Key Components:

  1. Hierarchical Contrastive Loss: The model is trained across five levels of abstraction (Pillar, Class, Base, Variant, etc.). This ensures that even if a specific "Variant" is rare in the training set, the model can learn from its broader "Pillar" or "Class" context.
  2. Geometric Spread (Anti-Collapse): To prevent "class collapse"—where all samples in a category become indistinguishable—the framework integrates a self-supervised loss. This forces the model to retain unique features of individual code samples while still learning the shared characteristics of the class.
  3. Max-Pooling for Long Sequences: Standard BERT-based models are limited to 512 tokens. The HCL framework segments long functions into chunks, processes them, and uses a max-pooling layer to capture the most salient features from the entire code block, effectively extending the context window.
  4. Label Expansion: By mapping each CWE ID to its full lineage in the MITRE hierarchy, the model receives a structured roadmap of vulnerability relationships.

Experimental Results

The framework was evaluated on two major datasets: Big-Vul (150k functions) and PrimeVul (a higher-quality, cleaner benchmark). The results demonstrate that specialized models with hierarchical inductive biases far exceed the performance of general-purpose LLMs.

Comparative performance of HCL-enhanced models against baselines on Big-Vul and PrimeVul datasets.

As shown in the results, the HCL-enhanced CodeBERT model achieved a state-of-the-art accuracy of 69.06% on Big-Vul and 58.12% on PrimeVul. This represents an improvement of up to 17.90% over previous baselines. Notably, the Weighted-F1 scores saw gains of up to 22.27%, proving the model's superior ability to handle rare "tail" vulnerabilities. In comparison, the study found that even massive models like GPT-4o struggle with this task (scoring between 22% and 34% accuracy), highlighting the necessity of domain-specific architectural choices.

Conclusion

The Hierarchical Contrastive Learning framework effectively bridges the gap between broad vulnerability detection and precise type classification. By treating the CWE hierarchy as a first-class citizen in the learning process, the model gains a deeper semantic understanding of code flaws. Future work will focus on handling even longer code sequences and incorporating natural language descriptions from CVE reports to further enrich the model's context.

Summary updated

BibTeX
@inproceedings{ji2024applying,
  author = {Chen Ji and Su Yang and Hongyu Sun and Yuqing Zhang},
  title = {{Applying Contrastive Learning to Code Vulnerability Type Classification}},
  booktitle = {Conference on Empirical Methods in Natural Language Processing},
  year = {2024},
  doi = {10.18653/v1/2024.emnlp-main.666},
}
Figure