mlsanityData to 14 Oct 2026

Grammar-Constrained Decoding Can Jailbreak LLMs into Generating Malicious Code

arXiv 2606.11817 ↗First posted 10 Jun 2026cs.CR cs.AI cs.CLcited by 0

On the map: LLM safety and security → Jailbreaking

Abstract (arXiv)

Large Language Models (LLMs) are increasingly used for code generation, raising concerns that they may be misused to produce malicious code. Meanwhile, Grammar-Constrained Decoding (GCD) has been widely adopted to improve the reliability of LLM-generated code by enforcing syntactic validity. In this paper, we reveal a counterintuitive risk: this reliability-oriented technique can itself become an attack surface. We uncover a new jailbreak attack, termed CodeSpear, that exploits GCD to induce LLMs into generating malicious code. Our experiments show that simply applying a benign code grammar constraint can effectively jailbreak LLMs. To address this vulnerability, we propose CodeShield, a safety alignment approach that robustly preserves safe behavior even under attacker-controlled grammar constraints. CodeShield aligns the model in the code modality by teaching it to generate honeypot code under GCD. Such code is semantically harmless, so it does not implement the malicious request, and structurally diverse, so it is difficult to suppress through grammar tightening. At the same time, CodeShield still preserves natural-language refusals when natural language is available. Experiments on 10 popular LLMs across 4 benchmarks show that CodeSpear outperforms representative jailbreak baselines and increases the attack success rate by more than 30 percentage points on average. CodeShield also restores safety under CodeSpear while preserving benign utility. Our findings reveal a fundamental risk of GCD and call for greater attention to its potential security implications.

Key facts. No citing papers in the map yet (data through 14 Oct 2026). Placed in Jailbreaking, one of 12 directions in LLM safety and security. Builds on 42 of its 54 references that are in the map. First posted on arXiv 10 Jun 2026.

Builds on

42 of 54 references.

Closest ancestors · 11

Same direction, newest first

  1. 2025DiffuGuard: How Intrinsic Safety is Lost and Found in Diffusion Large Language Models29 Sep
  2. 2025LLMs Caught in the Crossfire: Malware Requests and Jailbreak Challenges9 Jun
  3. 2025JULI: Jailbreak Large Language Models by Self-Introspection17 May
  4. 2025Smoke and Mirrors: Jailbreaking LLM-based Code Generation via Implicit Malicious Prompts23 Mar
  5. 2025Exploiting Prefix-Tree in Structured Output Interfaces for Enhancing Jailbreak Attacking19 Feb
  6. 2024RMCBench: Benchmarking Large Language Models' Resistance to Malicious Code23 Sep
  7. 2024Security Attacks on LLM-based Code Completion Tools20 Aug
  8. 2024Boosting Jailbreak Attack with Momentum2 May
  9. 2023Jailbreaking Black Box Large Language Models in Twenty Queries12 Oct
  10. 2023Low-Resource Languages Jailbreak GPT-43 Oct
  11. 2023"Do Anything Now": Characterizing and Evaluating In-The-Wild Jailbreak Prompts on Large Language Models7 Aug

Other references · 31

  1. 2024The Llama 3 Herd of Modelscited by 14,715
  2. 2021Evaluating Large Language Models Trained on Codecited by 8,320
  3. 2023Direct Preference Optimization: Your Language Model is Secretly a Reward Modelcited by 8,065
  4. 2024Qwen2.5 Technical Reportcited by 4,942
  5. 2021Program Synthesis with Large Language Modelscited by 3,637
  6. 2025gpt-oss-120b & gpt-oss-20b Model Cardcited by 1,386
  7. 2024Qwen2.5-Coder Technical Reportcited by 1,203
  8. 2023Fine-tuning Aligned Language Models Compromises Safety, Even When Users Do Not Intend To!cited by 1,154
  9. 2026DeepSeek-V4: Towards Highly Efficient Million-Token Context Intelligencecited by 1,040
  10. 2025OpenAI GPT-5 System Cardcited by 989
  11. 2024Safety Alignment Should Be Made More Than Just a Few Tokens Deepcited by 472
  12. 2025LLMs Get Lost In Multi-Turn Conversationcited by 429
19 more
  1. 2024PKU-SafeRLHF: Towards Multi-Level Safety Alignment for LLMs with Human Preferencecited by 261
  2. 2025AgentSpec: Customizable Runtime Enforcement for Safe and Reliable LLM Agentscited by 223
  3. 2025Safety Tax: Safety Alignment Makes Your Large Reasoning Models Less Reasonablecited by 122
  4. 2024XGrammar: Flexible and Efficient Structured Generation Engine for Large Language Modelscited by 75
  5. 2024SynCode: LLM Generation with Grammar Augmentationcited by 69
  6. 2025OpenCodeInstruct: A Large-scale Instruction Tuning Dataset for Code LLMscited by 60
  7. 2025SafeDPO: A Simple Approach to Direct Preference Optimization with Enhanced Safetycited by 37
  8. 2025Exploring the Potential of Diffusion Large Language Models in Code Generationcited by 16
  9. 2025RedCodeAgent: Automatic Red-teaming Agent against Diverse Code Agentscited by 9
  10. 2025Goal-Aware Identification and Rectification of Misinformation in Multi-Agent Systemscited by 8
  11. 2025DAVSP: Safety Alignment for Large Vision-Language Models via Deep Aligned Visual Safety Promptcited by 8
  12. 2025MOCHA: Are Code Language Models Robust Against Multi-Turn Malicious Coding Prompts?cited by 7
  13. 2026Lookahead-then-Verify: Reliable Constrained Decoding for Diffusion LLMs under Context-Free Grammarscited by 7
  14. 2024Using Grammar Masking to Ensure Syntactic Validity in LLM-based Modeling Taskscited by 6
  15. 2026Omni-Safety under Cross-Modality Conflict: Vulnerabilities, Dynamics Mechanisms and Efficient Alignmentcited by 6
  16. 2026What Papers Don't Tell You: Recovering Tacit Knowledge for Automated Paper Reproductioncited by 6
  17. 2025AI-Driven Self-Evolving Software: A Promising Path Toward Software Automationcited by 3
  18. 2026To See is Not to Master: Teaching LLMs to Use Private Libraries for Code Generationcited by 3
  19. 2026PackMonitor: Enabling Zero Package Hallucinations Through Decoding-Time Monitoringcited by 2

Built on by

Not cited yet.

Similar, not cited

Close in wording, no citation either way

  1. 2026CodeMimicry: Exploiting Safety Generalization Lag in Large Language Models via Structured Code Completion
  2. 2024SafeDecoding: Defending against Jailbreak Attacks via Safety-Aware Decoding
  3. 2025When Grammar Guides the Attack: Uncovering Control-Plane Vulnerabilities in LLMs with Structured Output
  4. 2024Root Defence Strategies: Ensuring Safety of LLM at the Decoding Level
  5. 2025Logic Jailbreak: Efficiently Unlocking LLM Safety Restrictions Through Formal Logical Expression
  6. 2023Analyzing the Inherent Response Tendency of LLMs: Real-World Instructions-Driven Jailbreak
  7. 2025Forewarned is Forearmed: Pre-Synthesizing Jailbreak-like Instructions to Enhance LLM Safety Guardrail to Potential Attacks
  8. 2026Defending Large Language Models Against Jailbreak Attacks via In-Decoding Safety-Awareness Probing