Abstract
Code Language Models (CLMs) are increasingly integrated into software development workflows but remain susceptible to adversarial identifier substitution attacks. We present EP-Shield, an "evaluate-and-purify" framework that utilizes a Large Language Model (LLM) as both a judge of code naturalness and a purifier of malicious modifications. By fine-tuning a 7B parameter model via knowledge distillation from GPT-4, we achieve a lightweight yet robust defense mechanism. EP-Shield demonstrates defense success rates of up to 99% across multiple software engineering tasks, significantly outperforming traditional adversarial fine-tuning methods.
Introduction
The rapid adoption of Code Language Models (CLMs) like CodeBERT and StarCoder has revolutionized software engineering. However, these models possess a significant vulnerability: adversarial attacks. Specifically, identifier substitution attacks—where variable or method names are renamed to mislead the model while preserving logic—can drastically degrade performance.
A key observation is that while these attacks are syntactically valid, they often result in "unnatural" code that violates human programming conventions. For instance, renaming a descriptive variable like copy to a misleading term like create can trick a model into misclassification while appearing suspicious to a human reviewer.
Methodology: The EP-Shield Framework
EP-Shield addresses this vulnerability through a two-stage process that treats the LLM as both a "judge" and a "doctor."
1. Naturalness Evaluation (The Judge)
The framework assesses code snippets using a Naturalness Evaluation Score (NES) on a scale of 1 to 5. Code falling below a specific threshold is flagged as potentially adversarial. This metric captures semantic anomalies that traditional statistical metrics, such as Edit Distance, often overlook.
2. Code Purification (The Doctor)
Once flagged, the framework "purifies" the code by restoring suspicious identifiers to names that better align with the code's actual semantics and standard human conventions.
3. Knowledge Distillation
To ensure practical deployment, the authors employed knowledge distillation. GPT-4 was used to generate a dataset of 50,000 evaluation annotations and 46,000 purification pairs. A smaller, open-source Qwen2.5-coder-7B model was then fine-tuned on this data, achieving GPT-4 level performance with significantly lower computational overhead.
Experimental Results
The framework was evaluated across three benchmarks: Clone Detection (BigCloneBench), Vulnerability Detection (OWASP), and Code Summarization (CodeSearchNet).
Detection of Unnaturalness
Systematic analysis revealed that over 80% of adversarial examples generated by state-of-the-art attackers are detectable due to their lack of naturalness. As shown in the density plots, adversarial code consistently clusters at the lower end of the NES scale.
Defense Performance
EP-Shield achieved remarkable success rates:
- Clone Detection: ~99%
- Vulnerability Detection: ~95%
- Code Summarization: ~90%
Compared to standard adversarial fine-tuning, EP-Shield improved model robustness by as much as 83.36% in certain scenarios. Furthermore, the framework demonstrated strong generalization to unseen attack patterns, maintaining success rates above 85%.
Conclusion and Future Work
EP-Shield provides a robust, semantic-aware defense against identifier substitution attacks. By focusing on code naturalness rather than statistical perturbations, it aligns model behavior with human expertise. Future research will focus on extending this framework to handle structural code attacks and optimizing inference latency for real-time integration into development environments.
Summary updated