Enchanting Program Specification Synthesis by Large Language Models using Static Analysis and Program Verification

Cheng Wen, Jialun Cao, Jie Su, Zhiwu Xu, Shengchao Qin, Mengda He, Haokun Li, Shing-Chi Cheung, Cong TianarXiv 2024

TL;DR

AutoSpec is a new framework that automates the most tedious part of formal software verification: writing the mathematical specifications (like loop invariants and pre/post-conditions) needed for a full proof. By combining the creative generation of Large Language Models (LLMs) with the mathematical rigor of static analysis and theorem provers, it successfully verifies 79% of complex C programs, outperforming existing specialized tools by nearly 60%.

What Problem Does This Paper Address?

Formal verification is the "gold standard" for ensuring software is bug-free, but it has a massive barrier to entry: the "specification bottleneck." To prove a program is correct, a human expert must manually write formal annotations (specifications) that describe exactly what every loop and function does in mathematical terms.

Existing automated tools for this task are often too narrow—they might only handle simple numerical loops and crumble when faced with real-world complexities like pointers, arrays, or nested function calls. While LLMs like ChatGPT are great at generating code, they have three major weaknesses when it comes to formal specs:

  1. Hallucinations: They often generate mathematically incorrect specifications.
  2. Context Fragmentation: They lose focus ("lost in the middle") when looking at large, complex programs.
  3. Error Accumulation: In a multi-step conversation, one wrong guess leads to a cascade of further errors.

Methods

AutoSpec addresses these challenges through a four-step pipeline that treats the LLM as a "candidate generator" rather than a final authority.

  1. Code Decomposition: Instead of feeding the whole program to the LLM, AutoSpec uses static analysis to break it into a hierarchical "extended call graph." In this graph, both functions and loops are treated as individual nodes.
  2. Hierarchical Specification Generation: The system works from the "bottom up." It starts with the simplest leaf nodes (like a basic swap function) and asks the LLM to generate specs. Once those are validated, it moves up to the functions that call them. This allows the LLM to focus on small, manageable chunks of logic.
  3. Specification Validation: Every candidate specification generated by the LLM is immediately checked by a theorem prover (Frama-C). If a spec is mathematically inconsistent with the code, it is discarded. This prevents the LLM's "hallucinations" from polluting the rest of the process.
  4. Iterative Enhancement: If the initial specs aren't enough to complete the full proof, AutoSpec goes back to the LLM for another round, providing the previously validated specs as context to help the model refine its understanding.
Fig. 3: Overview of AutoSpec

Datasets

The authors evaluated AutoSpec on a diverse set of 251 C programs across four major benchmarks:

  • Frama-C-problems: Programs featuring function calls, nested loops, arrays, and pointers.
  • SyGuS: A standard benchmark for loop invariant synthesis.
  • OOPSLA-13 & SV-COMP: Benchmarks containing complex nested and multiple loop structures.
  • Real-world Case Study: Six representative functions from an open-source X.509 certificate parser, a project that originally took human experts five months to verify manually.

Key Insights and Ideas

  • Static Analysis as a Guardrail: The core "aha moment" is that LLMs don't need to be perfect if you have a theorem prover to act as a filter. By using static analysis to structure the prompt and a prover to validate the output, you get the best of both worlds: LLM creativity and mathematical certainty.
  • The Power of Bottom-Up: By solving the "inner" parts of a program first, the system provides the LLM with a clear mathematical summary of dependencies when it tries to solve the "outer" parts.
  • Decomposition Solves "Lost in the Middle": Breaking the code into a call graph ensures the LLM always has a high "signal-to-noise" ratio in its prompt context.

Key Results

  • Superior Performance: AutoSpec successfully verified 79% of the benchmark programs. This is a 1.592x improvement over state-of-the-art specialized tools like Code2Inv and CLN2Inv.
  • Versatility: Unlike previous tools that only handled numerical loops, AutoSpec successfully generated specs for programs involving complex data structures like pointers and arrays.
  • Real-World Utility: In the X.509 parser case study, AutoSpec generated adequate specifications for all six functions in just a few minutes.
  • Efficiency: On average, AutoSpec processes a program in about 89 seconds, with the LLM querying phase taking less than 10 seconds.
Fig. 2: User Scenario of AutoSpec

Limitations and Future Work

  • External Dependencies: AutoSpec currently struggles when code depends on external libraries (like math.h) where the source code isn't provided in the prompt. The LLM can't reason about what it can't see.
  • LLM Sensitivity: The choice of LLM matters; while GPT-3.5 performed well, open-source models like Llama-2-70b were slightly less effective (solving 25/51 vs 31/51 on one benchmark).
  • Scaling to Massive Projects: While successful on individual functions, verifying a million-line codebase end-to-end without any human intervention remains a long-term challenge for the field.

Summary updated

BibTeX
@misc{wen2024enchanting,
  author = {Cheng Wen and Jialun Cao and Jie Su and Zhiwu Xu and Shengchao Qin and Mengda He and Haokun Li and Shing-Chi Cheung and Cong Tian},
  title = {{Enchanting Program Specification Synthesis by Large Language Models using Static Analysis and Program Verification}},
  year = {2024},
  eprint = {2404.00762},
  archivePrefix = {arXiv},
  url = {https://arxiv.org/abs/2404.00762},
}
Figure