07 Model Checking
07 Model Checking
Model Checking
Stavros Tripakis
University of California, Berkeley
Gψ or AGψ
Caveat: Deadlocks
We have implicitly assumed that our system is deadlock-free.
Deadlock: a state with no successors:
Reachability Algorithms
Symbolic
I Bounded model-checking using SAT/SMT solvers.
I Symbolic reachability.
DFS(s):
1: check s; /* is s a deadlock? is given p ∈ L(s)? ... */
2: V := V ∪ {s};
3: for all s0 such that (s, s0 ) ∈ R do
4: if s0 6∈ V then
5: DFS(s0 ); /* recursive call */
6: end if
7: end for
Stavros Tripakis (UC Berkeley) EE 244, Fall 2016 Model Checking 11 / 68
State explosion
How many states does a chip with 100 flip-flops have?
I 2100 (potentially reachable) states.
I That is 1267650600228229401496703205376 states.
I Even if each state costs 1 bit to store, this still makes
2100−60−3 = 237 = 137, 438, 953, 472 exabytes ...
1
I Even if only 32 states are reachable, this still makes
2100−5 = 295 states.
How many states does a piece of concurrent software have?
Assume n asynchronous processes (e.g., threads), with k states
each.
I k n (potentially reachable) states.
What if the processes also communicate with queues? Consider
a single queue of size ` (i.e., can hold ` messages), and m
possible types of messages.
I m` (potentially reachable) states for just one queue.
Stavros Tripakis (UC Berkeley) EE 244, Fall 2016 Model Checking 16 / 68
The real complexity of reachability
... until we realize that the size of the graph is exponential in the
number of state variables, processes, etc.
Key idea:
Instead of reasoning about individual states, reason about
sets of states.
Examples:
S2 : x > 0 ∧ (b → i ≥ 0)
Key idea:
Use a predicate on two copies of the state variables:
unprimed (current state) + primed (next state).
R(x, i, b, x0 , i0 , b0 )
R1 : (p → ¬p0 ) ∧ (¬p → p0 )
R2 : n0 = n + 1 ∨ n0 = n
Symbolic representation:
where
P = {x1 , x2 , ..., xn }: set of (boolean) state variables, also taken
to be the atomic propositions.1
Predicate Init(~x) on vector ~x = (x1 , ..., xn ) represents the set
S0 of initial states.
Predicate Trans(~x, ~x0 ) represents the transition relation R.
Basis of the language of NuSMV.
1
this is done for simplicity, the two could be separated
Stavros Tripakis (UC Berkeley) EE 244, Fall 2016 Model Checking 25 / 68
MODULE inverter(input)
VAR
output : boolean;
INIT
output = FALSE
TRANS
next(output) = !input | next(output) = output
Main idea:
Start with set of initial states S0 .
Compute S1 := S0 ∪ {all 1-step successors of S0 }.
Compute S2 := S1 ∪ {all 1-step successors of S1 }.
...
Until Sk+1 = Sk .
Sk contains all reachable states.
Example:
φ = 0≤x≤5
Trans = x ≤ x0 ≤ x + 1
succ(φ) = (∃x : 0 ≤ x ≤ 5 ∧ x ≤ x0 ≤ x + 1)[x0 ; x]
= (∃x : 0 ≤ x ≤ 5 ∧ 0 ≤ x0 ≤ 5 + 1)[x0 ; x]
= (0 ≤ x0 ≤ 6)[x0 ; x]
= 0≤x≤6
Stavros Tripakis (UC Berkeley) EE 244, Fall 2016 Model Checking 33 / 68
Predicate Transformer
∃x : φ ⇔ φ[x ; 0] ∨ φ[x ; 1]
CTL: AG(p ∨ q)
LTL: G(p ∨ q)
Example: BDD
x4 x3 (x2 + x2 x1 ) + x3 (x2 x1 + x2 ) + x4 x2 x1
(z1 ∧ z3 ) ∨ (z2 ∧ z3 )
Computation
(z1 z∧2 zTree Logic
a + b, and 3 ) ∨ (z2 ∧ z3 ) z2
394 Computation Tree Logic
Stavros Tripakis (UC Berkeley) EE 244, Fall 2016 Model Checking 43 / 68
z3 z3 z3 z3
1 0 1 0 1 0 0 0
From binary decision trees to BDDs
z1
z1
z2 z2 z1
z2 z2
z2 z2
z3 1: z3 z3 z3
z3
z3 z3 z3 z3
1 0 1 0 1 0 0 0
1 0
1 0 1 0 1 0 0 0
z1 z1
z1 z2
z2 z2
2: 3:
z2 z2
z3 z3
z3
1 0 1 0
1 0
z1
Figure taken from [Baier and Katoen, 2008].
Figure 6.22: Binaryz1decision diagrams for f = (z1 ∧ z3 ) ∨ (z2 ∧ z3 ).
z2
Stavros Tripakis (UC Berkeley) EE 244, Fall 2016 Model Checking 44 / 68
z2
BDDs: a canonical representation of boolean
functions
ROBDDs are a canonical representation of boolean functions.
This means that two boolean functions (or expressions) f1 and f2 are
equivalent iff their corresponding ROBDDs (for the same variable
ordering) are identical.
tmp ⇔ Reachable
x1 , y1 , x2 , y2 , x3 , y3
and
x1 , x2 , x3 , y1 , y2 , y3
Some BDDs have exponential size no matter which ordering we
pick.
Deciding whether a given order is optimal is NP-hard.
Land of heuristics ...
Stavros Tripakis (UC Berkeley) EE 244, Fall 2016 Model Checking 46 / 68
Operations on BDDs
Operations on BDDs
Complementation / negation.
I Replace the leaf 0 with 1, and 1 with 0.
Then
f ⇔ x · fx + x · fx
| {z }
this is called the Shannon expansion of f
Stavros Tripakis (UC Berkeley) EE 244, Fall 2016 Model Checking 49 / 68
Shannon expansion
f ⇔ x · fx + x · fx
f ·g ⇔ x · fx · gx + x · fx · gx
Computes as output:
I A BDD B representing f g:
R1 x1 S1 x1
R2 x2
x3 R3 x3 S2
+
R4 x4 S3 x4
R5 R6 S4 S5
0 1 0 1
of BTripakis
Stavros f and(UC
BgBerkeley)
downwards
to construct nodes
EE 244, Fall 2016 of the OBDD Bf op g . Let
Model rf be
Checking 55 / 68
the root node of Bf and rg the root node of Bg .
1. If both rf and rg are terminal nodes with labels lf and lg , respectively (recall
Existential quantifier elimination
that terminal labels are either 0 or 1), then we compute the value lf op lg and
let the resulting OBDD be B0 if that value is 0 and B1 otherwise.
2. In the remaining cases, at least one of the root nodes is a non-terminal. Suppose
Recallthatthat if x is a boolean variable then:
both root nodes are xi -nodes. Then we create an xi -node n with a dashed
line to apply (op, lo(rf ), lo(rg )) and a solid line to apply (op, hi(rf ), hi(rg )), i.e.
∃xapply
we call ⇔ f [x
: f recursively on ; 0] ∨ off [x
the basis ; 1] ⇔ fx ∨ fx
(6.2).
3. If rf is an xi -node, but rg is a terminal node or an xj -node with j > i,
Let Bthen we know that there is no x -node in Bg because
f be the BDD for f . Howi to compute
the twofor
the BDD OBDDs
∃x : have
f?
a compatible ordering of boolean variables. Thus, g is independent of xi
We know (g ≡ g[0/x
howi ]to≡ g[1/x i ]). Therefore,
compute we create
disjunction of an xi -node
BDDs n with aItdashed
already. sufficesline to
to apply (op, lo(rf ), rg ) and a solid line to apply (op, hi(rf ), rg ).
be 4.able
Thetocase
compute
in which rsubstitutions like f [x ; 0].
g is a non-terminal, but rf is a terminal or an xj -node with
j > i, is handled symmetrically to case 3.
This is simple:
The result of this procedure might not be reduced; therefore apply finishes
For every x-node v in Bf , eliminate v and redirect all incoming
by calling the function reduce on the OBDD it constructed. An example of
links to the 0-child of v.
apply (where op is +) can be seen in Figures 6.15–6.17. Figure 6.16 shows
the(Ifrecursive
we wanted f [x
descent ; 1]structure
control instead,of we would
apply redirect
and Figure them
6.17 showstothe
the
final result.of
1-child this example, the result of apply (+, Bf , Bg ) is Bf .
In v.)
Figure 6.16 shows that numerous calls to apply occur several times with
We
the same must then reduce
arguments. Efficiencythecould
resulting BDD.
be gained if these were evaluated only
FINITE-HORIZON REACHABILITY
(a.k.a. BOUNDED MODEL-CHECKING)
Key idea:
Bounded reachability
Suppose I have predicates Init(~x), Trans(~x, ~x0 ), and Bad (~x).
How to use them for bounded reachability?
...
Bad state reachable in n steps iff
SAT Init(~x0 )∧Trans(~x0 , ~x1 )∧· · ·∧Trans(~xn−1 , ~xn )∧Bad (~xn )
Claim: d ≤ r. Why?
⇒ using r instead of d is safe. Why?
Can we compute r? How?
Use a SAT solver!
r := max{i | SAT Init(~x0 ) ∧ Trans(~x0 , ~x1 ) ∧ · · · ∧ Trans(~xi−1 , ~xi )
i−1
^ i
^
∧ ~xj 6= ~xk }
j=0 k=j+1
Bibliography I
Baier, C. and Katoen, J.-P. (2008).
Principles of Model Checking.
MIT Press.
Biere, A., Cimatti, A., Clarke, E. M., Strichman, O., and Zhu, Y. (2003).
Bounded model checking.
Advances in Computers, 58:117–148.
Bryant, R. E. (1992).
Symbolic boolean manipulation with ordered binary-decision diagrams.
ACM Comput. Surv., 24(3):293–318.
Burch, J., Clarke, E., Dill, D., Hwang, L., and McMillan, K. (1990).
Symbolic model checking: 1020 states and beyond.
In 5th LICS, pages 428–439. IEEE.
Bibliography III
Valmari, A. (1990).
Stubborn sets for reduced state space generation.
LNCS 483.