arXiv:2610.38130cs.SEOct 2026

Manuscript Offprint

arXiv:2610.38130

Template-Free Dynamic Invariant Detection

Erfan Arvan, Michael D. Ernst, Martin Kellogg

ERFAN ARVAN, NJIT, USA

MICHAEL D. ERNST, UW CSE, USA

MARTIN KELLOGG, NJIT, USA

A dynamic invariant detector proposes invariants, such as method pre- and postconditions, and rules out those that do not hold at run time. Traditional detectors propose invariants from a fixed library of templates, which limits their expressiveness. A large language model (LLM) can propose invariants instead of, or in addition to, template-derived ones. With the right prompt and context choices, we found that this idea can both improve the invariants' expressiveness and reduce the cost of inferring the invariants.

Greater expressiveness brings complications: an LLM-proposed invariant can execute arbitrary code during invariant evaluation, causing side effects or infinite loops. Our novel instrumentation strategies mitigate these problems. We wrap each candidate and evaluate only one invariant per thread at a time, so nested invariant evaluations cannot trigger one another indefinitely. To recover from non-nested hangs, we monitor each run for stalls and retry it under a growing time budget. Because we cannot prove a candidate side-effect-free in advance, we use delta debugging over the invariants evaluated during a failing test to isolate and disable a side-effecting candidate without discarding the rest of the run.

We compared our implementation, Oca, to the popular template-based Daikon invariant detector on real open-source Java projects. Oca produces more expressive invariants: on average, 28% of its held invariants call a project-specific method, which Daikon's templates cannot express. Its invariants are more often valid, with a false-positive rate of 18% in a manual inspection, vs. 76% for Daikon, and more useful: as properties for property-based testing, they exposed 9 of 25 real bugs, compared with 4 for Daikon. Oca also costs less, using 566 minutes and 27 GB of disk, compared with 2,817 minutes and 827 GB for Daikon.

1 Introduction

Invariants capture essential semantic properties of programs, such as preconditions, postconditions, and design intent. They support program understanding and serve as a foundation for testing, verification, and debugging [15]. A dynamic invariant detection tool infers invariants from executions. Traditionally, the tool proposes invariants from a fixed template library. This can express only properties that match one of its predefined syntactic forms: it cannot represent invariants that depend on project-specific logic or domain abstractions, and it restricts relations to a small, fixed number of variables. These limits are not implementation details of any one tool; they are structural consequences of deriving candidates from a fixed grammar, whatever that grammar is. Daikon [15] (fig. 2, right) is the most widely used tool of this design, in both research and practice [1, 26, 29, 48].

Recent advances in large language models (LLMs) provide an opportunity to move beyond fixed invariant templates. When supplied with source code as context, an LLM can propose candidate invariants that reflect program-specific constraints.

This paper introduces Oca¹, a dynamic invariant detector realizing this idea. Like Daikon, Oca validates candidates against program executions; unlike Daikon, it obtains them from an LLM rather than a template grammar. For each method entry and exit, Oca asks an LLM for candidate invariants, filters them, injects the survivors into the program source as instrumentation probes, and runs the existing test suite (fig. 2, left). A candidate that executes at least once and is never falsified is reported as held. Daikon by default checks invariants offline, against an execution trace written by its Java frontend, Chicory. That is infeasible for arbitrary expressions, which may depend

¹Oca is a South American root vegetable, chosen as a nod to Daikon, which was named after another root vegetable.

Authors' Contact Information: Erfan Arvan, ea442@njit.edu, NJIT, Newark, NJ, USA; Michael D. Ernst, UW CSE, Seattle, WA, USA, mernst@cs.washington.edu; Martin Kellogg, NJIT, Newark, NJ, USA, martin.kellogg@njit.edu.

on any part of the program state; checking candidates in place is also simpler and faster. Further speedups come from evaluating only a comparatively small set of LLM-proposed candidates.

Oca also dispenses with the complex machinery Daikon uses to avoid computing and reporting uninformative invariants. For example, an invariant over unrelated quantities, such as myBoolean <= myPointer, should not be reported, so Daikon computes abstract types [6, 17] to determine which variables may meaningfully be compared. Daikon also groups variables whose values are always equal, so that each template is instantiated once per group rather than once per variable; streamlines the common case of quickly falsified invariants; and applies statistical tests to suppress invariants that hold only by coincidence. An advantage of checking a small, targeted set of potential invariants is that Oca needs none of these complexities.

Using an LLM to propose invariants raises two challenges. The first (C1) is obtaining useful, well-formed candidates from the LLM: without careful prompt design and context, models produce ill-formed, vacuous, or irrelevant candidates, and even under a good prompt, some candidates are syntactically invalid or fail to compile. The second (C2) is ensuring that invariants evaluated at run time do not corrupt the inference process itself: they may have side effects that perturb program state (C2.1), pass otherwise-unseen values to the program's own routines – causing a called method's own injected invariants to be evaluated against inputs that arise only from invariant evaluation (C2.2) – or fail to terminate (C2.3).

We address these challenges as follows. For C1, we empirically evaluated multiple prompt strategies and context configurations, deriving the configurations from a literature review of the context that LLM-based software engineering techniques supply to the model (section 3). Oca also filters ill-formed expressions before instrumentation, using both syntactic checks and compilation (section 4.1). For side effects (C2.1), Oca relies on the project's test suite, using delta debugging to isolate and disable the specific candidate invariant responsible for an observed test failure (section 4.2.2). For C2.2, Oca suppresses nested invariant evaluation when another check is already active on the same thread (section 4.2.3). For nontermination (C2.3), the same re-entrancy check rules out the nontermination that mutually triggering probes would cause, and a background monitor detects and disables suspected nonterminating candidates at the per-invariant level, without discarding results for other invariants (section 4.2.4).

We evaluated Oca against Daikon on real open-source Java projects along four complementary dimensions, and found that Oca produces more expressive, more valid, and more useful invariants at lower cost. First, we compared the expressiveness of the inferred invariants (section 5.2): on average, 28% of Oca's held invariants call a project-specific method and 5% relate more than three variables; Daikon's templates can express neither. Second, we manually checked both tools' output for false positives, invariants that hold for the given executions but not in general (section 5.3): 18% of sampled Oca invariants were false positives, compared with 76% for Daikon. Third, we compared the invariants as properties for property-based testing (section 5.4): Oca exposed 9 of 25 real bugs, compared with 4 for Daikon. Fourth, we compared the tools' cost (section 5.5): Oca used 566 minutes and 27 GB of disk in total, compared with 2,817 minutes and 827 GB for Daikon. We deliberately adopt conservative evaluation choices throughout to avoid overstating the benefits of LLM-generated invariants. In summary, this paper makes the following contributions:

  • A characterization of the research challenges in using large language models for dynamic invariant detection.
  • A language-agnostic design for template-free dynamic invariant detection: LLM-proposed candidates are checked in place by probes, with test-driven isolation of side effects, suppression of nested evaluation, and recovery from hangs.
@Override
public @Nullable Object convert(@Nullable Object source, TypeDescriptor sourceType, TypeDescriptor targetType) {
    if (source == null) {
        return null;
    }
    if (sourceType.isAssignableTo(targetType)) {
        return source;
    }
    Collection<?> sourceCollection = (Collection<?>) source;
    if (sourceCollection.isEmpty()) {
        return null;
    }
    Object firstElement = sourceCollection.iterator().next();
    return this.conversionService.convert(firstElement, sourceType.elementTypeDescriptor(firstElement), targetType);
}

Fig. 1: CollectionToObjectConverter.convert from Spring's [42] type-conversion infrastructure.

  • A literature review of the program context that LLM-based software engineering techniques supply to the model: a taxonomy derived by coding 181 papers from the latest editions of FSE, ICSE, ASE, and ISSTA, with the prevalence of each context type.
  • An empirical study of prompt and context strategies for LLM-based invariant generation.
  • Oca, an implementation of our template-free invariant detector design for Java that supports both cloud-hosted and locally hosted LLMs.
  • An empirical evaluation comparing Oca and Daikon for expressiveness, validity, downstream usefulness, and inference cost.

References (17)