Writing good code is hard, but writing the formal specifications that describe exactly what that code should do is often even harder. As a result, many programs lack formal specs, making them harder to verify, repair, or reuse.
In their OOPSLA 2023 paper, researchers from the University of Wisconsin-Madison present SPYRO, a framework that automates this process. SPYRO doesn't just guess what a program does; it synthesizes the "best" possible specification—one that is provably sound and as precise as the chosen language allows.
TL;DR
SPYRO is a customizable framework that automatically synthesizes provably sound and "most-precise" specifications for a given set of functions. By combining a user-provided Domain-Specific Language (DSL) with a novel synthesis algorithm, it generates readable, formal descriptions of code behavior across applications like specification mining, sensitivity analysis, and abstract interpretation.
What Problem Does This Paper Address?
There is a massive gap in how we document software. On one hand, we have manual specification writing, which is tedious and error-prone. On the other, we have "specification mining" tools that watch a program run and guess its behavior. However, these mining tools are usually unsound—they might be right about the test cases they saw, but wrong about the program in general.
The challenge is creating a tool that is:
- Sound: The specs it generates must be true for all possible inputs.
- Precise: It shouldn't just say "this function returns an integer"; it should give the most detailed description possible within a certain vocabulary.
- Customizable: Different users want different kinds of specs (e.g., one person wants to know about performance, another about data integrity).
Methods
The authors treat specification synthesis as an optimization problem: find the strongest formula $\psi$ in a language $\mathcal{L}$ that is implied by the program's actual behavior $\Phi$.
The core of SPYRO is a Counterexample-Guided Inductive Synthesis (CEGIS) loop. It relies on three main primitives:
Synthesize: Finds a candidate property that fits the current known examples.CheckSoundness: Uses an SMT solver (or a bounded model checker) to find "positive" counterexamples—cases where the program does something the candidate spec doesn't allow.CheckPrecision: This is the "secret sauce." It looks for "negative" counterexamples—cases that the current spec allows but the program never actually does. If it finds one, it proves the current spec isn't the "best" yet and forces the synthesizer to find a tighter one.
A key innovation is monotonic constraint hardening. To prevent the algorithm from oscillating between different properties, it accumulates "must-reject" negative examples. Once a sound property is found, any "maybe-reject" examples are promoted to "must-reject," narrowing the search space toward the most precise solution.
Datasets
The authors evaluated SPYRO across four distinct domains using a mix of existing and new benchmarks:
- Specification Mining: 45 problems, including 7 from the SyGuS (Syntax-Guided Synthesis) competition and 24 from the Synquid (type-directed synthesis) benchmark suite.
- Algebraic Specifications: Synthesizing rules for
ArrayList,HashSet, andHashMapmodules to enable modular program synthesis. - Sensitivity Analysis: 18 benchmarks focused on differential privacy, measuring how much a change in input (like adding/deleting a list element) affects the output.
- Abstract Domains: 9 benchmarks for bit-vector polyhedra, a complex domain used in formal verification.
Key Insights and Ideas
- The Power of the DSL: The "best" specification is relative to the language you use to describe it. By changing the DSL, a user can trade off between a spec that is incredibly precise but hard to read, and one that is simple and intuitive.
- Hyperproperties: SPYRO can discover relationships between multiple calls to the same function. For example, it can automatically discover that a
reversefunction is its own inverse (reverse(reverse(L)) == L) or that a function is injective. - Debugging via Synthesis: During the evaluation of
HashMap, SPYRO synthesized an empty specification (essentially saying "nothing is true"). This led the researchers to discover a bug in the underlying implementation's hash collision handling. - Efficiency through Decomposition: Instead of trying to synthesize one giant, complex formula, SPYRO synthesizes a conjunction of smaller, "best" individual properties. This makes the underlying synthesis tasks much easier for tools like Z3 or Sketch to handle.
Key Results
- Mining: SPYRO successfully synthesized properties for 41 out of 45 benchmarks. For the SyGuS benchmarks, it found properties that exactly characterized the functions.
- Performance: SPYRO was 2 to 10 orders of magnitude faster than a baseline enumerative search (which simply tries every possible formula in the DSL).
- Sensitivity Analysis: It achieved a 3.14x to 11.93x speedup over baselines while finding complex sensitivity bounds that would be nearly impossible for a human to derive manually.
- Precision: In the bit-vector polyhedra domain, SPYRO found abstractions that were 2-4 orders of magnitude smaller (fewer inequalities) than those produced by brute-force methods, without losing any precision.
Limitations and Future Work
- Bounded Semantics: The version of SPYRO built on the
Sketchtool uses "bounded" reasoning (e.g., checking lists up to length 7). While the properties found are usually general, they aren't guaranteed to be sound for infinite domains without further verification (like using the Dafny verifier). - Arithmetic Complexity: Like all SAT-based tools, SPYRO struggles with complex non-linear arithmetic (like multiplication) as the bit-width of integers increases.
- Language Expressivity: Currently, SPYRO works best with conjunctive properties. Extending it to handle more complex logical structures (like deep nesting of "OR" statements) while maintaining efficiency is an open challenge.
The authors suggest that future versions could integrate with newer synthesis engines like SemGuS or be applied to fields like code deobfuscation, where understanding the "best" spec of a garbled program is the primary goal.
Summary updated