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

人工智能生成的代码被认为是有害的:安全且高质量代码生成的路线图

Artificial-Intelligence Generated Code Considered Harmful: A Road Map for Secure and High-Quality Code Generation

New Jersey Institute of Technology(新泽西理工学院)

Chun Jie Chong, Zhihao Yao, Iulian Neamtiu

AI总结:

本研究对比了LLM生成与人类编写代码的安全性和质量,发现LLM代码易出错、缺乏防御性编程且更易崩溃,且反馈循环无法可靠修复甚至可能引入新问题。

摘要:

通过LLM生成代码(而非从头编写代码)已迅速普及。然而,LLM生成代码的安全影响仍属未知。我们进行了一项研究,针对广泛的编程任务(包括数据结构、算法、加密例程和LeetCode问题),将人类编写代码的安全性和质量与LLM生成的代码进行了比较。为了评估代码安全性,我们使用了单元测试、模糊测试和静态分析。对于代码质量,我们关注复杂度和规模。我们发现,LLM会生成无法实现所需功能的错误代码,尤其是在更复杂的任务中;此类错误可能非常隐蔽。例如,对于加密算法SHA1,LLM生成了一个虽然能编译但实现错误的代码。在功能正确的情况下,我们发现LLM生成的代码安全性较低,主要原因是缺乏防御性编程结构,这引发了缓冲区溢出或整数溢出等一系列安全问题。模糊测试表明,与人类编写的代码相比,LLM生成的代码更容易出现挂起和崩溃。在质量方面,我们发现LLM生成的是缺乏防御性编程结构的精简代码,并且(按代码行数计算)通常比人类编写的代码更复杂。接下来,我们构建了一个反馈循环,要求LLM重新生成代码并消除发现的问题(例如,malloc溢出、数组索引越界、空指针解引用)。我们发现LLM无法始终如一地消除此类问题:虽然在某些情况下取得了成功,但我们发现重新生成的、本应更安全的代码中包含了新的问题;我们还发现,在提示下,LLM可能会在之前没有问题的文件中引入问题。

英文摘要:

Generating code via a LLM (rather than writing code from scratch), has exploded in popularity. However, the security implications of LLM-generated code are still unknown. We performed a study that compared the security and quality of human-written code with that of LLM-generated code, for a wide range of programming tasks, including data structures, algorithms, cryptographic routines, and LeetCode questions. To assess code security we used unit testing, fuzzing, and static analysis. For code quality, we focused on complexity and size. We found that LLM can generate incorrect code that fails to implement the required functionality, especially for more complicated tasks; such errors can be subtle. For example, for the cryptographic algorithm SHA1, LLM generated an incorrect implementation that nevertheless compiles. In cases where its functionality was correct, we found that LLM-generated code is less secure, primarily due to the lack of defensive programming constructs, which invites a host of security issues such as buffer overflows or integer overflows. Fuzzing has revealed that LLM-generated code is more prone to hangs and crashes than human-written code. Quality-wise, we found that LLM generates bare-bones code that lacks defensive programming constructs, and is typically more complex (per line of code) compared to human-written code. Next, we constructed a feedback loop that asked the LLM to re-generate the code and eliminate the found issues (e.g., malloc overflow, array index out of bounds, null dereferences). We found that the LLM fails to eliminate such issues consistently: while succeeding in some cases, we found instances where the re-generated, supposedly more secure code, contains new issues; we also found that upon prompting, LLM can introduce issues in files that were issues-free before prompting.

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

↑