Evaluate-and-Purify: Fortifying Code Language Models Against Adversarial Attacks Using LLM-as-a-Judge

Wenhan Mu, Ling Xu, Shuren Pei, Le Mi, Huichi Zhou · 2025

Read Evaluate-and-Purify: Fortifying Code Language Models Against Adversarial Attacks Using LLM-as-a-Judge by Wenhan Mu, Ling Xu, Shuren Pei, Le Mi, Huichi Zhou (2025) in the Tower of Babel, a free online library of public-domain works preserved in clean, searchable text. No sign-up. The full text is served as a plain-text file at a predictable URL, where text is present: https://antarctic-labs.com/tower-of-babel/arxiv-2504-19730.txt.

Introduction

Wenhan Mu, Ling Xu, Shuren Pei, Le Mi, and Huichi Zhou defend code models by having a judge model sniff out attacks that look wrong. Renaming identifiers is the favorite way to fool code language models while keeping a program's behavior intact. The catch, revealed by systematic judging, is that most such attacks write unnatural code. Over eighty percent of examples from leading attackers were detectable on naturalness grounds.

Their framework, EP-Shield, first evaluates code naturalness to flag perturbed samples. It then purifies the flagged code so the victim model recovers its correct prediction. Experiments show the shield beating adversarial fine-tuning by wide margins, up to about eighty-three percent improvement on the headline measure. The judge runs at seven billion parameters while matching a flagship commercial model's judgment quality. Naturalness, long an aesthetic notion, becomes a security sensor.

Catalog record

CREATORWenhan Mu, Ling Xu, Shuren Pei, Le Mi, Huichi Zhou
YEAR2025
COLLECTIONPAPERS
FORMATTXT
FILE SIZE44 KB
VERSION1.0
LICENSEhttp://creativecommons.org/licenses/by/4.0/
RIGHTSCC BY
SOURCEarXiv

Source: https://arxiv.org/abs/2504-19730

Browse the full library · About the Tower of Babel

Full text

The opening of the work, from the library's hosted text:

Evaluate-and-Purify: Fortifying Code Language Models Against Adversarial Attacks Using LLM-as-a-Judge Wenhan Mu, Ling Xu, Shuren Pei, Le Mi, Huichi Zhou Abstract The widespread adoption of code language models in software engineering tasks has exposed vulnerabilities to adversarial attacks, especially the identifier substitution attacks. Although existing identifier substitution attackers demonstrate high success rates, they often produce adversarial examples with unnatural code patterns. In this paper, we systematically assess the quality of adversarial examples using LLM-as-a-Judge. Our analysis reveals that over 80% of adversarial examples generated by state-of-the-art identifier substitution attackers (e.g., ALERT) are actually detectable. Based on this insight, we propose EP-Shield, a unified framework for evaluating and purifying identifier substitution attacks via naturalness-aware reasoning. Specifically, we first evaluate the naturalness of code and identify the perturbed adversarial code, then purify it so that the victim model can restore correct prediction. Extensive experiments demonstrate the superiority of EP-Shield over adversarial fine-tuning (up to 83.36 % 83.36\% improvement) and its lightweight design ( 7 7 B parameters) with GPT- 4 4 -level performance. 1 Introduction Figure 1 : A motivating example of an identifier substitution attack, accompanied by an analysis of GPT-4 from the perspective of naturalness using LLM-as-a-Judge. Code Language Models (CLMs), including CodeBERT [ 1 ] , PLBART [ 2 ] , CodeGPT [ 3 ] , StarCoder [ 4 ] , and Code Llama [ 5 ] , have demonstrated remarkable capabilities across a range of software engineering tasks, such as code generation [ 6 ] , code summarization [ 7 ] , and vulnerability detection [ 8 ] . However, CLMs have been shown to be vulnerable to adversarial attacks, such as identifier substitution attacks, where variable or method names are manipulated to mislead models while preserving functionality [ 9 , 10 , 11 , 12 ] . Most identifier substitution attackers focus on ensuring syntactic validity while overlooking the naturalness of the code [ 13 , 14 , 15 , 12 ] . As shown in Figure 1 , the attacker fails to preserve naturalness in two key ways: (1) The logically meaningful identifier swapBlank is mutated to swapBlace , introducing a semantically meaningless subword ( Blace ); (2) The method name copy is replaced with create , creating a conflict with its associated copyof() function and introducing semantic ambiguity. By leveraging the code-understanding capabilities of Large Language Models (LLMs) such as GPT-4 [ 16 ] , we evaluate this case using a structured prompt (see Figure 4 ). GPT-4 assigns a score of 2 out of 5, reflecting a relatively low level of naturalness and suggesting that the perturbations violate programming conventions. In this paper, we begin by conducting an empirical study to assess the naturalness of adversarial code generated by state-of-the-art attackers, including MHM [ 10 ] , Beam-Attack [ 17 ] , and ALERT [ 12 ] . The experimental results demonstrate that all the current attackers produce adversarial examples with naturalness scores no higher than 2 out of 5, highlighting their unnatural code patterns. This finding emphasizes the critical need to maintain code naturalness when crafting adversarial attacks against CLMs. Inspired by the success of LLM-as-a-Judge in evaluating code-related tasks [ 18 , 19 ] and the self-correction ability of LLM [ 20 , 21 ] , this paper further puts forward EP-Shield, a two-stage evaluate-and-purify framework to fortify CLMs against adversarial attacks using LLM-as-a-Judge. We begin by constructing a dataset of 50K original-adversarial example pairs accompanied by GPT-4-generated evaluation annotations and a dataset of 46K code pairs of adversarial examples and GPT-4 purified counterparts. Based on both datasets, we then fine-tune an LLM (i.e., Qwen) and name it as EP-Shield. Specifically, we first evaluate the natural state of a code snippet to determine whether it has been attacked, and purify adversarial examples by transforming them into more natural code. Finally, the EP-Shield can also serve as an evaluation metric to identify adversarial code, and as a purifier to convert the code to benign code. To evaluate the effectiveness of EP-Shield, we perform experiments over three victim models (i.e., CodeBERT, CodeGPT, and PLBART) across three code-related tasks (i.e., clone detection, code summarization, and vulnerability detection), and compare it against four attack baselines (i.e., MHM, WIR, ALERT, and Beam-Attack). Experimental results show that EP-Shield can achieve defense success rates of approximately 99%, 95%, and 90% in clone detection, vulnerability detection, and code summarization tasks, respectively. Furthermore, EP-Shield demonstrates strong generalization ability with over 85\% defense success rate against unseen attack patterns. Additionally, EP-Shield achieves strong human evaluation alignment, significantly outperforming baseline metrics. The contributions of this paper are as follows. • Using LLM-as-a-Judge, we perform an empirical study to reveal existing identifier substitution attacks to CLMs have the naturalness issue. • We fine-tune a lightweight 7 B-parameter LLM to evaluate and purify the generated adversarial code examples. • We introduce a naturalness-aware defense strategy that aligns with human code review practices, enhancing the robustness of CLMs against identifier substitution attacks. 2 Background Adversarial Attack on CLMs. Given a code snippet x\in\mathcal{X} , an adversarial attack generates a perturbed sample x_{adv} by applying a slight perturbation \delta(x) such that \delta:X\rightarrow X_{adv} . For understanding tasks, a well-trained classifier f:\mathcal{X}\rightarrow\mathcal{Y} should correctly predict the label y_{truth}\in\mathcal{Y} , i.e., f(x)=y_{truth} , while the adversarial attack misleads the model into producing an incorrect output, i.e., f(x_{adv})\neq f(x)=y_{truth} . For generation tasks like code summarization [ 7 ] , adversarial examples degrade model performance such that P(y_{truth}|x_{adv}) is significantly lower than P(y_{truth}|x) . An adversarial example should adhere to the dual channel view of code [ 22 , 23 ] : (1) the formal channel , ensuring the perturbed code remains syntactically correct and semantically equivalent, and (2) the natural channel , requiring adherence to coding conventions with reasonable identifier names. Adversarial code attacks are categorized to three classes: (1) Token-level (e.g., identifier substitution [ 12 , 9 , 17 ] ), (2) Statement-level (e.g., dead code insertion [ 24 ] ), and (3) Structure-level (e.g., loop/conditional transformations [ 25 , 26 , 27 ] . Token-level attacks dominate due to their high success rate and low detectability [ 28 , 17 ] , with strategies ranging from random substitution [ 29 , 10 ] to context-aware methods [ 12 , 17 ] . Code Naturalness. Code exhibits a degree of naturalness analogous to human language, characterized by significant repetitiveness and systematic patterns that can be computationally modeled. This phenomenon arises not only from syntactic constraints inherent to programming languages but also emerges through project-specific conventions, including naming practices and API utilization patterns [ 15 , 30 , 31 ] . Empirical studies [ 32 ] have demonstrated developers’ systematic preference for code variants demonstrating higher predictability. Code naturalness has been attributed not only to syntactic formalism but more fundamentally reflects cognitive predispositions that prioritize ease of code generation and comprehension during software development processes. Evaluation of Adversarial Examples. Prior work focused on attack success rates and efficiency, with ALERT [ 12 ] introducing naturalness requirements to assess adversarial code, though manual evaluation may introduce bias. Beam-Attack [ 17 ] combines previous metrics [ 9 , 33 , 12 ] such as Identifier Change Rate and Average Code Similarity

Read the complete text