mlsanityData to 14 Oct 2026

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

arXiv 2403.07865 ↗First posted 12 Mar 2024cs.CL cs.AI cs.CRcited by 76

On the map: LLM safety and security → Safety alignment, refusal and guardrails

Abstract (arXiv)

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. 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.

Key facts. Cited by 76 papers in the map (data through 14 Oct 2026). Placed in Safety alignment, refusal and guardrails, one of 12 directions in LLM safety and security. Builds on 31 of its 44 references that are in the map. First posted on arXiv 12 Mar 2024.

Builds on

31 of 44 references.

Closest ancestors · 1

Same direction, newest first

  1. 2023Fine-tuning Aligned Language Models Compromises Safety, Even When Users Do Not Intend To!5 Oct

Other references · 30

  1. 2022Training language models to follow instructions with human feedbackcited by 15,971
  2. 2023Llama 2: Open Foundation and Fine-Tuned Chat Modelscited by 13,028
  3. 2017Deep reinforcement learning from human preferencescited by 3,912
  4. 2021Finetuned Language Models Are Zero-Shot Learnerscited by 3,780
  5. 2022Training a Helpful and Harmless Assistant with Reinforcement Learning from Human Feedbackcited by 3,720
  6. 2023Universal and Transferable Adversarial Attacks on Aligned Language Modelscited by 3,104
  7. 2022Constitutional AI: Harmlessness from AI Feedbackcited by 3,050
  8. 2023Code Llama: Open Foundation Models for Codecited by 2,139
  9. 2023Jailbreaking Black Box Large Language Models in Twenty Queriescited by 1,420
  10. 2022Red Teaming Language Models with Language Modelscited by 1,026
  11. 2022Red Teaming Language Models to Reduce Harms: Methods, Scaling Behaviors, and Lessons Learnedcited by 729
  12. 2023Baseline Defenses for Adversarial Attacks Against Aligned Language Modelscited by 651
18 more
  1. 2024How Johnny Can Persuade LLMs to Jailbreak Them: Rethinking Persuasion to Challenge AI Safety by Humanizing LLMscited by 573
  2. 2023SmoothLLM: Defending Large Language Models Against Jailbreaking Attackscited by 469
  3. 2023Jailbreak and Guard Aligned Language Models with Only Few In-Context Demonstrationscited by 431
  4. 2023GPT-4 Is Too Smart To Be Safe: Stealthy Chat with LLMs via Ciphercited by 423
  5. 2022The Stack: 3 TB of permissively licensed source codecited by 359
  6. 2022A Holistic Approach to Undesired Content Detection in the Real Worldcited by 356
  7. 2023Principle-Driven Self-Alignment of Language Models from Scratch with Minimal Human Supervisioncited by 318
  8. 2023CodeGeeX: A Pre-Trained Model for Code Generation with Multilingual Benchmarking on HumanEval-Xcited by 300
  9. 2023Exploiting Programmatic Behavior of LLMs: Dual-Use Through Standard Security Attackscited by 278
  10. 2023Pretraining Language Models with Human Preferencescited by 247
  11. 2021Gradient-based Adversarial Attacks against Text Transformerscited by 234
  12. 2023Scalable and Transferable Black-Box Jailbreaks for Language Models via Persona Modulationcited by 214
  13. 2023Automatically Auditing Large Language Models via Discrete Optimizationcited by 191
  14. 2019Build it Break it Fix it for Dialogue Safety: Robustness from Adversarial Human Attackcited by 176
  15. 2023AnnoLLM: Making Large Language Models to Be Better Crowdsourced Annotatorscited by 172
  16. 2023FLIRT: Feedback Loop In-context Red Teamingcited by 83
  17. 2024PsySafe: A Comprehensive Framework for Psychological-based Attack, Defense, and Evaluation of Multi-agent System Safetycited by 80
  18. 2024Identifying Semantic Induction Heads to Understand In-Context Learningcited by 47

Built on by

Cited by year (2026 to 14 Oct)

Citing papers per year: 2024: 7; 2025: 39; 2026: 30.

76 papers.

Sort:

By area:

  1. 2024Refuse Whenever You Feel Unsafe: Improving Safety in LLMs via Decoupled Refusal Trainingcited by 69
  2. 2025Foot-In-The-Door: A Multi-turn Jailbreak for LLMscited by 54
  3. 2024Code-Switching Red-Teaming: LLM Evaluation for Safety and Multilingual Understandingcited by 34
  4. 2024The Dark Side of Function Calling: Pathways to Jailbreaking Large Language Modelscited by 20
  5. 2025QueryAttack: Jailbreaking Aligned Large Language Models Using Structured Non-natural Query Languagecited by 18
  6. 2025Lifelong Safety Alignment for Language Modelscited by 15
  7. 2025From Code Foundation Models to Agents and Applications: A Comprehensive Survey and Practical Guide to Code Intelligencecited by 15
  8. 2024Black-Box Adversarial Attacks on LLM-Based Code Completioncited by 14
  9. 2026Obscure but Effective: Classical Chinese Jailbreak Prompt Optimization via Bio-Inspired Searchcited by 13
  10. 2025GuidedBench: Measuring and Mitigating the Evaluation Discrepancies of In-the-wild LLM Jailbreak Methodscited by 12
  11. 2025Response Attack: Exploiting Contextual Priming to Jailbreak Large Language Modelscited by 10
  12. 2025RedCoder: Automated Multi-Turn Red Teaming for Code LLMscited by 9
38 more
  1. 2024POROver: Improving Safety and Reducing Overrefusal in Large Language Models with Overgeneration and Preference Optimizationcited by 8
  2. 2025Why Not Act on What You Know? Unleashing Safety Potential of LLMs via Self-Aware Guard Enhancementcited by 8
  3. 2025ARMOR: Aligning Secure and Safe Large Language Models via Meticulous Reasoningcited by 8
  4. 2025Attention Eclipse: Manipulating Attention to Bypass LLM Safety-Alignmentcited by 7
  5. 2025OMNIGUARD: An Efficient Approach for AI Safety Moderation Across Languages and Modalitiescited by 7
  6. 2025Breaking the Code: Security Assessment of AI Code Agents Through Systematic Jailbreaking Attackscited by 7
  7. 2026AlignCoder: Aligning Retrieval with Target Intent for Repository-Level Code Completioncited by 7
  8. 2025HumorReject: Decoupling LLM Safety from Refusal Prefix via A Little Humorcited by 6
  9. 2025Bleeding Pathways: Vanishing Discriminability in LLM Hidden States Fuels Jailbreak Attackscited by 5
  10. 2025Smoke and Mirrors: Jailbreaking LLM-based Code Generation via Implicit Malicious Promptscited by 5
  11. 2025SoK: Systematizing LLM Prompt Security: Taxonomies, Datasets, and Unified Evaluation of Attacks and Defensescited by 5
  12. 2024FATS: A Prompt Injection Attack Utilizing Feign Security Agents with Deceptive Few-shots Learningcited by 4
  13. 2024SQL Injection Jailbreak: A Structural Disaster of Large Language Modelscited by 4
  14. 2025When Grammar Guides the Attack: Uncovering Control-Plane Vulnerabilities in LLMs with Structured Outputcited by 4
  15. 2025Imperceptible Jailbreaking against Large Language Modelscited by 4
  16. 2025Characterizing Selective Refusal Bias in Large Language Modelscited by 4
  17. 2026Defending Large Language Models Against Jailbreak Attacks via In-Decoding Safety-Awareness Probingcited by 4
  18. 2026What Breaks When LLMs Code? Characterizing Operational Safety Failures of Agentic Code Assistantscited by 4
  19. 2025HarmRLVR: Weaponizing Verifiable Rewards for Harmful LLM Alignmentcited by 3
  20. 2026STAR-S: Improving Safety Alignment through Self-Taught Reasoning on Safety Rulescited by 3
  21. 2026SoK: Robustness in Large Language Models against Jailbreak Attackscited by 3
  22. 2025StructTransform: A Scalable Attack Surface for Safety-Aligned Large Language Modelscited by 2
  23. 2025InfoFlood: Jailbreaking Large Language Models with Information Overloadcited by 2
  24. 2025ERIS: Evolutionary Real-world Interference Scheme for Jailbreaking Audio Large Modelscited by 2
  25. 2025Jailbreaking LLMs via Semantically Relevant Nested Scenarios with Targeted Toxic Knowledgecited by 2
  26. 2025ArtPerception: ASCII Art-based Jailbreak on LLMs with Recognition Pre-testcited by 2
  27. 2026CASA: Classification Augmented with Safety Attention for Robust Multimodal Alignmentcited by 2
  28. 2026Involuntary In-Context Learning: Exploiting Few-Shot Pattern Completion to Bypass Safety Alignment in GPT-5.4cited by 2
  29. 2026Furina: Fragmented Uncertainty-Driven Refusal Instability Attackcited by 2
  30. 2026Structuring the Space of Sociotechnical Alignmentcited by 2
  31. 2025Self-Instruct Few-Shot Jailbreaking: Decompose the Attack into Pattern and Behavior Learningcited by 1
  32. 2025GraphAttack: Exploiting Representational Blindspots in LLM Safety Mechanismscited by 1
  33. 2025Activation-Guided Local Editing for Jailbreaking Attackscited by 1
  34. 2025Layer-Wise Perturbations via Sparse Autoencoders for Adversarial Text Generationcited by 1
  35. 2025SafetyFlow: An Agent-Flow System for Automated LLM Safety Benchmarkingcited by 1
  36. 2025ASTRA: An Automated Framework for Strategy Discovery, Retrieval, and Evolution for Jailbreaking LLMscited by 1
  37. 2025Why Does Weak-OOD Help? A Further Step Towards Understanding Jailbreaking VLMscited by 1
  38. 2025Understanding and Mitigating Over-refusal for Large Language Models via Representation Interventioncited by 1

50 most cited of 76 All 76 (JSON)

Similar, not cited

Close in wording, no citation either way

  1. 2024Navigating the Safety Landscape: Measuring Risks in Finetuning Large Language Models
  2. 2025SafeMERGE: Preserving Safety Alignment in Fine-Tuned Large Language Models via Selective Layer-Wise Model Merging
  3. 2024SG-Bench: Evaluating LLM Safety Generalization Across Diverse Tasks and Prompt Types
  4. 2025Fine-Tuning Lowers Safety and Disrupts Evaluation Consistency
  5. 2026Safety Targeted Embedding Exploit via Refinement
  6. 2024ToolSword: Unveiling Safety Issues of Large Language Models in Tool Learning Across Three Stages
  7. 2026Beyond Safe Data: Pretraining-Stage Alignment with Regular Safety Reflection
  8. 2024Towards Understanding Safety Alignment: A Mechanistic Perspective from Safety Neurons