arXiv:2507.13290cs.PLcs.AI2025-07被引 16

让大模型生成的代码通过形式化验证,提升关键系统安全性。

Towards Formal Verification of LLM-Generated Code from Natural Language Prompts

  • 用自然语言风格的形式查询语言表达用户意图
  • 在21个任务中正确验证83%的代码,92%识别错误代码
  • 基于知识库减少对系统知识的依赖,适合运维场景

近年来,大模型可基于自然语言描述生成代码,但其可靠性与现有验证技术仍不足以支撑关键或安全敏感应用。本文提出为大模型生成代码提供形式化正确性保障,以提升通用AI编程助手的质量并支持其在关键场景中的应用。为此,我们设计了一种形式化查询语言,能以类似自然语言的方式表达用户意图,供用户确认是否匹配真实需求,并由此构建形式化规范来验证代码是否符合意图。系统Astrogator针对Ansible编程语言实现该方法,涵盖直观的形式化查询语言、描述Ansible程序行为的演算、符号解释器及合一算法。其关键创新在于引入知识库,捕获系统特异性依赖,大幅降低形式查询所需系统知识。在21个代码生成任务的基准测试中,验证器成功验证83%的正确代码,并识别出92%的错误代码。

原文摘要 · Abstract (English)

In the past few years LLMs have emerged as a tool that can aid programmers by taking natural language descriptions and generating code based on it. However, the reliability of LLM code generation and current validation techniques for it are far from strong enough to be used for mission-critical or safety-critical applications. In this work we explore ways to offer formal guarantees of correctness to LLM generated code; such guarantees could improve the quality of general AI Code Assistants and support their use for critical applications. To address this challenge we propose to incorporate a Formal Query Language that can represent a user's intent in a formally defined but natural language-like manner that a user can confirm matches their intent. We then have a formal specification of the user intent which we can use to verify that LLM-generated code matches the user's intent. We implement these ideas in our system, Astrogator, for the Ansible programming language, widely used for system administration, including for critical systems. The system includes an intuitive formal query language, a calculus for representing the behavior of Ansible programs, and a symbolic interpreter and a unification algorithm which together are used for the verification. A key innovation in Astrogator is the use of a Knowledge Base to capture system-specific implementation dependencies that greatly reduce the need for system knowledge in expressing formal queries. On a benchmark suite of 21 code-generation tasks, our verifier is able to verify correct code in 83% of cases and identify incorrect code in 92%.

代码生成形式验证大模型Ansible

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