Beyond Natural Language Perplexity: Detecting Dead Code Poisoning in Code Generation Datasets

Chi-Chien Tsai, Chia-Mu Yu, Ying-Dar Lin, Yu-Sung Wu, Wei-Bin LeearXiv 2025 · DOI

TL;DR

This paper introduces DEPA, a novel line-level method for detecting "dead code poisoning" in code generation datasets. By analyzing code line-by-line and comparing perplexity to the file's overall distribution, DEPA significantly outperforms existing token-level methods in accuracy and speed, making it a practical solution for securing code LLM training data.

What Problem Does This Paper Address?

The widespread use of Large Language Models (LLMs) for code-related tasks, like code completion and search, relies heavily on vast training datasets. However, these datasets are vulnerable to "dead code poisoning" attacks. This involves injecting syntactically valid but functionally useless code snippets into the training data. These snippets act as hidden triggers, manipulating the LLM's behavior to produce biased, insecure, or otherwise undesirable outputs. Existing detection methods, often borrowed from natural language processing and relying on token-level perplexity, struggle to effectively identify these poisoned code segments due to the inherent structural and contextual nature of programming languages. This gap leaves code LLMs susceptible to subtle attacks that can compromise their reliability and security.

Methods

The paper proposes DEPA (Dead Code Perplexity Analysis), a novel approach that shifts the focus from token-level analysis to line-level perplexity. The core idea is that dead code, being functionally redundant, creates anomalies that are more apparent at the line level than at the individual token level.

Here's how DEPA works:

  1. Line-Level Perplexity Calculation: For each line in a code snippet, DEPA creates variants by sequentially removing that specific line while keeping all others. It then uses a Code LLM (like CodeLlama) to compute the perplexity of the code snippet combined with its associated task description for each variant. The perplexity for a given line is derived from these calculations, effectively measuring how "surprising" that line is in the context of the entire file.
  2. Contextual Analysis: Crucially, DEPA doesn't evaluate a line's perplexity in isolation. Instead, it compares each line's perplexity score against the overall distribution of perplexity scores for all lines within the same file.
  3. Anomaly Detection: A line is flagged as potentially poisoned if its perplexity significantly exceeds the mean perplexity of the file, specifically by more than a defined threshold (T) times the standard deviation (T=1.5 in their experiments). This threshold is chosen to balance detection accuracy and false positives.

Datasets

The authors evaluated DEPA on four widely-used benchmark datasets for code generation tasks:

  • MBPP (Mostly Basic Python Problems): Targets beginners and covers fundamental programming concepts.
  • HumanEval: Consists of algorithmic and straightforward mathematical tasks.
  • MathQA-Python: Focuses on mathematical problem-solving by converting math problems into Python code.
  • APPS (Automated Programming Progress Standard): Includes problems from programming competitions.

The paper does not introduce new datasets but uses these existing ones to demonstrate the effectiveness of DEPA against poisoned versions of these standard benchmarks.

Key Insights and Ideas

  • Code is Structural: Unlike natural language, code has a rigid structure where each line often represents a distinct operational unit. This structural property makes line-level anomalies more indicative of dead code than token-level deviations.
  • Context is King: Analyzing a line's perplexity in isolation can be misleading. DEPA's strength lies in comparing a line's perplexity to the file's overall distribution, allowing it to distinguish truly anomalous lines from those that might appear unusual on their own but are valid within the broader context.
  • LLMs as Perplexity Oracles: By leveraging the understanding of code that a trained Code LLM possesses, DEPA uses perplexity not just to measure understanding but as a tool to identify deviations from expected code patterns, thereby detecting potential poisoning.

Key Results

DEPA demonstrates significant improvements over existing methods, particularly ONION (which uses token-level perplexity):

  • Detection Accuracy (F1-score): DEPA achieves an average F1-score of 0.28, substantially outperforming ONION(CodeGPT) (0.09) and ONION(CodeLlama) (0.14). This represents a 0.14-0.19 improvement.
  • Poisoned Segment Localization Precision: DEPA achieves an impressive average accuracy of 0.85, compared to ONION(CodeGPT) (0.29) and ONION(CodeLlama) (0.41). This is a 44-65% improvement.
  • Detection Speed: DEPA is significantly faster, processing an average of 88.16 samples per minute, compared to ONION(CodeGPT) (54.26) and ONION(CodeLlama) (3.71). This is a 0.62-23x speedup.
  • AUROC: DEPA achieves a robust AUROC of 0.80, indicating strong discriminative capability.

Limitations and Future Work

  • Language Generalization: DEPA is primarily tested on Python. Its seamless generalization to other programming languages with different syntaxes (e.g., C++ with semicolons, or languages with different line-splitting conventions) is an open question.
  • False Positives: While DEPA is effective, it can still produce false positives, especially against highly covert poisoning techniques that modify variable or function names.
  • Adaptive Attacks: The authors explored an adaptive attack scenario where a genetic algorithm was used to craft dead code to evade DEPA. While DEPA's performance degraded, it still maintained a degree of resilience.
  • Future Work: The authors suggest refining DEPA by incorporating static analysis and syntax rule checks to further reduce false positives. Exploring adaptations for diverse languages and coding styles is also a promising direction.

Summary updated

BibTeX
@misc{tsai2025beyond,
  author = {Chi-Chien Tsai and Chia-Mu Yu and Ying-Dar Lin and Yu-Sung Wu and Wei-Bin Lee},
  title = {{Beyond Natural Language Perplexity: Detecting Dead Code Poisoning in Code Generation Datasets}},
  year = {2025},
  eprint = {2502.20246},
  archivePrefix = {arXiv},
  url = {https://arxiv.org/abs/2502.20246},
}
Figure