Skip to main content
QUICK REVIEW

[论文解读] FLAG: Finding Line Anomalies (in code) with Generative AI

Baleegh Ahmad, Benjamin Tan|arXiv (Cornell University)|Jun 22, 2023
Software Engineering ResearchComputer Science被引用 3
一句话总结

FLAG 是一种新颖的、与语言无关的框架,通过将源代码的每一行与大型语言模型(LLM)生成的替代行进行比较,利用 LLM 检测代码异常。通过识别差异——尤其是相似度低、LLM 置信度低或注释对齐差的代码——FLAG 将手动审查的搜索空间缩小至原始代码的 12–17%,同时在 C、Python 和 Verilog 中检测出 121 个已知错误中的 101 个。

ABSTRACT

Code contains security and functional bugs. The process of identifying and localizing them is difficult and relies on human labor. In this work, we present a novel approach (FLAG) to assist human debuggers. FLAG is based on the lexical capabilities of generative AI, specifically, Large Language Models (LLMs). Here, we input a code file then extract and regenerate each line within that file for self-comparison. By comparing the original code with an LLM-generated alternative, we can flag notable differences as anomalies for further inspection, with features such as distance from comments and LLM confidence also aiding this classification. This reduces the inspection search space for the designer. Unlike other automated approaches in this area, FLAG is language-agnostic, can work on incomplete (and even non-compiling) code and requires no creation of security properties, functional tests or definition of rules. In this work, we explore the features that help LLMs in this classification and evaluate the performance of FLAG on known bugs. We use 121 benchmarks across C, Python and Verilog; with each benchmark containing a known security or functional weakness. We conduct the experiments using two state of the art LLMs in OpenAI's code-davinci-002 and gpt-3.5-turbo, but our approach may be used by other models. FLAG can identify 101 of the defects and helps reduce the search space to 12-17% of source code.

研究动机与目标

  • 解决在程序不完整或无法编译时,于开发过程中识别安全和功能错误的挑战。
  • 通过缩小潜在缺陷的搜索空间,减少代码审查所需的手动工作量。
  • 开发一种与语言无关、无需规则的漏洞检测方法,依赖 LLM 和编码及注释中编码的开发人员意图。
  • 探索 LLM 生成的代码对比作为异常检测的新机制的有效性,而无需正式规格说明或测试用例。

提出的方法

  • 对于源文件中的每一行,将该行及其上下文(包括注释)输入到预训练的 LLM 中,以生成替代行。
  • 使用多种相似度度量方法(包括 Levenshtein 距离和 LLM 日志概率(置信度))比较原始行与 LLM 生成的行。
  • 如果某行与 LLM 的预期延续存在显著偏差,特别是当 LLM 置信度较低或该行与注释距离较远时,将其分类为异常。
  • 应用两种分类标准:C1(基于距离和置信度)和 C2(进一步过滤掉低置信度的建议),以在检测率与误报率之间取得平衡。
  • 使用两种最先进的 LLM(code-davinci-002 和 gpt-3.5-turbo)在 C、Python 和 Verilog 的 121 个基准上评估该方法。
  • 利用注释接近度、Levenshtein 距离和 LLM 置信度等特征改进异常分类,重点在于在不牺牲真正阳性检测率的前提下最小化误报。

实验结果

研究问题

  • RQ1LLM 生成的代码延续是否能有效标记出偏离开发人员意图的代码行,从而指示潜在错误?
  • RQ2原始代码与生成代码之间的 Levenshtein 距离在识别异常行方面的有效性如何?
  • RQ3注释接近度和 LLM 置信度在多大程度上提升了异常检测的准确性?
  • RQ4FLAG 在不同编程语言(C、Python、Verilog)和 LLM(code-davinci-002、gpt-3.5-turbo)上的表现如何?
  • RQ5该框架是否能在保持高已知安全和功能错误检测率的同时,减少手动代码审查的搜索空间?

主要发现

  • FLAG 使用两种最先进的 LLM,在 C、Python 和 Verilog 基准中成功检测出 121 个已知安全和功能错误中的 101 个。
  • 平均而言,FLAG 将手动代码审查的搜索空间缩小至原始源代码的 12–17%,显著提升了开发效率。
  • gpt-3.5-turbo 的检测能力优于 code-davinci-002,但误报率也更高(FPR 分别为 0.172 和 0.121)。
  • 原始代码与生成代码之间的 Levenshtein 距离是分类异常的最重要特征,其次是注释接近度和 LLM 置信度。
  • 该框架在 C 代码上表现最佳,在 Python 上表现最差,且功能错误的检测比安全相关错误更可靠。
  • 应用 C2 过滤标准通过剔除低置信度的 LLM 建议,将误报率降低,但平均也导致检测率下降约 3.75 个缺陷。

更好的研究,从现在开始

从阅读论文到最终审阅,大幅缩短您的研究时间。

无需绑定信用卡

本解读由 AI 生成,并经人工编辑审核。