arXivDaily arXiv每日学术速递 周一至周五更新
arXiv 2412.14841cs.SEcs.AI
收藏

通过测试和静态分析反馈帮助LLM提升代码生成

Helping LLMs Improve Code Generation Using Feedback from Testing and Static Analysis

Greta Dolcetti, Vincenzo Arceri, Eleonora Iotti, Sergio Maffeis, Agostino Cortesi, Enea Zaffanella

AI总结:

本文提出利用测试和静态分析评估和指导通用开源LLM生成代码的质量及自我改进,发现生成代码常存在错误和安全问题,但提供反馈后能有效修复代码缺陷。

摘要:

大型语言模型(LLMs)是人工智能领域最具前景的发展之一,软件工程社区已意识到其在软件开发生命周期中的潜力。开发者经常要求LLMs生成代码片段,虽然提高了生产力,但也可能引入所有权、隐私、正确性和安全性问题。先前工作指出,主流商业LLMs生成的代码往往不安全,包含漏洞、bug和代码异味。本文提出一个框架,利用测试和静态分析评估通用开源LLMs生成代码的质量,并指导其自我改进。首先,我们让LLMs生成C代码解决多个编程任务。然后使用真实测试评估生成代码的正确性,使用静态分析工具检测潜在安全漏洞。接着评估模型评估生成代码的能力,通过让模型检测错误和漏洞。最后测试模型修复生成代码的能力,提供静态分析和不正确性评估阶段的报告作为反馈。我们的结果表明,模型常生成错误代码,生成代码可能包含安全问题。此外,它们在检测这些问题方面表现非常差。积极的一面是,当提供失败测试或潜在漏洞的信息时,模型显示出显著的修复能力,表明这是改进LLM基于代码生成工具安全性的有前途的途径。

英文摘要:

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.

机构、总结、中文摘要均由AI分析而来,可能存在错误,请以论文正文为准。

↑