[Paper Review] DRAINCODE: Stealthy Energy Consumption Attacks on Retrieval-Augmented Code Generation via Context Poisoning
DrainCode is a RAG-based code generation attack that poisons retrieval contexts to induce significantly longer outputs, increasing latency and energy consumption while preserving functional accuracy.
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 a 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 an 85% increase in latency, a 49% increase in energy consumption, and more than a 3x 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.
Motivation & Objective
- Highlight the security risk of energy- and latency-focused attacks on retrieval-augmented code generation (RAG).
- Introduce DrainCode as a mutation-based poisoning framework that targets retrieval context to inflate output length without breaking code function.
- Develop a gradient-guided trigger optimization approach with stealth constraints to maximize token counts and energy use.
- Demonstrate cross-model and cross-prompt generalizability and compare against baselines and defenses.
Proposed method
- Inject poisoned code context into the retrieval corpus to steer LLMs toward verbose outputs.
- Generate a hypothetical unfinished code query to enable query-agnostic poisoning of retrieved snippets.
- Use gradient-guided mutations on adversarial triggers with EOS loss and a KL-divergence constraint to preserve non-trigger behavior.
- Enhance poisoning efficiency with multi-position mutation and an attack buffer pool to accelerate convergence.
- Evaluate DrainCode on multiple code models and datasets, comparing against RawRAG, Prompt Injection, and LLMEffiChecker.
Experimental results
Research questions
- RQ1How effective is DrainCode at increasing latency, energy, and output length in RAG-based code generation?
- RQ2Which components of DrainCode contribute most to attack effectiveness?
- RQ3How well does DrainCode generalize across prompting strategies and models?
- RQ4Can common detection methods identify DrainCode-poisoned contexts?
- RQ5How does drainCode affect code readability and functional accuracy under attack?
Key findings
- DrainCode increases output length by 3×–10× and latency by up to 85% with a 49% rise in energy consumption.
- Functional accuracy remains high (95–99%) despite substantial overhead.
- DrainCode achieves up to 3.5× faster poisoning than prior attacks and outperforms baselines in overhead.
- Attack effectiveness is generally robust across prompting strategies and transfers to black-box settings.
- The approach remains stealthy against classifier- and perplexity-based defenses while degrading throughput in resource-constrained environments.
Read this paper
Open the full text in the Nubint reader and ask about any passage as you read.
Read this paper on NubintThis review was generated by AI from the original paper. Please check the paper before citing it.