Jia Li, Yitong Zhang, Shiteng Lu
We lifted 14 functions out of this paper's own repositories and ran 13 of them in a sandbox. "Ran" means the function executed on a synthesized input and returned a value. It is not a reproduction of the paper's results.
| Repository | Role | Ran |
|---|---|---|
| TsinghuaISE/CodeSpear-CodeShield | canonical | 13 of 14 |
| Function | Status | Where it lives |
|---|---|---|
| build_mr_user_prompt | Ran | TsinghuaISE/CodeSpear-CodeShield/Attack/LocalModels/script/evaluate_mr.py code served (permissive licence) · get_code("b2911c9f56f3f47c") |
| build_output_root | Ran | TsinghuaISE/CodeSpear-CodeShield/Attack/LocalModels/script/merge_shards.py code served (permissive licence) · get_code("4bf38ef0ebadef49") |
| build_result_file_name | Ran | TsinghuaISE/CodeSpear-CodeShield/Attack/LocalModels/script/run_main.py code served (permissive licence) · get_code("748e861da66aaa33") |
| choose_shards | Ran | TsinghuaISE/CodeSpear-CodeShield/Attack/LocalModels/script/merge_shards.py code served (permissive licence) · get_code("a4b7dce535911857") |
| get_result_prefix | Ran | TsinghuaISE/CodeSpear-CodeShield/Attack/LocalModels/script/run_main.py code served (permissive licence) · get_code("4c4af1cda9f9e155") |
| has_malicious_content | Ran | TsinghuaISE/CodeSpear-CodeShield/Defense/build_training_data.py code served (permissive licence) · get_code("cbdd6c14a82c9ee1") |
| infer_resume_index | Ran | TsinghuaISE/CodeSpear-CodeShield/Attack/LocalModels/script/run_main.py code served (permissive licence) · get_code("aecb899a3b7dc24b") |
| is_python | Ran | TsinghuaISE/CodeSpear-CodeShield/Defense/build_training_data.py code served (permissive licence) · get_code("5285b0b1a83df036") |
| list_shard_items | Ran | TsinghuaISE/CodeSpear-CodeShield/Attack/LocalModels/script/merge_shards.py code served (permissive licence) · get_code("719c374000448a22") |
| load_malicious_keywords | Ran | TsinghuaISE/CodeSpear-CodeShield/Defense/build_training_data.py code served (permissive licence) · get_code("4d2567d9174fe3cb") |
| strip_forced_prefix | Ran | TsinghuaISE/CodeSpear-CodeShield/Attack/LocalModels/script/evaluate_asr.py code served (permissive licence) · get_code("e3598a0158070938") |
| truncate_text | Ran | TsinghuaISE/CodeSpear-CodeShield/Attack/LocalModels/script/evaluate_asr.py code served (permissive licence) · get_code("d74ef718b83da46a") |
| truncate_text | Ran | TsinghuaISE/CodeSpear-CodeShield/Attack/LocalModels/script/evaluate_mr.py code served (permissive licence) · get_code("fd2cfb4a6f4f355b") |
| get_evaluation | Not yet run | TsinghuaISE/CodeSpear-CodeShield/Attack/LocalModels/script/evaluate_asr.py code served (permissive licence) · get_code("d5c2e4e4ec38a32f") |
Some links come from the archived Papers with Code dataset (CC BY-SA 4.0): attribution and licence.
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. Index Terms-grammar-constrained decoding, jailbreak attack, safety alignment, malicious code generation Large Language Models (LLMs) have demonstrated impressive capabilities across a wide range of tasks and are increasingly deployed in real-world applications [1][2][3][4]. At the same time, growing evidence shows that LLMs can be jailbroken to bypass safety alignment and produce harmful content [5][6][7][8]. This risk becomes especially concerning in code generation [9][10][11][12], where harmful outputs are not merely textual instructions but executable programs that can be directly weaponized against digital systems [13,14]. In this paper, we uncover a new jailbreak attack, termed CodeSpear, that leverages widely used Grammar-Constrained * Equal contribution: Yitong Zhang proposed the idea and wrote the paper; Shiteng Lu implemented the approaches and ran most of the experiments.
The same record, over MCP at https://syntology.ai/mcp:
get_harvested_code_for_paper("2606.11817")
get_code_for_paper("2606.11817")
have("2606.11817")
Connect an agent — have() is free.