Contrastive Learning for Source Code with Structural and Functional Properties

Yangruibo Ding, Luca Buratti, Saurabh Pujar, Alessandro Morari, Baishakhi Ray, Saikat Chakraborty

Abstract

Understanding the functional (dis)-similarity of source code is significant for code modeling tasks such as software vulnerability and code clone detection. We present DISCO (DIS-similarity of COde), a novel self-supervised model focusing on identifying (dis)similar functionalities of source code. Different from existing works, our approach does not require a huge amount of randomly collected datasets. Rather, we design structure-guided code transformation algorithms to generate synthetic code clones and inject real-world security bugs, augmenting the collected datasets in a targeted way. We propose to pre-train the Transformer model with such automatically generated program contrasts to better identify similar code in the wild and differentiate vulnerable programs from benign ones. To better capture the structural features of source code, we propose a new cloze objective to encode the local tree-based context (e.g., parents or sibling nodes). We pre-train our model with a much smaller dataset, the size of which is only 5% of the state-of-the-art models' training datasets, to illustrate the effectiveness of our data augmentation and the pre-training approach. The evaluation shows that, even with much less data, DISCO can still outperform the state-of-the-art models in vulnerability and code clone detection tasks.

Introduction

Current Transformer-based models for source code, such as CodeBERT or GraphCodeBERT, have demonstrated remarkable proficiency in capturing syntactic patterns—the "grammar" of programming languages. However, these models often struggle to grasp functional semantics. A primary challenge is that minor syntactic changes can lead to major functional differences; for instance, replacing a < operator with <= might be the sole difference between a secure program and a critical buffer overflow vulnerability. Conversely, two code snippets may appear syntactically distinct due to refactoring (e.g., variable renaming or statement reordering) while remaining functionally identical.

Furthermore, existing state-of-the-art models typically rely on massive, randomly collected datasets (often exceeding 20GB) that are not specifically curated to teach the model about the nuances of software vulnerabilities or code clones. DISCO addresses these gaps through targeted data augmentation and a novel structural encoding objective.

The DISCO Framework

DISCO introduces a self-supervised pre-training framework that utilizes "program contrasts" to bridge the gap between syntax and semantics. The framework employs three primary strategies:

Program Contrasts and Data Augmentation

Instead of training on raw code alone, DISCO uses structure-guided transformations to create triplets for contrastive learning:

  • Positive Samples ($x^+$): Functionally identical code generated through semantic-preserving transformations like variable/function renaming and statement permutation.
  • Hard Negative Samples ($x^-$): Created via "bug injection," where subtle, real-world security flaws (e.g., changing conditional logic or removing pointer initializations) are introduced into benign snippets. This teaches the model to distinguish between a bug and its fix.
  • Negative Samples: Randomly selected code snippets from the corpus that are functionally unrelated.
The DISCO pre-training framework using positive, hard negative, and negative program contrasts.

Structural Encoding (NT-MLM)

To capture structural features without the computational overhead of complex graph neural networks, DISCO proposes a Node Type Masked Language Modeling (NT-MLM) objective. For every token, the model encodes its local Abstract Syntax Tree (AST) context by concatenating the token's node type with its parent's node type. This allows the model to understand the hierarchical position of tokens, effectively disambiguating keywords based on their structural role.

Experimental Results

We evaluated DISCO on several benchmarks for vulnerability and clone detection. Despite using a pre-training corpus of only 1.8 GB (approximately 5% of the data used by baseline models), DISCO achieved superior performance.

Vulnerability Detection

On the REVEAL dataset, which contains real-world C/C++ vulnerabilities, DISCO achieved an F1 score of 46.4, outperforming GraphCodeBERT (45.8) and CodeBERT (45.2). Ablation studies demonstrate that the inclusion of the "hard negative" contrastive task (MLM+CLR$^{\pm}$) significantly boosts performance over standard masked language modeling.

Performance comparison of DISCO against state-of-the-art models on the REVEAL vulnerability detection dataset.

Clone Detection

In clone detection tasks, DISCO reached a MAP@R of 83.32 on the POJ-104 dataset, surpassing the previous state-of-the-art. While precision on the Java-based BigCloneBench was high, recall was slightly lower than GraphCodeBERT, likely due to the smaller size of the Java portion of our pre-training dataset.

Conclusion

DISCO demonstrates that the quality and relevance of training data are more critical than sheer volume for code modeling tasks. By focusing on functional (dis)-similarity through structure-guided contrasts and local tree-based encoding, DISCO achieves state-of-the-art results with significantly higher data efficiency. Future work will explore expanding transformation heuristics and extending these structural methods to dynamically typed languages.

Summary updated

BibTeX
@inproceedings{ding2022contrastive,
  author = {Yangruibo Ding and Luca Buratti and Saurabh Pujar and Alessandro Morari and Baishakhi Ray and Saikat Chakraborty},
  title = {{Contrastive Learning for Source Code with Structural and Functional Properties}},
  booktitle = {Annual Meeting of the Association for Computational Linguistics},
  year = {2022},
  doi = {10.18653/v1/2022.acl-long.436},
}
Figure