Tool-Guided Retrieval-Augmented Repair for Securing LLM-Generated C Code

📅 2026-07-21
📈 Citations: 0
Influential: 0
📄 PDF
🤖 AI Summary
This work addresses the critical challenge that C code generated by large language models (LLMs) often contains security vulnerabilities and compilation errors, rendering it unsuitable for safety-critical embedded systems. To enhance reliability, the authors propose an automated repair pipeline that integrates compiler diagnostics, CodeQL static analysis, and KLEE symbolic execution. Notably, this approach introduces, for the first time, a synergistic combination of lightweight program analysis and retrieval-augmented generation to refine LLM-produced code. Experimental evaluation on CodeLlama-7B and DeepSeek-Coder-1.3B demonstrates substantial improvements: security defect rates drop from 49% to 19% and from 35% to 15%, respectively; CodeQL-reported errors decrease by 83.7%; and compilation failure rates fall from 42% to 22%. These results collectively signify a marked enhancement in both the safety and compilability of generated code.
📝 Abstract
Large language models can generate C code from natural-language descriptions, but resulting programs often contain security vulnerabilities and compilation errors, posing risks for embedded and resource-constrained systems. This work investigates how feedback and retrieval improve reliability of LLM-generated C code. We present an analysis-and-repair workflow that combines compilation diagnostics, CodeQL static analysis, and KLEE symbolic execution with retrieval of prior repair patterns for iterative refinement. Evaluated on 5,000 C programming tasks exercising embedded relevant vulnerabilities, baseline models show substantial reliability gaps, with compilation failure rates up to 46% and security defect rates up to 49%. Our approach improves both metrics. For CodeLlama 7B, security defect rates decrease from 49% to 19% and total CodeQL errors drop from 15,088 to 2,463 (83.7%). For DeepSeek Coder 1.3B, compilation failures are reduced from 42% to 22% and security defects from 35% to 15%. These results show that integrating lightweight analysis tools can improve the safety of LLM-generated code for embedded development.
Problem

Research questions and friction points this paper is trying to address.

LLM-generated C code
security vulnerabilities
compilation errors
embedded systems
code reliability
Innovation

Methods, ideas, or system contributions that make the work stand out.

Retrieval-Augmented Repair
Static Analysis
Symbolic Execution
LLM-Generated Code
Embedded Security