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 CODA pipeline consists of three main stages:
- 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.
- Equivalent Structure Transformation (EST): Applying semantic-preserving rules (e.g., converting a
forloop to awhileloop) to align the target's structure with the reference's style. - 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.
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