Tool-Guided Retrieval-Augmented Repair for Securing LLM-Generated C Code
Summary
A new tool-guided workflow enhances the reliability and security of C code generated by large language models, particularly for embedded and resource-constrained systems. This approach integrates compilation diagnostics via GCC, CodeQL static analysis, and KLEE symbolic execution with a retrieval-augmented repository of prior repair patterns for iterative refinement. Evaluated on 5,000 C programming tasks, the method significantly improves outcomes for models like CodeLlama 7B and DeepSeek Coder 1.3B. CodeLlama 7B saw security defect rates drop from 49% to 19%, with total CodeQL errors decreasing from 15,088 to 2,463, an 83.7% reduction. DeepSeek Coder 1.3B experienced a reduction in compilation failures from 42% to 22% and security defects from 35% to 15%. These results demonstrate that combining lightweight analysis tools with retrieval can substantially improve LLM-generated C code safety.
Key takeaway
For AI Security Engineers deploying LLM-generated C code in embedded or resource-constrained systems, relying solely on initial generation or manual inspection is insufficient. You should integrate automated, multi-tool validation pipelines that combine compilation, static analysis like CodeQL, and symbolic execution such as KLEE. This approach, augmented by a repository of successful repair patterns, demonstrably reduces security defects and compilation failures, making LLM-generated C code safer for critical applications.
Key insights
Integrating multi-tool feedback and retrieval of repair patterns significantly improves LLM-generated C code security and reliability.
Principles
- Iterative repair with tool feedback enhances LLM code quality.
- Prioritize security issues before compilation failures.
- Accumulate and reuse validated repair patterns.
Method
The workflow involves initial generation, compilation/static analysis (GCC, CodeQL), retrieval-guided repair using a growing pattern repository, and final KLEE symbolic validation.
In practice
- Use GCC and CodeQL for C code analysis.
- Implement KLEE for symbolic execution checks.
- Maintain a repository of successful code repairs.
Topics
- LLM-Generated Code
- C Code Security
- Static Analysis
- Symbolic Execution
- Retrieval-Augmented Repair
- Embedded Systems
Best for: AI Engineer, Research Scientist, CTO, AI Scientist, Machine Learning Engineer, AI Security Engineer
Related on AIssential
See Counsel's argued verdicts on the open AI decisions leaders are weighing →
Editorial summary, takeaway, and curation by AIssential. Original article published by cs.SE updates on arXiv.org.