LLMDFA: Analyzing Dataflow in Code with Large Language Models

📄 arXiv: 2402.10754v2 📥 PDF

作者: Chengpeng Wang, Wuqi Zhang, Zian Su, Xiangzhe Xu, Xiaoheng Xie, Xiangyu Zhang

分类: cs.PL, cs.LG, cs.SE

发布日期: 2024-02-16 (更新: 2024-11-23)

备注: 26 pages, 15 figures, 6 tables, NeurIPS 2024 poster

🔗 代码/项目: GITHUB


💡 一句话要点

提出LLMDFA以解决编译无关的数据流分析问题

🎯 匹配领域: 支柱九:具身大模型 (Embodied Foundation Models)

关键词: 数据流分析 大型语言模型 编译无关 代码分析 自动化检测 软件开发 安全检测

📋 核心要点

  1. 现有的数据流分析方法通常依赖于成功编译和专家定制,限制了其在动态和复杂环境中的应用。
  2. LLMDFA通过利用大型语言模型,提供了一种编译无关且可定制的数据流分析框架,能够处理复杂的推理任务。
  3. 在实验中,LLMDFA在合成程序和真实Android应用中表现出色,平均精度达到87.10%,召回率为80.77%,显著优于现有技术。

📝 摘要(中文)

数据流分析是识别程序值之间依赖关系的基本代码分析技术。传统方法通常需要成功编译和专家定制,限制了其在现实场景中分析不可编译程序的适用性和可用性。本文提出了LLMDFA,一个基于大型语言模型(LLM)的编译无关且可定制的数据流分析框架。为了解决结果的幻觉问题,我们将问题分解为多个子任务,并引入一系列新策略。具体而言,我们利用LLM合成代码,将复杂推理外包给外部专家工具。此外,我们采用少量示例的思维链提示来总结各个函数中的数据流事实,从而减轻幻觉现象。我们在合成程序上评估LLMDFA以检测三种典型的bug,并在真实的Android应用程序上进行定制化bug检测。LLMDFA的平均精度为87.10%,召回率为80.77%,F1分数提升幅度达到0.35,超越了现有技术。

🔬 方法详解

问题定义:本文旨在解决传统数据流分析方法在编译无关和动态环境中的局限性,尤其是在处理不可编译程序时的适用性问题。现有方法往往需要成功编译和专家知识,导致其在实际应用中的可用性降低。

核心思路:LLMDFA的核心思路是利用大型语言模型(LLM)进行数据流分析,通过将复杂推理任务外包给外部工具,来提高分析的准确性和灵活性。通过分解问题为多个子任务,LLMDFA能够有效地处理多样化的分析需求。

技术框架:LLMDFA的整体架构包括多个模块:首先,使用LLM合成代码并调用解析库提取感兴趣的程序值;其次,利用自动定理证明器验证路径的可行性;最后,通过少量示例的思维链提示总结数据流事实,确保与程序语义的一致性。

关键创新:LLMDFA的主要创新在于将LLM与外部专家工具结合,形成了一种新的数据流分析方法。这种方法不仅提高了分析的灵活性,还有效减少了幻觉现象的发生。与传统方法相比,LLMDFA在处理不可编译程序时展现出更高的适应性和准确性。

关键设计:在设计中,LLMDFA采用了少量示例的思维链提示技术,以确保LLM生成的分析结果与程序语义相符。此外,系统的参数设置和损失函数设计也经过精心调整,以优化模型性能和结果的可靠性。

🖼️ 关键图片

fig_0
fig_1
fig_2

📊 实验亮点

LLMDFA在合成程序上检测三种典型bug,平均精度达到87.10%,召回率为80.77%。在真实Android应用中,LLMDFA的F1分数提升幅度达到0.35,显著优于现有技术,展示了其在实际应用中的有效性和优势。

🎯 应用场景

LLMDFA具有广泛的应用潜力,尤其是在需要动态分析和定制化检测的场景中,如软件开发、代码审计和安全检测等领域。其编译无关的特性使得它能够适应不断变化的代码环境,提升了程序分析的灵活性和实用性。未来,LLMDFA还可以扩展到更多编程语言和框架中,进一步推动自动化代码分析的发展。

📄 摘要(原文)

Dataflow analysis is a fundamental code analysis technique that identifies dependencies between program values. Traditional approaches typically necessitate successful compilation and expert customization, hindering their applicability and usability for analyzing uncompilable programs with evolving analysis needs in real-world scenarios. This paper presents LLMDFA, an LLM-powered compilation-free and customizable dataflow analysis framework. To address hallucinations for reliable results, we decompose the problem into several subtasks and introduce a series of novel strategies. Specifically, we leverage LLMs to synthesize code that outsources delicate reasoning to external expert tools, such as using a parsing library to extract program values of interest and invoking an automated theorem prover to validate path feasibility. Additionally, we adopt a few-shot chain-of-thought prompting to summarize dataflow facts in individual functions, aligning the LLMs with the program semantics of small code snippets to mitigate hallucinations. We evaluate LLMDFA on synthetic programs to detect three representative types of bugs and on real-world Android applications for customized bug detection. On average, LLMDFA achieves 87.10% precision and 80.77% recall, surpassing existing techniques with F1 score improvements of up to 0.35. We have open-sourced LLMDFA at https://github.com/chengpeng-wang/LLMDFA.