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

*Liang Zhen, Wentao Chen, Hai Huang*

[NeurIPS page](https://neurips.cc/virtual/2026/poster/148231) · [Open on the map](../papers.html#p148231)

Category: [AI Safety, Security & Alignment](../pcat/16.md) › [Jailbreak Defense and Analysis](../pgroup/130.md) · Type: new method · Keywords: LLM safety, jailbreak, code completion, safety generalization, black-box attack, code-based jailbreak

Areas: [Code & Coding Agents](../areas/codegen.md) › Code Verification › Coding agent benchmarks; [Safety & Security](../areas/safety.md) › Safe Alignment › Safety benchmarks

## Summary
CodeMimicry is an automated black-box jailbreak framework that exploits safety generalization lag in LLMs by generating structured, object-oriented code prompts to induce harmful code completions, achieving a 96.25% attack success rate with an average of 1.51 queries on eight commercial models.

## Explained to a 5-year-old
Computers that write words have rules to stop them from being mean. Those rules are learned from normal sentences, not from code. Code is a special way of writing instructions for computers. Sometimes people can hide bad ideas inside code that looks okay. The safety rules might not catch them.

## Explained to a 5-year-old, longer
Computers that write words have rules to stop them from being mean. Those rules are learned from normal sentences, not from code. Code is a special way of writing instructions for computers. Sometimes people can hide bad ideas inside code that looks okay. The safety rules might not catch them. We looked at this problem and found it happens often. We created a way to automatically make code that tricks the computer into saying bad things. The tool tries many code ideas until it finds one that works. It works on many big computer programs. It can do this with just a few tries. The results show it is very successful. This shows that we need to make safety rules better for code. We hope this helps make safer computer programs.

## For a technical reader
CodeMimicry automatically crafts object-oriented code prompts that embed malicious intent and uses black-box optimization to find prompts that bypass safety filters. It evaluates the approach on eight commercial LLMs, achieving a 96.25% success rate with only 1.51 queries on average. Latent-space analysis reveals that the model’s refusal directions are not activated by these code prompts, explaining the bypass. The work underscores a safety gap in code-generation models and calls for stronger alignment in structured domains.

## Problem
Large language models aligned on natural language often fail to generalize safety to the code domain, creating a blind spot where malicious intent can be hidden in syntactically valid code and evade safety mechanisms.

## Method
The approach automatically generates structured, object-oriented code prompts that embed malicious intent and uses black-box optimization to find prompts that cause harmful completions while evading safety filters. It also performs latent-space analysis, projecting activations onto refusal directions and steering to understand the bypass mechanism.

## Results
The approach achieves a 96.25% attack success rate with an average of 1.51 queries across the evaluated models, significantly outperforming baseline methods.

## Contributions
The paper identifies safety generalization lag in the code domain, proposes an automated black-box jailbreak framework for code prompts, provides a mechanistic analysis of how safety filters are bypassed, and demonstrates the vulnerability on commercial LLMs.

## Practical relevance
The work highlights a critical safety gap in code-generating AI systems, urging developers to strengthen alignment for structured programming contexts.

## Research questions this paper is counted under

- [How can we reliably prevent large language models from generating harmful content across diverse contexts and modalities?](../q/24.md)

This page was written by language models from the title and abstract of each paper, and the answers were checked by two other models. It can be wrong: check the paper itself. [Suggest a correction](../feedback/?on=p/148231)
