Skip to main content
QUICK REVIEW

[论文解读] Adversarial Attacks on Code Models with Discriminative Graph Patterns

Thanh-Dat Nguyen, Yang Zhou|arXiv (Cornell University)|Aug 22, 2023
Software Engineering Research被引用 7
一句话总结

本文提出 GraphCodeAttack,一种针对代码模型的黑盒对抗攻击框架,通过自动挖掘模型行为中具有区分性的抽象语法树(AST)模式,生成语义保持且影响重大的扰动。通过识别最影响模型预测的 AST 子图,并利用预训练代码模型将其合成到真实的死代码插入中,GraphCodeAttack 在 CodeBERT 和 GraphCodeBERT 上的攻击成功率(ASR)比 CARROT 高 30%,比 ALERT 高 33%,覆盖三个任务。

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.

研究动机与目标

  • 解决现有代码模型对抗攻击方法的局限性,这些方法依赖于固定的、手工设计的转换方式(如变量重命名和死代码插入)。
  • 开发一种自动化、与模型无关的方法,用于识别显著影响代码模型预测的对抗模式,且无需白盒访问权限。
  • 通过生成比当前最先进方法更有效、更复杂的对抗样本,提升预训练代码模型的鲁棒性评估能力。
  • 在保持高成功率的同时,减少成功攻击所需的标记(token)更改数量,从而提升攻击效率和隐蔽性。

提出的方法

  • GraphCodeAttack 在黑盒设置下运行,仅使用模型输出和输入程序来探测决策边界。
  • 从一组输入程序及其对应的模型预测中挖掘具有区分性的 AST 子图,识别出最影响模型决策的模式。
  • 该框架选择高影响力的 AST 模式,并利用独立的预训练代码模型生成符合这些模式的具体、语法正确的代码片段。
  • 这些具体代码片段被插入到原始程序中作为死代码,以生成保持语义但改变模型预测的对抗样本。
  • 攻击生成阶段利用预训练代码模型,以确保扰动后代码的语法正确性和自然性。
  • 该方法在两个主流代码模型(CodeBERT 和 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>

实验结果

研究问题

  • RQ1自动挖掘的 AST 模式是否比手工设计的转换方式在生成代码模型对抗样本方面更具有效性?
  • RQ2在多种代码模型任务中,GraphCodeAttack 与 CARROT 和 ALERT 等最先进黑盒攻击方法相比,攻击成功率(ASR)表现如何?
  • RQ3GraphCodeAttack 在多大程度上减少了实现成功攻击所需的标记更改数量,从而体现其效率和隐蔽性?
  • RQ4GraphCodeAttack 是否可用于通过对抗微调提升模型鲁棒性,而无需完整对抗训练或访问目标模型输出?

主要发现

  • 在 GraphCodeBERT 模型上,GraphCodeAttack 在作者归属识别任务中实现了平均 0.841 的攻击成功率(ASR),显著优于 CARROT(ASR: 0.598)和 ALERT(ASR: 0.615)。
  • 在所有评估任务和模型中,GraphCodeAttack 平均比 CARROT 提高 30% 的 ASR,比 ALERT 提高 33%。
  • 该攻击所需的标记更改数量少于基线方法,且随着输入文件大小的增加,代码更改率下降,表明其效率更高且更难被检测。
  • 使用 GraphCodeAttack 进行对抗微调,实现了与 ALERT 相当的鲁棒性提升,但无需访问目标模型输出或对数据集进行完整对抗训练。
  • 该方法在包括漏洞预测和代码克隆检测在内的多种任务中均表现出强泛化能力,证实其有效性不仅限于单一应用场景。
  • 利用预训练代码模型将 AST 模式具体化,确保了生成的对抗样本在语法正确性和自然性方面表现良好。
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

更好的研究,从现在开始

从阅读论文到最终审阅,大幅缩短您的研究时间。

无需绑定信用卡

本解读由 AI 生成,并经人工编辑审核。