Abstract
Pre-trained models of code have achieved success in many important software engineering tasks. However, these powerful models are vulnerable to adversarial attacks that slightly perturb model inputs to make a victim model produce wrong outputs. Current works mainly attack models of code with examples that preserve operational program semantics but ignore a fundamental requirement for adversarial example generation: perturbations should be natural to human judges, which we refer to as naturalness requirement. In this paper, we propose ALERT (nAturaLnEss AwaRe ATtack), a black-box attack that adversarially transforms inputs to make victim models produce wrong outputs. Different from prior works, this paper considers the natural semantic of generated examples at the same time as preserving the operational semantic of original inputs. Our user study demonstrates that human developers consistently consider that adversarial examples generated by ALERT are more natural than those generated by the state-of-the-art work by Zhang et al. that ignores the naturalness requirement. On attacking CodeBERT, our approach can achieve attack success rates of 53.62%, 27.79%, and 35.78% across three downstream tasks: vulnerability prediction, clone detection and code authorship attribution. On GraphCodeBERT, our approach can achieve average success rates of 76.95%, 7.96% and 61.47% on the three tasks. The above outperforms the baseline by 14.07% and 18.56% on the two pre-trained models on average. Finally, we investigated the value of the generated adversarial examples to harden victim models through an adversarial fine-tuning procedure and demonstrated the accuracy of CodeBERT and GraphCodeBERT against ALERT-generated adversarial examples increased by 87.59% and 92.32%, respectively.
Introduction
Pre-trained models of code, such as CodeBERT and GraphCodeBERT, are increasingly deployed for critical software engineering tasks like vulnerability detection and clone detection. Despite their power, these models are susceptible to adversarial attacks where small, semantics-preserving changes to input code—like renaming local variables—can trigger incorrect model outputs.
A significant limitation of current attack methods, such as the Metropolis-Hastings Modifier (MHM), is their focus solely on operational semantics. While the modified code remains functional, the resulting variable names are often nonsensical (e.g., renaming buffer to qmp_async_cmd_handler). In real-world software development, such unnatural code would be easily flagged during human code reviews. This paper introduces ALERT (nAturaLnEss AwaRe ATtack), a black-box attack designed to generate adversarial examples that are both effective at fooling models and natural to human developers.
The ALERT Framework
ALERT achieves naturalness and high attack success rates through three primary components:
- Naturalness-Aware Substitution: ALERT leverages the Masked Language Modeling (MLM) capabilities of pre-trained models to predict contextually appropriate variable substitutes. It filters these candidates using cosine similarity of contextualized embeddings to ensure the new names are semantically related to the originals.
- Greedy-Attack: To maximize query efficiency, ALERT calculates an "Overall Importance Score" (OIS) for each variable. It prioritizes substituting variables that have the greatest influence on the model's prediction.
- GA-Attack (Genetic Algorithm): If the greedy approach fails, ALERT employs a genetic algorithm to explore a broader range of variable substitution combinations.
Experimental Evaluation
The effectiveness of ALERT was evaluated across three tasks: Vulnerability Prediction, Clone Detection, and Authorship Attribution. The study utilized standard benchmarks including CodeXGLUE and BigCloneBench.
Key Results
Human-Verified Naturalness
A core contribution of ALERT is the naturalness of its generated code. In a user study, experienced developers rated ALERT's adversarial examples significantly higher than those produced by the MHM baseline, which often produced nonsensical results.
Attack Performance and Efficiency
ALERT achieved average Attack Success Rates (ASR) of 39.06% on CodeBERT and 48.79% on GraphCodeBERT, outperforming the naturalness-aware baseline (MHM-NS) by over 14% on average. Furthermore, ALERT proved highly efficient, requiring up to 82% fewer queries than baselines when the greedy approach succeeded.
Adversarial Defense
The study also demonstrated that ALERT-generated examples are valuable for model hardening. Adversarial fine-tuning significantly improved the robustness of CodeBERT and GraphCodeBERT, restoring accuracy against these attacks by 87.59% and 92.32%, respectively.
Conclusion
ALERT addresses a critical gap in code-based adversarial attacks by prioritizing human-perceived naturalness. By weaponizing the contextual understanding of pre-trained models against themselves, ALERT produces stealthy, effective attacks that highlight the need for more robust software engineering models. Future work will expand ALERT's capabilities to more programming languages and explore advanced defensive mechanisms.
Summary updated