Introduction
Pre-trained models of code (PTMCs), such as CodeBERT and CodeGPT, have become foundational tools in software engineering, powering critical tasks like vulnerability detection, clone detection, and automated code summarization. However, these models inherit a significant vulnerability from their natural language processing counterparts: a susceptibility to adversarial attacks. By making minor, semantics-preserving changes to source code—such as renaming a variable or altering a coding style—attackers can manipulate model outputs, potentially allowing malicious code to bypass security filters.
Despite the proliferation of adversarial attack methods, the field has lacked a systematic comparison of their effectiveness, efficiency, and the quality of the adversarial examples they produce. This study addresses this gap through a comprehensive "stress test" of current techniques and introduces BeamAttack, a novel method designed to optimize the trade-off between attack success and code naturalness.
Systematic Evaluation of PTMCs
The researchers conducted a large-scale evaluation involving five prominent adversarial attack methods: MHM, ACCENT, WIR-Random, ALERT, and StyleTransfer. These were tested against three distinct model architectures—Encoder-only, Decoder-only, and Encoder-Decoder—across three primary tasks:
- Clone Detection: Identifying if two code snippets are functionally identical (using BigCloneBench and Google Code Jam).
- Vulnerability Detection: Identifying security flaws (using OWASP and the Juliet Test Suite).
- Code Summarization: Generating natural language descriptions of code (using CodeSearchNet and TL-CodeSum).
Key Findings from the Study
- The Naturalness Trade-off: There is a persistent conflict between an attack's success rate and the quality of the resulting code. While "brute-force" methods like MHM achieve high success rates, they often produce "gibberish" identifiers that are easily flagged by human reviewers.
- Contextual Sensitivity: Not all code elements are equally influential. The study found that perturbing identifiers within specific control structures, such as
ifandforstatements, is significantly more effective at fooling models than changing method names or local variables. - Architecture Robustness: Model architecture plays a role in inherent resistance. CodeGPT (decoder-only) proved more robust than CodeBERT (encoder-only), suggesting that pre-training objectives (e.g., next-token prediction vs. masked language modeling) influence adversarial resilience.
- Task Difficulty: Generative tasks, such as code summarization, were found to be much easier to disrupt than classification tasks like clone or vulnerability detection.
BeamAttack: A Context-Aware Approach
Building on the insight that specific code contexts are more influential, the authors developed BeamAttack. This method improves upon previous state-of-the-art attacks through two primary innovations:
- Statement Prioritization: Instead of treating all identifiers equally, BeamAttack targets identifiers within high-impact control structures (e.g.,
if,for, andtryblocks). - Beam Search Optimization: Unlike greedy search algorithms that may settle for local optima, BeamAttack maintains a set of the most promising adversarial candidates at each step. This allows the algorithm to explore multiple substitution sequences simultaneously, leading to more effective perturbations.
Results and Performance
In head-to-head comparisons, BeamAttack demonstrated superior performance across multiple metrics:
- Attack Success Rate (ASR): BeamAttack outperformed the previous state-of-the-art method (ALERT) by an average of 20.85%.
- Efficiency: The method is significantly more query-efficient, requiring 11.98% fewer queries to the target model than ALERT to achieve a successful attack.
- Statistical Significance: Using Mann-Whitney U tests, the researchers confirmed that these improvements in success rate and efficiency were statistically significant across the majority of test cases.
- MHM Comparison: While the MHM attack achieved the highest raw success rate (averaging 57.83%), it was computationally expensive and produced the lowest quality code, highlighting the value of BeamAttack’s balanced approach.
Conclusion and Future Work
This study provides a rigorous framework for understanding the vulnerabilities of PTMCs. While BeamAttack offers a more potent "sword" for probing model weaknesses, the authors emphasize the need for better "shields." Current limitations include a focus on identifier substitution rather than complex semantic refactoring. Future research should explore adversarial training—incorporating these broken examples back into the training set—and developing query-efficient attacks suitable for large-scale, closed-source LLMs where query costs are prohibitive.
Summary updated