[Paper Review] SpecGen: Automated Generation of Formal Program Specifications via Large Language Models
SpecGen uses a two-phase approach (conversation-driven generation and mutation-based refinement) with LLMs to generate verifiable Java/JML specifications, outperforming Houdini, Daikon, and purely LLM baselines on 120 programs.
Formal program specifications play a crucial role in various stages of software development. However, manually crafting formal program specifications is rather difficult, making the job time-consuming and labor-intensive. 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. 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 to generate appropriate specifications for a given program. 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. We evaluate SpecGen on two datasets, including the SV-COMP Java category benchmark and a manually constructed dataset. Experimental results demonstrate that SpecGen succeeds in generating verifiable specifications for 279 out of 385 programs, outperforming the existing purely 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.
Motivation & Objective
- Leverage LLM code comprehension to generate high-quality formal specifications.
- Overcome template/grammar limitations of prior tools (Houdini, Daikon) for complex programs.
- Introduce mutation-based generation to repair LLM gaps via verifiable refinements.
- Propose a heuristic selection strategy to efficiently verify mutated specifications.
- Provide a dataset of 120 Java programs with expert-written specifications for evaluation.
Proposed method
- Conversation-driven specification generation where an LLM is guided by an initial prompt and verification feedback to produce JML specs.
- Verifier feedback is used to refine subsequent LLM outputs in a multi-turn conversation.
- Mutation-based specification generation that mutates failed LLM outputs using four operator families (predicative, logical, comparative, arithmetic).
- Heuristic selection selects mutated variants with weights to efficiently pass verification.
- An evaluation setup with 120 Java programs and OpenJML verifier comparing against Houdini, Daikon, and purely LLM baselines.
Experimental results
Research questions
- RQ1How does SpecGen perform compared with baseline specification generation approaches?
- RQ2What is the contribution of each mutation type to SpecGen’s effectiveness?
- RQ3How do candidate selection strategies affect SpecGen’s efficiency?
- RQ4To what extent do generated specifications capture the semantic behavior of input programs?
Key findings
- SpecGen verifiably generates specifications for 100 of 120 programs, outperforming Houdini (42) and Purely LLM-based methods (72 conversational, up to 70 with 4-shot settings).
- Conversation-driven generation plus mutation-based refinement yields substantial gains over purely LLM or template-based approaches.
- A 20.22% efficiency improvement is achieved by the heuristic selection strategy over random selection in mutation verification.
- An ablation study confirms the effectiveness of all four mutation operators.
- The dataset includes 120 Java programs with expert-authored oracle specifications, enabling robust evaluation.
Read this paper
Open the full text in the Nubint reader and ask about any passage as you read.
Read this paper on NubintThis review was generated by AI from the original paper. Please check the paper before citing it.