Program Structure Aware Precondition Generation

Elizabeth Dinella, Shuvendu K. Lahiri, Mayur NaikarXiv 2023 · DOI

Abstract

Automated precondition inference is critical for software reliability, yet existing tools often produce complex, symbolic logic that is difficult for developers to interpret. This paper presents a novel approach that generates "natural" preconditions by transforming the target program's own structure. By leveraging the method's existing logic as a seed and iteratively refining it through dynamic analysis and program reduction, the proposed system creates preconditions that are significantly more readable for humans and more effective for Large Language Model (LLM) reasoning. Evaluation on real-world Java projects demonstrates that these natural preconditions improve human verification accuracy to 88.84% while maintaining high correctness and safety standards.

Introduction

In software engineering, a precondition is a boolean requirement that must be satisfied for a method to execute safely without crashing or causing undefined behavior. While automated tools can infer these conditions, they typically synthesize them by combining various boolean predicates (e.g., x > 0 && y != null). While mathematically sound, these results are often "unnatural"—they include irrelevant variables and follow logic that differs significantly from how a human would write code. This lack of readability makes them difficult to maintain or verify in interactive development environments. This research argues that for a precondition to be truly useful, it must follow the "natural" logic of the program it protects.

Methods

The authors propose a dynamic, transformation-based pipeline that uses the target method itself as a "seed" for the precondition. This approach ensures the resulting logic remains grounded in the program's original structure.

The iterative pipeline for generating natural preconditions, involving seed generation, crash-based instrumentation, and program reduction.

The pipeline consists of three main phases:

  1. Seed Generation: The target method is transformed into a boolean function. Error-throwing statements are replaced with return false, and successful execution paths are capped with return true.
  2. Iterative Exception Check Insertion: Using the EvoSuite test generator, the system identifies inputs that cause crashes. A "Check Instrumentor" analyzes stack traces to insert specific guards (e.g., if (val == null) return false;) at the exact point of failure. This loop continues until no further crashing inputs are detected.
  3. Program Reduction: To remove logic irrelevant to the final boolean outcome, the authors apply the Perses syntax-guided program reducer. This strips away bloated segments, leaving a lean, readable "natural" precondition.

Datasets and Evaluation

The approach was evaluated using two primary resources:

  • NetBigInteger Benchmark: A set of 39 methods used for head-to-head comparison against state-of-the-art tools like DAIKON and PROVISO.
  • Large-Scale Dataset: A new dataset of approximately 18,000 (method, precondition) pairs derived from 87 real-world Java projects. This represents the first large-scale dataset of its kind for training and evaluating precondition inference.

Key Results and Insights

The core insight of this work is that a program's structure already contains the necessary logic to define its boundaries. Transforming existing code proves more effective than synthesizing logic from scratch.

Human Readability and Efficiency

A user study comparing the proposed "natural" preconditions against those from prior work (PROVISO) revealed a significant improvement in human performance. Participants were not only more accurate but also faster when reasoning about the structure-aware preconditions.

Comparison of human accuracy and time taken when reasoning over preconditions generated by the proposed method versus PROVISO.

LLM Synergy and Correctness

The study found that LLMs like GPT-4 are significantly better at reasoning about code when provided with these natural preconditions as few-shot examples. GPT-4's accuracy in inferring preconditions rose to 71.78%, compared to just 58.97% when using examples from traditional symbolic tools. Furthermore, the approach generated safe and maximal preconditions for 32 out of 39 methods in the core benchmark, performing comparably to symbolic tools while offering superior readability.

Limitations and Future Work

Despite its success, the approach faces challenges regarding tool brittleness; the system could only process 14% of the 126,000 methods initially targeted due to limitations in automated test generation for complex object dependencies. Additionally, while the reduction phase minimizes side effects, the risk remains. Future work will focus on improving interprocedural logic handling to make guards across method calls more readable and less cognitively demanding for developers.

Summary updated

BibTeX
@misc{dinella2023program,
  author = {Elizabeth Dinella and Shuvendu K. Lahiri and Mayur Naik},
  title = {{Program Structure Aware Precondition Generation}},
  year = {2023},
  eprint = {2310.02154},
  archivePrefix = {arXiv},
  url = {https://arxiv.org/abs/2310.02154},
}
Figure