arXiv:2604.11556cs.SEcs.AI2026-04被引 8

用大模型自动生成函数规格,自动验证大型代码系统的正确性。

FM-Agent: Scaling Formal Methods to Large Systems via LLM-Based Hoare-Style Reasoning

论文配图:FM-Agent: Scaling Formal Methods to Large Systems via LLM-Based Hoare-Style Reasoning
图 1 · 摘自论文原文
  • 基于调用方意图自动生成函数规格,无需人工编写
  • 2天内完成143k行代码的推理,发现522个新漏洞
  • 支持自然语言规格,适合开发者与大模型协同开发

大模型辅助软件开发日益普遍,可生成编译器等大规模系统,但生成代码的正确性保障仍具挑战。传统霍尔逻辑虽支持分而治之的组合式推理,但需为每个函数手动编写形式化规格,人力成本高。当代码由大模型生成时,开发者对函数行为理解有限,更难编写规格。本文提出FM-Agent,首个实现大规模系统自动化组合式推理的框架。该框架利用大模型,采用自顶向下的范式,从函数调用者的期望行为推导函数规格,使生成的规格能反映开发者意图,即使实现存在缺陷。由于开发者意图多以自然语言表达,而现有验证器仅支持公式,因此FM-Agent将霍尔风格推理推广至自然语言规格。最终,为确认漏洞并解释原因,系统自动构造测试用例触发潜在错误。在评估中,FM-Agent在两天内成功推理了每篇高达143k行代码的大规模系统,这些系统已通过开发者测试,但依然发现522个新漏洞,可能引发系统崩溃或执行结果错误。

原文摘要 · Abstract (English)

LLM-assisted software development has become increasingly prevalent, and can generate large-scale systems, such as compilers. It becomes crucial to strengthen the correctness of the generated code. However, automated reasoning for large-scale systems remains challenging due to code complexity. Hoare logic offers an approach to decomposing a large system into smaller components and reasoning about them separately (i.e., compositional reasoning). However, existing works still struggle to scale, because Hoare logic requires writing formal specifications for each function, imposing a heavy human burden. The problem is exacerbated when code is generated by LLMs, as developers lack a deep understanding of each function's expected behavior. This paper presents FM-Agent, the first framework that realizes automated compositional reasoning for large-scale systems. Leveraging LLMs, FM-Agent introduces a top-down paradigm to automatically generate function-level specifications. Specifically, FM-Agent derives the specification of a function from how its callers expect the function to behave, so the generated specifications can reflect the developer's intent of a function even if the implementation is buggy. Developers' intent is usually expressed in natural language, while existing verifiers only support formulas. Therefore, FM-Agent generalizes Hoare-style inference to reason about functions against natural-language specifications. Finally, to confirm bug existence and explain bug causes, FM-Agent automatically generates test cases to trigger potential bugs. In our evaluation, FM-Agent successfully reasons about large-scale systems within 2 days, each of which has up to 143k LoC. These systems have already been tested by their developers, but FM-Agent still finds 522 newly discovered bugs. These bugs can cause serious consequences, including system crashes and incorrect execution results.

形式化方法大模型自动验证程序推理

Thank you to arXiv for use of its open access interoperability. PaperDance 不是 arXiv 官方产品;中文卡片由大模型生成,请以原文为准。