Abstract
Pre-trained language models of code are now widely used in various software engineering tasks such as code generation, code completion, and vulnerability detection. This, in turn, poses security and reliability risks to these models. One of the important threats is adversarial attacks, which can lead to erroneous predictions and largely affect model performance on downstream tasks. Current adversarial attacks on code models usually adopt fixed sets of program transformations, such as variable renaming and dead code insertion, leading to limited attack effectiveness. To address these challenges, we propose a novel adversarial attack framework, GraphCodeAttack, to better evaluate the robustness of code models. Given a target code model, GraphCodeAttack automatically mines important code patterns that influence the model's decisions to perturb the structure of input code. GraphCodeAttack identifies discriminative AST patterns, concretizes them using a separate pre-trained filler model, and inserts them as dead code. Our evaluation on CodeBERT and GraphCodeBERT across three tasks—Authorship Attribution, Vulnerability Prediction, and Clone Detection—suggests that GraphCodeAttack significantly outperforms state-of-the-art approaches like CARROT and ALERT.
Introduction
Pre-trained language models for code, such as CodeBERT and GraphCodeBERT, have become the backbone of modern software engineering automation. However, these models possess a critical "blind spot": they are susceptible to adversarial attacks—minor, semantics-preserving changes that trick the AI into making incorrect predictions.
Existing attack methodologies often rely on handcrafted rules, such as renaming variables or inserting generic dead code (e.g., if(false)). These manual transformations are limited because they do not adapt to the specific nuances of a target model or the complex structural patterns of different programming languages. GraphCodeAttack addresses this by automatically discovering which code structures a model is most sensitive to, allowing for more surgical and effective perturbations.
The GraphCodeAttack Framework
GraphCodeAttack moves away from fixed rules toward automated discovery. The framework operates in three distinct phases: pattern mining, attack synthesis, and the final attack execution.
1. Discriminative Pattern Mining
The system "probes" the target model $\mathcal{M}_t$ using a set of source codes to observe its reactions. It utilizes a graph-mining algorithm called gSpan-CORK to find frequent subgraphs in the code's Abstract Syntax Tree (AST) that are highly correlated with specific model predictions. This process identifies the specific "shapes" of code that the model finds most influential, termed "discriminative patterns."
2. Attack Synthesis
The mined patterns are initially abstract (e.g., a binary operation with a missing right node). To transform these into valid code, GraphCodeAttack uses a separate pre-trained "filler" model $\mathcal{M}_f$. This model completes the AST patterns with contextually relevant code snippets, using <MASK> tokens to guide the generation of syntactically correct but semantically neutral code.
3. The Attack Phase
To attack a specific file, the tool employs a greedy strategy. It identifies the most "important" line of code—the one that most affects the model's confidence—and inserts the synthesized patterns as "dead code." Because this code is placed inside non-executing blocks (like an if statement that is always false), the program's original functionality remains intact while its structural representation is altered enough to fool the AI.
Experimental Evaluation
The researchers evaluated GraphCodeAttack using the CodeXGLUE benchmark across three tasks:
- Vulnerability Prediction: Detecting bugs in C code (FFmpeg and Qemu datasets).
- Clone Detection: Identifying functional equivalence in Java (BigCloneBench).
- Authorship Attribution: Identifying programmers in Python (Google Code Jam dataset).
Key Results
GraphCodeAttack significantly outperformed existing baselines like CARROT and ALERT:
- Higher Success Rate: It achieved a 30% improvement over CARROT and a 33% improvement over ALERT in average Attack Success Rate (ASR).
- Peak Performance: On the Authorship Attribution task using GraphCodeBERT, it reached an ASR of 0.841, compared to 0.598 and 0.615 for the baselines.
- Model-Specific Insights: The research revealed that CodeBERT is particularly sensitive to control-flow elements, while GraphCodeBERT is more easily tricked by patterns involving variable relationships and data operations.
Conclusion
GraphCodeAttack demonstrates that code models are deeply influenced by the structural AST of the code, not just the text. By hiding influential graph patterns inside non-executing blocks, an attacker can flip a model's prediction without changing the program's behavior. Furthermore, the study shows that using GraphCodeAttack to generate examples for adversarial retraining can significantly enhance model robustness against future threats.
Summary updated