Finding Inductive Loop Invariants using Large Language Models

Adharsh Kamath, Aditya Senthilnathan, Saikat Chakraborty, Pantazis Deligiannis, Shuvendu K. Lahiri, Akash Lal, Aseem Rastogi, Subhajit Roy, Rahul SharmaarXiv 2023 · DOI

Introduction

Formal software verification is a critical process for ensuring program correctness, yet it remains hindered by the "undecidable" problem of finding inductive loop invariants. These logical propositions must hold true throughout a loop's execution to prove safety properties. Traditional symbolic verification tools rely on mathematical heuristics that often struggle with complex logic. This paper introduces Loopy, a novel toolchain that leverages the pattern-recognition capabilities of Large Language Models (LLMs) to generate candidate invariants, which are then rigorously filtered using symbolic solvers.

The Loopy Framework: Ingredients and Recipes

The core philosophy of Loopy is to treat LLMs as "ingredient generators" rather than end-to-end solvers. The framework consists of four primary stages:

  1. Candidate Generation: An LLM (such as GPT-4) is prompted to generate multiple potential invariant clauses for a given C program.
  2. Houdini Integration: Rather than relying on a single "perfect" guess, Loopy collects the union of all generated clauses. It then employs the Houdini algorithm, which uses the Frama-C symbolic verifier to iteratively prune clauses that are not inductive. This approach allows the system to synthesize a correct "recipe" from a pool of potentially noisy "ingredients."
  3. Iterative Repair: If the initial set of invariants fails to prove the program, Loopy enters a repair loop. It feeds the verifier's error messages back to the LLM, providing context on why previous attempts failed and asking for targeted corrections.
  4. Verification Oracle: Frama-C, backed by SMT solvers like Z3 and CVC4, serves as the final arbiter of mathematical correctness.
The impact of the Houdini algorithm on verification success across different LLMs.

Experimental Setup

The researchers evaluated Loopy on a curated dataset of 1,005 C programs sourced from Code2Inv, SV-COMP, and SyGuS. To isolate the difficulty of invariant generation, the study focused on a subset of 555 benchmarks containing exactly one loop and one method, excluding complex memory operations like pointers or heap allocation.

Key Results and Insights

The integration of symbolic pruning via Houdini proved to be a game-changer. As shown in the results, the success rate for models like GPT-4 increased significantly when using Houdini compared to raw generation.

Scaling of verified benchmarks as the number of completions (samples) increases for different model configurations.

Performance Highlights:

  • State-of-the-Art Comparison: While the specialized symbolic tool Ultimate Automizer solved 430 benchmarks, Loopy (using GPT-4) successfully verified 31 programs that Ultimate failed to solve. This demonstrates that LLMs can navigate logical structures that stymie traditional heuristics.
  • Scaling with Samples: Performance improves as the number of completions increases, eventually saturating. GPT-4 consistently outperformed GPT-3.5 and Code Llama, particularly in its ability to utilize feedback during the repair phase.
  • The Power of Prompting: Simple "nudges"—generic instructions to focus on variable bounds or relationships—boosted performance by 23%, highlighting the importance of how these models are queried.

Limitations and Future Directions

Despite its success, Loopy faces challenges with "multi-phase" loops where behavior changes mid-execution, and with invariants requiring complex disjunctions (OR logic). Additionally, reasoning about floating-point arithmetic and high-dimensional invariants remains difficult for current LLMs. Future work aims to extend this approach to programs with complex data structures like arrays and pointers, which would require the LLM to model heap memory semantics.

Conclusion

Loopy demonstrates that LLMs and symbolic tools are highly complementary. By combining the creative, pattern-based guessing of LLMs with the rigorous, deductive pruning of symbolic solvers, researchers have opened a new frontier in automated software verification, solving problems that were previously out of reach for purely mathematical methods.

Summary updated

BibTeX
@misc{kamath2023finding,
  author = {Adharsh Kamath and Aditya Senthilnathan and Saikat Chakraborty and Pantazis Deligiannis and Shuvendu K. Lahiri and Akash Lal and Aseem Rastogi and Subhajit Roy and Rahul Sharma},
  title = {{Finding Inductive Loop Invariants using Large Language Models}},
  year = {2023},
  eprint = {2311.07948},
  archivePrefix = {arXiv},
  url = {https://arxiv.org/abs/2311.07948},
}
Figure