vers3: Explicit-State Model Checking Library
vers3 is an explicit-state model checking library for Rust. Protocols are
described as a state type and a set of guarded rules, and vers3 explores
every reachable state. When an invariant fails, a model assertion fires, or the
system deadlocks, it reports the shortest trace that reaches the error, with
the state changes at each step.
Usage
The model below is a lock in which two processes test and set a flag in
separate steps. vers3 finds the interleaving that puts both processes in the
critical section:
use fmt;
use ExitCode;
use ;
// States must be Clone + Hash + Eq + Display.
// Reports diff states line by line, so print one field per line.
It exits with status 1 and prints:
═══ COUNTEREXAMPLE ══════════════════════════════════
Error: property violation: mutual exclusion
Search stats: visited=8 queued=1
Trace length: 4 steps
── Step 0 (initial)
+ locked = false
+ p0 = Idle
+ p1 = Idle
── Step 1 via rule: p0 sees lock free
locked = false
- p0 = Idle
+ p0 = Checked
p1 = Idle
── Step 2 via rule: p1 sees lock free
locked = false
p0 = Checked
- p1 = Idle
+ p1 = Checked
── Step 3 via rule: p0 takes lock
- locked = false
+ locked = true
- p0 = Checked
+ p0 = Critical
p1 = Checked
── Step 4 via rule: p1 takes lock
locked = true
p0 = Critical
- p1 = Checked
+ p1 = Critical
═════════════════════════════════════════════════════
Merging the two steps into one rule fixes the lock, and the run then prints
VERIFIED with the number of states visited.
Features
- Two searches.
Bfsstores full states, so two states merge only when they are equal.BfsHashingstores 64-bit state hashes and needs far less memory. - Symmetry reduction.
ArraySet<T, N>hashes and compares its elements without regard to slot order. States that differ only in how identical actors are numbered are therefore equal, and the search explores them once. - Model errors. A rule built with
rule_trycan report a situation the protocol should never reach througherror_if. The search stops and prints a trace, where apanic!would abort without one. - Unknowns. A rule can return an
Unknownfor a case the model does not cover yet: the state is reachable, but what happens next is not modelled. The state does not count as a deadlock, so a model can grow one case at a time, and the search stats count the unknowns it hit. - Liveness.
LivelockFreedomreports cycles among transient states, in which an actor never returns to a stable state. - Refinement.
TransitionSysSimulationchecks that every step of one system has a matching step in a reference system. - Reports. The
reportmodule prints each step of a counterexample as a line diff of the state'sDisplayoutput.main_reportturns a search result into an exit code.
Choosing a search
Start with Bfs. It holds every visited state in memory, never confuses two
distinct states, and records the parent of each state, so traces are always
complete.
Switch to BfsHashing when the visited states no longer fit in memory. It
keeps about 16 bytes per state. Two distinct states with the same hash count as
one, so the search can miss states; with 64-bit hashes this is unlikely below
about 10⁹ states. On an error it rebuilds the trace by replaying the model from
the initial states. If the replay cannot reproduce the recorded path, the report
says so. BfsHashing::with_verbose(false) skips the replay and reports only
the hash trace.
Example: MSI cache coherence
examples/msi_directory.rs models the MSI
directory protocol from Sorin, Hill and Wood, A Primer on Memory Consistency
and Cache Coherence (2011), with three processors. It uses symmetric channels,
model errors and livelock checking, and verifies 15,500 states:
Related projects
- Murphi is the explicit-state model
checker whose guarded-rule style
vers3follows. - VerC3 is the C++ library that
vers3is ported from. - stateright checks actor-based
implementations of distributed systems and can run them on a real network,
with an interactive explorer and linearizability testing.
vers3is smaller and lower-level: it checks abstract protocol models and leaves the tooling around it to you.
License
Licensed under the Apache License, Version 2.0.