自动为大型C++代码库生成可验证的精确规范,解决传统方法无法兼顾规模与准确性的难题。
Agentic Separation Logic Specification Synthesis

- 采用代理式架构,结合静态分析与运行时堆追踪,动态选择最适配的逻辑语言
- 在百万行级代码中对85%函数生成有效规范,零误报且通过模糊测试和专家验证
- 相比顶级模型效率提升10倍,适合系统级代码重构与形式化验证场景
规格合成旨在从程序实现和自然语言中自动推导出形式化规格,对重构、跨语言转换、优化和验证至关重要,但对大型C++仓库仍是开放挑战。现有基于大模型的方法难以同时满足规模化、表达能力(如动态内存与堆分配数据结构)和系统性验证需求。本文提出Spec-Agent,一种面向大规模C++代码库的代理式规格合成系统。该系统针对命题逻辑、一阶逻辑、命题分离逻辑和一阶分离逻辑构建多层级规格语言体系。对于每个函数,Spec-Agent通过静态分析与运行时堆追踪选择目标语言,将现有功能测试泛化为模糊测试例程,并利用反例引导反馈迭代优化大模型生成的候选规格。我们在开源C++代码库(含数百万行代码)上评估Spec-Agent,结果表明其对85%的目标函数成功生成有效规格,模糊测试和专家验证均未发现误报,性能优于Claude Code Opus 4.6,且仅需其十分之一的令牌开销。
原文摘要 · Abstract (English)
Specification synthesis, the task of automatically inferring formal specifications from program implementations and natural language, is important for refactoring, transpilation, optimization, and verification, yet remains an open challenge for large C++ repositories. Existing LLM-based approaches fail to simultaneously scale to such repositories, produce specifications expressive enough to capture systems-code features such as dynamic memory and heap-allocated data structures, and systematically validate those specifications to rule out incorrect candidates. We present Spec-Agent, an agentic system for synthesizing expressive, well-validated specifications across large C++ codebases. Spec-Agent targets a ladder of specification languages: propositional logic, first-order logic, propositional separation logic, and first-order separation logic. For each function, Spec-Agent uses static analysis and runtime heap tracing to select the appropriate target specification language, generalizes existing functional tests into fuzz harnesses, and iteratively refines LLM-generated candidates via counterexample-guided feedback. We evaluate Spec-Agent on open source C++ codebases comprising millions of lines of code. Spec-Agent synthesizes valid specifications for 85% of target functions, with no false positives observed under fuzzing and expert validation, outperforming Claude Code Opus 4.6 at 10x lower token cost.
Thank you to arXiv for use of its open access interoperability. PaperDance 不是 arXiv 官方产品;中文卡片由大模型生成,请以原文为准。