Code2Inv: A Deep Learning Framework for Program Verification

Xujie Si, Aaditya Naik, Hanjun Dai, Mayur Naik, Le Song

Introduction

A central bottleneck in automated program verification is the discovery of loop invariants—logical conditions guaranteed to hold before and after a loop's execution. Traditional approaches rely on Counterexample-Guided Inductive Synthesis (CEGIS), where a generator proposes an invariant and a checker validates it. However, these generators typically depend on fixed search heuristics or rigid templates, making them brittle when faced with complex mathematical constraints or new program domains.

Code2Inv introduces an end-to-end deep learning framework that automates this process by learning to generate loop invariants through interaction with a proof checker. By combining Graph Neural Networks (GNNs) and Reinforcement Learning (RL), it bypasses the need for human-labeled data and hardcoded heuristics.

Methods

Code2Inv treats invariant generation as a reinforcement learning problem. The framework is composed of several specialized neural components designed to handle the structural complexity of source code and formal logic.

Graph-Based Code Representation

Instead of treating code as plain text, Code2Inv converts programs into a graph structure. It utilizes Abstract Syntax Trees (ASTs) augmented with control-flow and data-flow edges via Static Single Assignment (SSA). A Graph Neural Network (GNN) then embeds this structure into numeric vectors, capturing the semantic relationships between variables and operations.

Figure 4: Code2Inv converts program source code (a) into a graph representation (b) using AST and SSA edges to facilitate GNN embedding.

Decoupled Grammar and Step-by-Step Generation

The system is guided by a grammar that defines the space of valid invariants. This grammar is embedded into the neural network, allowing the model to build invariants one step at a time. A Tree-LSTM tracks the state of the partial invariant, while an attention mechanism selects the next grammar rule or program variable to insert. This decoupling allows Code2Inv to adapt to different problem domains, such as Constrained Horn Clauses (CHCs), simply by swapping the grammar and checker.

Figure 3: The framework utilizes a decoupled grammar (c) to generate invariants for various logic formats, including Constrained Horn Clauses (a).

Continuous RL Reward

To overcome the "sparse reward" problem in formal logic (where a guess is usually just right or wrong), Code2Inv uses a continuous reward signal. The RL agent is rewarded based on the fraction of counterexamples satisfied by its current guess, providing a smooth gradient that guides the policy toward a valid proof.

Key Insights and Results

  • Flexibility and Generalization: Code2Inv can be applied to new domains without architectural changes. In tests, it solved 94 out of 120 CHC instances with minimal tuning.
  • Handling Non-Linear Arithmetic: Traditional SMT solvers often struggle with non-linear constraints. Code2Inv successfully solved 5 out of 7 non-linear CHC instances, whereas state-of-the-art solvers like Spacer and LinearArbitrary failed to solve any.
  • Transfer Learning: The model learns structural patterns that generalize. Pre-training on similar programs significantly accelerates the verification of new, perturbed variants, making it ideal for incremental code changes in CI/CD pipelines.
  • Performance: On standard C benchmarks, Code2Inv outperformed existing tools (C2I, LoopInvGen) by requiring significantly fewer SMT queries to reach a valid solution.

Conclusion

Code2Inv demonstrates that mapping discrete logical problems into a continuous learning space via RL and GNNs is a viable path for automated verification. While currently limited to single-loop programs and slower than specialized solvers in wall-clock time, its ability to handle non-linear logic and learn from experience marks a significant shift away from brittle, manually-engineered verification tools.

Summary updated

BibTeX
@inproceedings{si2020code,
  author = {Xujie Si and Aaditya Naik and Hanjun Dai and Mayur Naik and Le Song},
  title = {{Code2Inv: A Deep Learning Framework for Program Verification}},
  booktitle = {CAV},
  year = {2020},
  doi = {10.1007/978-3-030-53291-8\_9},
}
Figure