Manuscript Offprint

arXiv:2609.39902

CodeMimicry: Exploiting Safety Generalization Lag in Large Language Models via Structured Code Completion

Zhen Liang, Hai Huang, Wentao Chen

Abstract

Large language models have achieved remarkable capabilities across diverse domains, yet their safety alignment remains vulnerable to jailbreak attacks. In this work, we identify a previously underexplored failure mode—safety generalization lag—where alignment trained predominantly on natural language fails to transfer to the code domain. We show that this lag induces a code-completion blind spot, allowing malicious intent embedded within syntactically valid code to evade safety mechanisms. To exploit this vulnerability, we propose CodeMimicry, a fully automated black-box jailbreak framework that generates structured, object-oriented code prompts to induce harmful outputs via code completion. Experiments on 8 state-of-the-art commercial LLMs demonstrate that CodeMimicry achieves a 96.25% attack success rate with 1.51 queries on average, significantly outperforming both template-based and optimization-based baselines. Beyond empirical performance, we provide a mechanistic analysis of code-based jailbreaks through latent space representations, including projection onto refusal-related directions and activation steering. This analysis offers an explanation of how CodeMimicry bypasses safety mechanisms in code-related domains. Our findings reveal a weakness in current safety alignment and highlight the need for robust alignments in structured domains such as code. Our code is on https://github.com/lzzzr123/CodeMimicry.

1 Introduction

Large language models (LLMs) have demonstrated unprecedented capabilities in Natural Language Processing [1, 2], information retrieval [3], and code tasks [4, 5, 6], driving their integration into critical applications. Therefore, ensuring these models adhere to security guidelines has become crucial. Techniques such as reinforcement learning based on human feedback (RLHF) [7] and supervised fine-tuning [8] are widely used to align models with human values, thus building guardrails against malicious abuse. However, LLMs remain vulnerable to sophisticated jailbreak attacks, in which carefully crafted malicious prompts can induce the model to produce harmful or unintended outputs.

Jailbreak attacks against LLMs are generally divided into two categories: (1) White-box attacks, such as GCG [9], which use gradient-based optimization to craft adversarial suffixes. However, they require access to model parameters and gradients, making them impractical for closed-source LLMs. (2) Black-box attacks, including template-based and automated methods. Template-based approaches

∗^{*}Corresponding author. Email: haihuang1005@gmail.com

40th Conference on Neural Information Processing Systems (NeurIPS 2026).

such as CodeChameleon [10] and FlipAttack [11] rely on manually designed prompts to bypass safety mechanisms, but are often mitigated by simple rule-based or pattern-based defenses due to their fixed structures. Automated methods such as PAIR [12], GPTfuzzer [13], and AutoDan-Turbo [14] generate jailbreak prompts via model feedback, removing the need for manual design. However, they typically lack principled, domain-specific strategies (e.g., program structure or code completion), relying instead on heuristic exploration in natural language space, leading to limited effectiveness and efficiency.

A critical vulnerability arises from the rapid advancement of large language models: mismatched generalization [15], wherein models' broad pretraining capabilities substantially outpace the narrower scope of their safety alignment. While modern general LLMs have achieved near-expert proficiency in code generation and mathematical reasoning [16, 17], their safety alignment is still predominantly trained on natural language data. Motivated by this observation, we hypothesize the existence of a Safety Generalization Lag in the code domain: as LLMs' capabilities in the code domain continue to advance, their safety alignment fails to generalize effectively to this rigid, syntactic subspace. This lag creates a code-completion blind spot, where safety-aligned representations fail to generalize to the structured code space. As refusal behaviors are primarily learned from natural language, they transfer poorly to code generation, allowing malicious intent embedded in syntactically valid code to bypass safeguards.

In this paper, to address these limitations and support our hypothesis, we propose CodeMimicry, an efficient and fully automated jailbreak framework that eliminates manual strategy design by automatically generating jailbreak prompts in which malicious intent is embedded within structured code. Extensive experiments on 8 black-box commercial LLMs demonstrate the superiority of CodeMimicry. Notably, it achieves a 90% or higher attack success rate on each model with a very low average of queries. To better understand the behavioral differences between successful and failed code-based jailbreaks, we further conduct an in-depth internal analysis of model representations and refusal behaviors. These results suggest deficiencies in code-domain safety alignment and indicate that robust code-domain safety guardrails have not been sufficiently established within these LLMs. Our contributions are three-fold:

• Code-domain Jailbreaking Utilizing Safety Generalization Lag: We identify a previously underexplored vulnerability in LLMs—safety generalization lag—where alignment does not reliably transfer to the code domain. Leveraging this insight, we propose CodeMimicry, an automated jailbreak framework that embeds malicious intent into structured object-oriented code, shifting models from safety-aligned dialogue to compliance-driven code completion.

• Effectiveness with Minimal Query Budget: Across 8 state-of-the-art LLMs, CodeMimicry achieves a 96.25% attack success rate with 1.51 queries on average on AdvBench, and 96.07% ASR with 1.46 queries on HarmBench, substantially outperforming both template-based and optimization-based baselines.

• Mechanistic Insight via Latent Space Analysis: We conduct a mechanistic analysis of code-based jailbreaks in the latent representation space, including projection onto refusal-related directions and activation steering. The results show that successful jailbreaks remain close to benign code representations while avoiding refusal-associated features, providing an explanation of how CodeMimicry bypasses safety mechanisms in code domains.

References (40)