[论文解读] DeepCode AI Fix: Fixing Security Vulnerabilities with Large Language Models
本文提出 DeepCode AI Fix,一种利用程序分析将代码缩减为关键片段后再应用大语言模型(LLMs)修复安全漏洞的系统。通过聚焦 LLM 于最小且上下文相关的代码,该方法提升了修复准确率——实现超过 80% 的缺陷修复率,且在 10%–50% 的案例中与人工修复完全匹配,优于 GPT-3.5、GPT-4 和 TFix 基线模型,在一个包含 5,000 多个真实世界修复案例的高质量新数据集上表现优异。
The automated program repair field has attracted substantial interest over the years, but despite significant research efforts, creating a system that works well for complex semantic bugs such as security vulnerabilities has proven difficult. A promising direction to solve this challenge is by leveraging large language models (LLMs), which are increasingly used to solve various programming tasks. In this paper, we investigate the effectiveness of LLMs for solving code-repair task. We show that the task is difficult as it requires the model to learn long-range code relationships, a task that inherently relies on extensive amounts of training data. At the same time, creating a large, clean dataset for complex program bugs and their corresponding fixes is non-trivial. We propose a technique to address these challenges with a new approach for querying and fine-tuning LLMs. The idea is to use program analysis to limit the LLM's attention mechanism on the portions of code needed to perform the fix, drastically reducing the amount of required training data. Concretely, for training and inference, rather than feeding the entire program to the LLM, we reduce its code to a much shorter snippet that contains the reported defect together with the necessary context - and use that instead. Our evaluation shows that this code reduction approach substantially improves available models such as GPT-4 using few-shot learning, as well as fine-tuning models. To train and evaluate our system, we created a comprehensive code fixing dataset by extensively labeling 156 bug patterns (including 40 security rules), requiring complex interprocedural dataflow to discover. Our best system with Mixtral-8x7B can remove more than 80% of the reported defects while exactly matching the human fix in between 10 and 50% of cases, outperforming baselines based on GPT-3.5 and GPT-4, or based on window-based models like TFix.
研究动机与目标
- 为解决复杂语义错误(尤其是安全漏洞)的自动化修复挑战,此类错误因搜索空间过大且缺乏测试用例,传统自动化程序修复(APR)工具难以应对。
- 通过构建一个包含 5,000 多个来自真实 GitHub 提交的、人工标注的修复案例的全新综合性数据集,克服程序修复领域高质量真实世界训练数据稀缺的问题。
- 通过程序分析仅提取修复所需的最小上下文,减少 LLM 对无关代码的关注,从而提升基于 LLM 的修复效果。
- 证明聚焦于短小、经分析精简的代码片段,相比全程序输入,能显著提升 LLM 修复的准确率与泛化能力。
- 通过包含人工验证修复结果且保护训练/测试集划分的数据集,建立用于评估基于 LLM 的修复系统的基准。
提出的方法
- 提出 CodeReduce:一种基于程序分析的技术,仅在输入 LLM 前提取存在错误的代码及其最小必要上下文,从而减少输入长度并聚焦于相关代码。
- 使用静态分析识别生成正确修复所需的关键代码区域,包括报告的缺陷及跨过程的数据流依赖关系。
- 通过少样本提示(few-shot prompting)或微调 LLM(如 Mixtral-8x7B、StarCoderBase-7B)于精简后的代码片段,以提升修复生成质量。
- 实施 MergeBack 步骤,将生成的修复重新插入原始完整程序中,并通过重新分析或执行验证其正确性。
- 设计训练与评估流程,将宽松许可的数据(用于训练)与非宽松许可的数据(用于评估)分离,防止数据泄露。
- 利用 Snyk Code SAST 引擎识别真实安全警报,并通过具备安全专业知识的人工标注者验证修复是否正确解决了报告的问题,避免误报或漏报。
实验结果
研究问题
- RQ1当提供完整程序代码时,LLM 是否能有效修复复杂且非平凡的安全漏洞?还是长距离代码依赖关系会阻碍性能?
- RQ2将输入代码缩减为修复所必需的最小上下文,会对基于 LLM 的修复系统的准确率与可靠性产生何种影响?
- RQ3在 LLM 流程中多大程度上可集成程序分析,以减轻注意力机制的负担并提升学习效率?
- RQ4在真实世界、人工验证的修复案例上评估时,基于 LLM 的修复性能与传统 APR 工具及先前基于学习的基线(如 TFix)相比如何?
- RQ5能否大规模构建一个高质量、真实世界的漏洞修复数据集,并通过人工验证,以支持未来基于 LLM 的修复系统进行可靠基准测试?
主要发现
- CodeReduce 技术通过聚焦 LLM 于关键代码,显著提升了修复准确率,减少了对大型程序中远距离依赖关系的学习需求。
- 在包含 5,000 多个真实世界、人工验证修复案例的新数据集上,最佳模型(经 CodeReduce 微调的 Mixtral-8x7B)成功修复了超过 80% 的报告安全缺陷。
- 系统在 10%–50% 的案例中与人工编写的修复完全匹配,具体比例取决于漏洞模式,优于 GPT-3.5、GPT-4 和 TFix 基线。
- 完整修复预测的平均推理时间(包括代码缩减与模型生成)低于 3 秒,使其适用于 IDE 环境。
- 直接对完整程序应用 LLM 的表现劣于使用 CodeReduce,表明完整程序中的长距离依赖关系会阻碍有效学习与注意力机制的发挥。
- 数据集构建过程揭示,许多自动化修复提交存在错误或不完整,凸显在程序修复研究中进行人工验证与精心数据清洗的必要性。
阅读本论文
在 Nubint 阅读器中打开原文,边读边就任意段落提问。
在 Nubint 中阅读本论文本解读由 AI 根据论文原文生成,引用前请核对原文。