Proving Conditional Termination

Byron Cook, Sumit Gulwani, Tal Lev-Ami, Andrey Rybalchenko, Mooly SagivCAV 2008

Abstract

Automated termination analysis typically focuses on proving that a program terminates for all possible inputs. However, many real-world programs are only guaranteed to terminate under specific preconditions. This paper introduces an automated method for synthesizing these "conditional termination" preconditions. By integrating termination provers with abstract interpretation and quantifier elimination, the authors develop a framework to find useful, human-readable conditions for code that might otherwise loop infinitely. The approach is validated on both difficult benchmarks and industrial code fragments, demonstrating its ability to find practical preconditions where previous methods failed.

Introduction: The Conditional Termination Problem

Traditional termination analysis is often a binary affair: a tool attempts to prove that a program always terminates. Yet, software in practice frequently relies on specific environmental or input constraints to ensure completion. For instance, a loop might only terminate if a counter is positive or if a step-size variable is non-zero.

The "conditional termination problem" asks: Under what preconditions is this code safe to call? The theoretical "weakest precondition"—the most general set of starting states—is often impossible to compute or express in simple logic like linear arithmetic. Previous methods often produced conditions that were either too restrictive to be useful or too complex for automated tools to handle. This research seeks a "Goldilocks" zone: preconditions that are weak enough to be useful but simple enough to be automatically synthesized and verified.

Example of a loop requiring conditional termination analysis.

Consider the code snippet above. Without specific constraints on $y$ and $z$, the loop while(x > 0) might never terminate. Synthesizing a precondition like $y < 0 \land z \le 0$ would guarantee termination, whereas the default @requires true is insufficient for a general proof.

Methodology

The authors propose two primary procedures: PreSynth and PreSynthPhase, which operate through an iterative workflow.

  1. Potential Ranking Functions: Rather than searching for a perfect ranking function (a value that decreases every iteration until it hits a bound) from scratch, the method identifies potential ranking functions. These are expressions already present in the code that are bounded from below but may not yet decrease in every iteration.
  2. Strengthening via Quantifier Elimination: The tool employs quantifier elimination (specifically Cooper’s procedure) to determine the exact conditions under which these potential functions are guaranteed to decrease.
  3. Weakest Liberal Precondition (WLP) Propagation: Once a local condition for termination is identified, the tool uses abstract interpretation (via the INTERPROC analyzer) to propagate that condition backward to the program's entry point.
  4. Handling "Phases": Some programs terminate in stages—for example, one variable must decrease before another can begin decreasing. PreSynthPhase handles this by recursively identifying these phases, "peeling" away conditions that guarantee eventual termination, and analyzing the remaining state space.

This process is collaborative: a termination prover (such as ARMC) identifies a counterexample (a potential infinite loop), and the synthesis tool generates a precondition to "block" that specific counterexample.

Evaluation and Results

The method was evaluated on two distinct datasets:

  • Handwritten Benchmarks: These included classic difficult cases for termination analysis, such as the Euclidean algorithm for GCD and loops with complex phase transitions.
  • Industrial Examples: Code fragments were drawn from Windows device drivers and audio compression modules. These represent "in-the-wild" code where termination depends on complex arithmetic modeled from bitwise operations or overflow checks.

Key Results

  • Effectiveness: The tool successfully synthesized preconditions for programs that previously required manual annotation. In one audio compression module, it identified a three-part disjunctive precondition in just 22 seconds.
  • Performance: Most benchmarks, including industrial fragments, were processed in under 30 seconds. Even the most complex audio application example was resolved in 106 seconds.
  • Logic Support: The method successfully produced linear arithmetic preconditions for programs whose actual weakest preconditions were non-linear, validating the strategy of using useful under-approximations.

Discussion

A core insight of this work is that under-approximation is a feature. Since the true weakest precondition is often non-linear and uncomputable, providing a safe, simple condition (e.g., $y < 0$) is more valuable to a programmer than a perfectly precise but incomprehensible non-linear formula. By starting with "potential" functions, the tool significantly narrows the search space.

Limitations and Future Work

Currently, the implementation is limited to sequential programs and arithmetic operations. It does not handle complex heap-based data structures (like linked lists) or concurrency. Furthermore, the quality of the output is tied to the underlying abstract domain (e.g., Octagons); if a precondition cannot be expressed in that domain, the tool may fail to find a safe condition. Future work aims to extend this to interprocedural analysis and use synthesized preconditions to assist in disproving safety properties.

Summary updated

BibTeX
@inproceedings{cook2008proving,
  author = {Byron Cook and Sumit Gulwani and Tal Lev-Ami and Andrey Rybalchenko and Mooly Sagiv},
  title = {{Proving Conditional Termination}},
  booktitle = {CAV},
  year = {2008},
  doi = {10.1007/978-3-540-70545-1\_32},
}
Figure