SpecGen: Automated Generation of Formal Program Specifications via Large Language Models
Lezhi Ma, Shangqing Liu, Yi Li, Xiaofei Xie, Lei Bu · 2025 · 242 citationsRead the paper
In the software development process, formal program specifications play a crucial role in various stages, including requirement analysis, software testing, and verification. However, manually crafting formal program specifications is rather difficult, making the job time-consuming and labor-intensive. Moreover, it is even more challenging to write specifications that correctly and comprehensively describe the semantics of complex programs. To reduce the burden on software developers, automated specification generation methods have emerged. However, existing methods usually rely on predefined templates or grammar, making them struggle to accurately describe the behavior and functionality of complex real-world programs. To tackle this challenge, we introduce SpecGen, a novel technique for formal program specification generation based on Large Language Models (LLMs). Our key insight is to overcome the limitations of existing methods by leveraging the code comprehension capability of LLMs. The process of SpecGen consists of two phases. The first phase employs a conversational approach that guides the LLM in generating appropriate specifications for a given program, aiming to utilize the ability of LLM to generate high-quality specifications. The second phase, designed for where the LLM fails to generate correct specifications, applies four mutation operators to the model-generated specifications and selects verifiable specifications from the mutated ones through a novel heuristic selection strategy by assigning different weights of variants in an efficient manner. We evaluate SpecGen on two datasets, including the SV-COMP Java category benchmark and a manually constructed dataset containing 120 programs. Experimental results demonstrate that SpecGen succeeds in generating verifiable specifications for 279 out of 385 programs, outperforming the existing LLM-based approaches and conventional specification generation tools like Houdini and Daikon. Further investigations on the quality of generated specifications indicate that SpecGen can comprehensively articulate the behaviors of the input program.
What the full text shows
Working prototype- SpecGen generated verifiable specifications for a higher number of programs than all baselines. — 279 out of 385 programs (59.97% success probability) (Evaluated on SV-COMP Java category and SpecGenBench)
- SpecGen showed a clear advantage in generating specifications for complex program structures compared to other LLM-based approaches. — 92.31% success on Branched and 60.38% on Multi-path Loop programs (SpecGenBench dataset)
- Comparative mutation was identified as the most critical mutation operator for success. — Success dropped to 223 programs when comparative mutation was removed (Ablation study on 385 programs)
- The heuristic selection strategy improved verification efficiency over random selection. — 21.23% improvement in verifier calls (SpecGenBench dataset)
- Setup
- Evaluated on 385 programs total, consisting of the SV-COMP Java category (265 programs) and a manually constructed SpecGenBench (120 programs).
- How
- SpecGen uses a two-phase approach: first, a conversational LLM process that uses verifier failure information as feedback; second, a mutation-based phase applying four operators (predicative, logical, comparative, arithmetic) with a heuristic selection strategy to refine failed specifications.
- Limitations
- Relatively poor performance in generating verifiable specifications for programs with nested loops. · Inherent randomness of LLM-generated content.
Summarized by Gemma 4 from the open-access full text · 24 Nov 2026. Read it