paper-with-me

Papers

Grammar-Constrained Decoding Can Jailbreak LLMs into Generating Malicious Code

2026-06-10 · Yitong Zhang, Shiteng Lu, Jia Li 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.

📄 PDF Abstract BibTeX arXiv:2606.11817

Code (0)

등록된 구현이 없습니다.

Tasks

Code Generation

Similar Papers 제목 키워드 기반

Using Grammar Masking to Ensure Syntactic Validity in LLM-based Modeling Tasks

2024-07-08 · Lukas Netz, Jan Reimer, Bernhard Rumpe

We present and evaluate a method called grammar masking, which is used to guide large language models (LLMs) toward producing syntactically correct models for a given context-free grammar. Prompt engineering methods such…

Few-Shot LearningLanguage ModelingLanguage ModellingPrompt Engineering+1

CRANE: Reasoning with constrained LLM generation

2025-02-13 · Debangshu Banerjee, Tarun Suresh, Shubham Ugare, Sasa Misailovic 외

Code generation, symbolic math reasoning, and other tasks require LLMs to produce outputs that are both syntactically and semantically correct. Constrained LLM generation is a promising direction to enforce adherence to …

Code GenerationMathvalid

Grammar-Aligned Decoding

2024-05-31 · Kanghee Park, Jiayu Wang, Taylor Berg-Kirkpatrick, Nadia Polikarpova 외

Large Language Models (LLMs) struggle with reliably generating highly structured outputs, such as program code, mathematical formulas, or well-formed markup. Constrained decoding approaches mitigate this problem by greed…

Code Generation

Lookahead-then-Verify: Reliable Constrained Decoding for Diffusion LLMs under Context-Free Grammars

2026-01-31 · Yitong Zhang, Yongmin Li, Yuetong Liu, Jia Li 외 arxiv

Diffusion Large Language Models (dLLMs) have demonstrated promising generative capabilities and are increasingly used to produce formal languages defined by context-free grammars, such as source code and chemical express…

SafeDecoding: Defending against Jailbreak Attacks via Safety-Aware Decoding

2024-02-14 · Zhangchen Xu, Fengqing Jiang, Luyao Niu, Jinyuan Jia 외

As large language models (LLMs) become increasingly integrated into real-world applications such as code generation and chatbot assistance, extensive efforts have been made to align LLM behavior with human values, includ…

ChatbotCode Generation