Towards General Loop Invariant Generation: A Benchmark of Programs with Memory Manipulation

Chang Liu, Xiwei Wu, Yuan Feng, Qinxiang Cao, Junchi Yan

TL;DR

This paper introduces LIG-MM, the first comprehensive benchmark for generating loop invariants in programs that perform complex memory manipulations (like linked lists and trees). To tackle this challenge, the authors propose LLM-SE, a hybrid framework that combines the pattern-recognition power of Large Language Models (LLMs) with the precision of symbolic execution, significantly outperforming state-of-the-art tools and even GPT-4.

What Problem Does This Paper Address?

Program verification is the "holy grail" of software engineering—mathematically proving that code behaves exactly as intended. A cornerstone of this process is the loop invariant: a condition that remains true before and after every iteration of a loop. If you can find the right invariant, you can prove the loop's correctness.

While researchers have become quite adept at generating invariants for numerical programs (e.g., "x is always less than 10"), they struggle with memory-involved programs. Real-world software relies heavily on pointers, heaps, and complex data structures like doubly linked lists or hash tables. Traditional tools are often too rigid to handle these, and existing AI models lack the formal grounding to understand the "shape" of memory. This paper addresses the lack of both a specialized benchmark for these programs and a framework capable of solving them.

Methods: The LLM-SE Framework

The authors propose LLM-SE, a framework that coordinates a Large Language Model with Symbolic Execution (SE) to bridge the gap between pattern recognition and formal logic.

Overview of the LLM-SE framework, detailing the self-supervised training process and the interactive querying loop.
  1. Offline Training (Self-Supervised): Since labeled data for loop invariants is incredibly scarce, the authors created a "predicate recovery" task. They took known data structure definitions written in Separation Logic (a formal language for reasoning about memory) and "unfolded" them into complex, messy assertions. They then fine-tuned a CodeGen-350M model to "reverse" this process—taking the messy assertions and recovering the original, clean logic predicates.
  2. Online Querying: When faced with a new program, the system uses symbolic execution to run the loop body several times "symbolically," generating a sequence of logic assertions.
  3. The Hybrid Loop: The LLM looks at these assertions to spot patterns and suggest invariant candidates. These candidates are then fed into an entailment solver (a formal verification tool) to check if they are mathematically sound. If they fail, the system iterates and tries again.

Datasets: Introducing LIG-MM

To evaluate their approach, the authors introduced LIG-MM (Loop Invariant Generation - Memory Manipulation), a benchmark consisting of 312 programs. These were curated from diverse, high-stakes sources:

  • Academic: College data structure homework.
  • Competitions: SV-COMP (the premier software verification competition).
  • Real-World Systems: Critical code from the Linux Kernel, GlibC, LiteOS, and Zephyr.

These programs cover singly/doubly linked lists, trees, and hash tables, often involving multi-level pointers and complex pointer arithmetic.

Key Insights and Ideas

  • LLMs as Pattern Matchers for Logic: The "aha moment" of this research is realizing that while LLMs struggle with raw mathematical proofs, they are excellent at recognizing the syntactic patterns that emerge in logic assertions during symbolic execution.
  • Separation Logic is Key: By using Separation Logic instead of standard first-order logic, the framework can reason about "disjoint" memory segments. This prevents the "aliasing" problem (where two pointers point to the same place) from exploding the complexity of the proof.
  • Self-Supervision Solves Data Scarcity: By programmatically generating "messy" logic from "clean" predicates, the authors created a virtually infinite training set for a niche task where human-labeled data is almost non-existent.

Key Results

The LLM-SE framework demonstrated a massive leap over existing methods on the LIG-MM benchmark.

Comparison of Pass Rate @ 1 and @ 8 between LLM-SE and baseline methods on the LIG-MM benchmark.
  • Pass Rate @ 1 (First attempt): LLM-SE achieved a 38.78% pass rate, tripling the performance of the traditional state-of-the-art tool SLING (12.82%) and GPT-4 (12.50%). AutoSpec, an LLM focused on numerical invariants, completely failed (0.00%).
  • Pass Rate @ 8 (Eight attempts): Given more attempts, LLM-SE improved to 42.95%, while GPT-4 only reached 17.68%.

Notably, LLM-SE was particularly dominant in complex structures like "Listbox" (a specialized list structure), where it achieved a 41.67% pass rate compared to GPT-4's 6.67%.

Limitations and Future Work

Despite the success, a ~43% maximum pass rate means the problem is far from solved. The authors identify several hurdles:

  • Scaling to Massive Systems: While it works on individual functions and loops, scaling this to multi-million line codebases with deeply nested loops remains a challenge.
  • Predicate Dependency: The system currently relies on a set of predefined predicates (like listrep or lseg). If a program uses a highly unique, user-defined data structure not in the training set, the model's performance may degrade.
  • Computational Overhead: Combining LLM inference with symbolic execution and formal solvers is computationally expensive compared to traditional static analysis.

Future work will likely focus on refining the self-supervised learning paradigm and integrating more sophisticated symbolic execution techniques to handle even more "exotic" memory manipulations.

Summary updated

BibTeX
@inproceedings{liu2024towards,
  author = {Chang Liu and Xiwei Wu and Yuan Feng and Qinxiang Cao and Junchi Yan},
  title = {{Towards General Loop Invariant Generation: A Benchmark of Programs with Memory Manipulation}},
  booktitle = {Advances in Neural Information Processing Systems},
  year = {2024},
  url = {https://openreview.net/forum?id=MQrkP1JygE},
}
Figure