Adversarial Attacks on Neural Models of Code via Code Difference Reduction

Zhao Tian, Junjie Chen, Zhi Jin

Abstract

Adversarial examples are important to test and enhance the robustness of deep code models. As source code is discrete and has to strictly stick to complex grammar and semantics constraints, the adversarial example generation techniques in other domains are hardly applicable. Moreover, the adversarial example generation techniques specific to deep code models still suffer from unsatisfactory effectiveness due to the enormous ingredient search space. In this work, we propose a novel adversarial example generation technique (i.e., CODA) for testing deep code models. Its key idea is to use code differences between the target input (i.e., a given code snippet as the model input) and reference inputs (i.e., the inputs that have small code differences but different prediction results with the target input) to guide the generation of adversarial examples. It considers both structure differences and identifier differences to preserve the original semantics. Hence, the ingredient search space can be largely reduced as the one constituted by the two kinds of code differences, and thus the testing process can be improved by designing and guiding corresponding equivalent structure transformations and identifier renaming transformations. Our experiments on 15 deep code models demonstrate the effectiveness and efficiency of CODA, the naturalness of its generated examples, and its capability of enhancing model robustness after adversarial fine-tuning. For example, CODA reveals 88.05% and 72.51% more faults in models than the state-of-the-art techniques (i.e., CARROT and ALERT) on average, respectively.

Introduction

Deep learning models for code, such as those powering GitHub Copilot or AlphaCode, are increasingly utilized for critical software engineering tasks like vulnerability detection and clone identification. However, these models are often brittle. Similar to how image classifiers can be fooled by imperceptible pixel changes, deep code models can be tricked by functionally identical code snippets.

Generating these "adversarial examples" for code is uniquely challenging. Unlike continuous image data, source code is discrete and must adhere to strict grammatical and semantic rules. Existing generation tools often struggle with an "enormous ingredient search space." For example, when renaming a variable to deceive a model, the number of valid names is nearly infinite. This leads to inefficient searching, high computational costs, and the generation of "unnatural" code that does not resemble human-written software.

Methodology: The CODA Approach

To address these challenges, we propose CODA (COde Difference guided Adversarial example generation). The core innovation of CODA is the use of reference inputs—code snippets from the training set that the model already classifies differently than the target—to guide the perturbation process.

The workflow of CODA, illustrating the transition from target inputs to adversarial examples via reference-guided transformations.

The CODA workflow consists of three primary stages:

  1. Reference Input Selection: CODA identifies snippets from the training set that the model predicts as the "second most likely" class for the target input. By selecting the most similar snippets (using CodeBERT embeddings), CODA targets the model's existing uncertainty, making it easier to bridge the gap to a misclassification.
  2. Equivalent Structure Transformation (EST): CODA analyzes structural differences (e.g., for vs. while loops) between the target and the reference. It then applies semantics-preserving transformations to the target to mirror the reference's structure.
  3. Identifier Renaming Transformation (IRT): CODA renames variables in the target using names found in the reference snippet. Semantic similarity (via FastText) is used to ensure the new names remain natural in context.

By restricting the search space to the differences between the target and a specific reference, CODA collapses an infinite search space into a manageable one, significantly improving efficiency.

Experimental Evaluation

We evaluated CODA across 15 deep code models based on CodeBERT, GraphCodeBERT, and CodeT5. The evaluation covered five diverse tasks: Vulnerability Prediction (C), Clone Detection (Java), Authorship Attribution (Python), Functionality Classification (C), and Defect Prediction (C/C++).

Effectiveness and Efficiency

CODA significantly outperformed state-of-the-art baselines, CARROT and ALERT, in terms of Attack Success Rate (ASR). On average, CODA revealed 88.05% more faults than CARROT and 72.51% more than ALERT.

Quantitative comparison of Attack Success Rates (ASR) across different models and tasks, highlighting CODA's superior performance.

Beyond effectiveness, CODA proved highly efficient. It completed the entire testing suite in 196.96 hours, compared to 290.87 for CARROT and 374.51 for ALERT. This efficiency stems from CODA's ability to find faults with 65-78% fewer model invocations by avoiding the need to query the model at every step of the search.

Robustness and Naturalness

Adversarial examples generated by CODA are not just effective; they are also "natural." Because transformations are guided by real-world code from the training set, the resulting snippets follow human-like coding patterns. Furthermore, using CODA-generated examples for adversarial fine-tuning significantly boosted model robustness, reducing the success rate of subsequent attacks by up to 73.95%.

Conclusion

CODA introduces a guided approach to adversarial testing that leverages existing model uncertainties and real-world code differences. By reducing the search space and focusing on natural transformations, CODA provides a more effective and efficient means of identifying and fixing vulnerabilities in deep code models. Future work will explore more sophisticated similarity metrics and methods to handle datasets with limited reference classes.

Summary updated

BibTeX
@inproceedings{tian2023adversarial,
  author = {Zhao Tian and Junjie Chen and Zhi Jin},
  title = {{Adversarial Attacks on Neural Models of Code via Code Difference Reduction}},
  booktitle = {ASE},
  year = {2023},
  doi = {10.48550/arxiv.2301.02412},
}
Figure