用测试和静态分析反馈,让大模型自修代码缺陷。
Helping LLMs Improve Code Generation Using Feedback from Testing and Static Analysis
- 用测试与静态分析评估大模型生成的代码质量
- 模型纠错能力弱但给反馈后修复率显著提升
- 适合想提升代码安全性的开发者与研究者
大型语言模型(LLMs)在人工智能领域发展迅速,软件工程界已关注其在开发周期中的潜力。开发者常请求大模型生成代码片段,虽提高效率,但也可能引入所有权、隐私、正确性及安全问题。此前研究表明,主流商业大模型生成的代码常存在漏洞、错误和代码异味。本文提出一个框架,利用测试与静态分析评估通用开源大模型生成代码的质量,并指导其自我改进。首先,让模型生成解决若干编程任务的C代码;随后,通过真实测试判断代码正确性,使用静态分析工具检测潜在安全漏洞;接着,评估模型识别错误与漏洞的能力;最后,在提供测试失败报告和漏洞分析结果作为反馈的前提下,测试其修复能力。实验表明,模型生成代码常出错且含安全隐患,且识别能力极差;但若给予具体反馈,修复能力显著提升,为改善基于大模型的代码生成安全性提供了可行路径。
原文摘要 · Abstract (English)
Large Language Models (LLMs) are one of the most promising developments in the field of artificial intelligence, and the software engineering community has readily noticed their potential role in the software development life-cycle. Developers routinely ask LLMs to generate code snippets, increasing productivity but also potentially introducing ownership, privacy, correctness, and security issues. Previous work highlighted how code generated by mainstream commercial LLMs is often not safe, containing vulnerabilities, bugs, and code smells. In this paper, we present a framework that leverages testing and static analysis to assess the quality, and guide the self-improvement, of code generated by general-purpose, open-source LLMs. First, we ask LLMs to generate C code to solve a number of programming tasks. Then we employ ground-truth tests to assess the (in)correctness of the generated code, and a static analysis tool to detect potential safety vulnerabilities. Next, we assess the models ability to evaluate the generated code, by asking them to detect errors and vulnerabilities. Finally, we test the models ability to fix the generated code, providing the reports produced during the static analysis and incorrectness evaluation phases as feedback. Our results show that models often produce incorrect code, and that the generated code can include safety issues. Moreover, they perform very poorly at detecting either issue. On the positive side, we observe a substantial ability to fix flawed code when provided with information about failed tests or potential vulnerabilities, indicating a promising avenue for improving the safety of LLM-based code generation tools.
Thank you to arXiv for use of its open access interoperability. PaperDance 不是 arXiv 官方产品;中文卡片由大模型生成,请以原文为准。