Skip to main content
QUICK REVIEW

[Paper Review] Adversarial Attacks on Code Models with Discriminative Graph Patterns

Thanh-Dat Nguyen, Yang Zhou|arXiv (Cornell University)|Aug 22, 2023
Software Engineering ResearchComputer Science7 citations
TL;DR

This paper proposes GraphCodeAttack, a black-box adversarial attack framework for code models that automatically mines discriminative Abstract Syntax Tree (AST) patterns from model behavior to generate semantically preserving, high-impact perturbations. By identifying AST subgraphs that most influence model predictions and synthesizing them into realistic dead code insertions using a pre-trained code model, GraphCodeAttack achieves a 30% higher attack success rate (ASR) than CARROT and 33% higher than ALERT on CodeBERT and GraphCodeBERT across three tasks.

ABSTRACT

Pre-trained language models of code are now widely used in various software engineering tasks such as code generation, code completion, vulnerability detection, etc. This, in turn, poses security and reliability risks to these models. One of the important threats is extit{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 the aforementioned 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, which can influence the model's decisions, to perturb the structure of input code to the model. To do so, GraphCodeAttack uses a set of input source codes to probe the model's outputs and identifies the extit{discriminative} ASTs patterns that can influence the model decisions. GraphCodeAttack then selects appropriate AST patterns, concretizes the selected patterns as attacks, and inserts them as dead code into the model's input program. To effectively synthesize attacks from AST patterns, GraphCodeAttack uses a separate pre-trained code model to fill in the ASTs with concrete code snippets. We evaluate the robustness of two popular code models (e.g., CodeBERT and GraphCodeBERT) against our proposed approach on three tasks: Authorship Attribution, Vulnerability Prediction, and Clone Detection. The experimental results suggest that our proposed approach significantly outperforms state-of-the-art approaches in attacking code models such as CARROT and ALERT.

Motivation & Objective

  • To address the limitations of existing adversarial attacks on code models that rely on fixed, hand-crafted transformations such as variable renaming and dead code insertion.
  • To develop an automated, model-agnostic method for identifying adversarial patterns that significantly influence code model predictions without requiring white-box access.
  • To improve the robustness evaluation of pre-trained code models by generating more effective and complex adversarial examples than current state-of-the-art approaches.
  • To reduce the number of token changes required for successful attacks while maintaining high success rates, enhancing attack efficiency and stealth.

Proposed method

  • GraphCodeAttack operates in a black-box setting, using only model outputs and input programs to probe decision boundaries.
  • It mines discriminative AST subgraphs from a set of input programs and their corresponding model predictions, identifying patterns that most influence model decisions.
  • The framework selects high-impact AST patterns and uses a separate pre-trained code model to generate concrete, syntactically valid code snippets that conform to these patterns.
  • These concrete snippets are inserted as dead code into the original program to create adversarial examples that preserve semantics but alter model predictions.
  • The attack synthesis phase leverages a pre-trained code model to ensure syntactic correctness and naturalness of the perturbed code.
  • The approach is evaluated across three tasks—authorship attribution, vulnerability prediction, and clone detection—on two popular code models: CodeBERT and GraphCodeBERT.
Figure 1. Overview of GraphCodeAttack ’s method. $\mathcal{M}_{t}$ is the target victim model, $\mathcal{M}_{f}$ is the language model used to fill in the <MASK>
Figure 1. Overview of GraphCodeAttack ’s method. $\mathcal{M}_{t}$ is the target victim model, $\mathcal{M}_{f}$ is the language model used to fill in the <MASK>

Experimental results

Research questions

  • RQ1Can automatically mined AST patterns be more effective than hand-crafted transformations in crafting adversarial examples for code models?
  • RQ2How does GraphCodeAttack compare to state-of-the-art black-box attacks like CARROT and ALERT in terms of attack success rate (ASR) across diverse code model tasks?
  • RQ3To what extent does GraphCodeAttack reduce the number of token changes required to achieve successful attacks, indicating efficiency and stealth?
  • RQ4Can GraphCodeAttack be used to improve model robustness through adversarial fine-tuning without requiring full adversarial training or model output access?

Key findings

  • GraphCodeAttack achieved an average attack success rate (ASR) of 0.841 on the GraphCodeBERT model for authorship attribution, significantly outperforming CARROT (ASR: 0.598) and ALERT (ASR: 0.615).
  • On average, GraphCodeAttack improved ASR by 30% over CARROT and 33% over ALERT across all evaluated tasks and models.
  • The attack required fewer token changes than baseline methods, with code change rate decreasing as input file size increased, indicating higher efficiency and lower detectability.
  • Adversarial fine-tuning using GraphCodeAttack achieved robustness improvement comparable to ALERT, but without requiring access to the target model’s outputs or full adversarial training on the dataset.
  • The method demonstrated strong generalization across diverse tasks, including vulnerability prediction and clone detection, confirming its effectiveness beyond a single application.
  • The use of a pre-trained code model to concretize AST patterns ensured syntactic correctness and naturalness of the generated adversarial examples.
Figure 2. Attacking with pattern: Given the original source code (a), GraphCodeAttack identify the important statement on line 2: f = sys.stdin . GraphCodeAttack then chooses the pattern $(b)$ consisting of an if statement with unknown condition and body. GraphCodeAttack inserts this text pattern in
Figure 2. Attacking with pattern: Given the original source code (a), GraphCodeAttack identify the important statement on line 2: f = sys.stdin . GraphCodeAttack then chooses the pattern $(b)$ consisting of an if statement with unknown condition and body. GraphCodeAttack inserts this text pattern in

Read this paper

Open the full text in the Nubint reader and ask about any passage as you read.

Read this paper on Nubint

This review was generated by AI from the original paper. Please check the paper before citing it.