Control Flow Graphs (CFGs) are essential for visualizing, understanding and analyzing program behavior. For statically-typed programming language like Java, developers obtain CFGs by using bytecode-based methods for compilable code and Abstract Syntax Tree (AST)-based methods for partially uncompilable code. However, explicit syntax errors during AST construction and implicit semantic errors caused by bad coding practices can lead to behavioral loss and deviation of CFGs.To address the issue, we propose a novel approach that leverages the error-tolerant and understanding ability of pre-trained Large Language Models (LLMs) to generate CFGs. Our approach involves a Chain of Thought (CoT) with four steps: structure hierarchy extraction, nested code block extraction, CFG generation of nested code blocks, and fusion of all nested code blocks' CFGs. To address the limitations of the original CoT's single-prompt approach (i.e., completing all steps in a single generative pass), which can result in an ``epic'' prompt with hard-to-control behavior and error accumulation, we break down the CoT into an AI chain with explicit sub-steps. Each sub-step corresponds to a separate AI-unit, with an effective prompt assigned to each unit for interacting with LLMs to accomplish a specific purpose.Our experiments confirmed that our method outperforms existing CFG tools in terms of node and edge coverage, especially for incomplete or erroneous code. We also conducted an ablation experiment and confirmed the effectiveness of AI chain design principles: Hierarchical Task Breakdown, Unit Composition, and Mix of AI Units and Non-AI Units.Our work opens up new possibilities for building foundational software engineering tools based on LLMs, as opposed to traditional program analysis methods.
翻译:控制流图(CFG)是可视化、理解和分析程序行为的关键工具。对于Java等静态类型编程语言,开发者通过基于字节码的方法为可编译代码生成CFG,并借助基于抽象语法树(AST)的方法处理部分不可编译代码。然而,AST构建过程中的显式语法错误以及不良编码实践导致的隐式语义错误,会引发CFG的行为丢失与偏差。针对该问题,我们提出一种利用预训练大语言模型的容错与理解能力生成CFG的新方法。该方法采用包含四个步骤的思维链:结构层次提取、嵌套代码块提取、嵌套代码块CFG生成,以及所有嵌套代码块CFG的融合。为解决原始思维链单提示方法(即通过单次生成完成所有步骤)导致行为难以控制且错误累积的"史诗级"提示局限,我们将思维链拆解为包含显式子步骤的AI链。每个子步骤对应独立的AI单元,通过为每个单元分配高效提示与大语言模型交互以完成特定目标。实验证实,本方法在节点与边覆盖率上优于现有CFG工具,尤其处理不完整或错误代码时表现突出。消融实验进一步验证了AI链设计原则的有效性:分层任务分解、单元组合,以及AI单元与非AI单元的混合。本研究为基于大语言模型(而非传统程序分析方法)构建基础软件工程工具开辟了新的可能性。