Code Difference Guided Adversarial Example Generation for Deep Code Models

Zhao Tian, Junjie Chen, Zhi Jin · DOI

Abstract

Adversarial examples are essential for testing and enhancing the robustness of deep code models. However, generating these examples is challenging because source code is discrete and must adhere to strict grammatical and semantic constraints. Existing techniques often struggle with an enormous search space, leading to inefficient generation and unnatural code. In this work, we propose CODA (COde Difference guided Adversarial example generation), a novel technique that uses code differences between a target input and "reference inputs" (similar snippets with different prediction results) to guide the generation process. CODA employs equivalent structure transformations and identifier renaming to preserve original semantics while drastically reducing the search space. Our experiments on 15 deep code models across five tasks demonstrate that CODA reveals significantly more faults (88.05% and 72.51% more than state-of-the-art tools CARROT and ALERT, respectively) while maintaining high efficiency and code naturalness.

Introduction

Deep learning models for code, such as CodeBERT and CodeT5, are increasingly deployed in critical software engineering tasks like vulnerability detection and clone identification. However, these models are vulnerable to adversarial examples—inputs modified to deceive the model while maintaining original semantics. Generating such examples for code is difficult due to the discrete nature of code and strict syntax/semantic constraints. Existing methods often struggle with an overwhelming search space, leading to inefficient generation and "unnatural" code that human developers would not typically write.

Methodology: The CODA Approach

CODA addresses these challenges by leveraging "reference inputs" from the training set—code snippets that are structurally similar to the target input but yield different model predictions. This approach bridges the gap between the target and a potential adversarial state by extracting and applying specific differences.

The workflow of CODA, illustrating how reference inputs guide transformations to create adversarial examples.

The CODA pipeline consists of three main stages:

  1. Reference Input Selection: Identifying training samples with high structural similarity to the target but belonging to the class the model is most likely to "flip" to.
  2. Equivalent Structure Transformation (EST): Applying semantic-preserving rules (e.g., converting a for loop to a while loop) to align the target's structure with the reference's style.
  3. Identifier Renaming Transformation (IRT): Extracting variable names from reference inputs and prioritizing them based on semantic similarity using word embeddings to ensure the resulting code remains natural.

By limiting the "ingredients" for transformations to those found in similar reference inputs, CODA transforms a blind search into a directed, efficient process.

Experimental Evaluation and Results

We evaluated CODA across 15 deep code models and five distinct tasks, including vulnerability prediction, clone detection, and authorship attribution. The results indicate that CODA is both more effective and more efficient than existing state-of-the-art techniques.

Comparison of CODA against CARROT and ALERT across various tasks and models, showing superior prediction confidence decrement and success rates.

Key Findings:

  • Effectiveness: CODA revealed 88.05% more faults than CARROT and 72.51% more than ALERT on average. As shown in the performance comparison, CODA consistently achieves higher prediction confidence decrement (PCD) across different model architectures.
  • Efficiency: CODA completed testing for all subjects in 196.96 hours, significantly faster than CARROT (290.87h) and ALERT (374.51h), largely due to a reduction in required model invocations.
  • Robustness Enhancement: Adversarial fine-tuning using CODA-generated examples improved model robustness, reducing the rate of revealed faults by up to 73.95%.
  • Naturalness: In human user studies, CODA’s generated examples received high naturalness scores (averaging ~4.5/5), indicating they are indistinguishable from human-written code.

Conclusion

CODA provides a directed and efficient framework for adversarial testing of deep code models. By using code differences from reference inputs to guide transformations, it effectively navigates the discrete search space of source code. This results in high-quality, natural adversarial examples that help developers identify and mitigate critical model vulnerabilities.

Summary updated

BibTeX
@inproceedings{tian2023code,
  author = {Zhao Tian and Junjie Chen and Zhi Jin},
  title = {{Code Difference Guided Adversarial Example Generation for Deep Code Models}},
  booktitle = {ASE},
  year = {2023},
  doi = {10.48550/arxiv.2301.02412},
}
Figure