Detecting Functional Memorization in Code Language Models

Matthieu Meeus, Anil Ramakrishna, Shengyuan Hu, Matthew Grange, Zheng Xu, Luca Melis

Here is a blog-style summary of the research paper.

TL;DR

Code language models don't just memorize text verbatim; they can internalize and replicate the underlying logic of their training data using entirely different syntax and structure. By introducing a counterfactual, execution-based testing framework, this paper reveals that standard textual audits miss over 90% of this "functional memorization," posing hidden intellectual property and data leakage risks.

What Problem Does This Paper Address?

When we evaluate whether a Large Language Model (LLM) has leaked or memorized its training data, we usually look for textual overlap—checking if the model reproduces code token-for-token or nearly so (using metrics like BLEU). However, code is unique: the exact same algorithmic logic can be written in countless textually distinct ways by renaming variables, restructuring control flows, or changing formatting.

If a model internalizes a proprietary algorithm or business rule and reproduces it in a completely rewritten form, traditional textual audits will completely fail to detect it. This paper addresses this critical gap by defining, quantifying, and detecting functional memorization—instances where a model replicates the exact behavior of training data while evading text-based detection.

An example of execution-based testing revealing counterfactual functional memorization where the target model reproduces hard-coded logic in a restructured format.

Methods

The authors formalize functional memorization using a strict counterfactual framework combined with execution-based testing:

  1. Counterfactual Setup: To distinguish true memorization from general coding capability, they compare a target model (exposed to the specific training data) against a reference model (sharing the same architecture but unexposed to the target data). A function is only considered memorized if the target model can reproduce it but the reference model cannot.
  2. Agentic Execution Testing: Instead of relying on static analysis, the authors deploy state-of-the-art AI coding agents (using Claude Code) to dynamically build execution environments for training functions. The agent generates diverse test inputs, executes both the original training function and the model's generation, and measures the output match rate ($\text{SIM}_{\text{func}}$).
  3. Proxy Evaluation: Because running live code execution is computationally expensive (taking 2–5 minutes per sample), they evaluate more scalable alternatives, including structural code metrics (CodeBLEU, TSED), embedding similarities, and LLM-as-a-judge prompting strategies.

Datasets

The framework was evaluated across 4 open-source models (OLMo-3-32B, OLMo-3-7B, StarCoder-15B, and CrystalCoder-7B) across 5 programming languages (Python, Java, C, Rust, and Go). The data subsets were drawn from:

  • Dolmino corpus subsets: CraneCode (heavily rewritten/filtered Python code) and Stack-Edu (high-quality public code).
  • StarCoderData: The Python subset used to train StarCoder.

To ensure the study focused on high-stakes leakage, the authors used LLaMA-3.1-8B-Instruct as a preliminary judge to filter out boilerplate code, retaining only functions containing meaningful proprietary logic or hard-coded business rules.

Key Insights and Ideas

  • Textual Audits Create a False Sense of Security: Functional memorization is largely orthogonal to verbatim leakage. Over 90% of the functions flagged as functionally memorized by execution testing were completely missed by textual similarity metrics.
  • Driven by Semantic Duplication: Traditional textual deduplication (like MinHash) is insufficient to prevent this type of leakage. The authors discovered that functional memorization is strongly driven by semantic duplication—the presence of multiple textually diverse but logically identical variants of a function within the training corpus.
  • LLMs are Excellent Code Judges: While structural and embedding-based metrics struggle to accurately predict execution behavior, advanced LLMs (like Claude Sonnet 4.6) acting as judges serve as highly accurate, scalable proxies for full execution testing.
A comparison of various metrics attempting to approximate execution-based functional similarity.

Key Results

  • Prevalence: Across the open-source models evaluated, 0.3% to 3.4% of the filtered functions were counterfactually functionally memorized.
  • Scale Matters: Larger models are more prone to functional leakage; OLMo-3-32B exhibited 20–70% higher functional memorization rates than its 7B counterpart.
  • Language Variations: Memorization rates varied significantly by language; C and Rust showed the highest rates of functional leakage (up to 3.4%), while Java and Go were memorized roughly half as often.
  • Proxy Performance: The best-performing LLM-judge configuration (Sonnet-4.6) achieved a True Positive Rate (TPR) of 68% at a strict 1% False Positive Rate (FPR), making it a viable tool for codebase auditing.
The fraction of functions textually vs. functionally memorized across different models, datasets, and programming languages.

Limitations and Future Work

  • Non-Binary Equivalence: The study treats functional equivalence as a threshold ($\ge 0.8$), but code behavior isn't strictly binary. A generated function that only replicates a specific subset of execution paths might still constitute a meaningful IP leak.
  • Harness Noise: The agentic execution framework relies on LLM generation for scaffolding and test inputs, which introduces non-determinism and potential measurement noise.
  • Counterfactual Imperfection: Creating a perfect "leave-one-out" model at scale is computationally impossible, meaning the reference models used are close approximations rather than perfect baselines.

Future Directions: The authors suggest extending this audit to instruction-tuned models, developing specialized fine-tuned LLM judges for faster detection, and exploring optimal thresholds for semantic deduplication during data preprocessing to mitigate functional leakage before training begins.

Summary updated

Abstract

Large language models (LLMs) are increasingly used to generate code at scale. Meanwhile, prior work has investigated whether training data may be recoverable from model outputs, by auditing the textual overlap between training examples and model generations. Code, however, can preserve the same logic while differing substantially in syntax and structure. We here study functional memorization: the leakage of training data logic from LLM generations in ways that textual audits fail to detect. We leverage AI coding agents to generate diverse test inputs for training data functionality and evaluate whether model-generated continuations produce the same outputs. We formalize this through a counterfactual framework, comparing target models (exposed to specific code) against reference models (not exposed) and requiring functional equivalence only for the target. We instantiate this framework across 4 open-source models and explicitly filter for functions with meaningful logic in 5 programming languages. We find that 0.3-3.4% of filtered functions are counterfactually functionally memorized, i.e., reproduced with equivalent behavior in restructured code that textual metrics fail to detect. We further show that LLM-based judges offer a scalable proxy for execution-based testing, achieving a true positive rate of 68% at 1% false positive rate, and find that functional memorization is associated with semantic duplication in the training corpus.
BibTeX
@inproceedings{meeus2026detecting,
  author = {Matthieu Meeus and Anil Ramakrishna and Shengyuan Hu and Matthew Grange and Zheng Xu and Luca Melis},
  title = {{Detecting Functional Memorization in Code Language Models}},
  booktitle = {DL4C @ ICML},
  year = {2026},
  url = {https://openreview.net/forum?id=yWBrNBcRLj},
}

Loading related content...

Figure