用工具反馈+代码修复模式,提升大模型生成嵌入式C代码的安全性。
Tool-Guided Retrieval-Augmented Repair for Securing LLM-Generated C Code
- 结合编译诊断与代码分析,检索历史修复模式迭代优化代码。
- 对CodeLlama 7B,安全缺陷率从49%降至19%,错误数降83.7%。
- 适合嵌入式系统开发者关注大模型生成代码的可靠性问题。
大型语言模型可从自然语言描述生成C代码,但生成程序常含安全漏洞和编译错误,对嵌入式及资源受限系统构成风险。本文研究反馈与检索如何提升LLM生成C代码的可靠性。提出一种分析-修复工作流,结合编译诊断、CodeQL静态分析和KLEE符号执行,并检索过往修复模式实现迭代优化。在5,000个涉及嵌入式相关漏洞的C编程任务上评估,基线模型存在显著可靠性差距:编译失败率最高达46%,安全缺陷率最高达49%。所提方法显著改善性能:对于CodeLlama 7B,安全缺陷率由49%降至19%,CodeQL总错误数从15,088降至2,463(降幅83.7%);对于DeepSeek Coder 1.3B,编译失败率由42%降至22%,安全缺陷率由35%降至15%。结果表明,集成轻量级分析工具可有效提升嵌入式开发中LLM生成代码的安全性。
原文摘要 · Abstract (English)
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.
Thank you to arXiv for use of its open access interoperability. PaperDance 不是 arXiv 官方产品;中文卡片由大模型生成,请以原文为准。