ClassInvGen: Class Invariant Synthesis using Large Language Models

Chuyue Sun, Viraj Agashe, Saikat Chakraborty, Jubi Taneja, Clark W. Barrett, David L. Dill, Xiaokang Qiu, Shuvendu K. Lahiri · DOI

Abstract

Class invariants are essential predicates that define the valid states of a data structure, yet they are notoriously difficult to author manually. Traditional automated tools often rely on rigid templates or struggle with complex, heap-heavy code. This paper introduces ClassInvGen, a framework that utilizes Large Language Models (LLMs) to automatically synthesize executable class invariants for C++ programs. By employing a "generate-filter-refine" loop that co-generates invariants alongside test suites, ClassInvGen produces high-quality formal specifications that outperform existing tools and identify bugs missed by standard unit testing.

1. Introduction

In software engineering, a class invariant is a property that must hold true for every instance of a class before and after the execution of any public method. These invariants serve as the "golden rules" for data structures—for example, ensuring that a binary search tree remains balanced or that a linked list does not contain cycles. While vital for debugging, documentation, and formal verification, writing these invariants is a tedious and error-prone task for developers.

Existing automated approaches generally fall into two categories: static analysis and dynamic inference. Static analysis often fails when confronted with complex pointers and recursive properties common in C++. Dynamic inference tools, such as Daikon, typically rely on predefined templates, which limits their expressiveness and often leads to "overfitting" the invariants to specific, limited test data. ClassInvGen addresses these gaps by treating invariant generation as a code synthesis task powered by LLMs.

2. The ClassInvGen Framework

ClassInvGen implements an automated pipeline designed to generate expressive, code-based invariants. The framework consists of four primary stages:

2.1 Preprocessing

The system utilizes Tree-Sitter to parse C++ source code into an Abstract Syntax Tree (AST). This allows the tool to identify class members, method signatures, and internal dependencies, providing the necessary context for the LLM.

2.2 Co-generation

Rather than prompting the LLM (specifically GPT-4o) for invariants in isolation, ClassInvGen requires the model to co-generate both candidate invariants and a comprehensive test suite of valid API calls. This dual output ensures that the generated invariants are immediately testable against realistic usage patterns.

2.3 Heuristic Pruning

The candidate invariants are instrumented into the original source code. The system then executes the co-generated test suite. If an invariant is violated during a sequence of valid API calls, it is identified as a "false positive" and discarded. This execution-based filtering ensures that only invariants consistent with the class's intended behavior are retained.

2.4 Feedback-Driven Refinement

For invariants that fail to compile or cause runtime crashes, ClassInvGen implements a repair loop. The system feeds error messages back to the LLM, allowing up to three attempts to refine and fix the code, significantly increasing the robustness of the output.

For complex, large-scale classes, the framework employs a compositional approach. By processing classes in a bottom-up fashion—starting with leaf classes and using their invariants as context for parent classes—ClassInvGen can handle codebases that exceed the standard context window of an LLM.

3. Experimental Evaluation

The researchers evaluated ClassInvGen using two distinct datasets:

  1. C++ Micro-benchmarks: Nine classic data structures, including AVL Trees, Red-Black Trees, and Hash Tables. These were tested against "mutants" (intentionally buggy versions) to measure the strength of the generated invariants.
  2. Real-World Case Study: Seven utility classes from the Z3 SMT solver, including the complex bdd_manager (over 1,600 lines of code).

4. Key Results

The evaluation demonstrated that ClassInvGen significantly advances the state-of-the-art in invariant synthesis:

  • Correctness: While raw LLM output achieved a 77% pass rate against unit tests, the ClassInvGen filtering process improved the correctness of the final invariants to 100%.
  • Expressiveness: Unlike template-based tools, ClassInvGen successfully generated "heap-aware" properties involving loops, lambdas, and recursive checks.
  • Bug Detection: In mutation testing, invariants generated by ClassInvGen killed 11.2% more mutants than standard unit tests alone, highlighting their ability to catch subtle state-corruption bugs.
  • Industrial Utility: A lead developer of the Z3 SMT solver validated the results, confirming that ClassInvGen produced 11 "correct and useful" invariants for the bdd_manager class, some of which matched properties the developers had previously written by hand.

5. Discussion and Limitations

Despite its success, ClassInvGen faces certain challenges. There is a risk of co-adaptation, where an LLM might generate a weak test suite that fails to trigger a violation in a similarly flawed invariant. Furthermore, the tool currently functions best as a "specification-drafting aid," as human developers are still needed to distinguish between "useful" invariants and those that are technically correct but trivial (e.g., checking a boolean state that is already obvious).

Future work will focus on integrating ClassInvGen into CI/CD pipelines, enabling automated invariant suggestions during the pull request process to assist maintainers in protecting and documenting new codebases.

Summary updated

BibTeX
@inproceedings{sun2025classinvgen,
  author = {Chuyue Sun and Viraj Agashe and Saikat Chakraborty and Jubi Taneja and Clark W. Barrett and David L. Dill and Xiaokang Qiu and Shuvendu K. Lahiri},
  title = {{ClassInvGen: Class Invariant Synthesis using Large Language Models}},
  booktitle = {SAIV Proceedings},
  year = {2025},
  url = {https://openreview.net/forum?id=7iwJ2ZQS3s},
}
Figure