DrainCode: Stealthy Energy Consumption Attacks on Retrieval-Augmented Code Generation via Context Poisoning

Yuyi Wang, Jiadong Wu, Tianyue Jiang, Mingwei Liu, Ting Chen, Chenyu Wang, Ensheng Shi, Xilin Liu, Yuchi Ma, Zibin Zheng

TL;DR

This paper introduces DRAINCODE, a novel attack that exploits retrieval-augmented code generation systems by poisoning their retrieval contexts. DRAINCODE forces these models to generate significantly longer, yet functionally correct, code outputs, leading to substantial increases in latency and energy consumption without compromising code quality. This work highlights a new security vulnerability in LLMs, focusing on computational efficiency rather than functional correctness.

What Problem Does This Paper Address?

Large Language Models (LLMs) are increasingly used for code generation, often leveraging Retrieval-Augmented Generation (RAG) to improve their capabilities. While the functional aspects of LLM security have been explored, their computational costs, particularly latency and energy consumption, have been largely overlooked from a security perspective. This is a critical gap, as LLMs are being integrated into developer tools where frequent, resource-intensive operations can lead to significant operational overhead and environmental impact. The paper addresses this by introducing a new class of attacks, LLM-Denial-of-Service (LLM-DoS), specifically targeting the computational efficiency of RAG-based code generation systems. The challenge lies in creating attacks that increase resource usage without breaking the code's functionality or introducing obvious errors, making them stealthy and difficult to detect.

Methods

DRAINCODE employs a three-component strategy to achieve its goals:

  1. Hypothetical Query Construction: To overcome the limitation of not knowing the user's actual query, DRAINCODE uses the LLM itself to generate plausible "unfinished code" snippets based on retrieved context. This allows the attacker to simulate real-world query scenarios and perform query-agnostic poisoning.
  2. Adversarial Trigger Generation: The core of the attack involves injecting "adversarial triggers" into the retrieval corpus. These triggers are short, learnable token sequences that, when processed by the LLM, influence its generation process. The triggers are optimized using a gradient-guided mutation approach. This process is guided by two loss functions:
  3. EOS Loss: Minimizes the probability of the model generating an End-of-Sequence (EOS) token prematurely, encouraging longer outputs.
  4. Token Diversity Loss: Encourages a wider range of hidden states across generated tokens, further promoting verbosity and exploring more output possibilities. A KL-divergence constraint is also applied to ensure that the model's output distribution outside the trigger's influence remains consistent with clean generation, preserving functional correctness and stealth.

  5. Efficient Adversary Sample Optimization: To speed up the attack process, DRAINCODE introduces:

  6. Multi-Position Mutation: Updates multiple tokens in the trigger simultaneously, significantly reducing convergence time.
  7. Attack Buffer Pool: Stores recently generated attack strings, allowing the system to select the best-performing ones and efficiently manage the search space.

Datasets

The paper evaluates DRAINCODE using two established code completion benchmarks:

  • RepoEval: Comprises 373 repository-level function completion tasks, utilizing original GitHub repositories (1.7M files) as a knowledge base.
  • Odex: Contains 945 open-domain coding problems, with 34,000 Python library documentation snippets serving as the retrieval corpus.

These datasets are chosen for their suitability for execution-based evaluation using pass rate metrics.

Key Insights and Ideas

  • Shifting the Attack Surface: DRAINCODE moves beyond functional attacks to target the computational efficiency of LLMs, revealing a new dimension of LLM security.
  • Stealthy Resource Exhaustion: The attack is designed to be stealthy by preserving functional correctness, making it harder for users and systems to detect. The increased output length is achieved through "coerced verbosity" rather than outright errors.
  • Query-Agnostic Poisoning: The hypothetical query construction mechanism is a key innovation, enabling attackers to poison retrieval corpora without needing to know the specific queries users will make.
  • Dual Loss for Controlled Verbosity: The combination of EOS loss and token diversity loss effectively drives longer outputs while the KL-divergence constraint maintains functional integrity.
  • Efficiency Matters: The multi-position mutation and attack buffer pool significantly speed up the poisoning process, making the attack more practical.

Key Results

DRAINCODE demonstrates significant effectiveness across various models and datasets:

  • Output Length: Achieves up to a 3x increase in output length on RepoEval and up to 10x on Odex compared to baselines.
  • Latency: Increases inference latency by up to 85%.
  • Energy Consumption: Boosts energy consumption by up to 49%.
  • Functional Accuracy: Maintains high functional accuracy, with Pass@1 scores dropping by only 1.2% to 4.6% compared to unattacked RAG.
  • Efficiency: Outperforms LLMEffiChecker by achieving a 3.5x speedup in poisoning time.
  • Generalizability: The attack is effective across different prompting strategies (Zero-Shot, Few-Shot, CoT) and transfers well in black-box settings.
  • Detection Evasion: DRAINCODE evades common detection methods like SVM classifiers and perplexity-based detection, with finetuned CodeBERT also showing limitations.

Limitations and Future Work

  • Threat Model: The attacker is assumed to have access to the model's weights for trigger optimization and can poison the retrieval corpus. While the attack transfers to black-box settings, its effectiveness is reduced.
  • Detection Focus: Current detection methods focus on the retrieval corpus. Future work could explore advanced techniques for detecting poisoned generated code.
  • Model Architectures: The evaluation used specific LLM architectures. Larger, proprietary, or mixture-of-experts models might exhibit different energy-latency profiles.
  • Dataset Limitations: The benchmarks used are limited. Expanding to more diverse datasets is a potential future direction.
  • CPU/System Overhead: Energy and latency measurements are confined to GPU metrics, potentially underestimating true consumption.

The authors suggest exploring more advanced detection techniques for generated code and investigating the attack's impact on a wider range of LLM architectures and datasets.

Summary updated

Abstract

Large language models (LLMs) have demonstrated impressive capabilities in code generation, by leveraging retrieval-augmented generation (RAG) methods. However, the computational costs associated with LLM inference, particularly in terms of latency and energy consumption, have received limited attention in the security context. This paper introduces DrainCode, the first adversarial attack targeting the computational efficiency of RAG-based code generation systems. By strategically poisoning retrieval contexts through mutation-based approach, DrainCode forces LLMs to produce significantly longer outputs, thereby increasing GPU latency and energy consumption. We evaluate the effectiveness of DrainCode across multiple models. Our experiments show that DrainCode achieves up to a 85% increase in latency, a 49% increase in energy consumption, and more than a 3× increase in output length compared to the baseline. Furthermore, we demonstrate the generalizability of the attack across different prompting strategies and its effectiveness compared to different defenses. The results highlight DrainCode as a potential method for increasing the computational overhead of LLMs, making it useful for evaluating LLM security in resource-constrained environments. We provide code and data at https://github.com/DeepSoftwareAnalytics/DrainCode.
BibTeX
@misc{wang2026draincode,
  author = {Yuyi Wang and Jiadong Wu and Tianyue Jiang and Mingwei Liu and Ting Chen and Chenyu Wang and Ensheng Shi and Xilin Liu and Yuchi Ma and Zibin Zheng},
  title = {{DrainCode: Stealthy Energy Consumption Attacks on Retrieval-Augmented Code Generation via Context Poisoning}},
  year = {2026},
  eprint = {2601.20615},
  archivePrefix = {arXiv},
  url = {https://arxiv.org/abs/2601.20615},
}
Figure