[Paper Review] CodeAttack: Code-Based Adversarial Attacks for Pre-trained Programming Language Models
CodeAttack is a black-box adversarial attack framework that generates imperceptible, code-structure-aware perturbations to evade pre-trained programming language models (PLMs) in the natural channel of code. By leveraging masked code models and greedy search with syntactic and semantic constraints, it achieves high attack success rates—especially on GraphCodeBERT—while maintaining fluency, consistency, and minimal changes, exposing critical vulnerabilities in state-of-the-art PLMs.
Pre-trained programming language (PL) models (such as CodeT5, CodeBERT, GraphCodeBERT, etc.,) have the potential to automate software engineering tasks involving code understanding and code generation. However, these models operate in the natural channel of code, i.e., they are primarily concerned with the human understanding of the code. They are not robust to changes in the input and thus, are potentially susceptible to adversarial attacks in the natural channel. We propose, CodeAttack, a simple yet effective black-box attack model that uses code structure to generate effective, efficient, and imperceptible adversarial code samples and demonstrates the vulnerabilities of the state-of-the-art PL models to code-specific adversarial attacks. We evaluate the transferability of CodeAttack on several code-code (translation and repair) and code-NL (summarization) tasks across different programming languages. CodeAttack outperforms state-of-the-art adversarial NLP attack models to achieve the best overall drop in performance while being more efficient, imperceptible, consistent, and fluent. The code can be found at https://github.com/reddy-lab-code-research/CodeAttack.
Motivation & Objective
- To expose the vulnerability of pre-trained programming language models (PLMs) to adversarial attacks in the natural channel of code, where human-readable elements like variable names and comments are manipulated.
- To develop a realistic black-box attack method that does not require access to model parameters, relying only on input queries and model predictions.
- To ensure adversarial examples are imperceptible, fluent, consistent with original code style, and syntactically valid across multiple programming languages.
- To evaluate transferability of attacks across diverse PLM tasks, including code translation, code repair, and code-NL summarization.
- To demonstrate that code-specific constraints and structure-aware perturbations significantly outperform general NLP adversarial methods in effectiveness and quality.
Proposed method
- CodeAttack uses a pre-trained masked CodeBERT model as a generator to produce contextually relevant, semantically valid substitutes for vulnerable tokens in code.
- It applies a greedy search strategy to iteratively replace tokens—specifically identifiers, operators, and keywords—while enforcing code consistency and fluency constraints.
- The method enforces three key constraints: (1) code syntax must remain valid, (2) perturbed code must preserve original semantics, and (3) changes must be minimal and imperceptible to human readers.
- It combines three attack variants: CodeAttack_VUL (vulnerable tokens), CodeAttack_OP (operators), and CodeAttack_TOK (tokens), with joint constraints for optimal performance.
- The attack is evaluated using metrics like attack success rate, performance drop (Δ_drop), CodeBLEU score (q), and query efficiency, with human evaluation for fluency and consistency.
- It is designed to be input language-agnostic, enabling transferability across different programming languages and downstream tasks such as code summarization and code translation.
Experimental results
Research questions
- RQ1Can adversarial attacks in the natural channel of code—targeting human-readable elements like variable names and comments—significantly degrade the performance of state-of-the-art pre-trained programming language models?
- RQ2How effective is CodeAttack compared to existing NLP-based adversarial attack methods in generating imperceptible, fluent, and consistent adversarial code samples?
- RQ3To what extent is CodeAttack transferable across different programming language models, tasks (e.g., code translation, repair, summarization), and programming languages?
- RQ4Why is GraphCodeBERT more vulnerable than CodeT5 to CodeAttack, and how do pre-training objectives influence model robustness?
- RQ5Can human evaluators reliably distinguish adversarial code samples from original ones, and how fluent and consistent are the perturbed examples?
Key findings
- CodeAttack achieved the highest attack success rate across all evaluated models and tasks, with a Δ_drop of up to 50% on code summarization tasks, significantly outperforming NLP baselines.
- On GraphCodeBERT, CodeAttack induced a Δ_drop of over 50% in code translation tasks, demonstrating its high effectiveness due to the model’s reliance on data flow and identifier relationships.
- Human evaluation showed that 72.1% of adversarial samples were misclassified as original by annotators, and the average code understanding score was 4.14 out of 5, confirming fluency and imperceptibility.
- The combination of operator-level and token-level constraints improved attack success rate and Δ_drop significantly, with CodeBLEU q scores indicating high semantic fidelity.
- CodeT5 showed higher robustness than other models due to its pre-training on masked identifier prediction, which reduces sensitivity to name changes.
- CodeAttack outperformed all NLP-based adversarial attack baselines in attack quality, efficiency, and syntactic correctness, establishing a new benchmark for code-specific adversarial attacks.
Read this paper
Open the full text in the Nubint reader and ask about any passage as you read.
Read this paper on NubintThis review was generated by AI from the original paper. Please check the paper before citing it.