CodeAttack: Revealing Safety Generalization Challenges of Large Language Models via Code Completion

Qibing Ren, Chang Gao, Jing Shao, Junchi Yan, Xin Tan +2 more
3/12/2024
113 citations
cs.CLcs.AIcs.CR

No official code found. Best implementation: AI45Lab/CodeAttack (61 stars, Python, MIT, last commit 2025-10-01).

Abstract

The rapid advancement of Large Language Models (LLMs) has brought about remarkable generative capabilities but also raised concerns about their potential misuse. While strategies like supervised fine-tuning and reinforcement learning from human feedback have enhanced their safety, these methods primarily focus on natural languages, which may not generalize to other domains.

Show full abstract

This paper introduces CodeAttack, a framework that transforms natural language inputs into code inputs, presenting a novel environment for testing the safety generalization of LLMs. Our comprehensive studies on state-of-the-art LLMs including GPT-4, Claude-2, and Llama-2 series reveal a new and universal safety vulnerability of these models against code input: CodeAttack bypasses the safety guardrails of all models more than 80\% of the time. We find that a larger distribution gap between CodeAttack and natural language leads to weaker safety generalization, such as encoding natural language input with data structures. Furthermore, we give our hypotheses about the success of CodeAttack: the misaligned bias acquired by LLMs during code training, prioritizing code completion over avoiding the potential safety risk. Finally, we analyze potential mitigation measures. These findings highlight new safety risks in the code domain and the need for more robust safety alignment algorithms to match the code capabilities of LLMs.

View on arXivView PDF

Code Implementations(1)

AI45Lab/CodeAttackCommunity100%
MIT

[ACL 2024] CodeAttack: Revealing Safety Generalization Challenges of Large Language Models via Code Completion

618PythonMay 19, 20241 years agoMIT

Similar Papers

Cite this paper

@article{ren2024codeattack,
  title  = {CodeAttack: Revealing Safety Generalization Challenges of Large Language Models via Code Completion},
  author = {Qibing Ren and Chang Gao and Jing Shao and Junchi Yan and Xin Tan and Wai Lam and Lizhuang Ma},
  year   = {2024},
  eprint = {2403.07865},
  archivePrefix = {arXiv},
  url    = {http://arxiv.org/abs/2403.07865v5}
}

Add a badge to your README

Shows whether official code is linked for this paper.

Code of Paper badge
[![Code of Paper](https://codeofpaper.com/badge/2403.07865.svg)](https://codeofpaper.com/paper/2403.07865)

Discussion