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

Zhen Liang, Hai Huang, Wentao Chen
9/30/2026
cs.CRcs.AI

No confident code implementation found for this paper.

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.

Show full abstract

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.

View on arXivView PDF

Code Implementations

No confident code match yet

We couldn't find an author-owned or strongly-evidenced community implementation for this paper. Any repos shown below are weak matches — verify before relying on them.

No code implementations found yet.

Know of an implementation? Let us know in the comments below!

Similar Papers

Cite this paper

@article{liang2026codemimicry,
  title  = {CodeMimicry: Exploiting Safety Generalization Lag in Large Language Models via Structured Code Completion},
  author = {Zhen Liang and Hai Huang and Wentao Chen},
  year   = {2026},
  eprint = {2609.39902},
  archivePrefix = {arXiv},
  url    = {http://arxiv.org/abs/2609.39902v1}
}

Add a badge to your README

Shows whether official code is linked for this paper.

Code of Paper badge
[![Code of Paper](https://codeofpaper.com/badge/2609.39902.svg)](https://codeofpaper.com/paper/2609.39902)

Discussion