
































Studia grazie alle numerose risorse presenti su Docsity
Guadagna punti aiutando altri studenti oppure acquistali con un piano Premium
Questa pagina non è visibile nell’anteprima
Non perderti parti importanti!

































These notes cover the course Software Verification: Principles and Techniques (University of Pisa, F. Gadducci and L. Ceragioli). The course follows chapters 1–7 of C. Baier and J.-P. Katoen, Principles of Model Checking , MIT Press 2008, and its slides are the authors’ slides. The notes merge four sources:
Legend
Definition 0.1 – Definitions Blue boxes: definitions, numbered by chapter.
Theorem 0.2 – Theorems, lemmas, corollaries Red boxes. The book reference is given in brackets, e.g. [book Thm. 3.15].
Algorithm 0.3 – Algorithms Purple boxes: pseudocode and complexity.
In class (Lxx, mm:ss) Remarks, examples and warnings from the lectures. “L05, 23:26” means recording 05, minute 23:26.
Exam tip What examiners typically ask, and how to answer quickly.
Pitfall Typical mistakes.
How to solve: a type of exercise Step-by-step methods for each family of exercises.
The exam
The exam consists of a written test (or the mid-term, scheduled for Friday 4 December, 11:00– 13:00, after two Q&A sessions on 1 and 3 December) plus a seminar on an advanced topic. In the
v
Chapter map
Sources. Slides sv_01; book Ch. 1 (pp. 1–18); lecture 1t (recording L01, first hour). Exam relevance. Low: no exercise has ever been asked on this chapter, but the vocabulary (verification vs. validation, what a model checker returns, strengths and weaknesses) is useful for the oral part and for the seminar.
1.1 Why formal verification
Software and hardware failures can be fatal or extremely costly. Classical examples from the slides:
Classical techniques. Peer review (static, manual code inspection: finds roughly 60% of de- fects, but subtle concurrency errors are hard to catch) and testing (dynamic: 30–50% of project costs). Testing and simulation explore only some behaviours: they “can show the presence of errors, not their absence”. The sooner a bug is found, the cheaper it is to fix.
2 Chapter 1. Introduction
Formal methods are “applied mathematics for modelling and analysing ICT systems”. Three families:
technique method applicable if deductive methods formal proof that P holds (theorem prover, proof assistant)
the system is a mathematical theory model checking systematic check of P in all states (Spin, NuSMV, UppAal, …)
the system has a finite be- havioural model model-based testing test P by exploring some behaviours the system defines an exe- cutable model
Milestones: Turing (1949, program correctness), Hoare (1969, proof rules for sequential programs), Pnueli (1977, temporal logic for concurrent programs), then automated, model-based verification of concurrent programs. Awards: Kanellakis 1998 (Bryant, Clarke, Emerson, McMillan — sym- bolic model checking), Gödel Prize 2000 (Vardi, Wolper — automata-theoretic model checking), ACM Software System 2001 (Holzmann — SPIN), Turing Award 2007 (Clarke, Emerson, Sifakis).
1.2 What is model checking
Definition 1.2 – Model checking (informal) Model checking is an automated technique that, given a finite-state model of a system and a formal property , systematically checks whether this property holds for (a given state in) that model.
system requirements
system model (transition system)
property specification (temporal logic)
model checker
satisfied violated + counterexample out of memory
modelling formalising
In class (L01, 05:48–07:09 and 26:15–27:27) “The model checker is a kind of black box: you provide the property and a model of your program, and it tells you yes; or no, usually with a counterexample showing where the problem is; or ‘I don’t know’, because the analysis is
4 Chapter 1. Introduction
In class (L01, 01:03–04:38 and 31:32–39:13) The first three quarters of the course are a theoret- ical introduction to temporal logics (“more on the side of the theoretical foundations than of practical applications”); the last part is devoted to advanced topics, from which the seminar is chosen. Preview of a recurring theme: LTL is simple and intuitive, CTL is less intuitive but its model checking is cheaper; and for CTL there is always a smallest equivalent model (the bisimulation quotient), something that does not exist for linear-time equivalence. Prerequi- sites: automata and formal languages, algorithms, complexity (NP, PSPACE), propositional logic.
Chapter map
Sources. Slides sv_02 (transition systems, circuits, program graphs), sv_03 (interleaving, shared variables, handshaking), sv_04 (channel systems, synchronous product, state explo- sion); book Ch. 2 (pp. 19–88; NanoPromela, Sect. 2.2.5, was not covered); lectures 1t–3t (recordings L01–L03); exercise session 1e (L04, Sheet 1). What you must be able to do at the exam.
2.1 Transition systems
A transition system is the semantic model of a real system: an “extended digraph” whose nodes are the states and whose edges are the stepwise behaviour, enriched with initial states, actions (for communication) and atomic propositions (for state properties).
Definition 2.1 – Transition system [book Def. 2.1] A transition system is a tuple T = (S, Act , →, S 0 , AP , L) where
For s ∈ S and α ∈ Act : Post (s, α) = {s′^ | s −→α s′}, Post (s) =
α Post (s, α), and dually^ Pre (s, α), Pre (s). A state is terminal if Post (s) = ∅.
Definition 2.2 – Deterministic transition systems [book Def. 2.5]
2.3. Modelling software: program graphs 7
(for outputs: λyi = 1). Number of states: 2 n+k^ (e.g. 100 registers, no input: 2100 ; plus one input bit: 2101 ).
Example 2.6 – Circuit with λy = ¬(x ⊕ r) , δr = x ∨ r , initially r = 0 [book Ex. 2.11]
x=0, r=0 x=1, r=
x=0, r=1 x=1, r=
{y} {x}
{r} {x, r, y}
From [x, r] the next register value is x ∨ r and the next input is arbitrary: [0, 0] → [0, 0], [1, 0]; [1, 0] → [0, 1], [1, 1]; [0, 1] → [0, 1], [1, 1]; [1, 1] → [0, 1], [1, 1]. The out- put y = ¬(x ⊕ r) is 1 iff x = r. Once r = 1 it stays 1 : the states with r = 0 are left forever as soon as x = 1.
In class (L01, 61:59–67:50) The initial register value r = 0 is a modelling choice (“could be 1”). The example shows how quickly even simple hardware produces large state spaces: states are all combinations of input and register values.
2.3 Modelling software: program graphs
Definition 2.7 – Typed variables, evaluations, conditions, effects A typed variable x has a domain Dom (x). An evaluation of a set Var is a type-consistent function η : Var →
x Dom (x);^ Eval ( Var )^ is the set of evaluations.^ Cond ( Var )^ is the set of Boolean conditions over Var , with the obvious η |= g (e.g. [x=0, y=3, z=6] |= ¬x ∧ y < z). The effect of actions is a function Effect : Act × Eval ( Var ) → Eval ( Var ).
Example 2.8 – Effects: sequential vs. simultaneous assignment From [x=1, y=3]: α = x:=2x+y gives [x=5, y=3]; β = x:=2x+y; y:=1-x gives [x=5, y=−4] (the second assignment sees the new x); γ = (x,y):=(2x+y,1-x) gives [x=5, y=0] (both use the old values).
Definition 2.9 – Program graph [book Def. 2.13] A program graph over Var is PG = ( Loc , Act , Effect , ↪→, Loc 0 , g 0 ) where Loc is a set of loca- tions (control states), Act a set of actions, Effect as above, ↪→ ⊆ Loc × Cond ( Var ) × Act × Loc the conditional transitions `
g:α ↪−−→ ′, _Loc_ 0 ⊆ _Loc_ the initial locations, and g 0 ∈ _Cond_ ( _Var_ ) the initial condition. ( α ↪−→ ′^ abbreviates true :α ↪−−−→ `′.)
Definition 2.10 – TS semantics of a program graph (unfolding) [book Def. 2.15] T ( PG ) = (S, Act , →, S 0 , AP , L) with
`
g:α ↪−−→ ′^ η |= g 〈, η〉 −→ 〈α `′, Effect (α, η)〉
8 Chapter 2. Modelling Parallel Systems
Intuition SOS notation (^) conclusionpremise means: → is the smallest relation such that whenever the premise holds, the conclusion holds. Guards are evaluated on the current memory snapshot η; the action transforms η into Effect (α, η). In practice one only builds the reachable part, starting from S 0 (breadth-first).
In class (L01, 77:00–81:48) “This way of building relations through inference rules always means: take the smallest relation satisfying the rules” — you start from what the axioms give you and close under the rules, i.e. a least fixed point. Note that with AP = Loc ∪ Cond ( Var ) each state satisfies infinitely many propositions (x=3, x > 1 , x > 2 , …); in practice one only uses the propositions needed.
Example 2.11 – A while loop (slides sv_02 p. 88)
1 : while x>0 do x := x-1; (α) 2 : y := y+1 (β) od 3 : ... Program graph: 1 x>0:α ↪−−−→ 2 , 2
β ↪−→ 1 , 1 x≤0: exit ↪−−−−−→ ` 3.
ℓ 1 , x=2, y=
ℓ 2 , x=1, y=
ℓ 1 , x=1, y=
ℓ 2 , x=0, y=
ℓ 1 , x=0, y=
ℓ 3 , x=0, y=
α
β
α
β
exit
Guarded Command Language (Dijkstra). A high-level language with nondeterministic choice whose semantics is given by program graphs (GCL program → PG → TS):
stmt ::= x := expr | stmt 1 ; stmt 2 | DO :: g 1 ⇒ stmt 1... :: gn ⇒ stmt n OD | IF :: g 1 ⇒ stmt 1... :: gn ⇒ stmt n FI
A guarded command g ⇒ stmt is enabled if g holds; “::” is the nondeterministic choice among enabled commands. WHILE g DO s OD = DO :: g ⇒ s OD; IF g THEN s1 ELSE s2 = IF :: g ⇒ s1 :: ¬g ⇒ s2 FI.
Example 2.12 – GCL beverage machine Variables # sprite , # coke ∈ { 0 .. max }; two locations start and select : start → DO :: true ⇒ insert_coin; select → IF :: #sprite = #coke = 0 ⇒ return_coin :: #coke > 0 ⇒ get_coke (#coke := #coke-1) :: #sprite > 0 ⇒ get_sprite (#sprite := #sprite-1) FI :: true ⇒ refill (#sprite := max; #coke := max) OD
The unfolded TS has states 〈 start | select , # sprite , # coke 〉: 2( max + 1)^2 states.
10 Chapter 2. Modelling Parallel Systems
Pitfall In general T ( PG 1 ||| PG 2 ) 6 = T ( PG 1 )|||T ( PG 2 ): with shared variables first compose the program graphs, then unfold. (Equality holds when the PGs share no variables.) In the example above T (P 1 |||P 2 ) from x=3 correctly reaches x=7 (α then β) or x=8 (β then α).
Intuition Atomicity [book Remark 2.23]. The interleaving of program graphs treats each action label as indivisible : a sequence of statements written on a single edge (e.g. 〈b 1 := 1; x := 2〉, or “atomic{...}”) is executed without interference. Where you put the atomic brackets changes the set of interleavings — and possibly the correctness of the algorithm (see Peterson below).
Example 2.17 – Mutual exclusion with a semaphore [book Ex. 2.24]
noncrit i
wait i
crit i
y > 0 : y := y − 1
y := y + 1
Protocol for Pi: loop: noncritical; AWAIT y>0 DO y:=y-1 OD; critical; y:=y+1. Initially y = 1. P 1 |||P 2 has 3 × 3 = 9 locations; in the unfolding the location 〈 crit 1 , crit 2 〉 would require y > 0 when the sec- ond process enters, but whenever one process is crit- ical y = 0 : it is unreachable. The reachable frag- ment is the 8-state TS of example 3.6 (Chapter 3). In 〈 wait 1 , wait 2 , y=1〉 the two processes compete ; the re- quests ( noncrit i → wait i) are independent and simply interleave.
In class (L02, 14:58–21:19) Diamonds. Two independent actions produce a “diamond” in the TS (both orders reach the same state), e.g. the two request actions of the semaphore. Dependent actions break the diamond: with x := 2x and x := x + 1 from x = 3 the two orders end in x = 7 and x = 8; with a competition (both processes waiting on the semaphore) one of the two orders is simply disabled. The typical model-checking question is: “is a state with locations 〈 crit 1 , crit 2 〉 reachable?” — its only possible predecessors require y > 0 while the other process is critical, which never happens.
Example 2.18 – Peterson’s algorithm [book Ex. 2.25] Shared variables b 1 , b 2 ∈ { 0 , 1 } (“Pi wants to enter”), x ∈ { 1 , 2 } (“whose turn it is to wait”). Protocol for P 1 (P 2 symmetric): loop: noncritical; atomic{b1:=1; x:=2}; AWAIT (x=1 ∨ ¬b2) DO critical OD; b1:=
PG of P 1 : noncrit 1
b 1 :=1;x:= ↪−−−−−−→ wait 1 x=1∨¬b 2 ↪−−−−−→ crit 1 b 1 := ↪−−−→ noncrit 1. Since bi = 1 iff Pi ∈ { wait i, crit i}, states are determined by the two locations and x. Reachable TS (both states with noncrit 1 , noncrit 2 are initial, as x is arbitrary initially):
2.4. Parallelism and communication 11
n 1 n 2 , x=2 n 1 n 2 , x=
w 1 n 2 , x=2 n 1 w 2 , x=
c 1 n 2 , x=2 n 1 c 2 , x= w 1 w 2 , x=1 w 1 w 2 , x=
c 1 w 2 , x=1 w 1 c 2 , x=
No state has both processes critical, and in 〈w 1 , w 2 〉 the process that set x last waits: T Pet |= MUTEX and (unlike the semaphore) every waiting process eventually enters. The atomicity of b 1 := 1; x := 2 matters only in one direction. If the two assignments are split in the order x := 2 then b 1 := 1 (variant P 1 ′: noncrit 1 x:= ↪−−−→ request 1 b 1 := ↪−−−→ wait 1 ), mutual exclusion fails:
P 1 P 2 x b 1 b 2 noncrit 1 noncrit 2 1 0 0 noncrit 1 request 2 1 0 0 request 1 request 2 2 0 0 wait 1 request 2 2 1 0 crit 1 request 2 2 1 0 (¬b 2 holds) crit 1 wait 2 2 1 1 crit 1 crit 2 2 1 1 (x = 2 holds)
(With the order b 1 := 1 then x := 2 the algorithm is still correct.)
In class (L02, 22:43–30:20) “It is very important that the two assignments are written on the same edge”: the motivation for the variant is hardware that cannot write two variables atomically. Peterson also enjoys a form of fairness: if both processes are waiting, both will be served — a process can overtake the other only if the other is not waiting.
Definition 2.19 – Handshaking ‖H [book Def. 2.26] For T 1 , T 2 and H ⊆ Act 1 ∩ Act 2 (with τ ∈/ H): T 1 ‖H T 2 = (S 1 × S 2 , Act 1 ∪ Act 2 , →, S 0 , 1 × S 0 , 2 , AP 1 ∪ AP 2 , L), L(〈s 1 , s 2 〉) = L 1 (s 1 ) ∪ L 2 (s 2 ):
s 2 α −→ 2 s′ 2 〈s 1 , s 2 〉 −→ 〈α s 1 , s′ 2 〉
s 1 −→α 1 s′ 1 s 2 −→α 2 s′ 2 〈s 1 , s 2 〉 −→ 〈α s′ 1 , s′ 2 〉 T 1 ‖T 2 abbreviates T 1 ‖H T 2 with H = Act 1 ∩ Act 2 (synchronise on all common actions).
(T 1 ‖H T 2 )‖H′^ T 3 6 = T 1 ‖H (T 2 ‖H′^ T 3 )
2.4. Parallelism and communication 13
Definition 2.23 – TS semantics of a channel system [book Def. 2.33] States: 〈1 ,... ,n, η, ξ〉 (`i ∈ Loc i, η ∈ Eval ( Var ), ξ ∈ Eval ( Chan ); only channels with cap ≥ 1 matter). Initial states: initial locations, η |= g 0 , 1 ∧ · · · ∧ g 0 ,n, all channels empty. Rules:
`i
g:α ↪−−→ ′ i η |= g 〈... ,i,... , η, ξ〉 −→ 〈α... , `′ i,... , Effect i(α, η), ξ〉
i c?x ↪−−→′ i ξ(c) = v 1 v 2... vk, k ≥ 1 〈... , i,... , η, ξ〉 −→ 〈τ... ,′ i,... , η[x := v 1 ], ξ[c := v 2... vk]〉
i c!v ↪−→′ i ξ(c) = v 1... vk, k < cap (c) 〈... , i,... , η, ξ〉 −→ 〈τ... ,′ i,... , η, ξ[c := v 1... vkv]〉
i c?x ↪−−→′ i j c!v ↪−→′ j 〈... , i,... ,j ,... , η, ξ〉 τ −→ 〈... , ′ i,... ,′ j ,... , η[x := v], ξ〉
Example 2.24 – How many states? 2 processes with 2 locations each, 2 Boolean variables, 2 channels of capacity 10 with Boolean values: 2 · 2 · 2 · 2 · (2^11 − 1)^2 > 224 (more than 25 million), since a channel of capacity 10 over { 0 , 1 } has 1 + 2 + 2^2 + · · · + 2^10 = 2^11 − 1 possible contents. With an unbounded channel: infinitely many.
Example 2.25 – Alternating bit protocol (ABP) [book Ex. 2.32, 2.34] A sender transmits messages to a receiver over an unreliable channel c (messages may be lost), attaching a control bit y that alternates 0 , 1 , 0 ,.. .; the receiver returns the received bit as acknowledgement on channel d. The sender: (1) send message + bit y and start the timer; (2) wait for timeout or acknowledgement x: on timeout resend; on x = y stop the timer and flip y; otherwise ignore x. The model is the channel system [ Sender | Timer | Receiver ]: synchronous channels e, f between sender and timer ( timer_on , timer_off , timeout ), asyn- chronous channels c, d between sender and receiver. Executions show messages being lost and retransmitted, and duplicates (c = 00) being discarded by the receiver because the bit does not match its expected value. Size: 10 · 2 · 6 · #channel evaluations, more than 108 states for FIFOs of capacity 10.
In class (L02, 57:19–78:12) Channels are assumed FIFO — not always realistic (Internet packets may be reordered). Go channels are a close analogue: capacity 0 gives synchronous communication. A synchronous send/receive pair is a one-to-one communication whose result is τ , so it cannot synchronise with anybody else. Unbounded channels, or an unbounded number of instances of the same process, make verification undecidable in general. In the ABP the sender and the timer live on the same machine (synchronous channels), while sender and receiver are on different machines (asynchronous channels).
Variants: conditional communication `
g:c?x ↪−−−→ `′; sending expressions c!(2x + 7); open channel systems P 1 |... |Pn (some channels connected to the environment) vs. closed ones [P 1 |... |Pn]. In class (L03, 49:29–50:54 and 44:24) Open vs. closed channel systems: in an open system some channels are connected to an unknown environment, which may send or receive at any time — the right view for security (“consider all possible attackers”) and the only composi- tional one: a closed system can only be analysed with all its processes together. On the ABP state count: “this is why we use model checkers — we cannot compute these things by hand;
14 Chapter 2. Modelling Parallel Systems
and even for model checkers state explosion is a real problem”. Real model checkers come with their own description languages (e.g. Promela for SPIN) and build the TS themselves from the syntactic description.
Definition 2.26 – Synchronous product ⊗ [book Def. 2.42] For T 1 , T 2 and a mapping Act 1 × Act 2 → Act , (α, β) 7 → α ∗ β: T 1 ⊗ T 2 = (S 1 × S 2 , Act , → , S 0 , 1 × S 0 , 2 , AP 1 ∪ AP 2 , L) with L(〈s 1 , s 2 〉) = L 1 (s 1 ) ∪ L 2 (s 2 ) and
s 1 −→α 1 s′ 1 s 2 −→^ β 2 s′ 2 〈s 1 , s 2 〉 α∗β −−→ 〈s′ 1 , s′ 2 〉
No interleaving: both components always move together (lock-step, like clocked hardware).
Example 2.27 – Product of two circuits [book Ex. 2.43] C 1 : no input, register r 1 , λy = r 1 , δr 1 = ¬r 1 (states 0 ↔ 1 ). C 2 : input x, register r 2 , λy′ = δr 2 = x∨r 2 (4 states [x, r 2 ]). TC 1 ⊗TC 2 has the 8 states [r 1 , x, r 2 ]; e.g. [0, 0 , 0] → [1, 0 , 0], [1, 1 , 0] and [1, 1 , 0] → [0, 0 , 1], [0, 1 , 1]: r 1 flips at every step, r 2 becomes x ∨ r 2 , x is chosen freely.
2.5 The state-space explosion problem
TSs of reactive systems can be enormous:
|Si|) and of variables/channels. For a channel system:
| Loc 1 | · · · | Loc n| ·
x∈ Var
| Dom (x)| ·
c∈ Chan
| Dom (c)| cap (c)^ (roughly).
Remedies (later in the course and beyond): abstraction and quotienting (Chapter 7), symbolic representations (BDDs), partial-order reduction.
operator on features T 1 |||T 2 TS pure interleaving; no communication; wrong for compet- ing/dependent actions T 1 ‖ Syn T 2 TS interleaving + synchronisation (handshaking) over Syn PG 1 ||| PG 2 PG interleaving + shared variables; unfold afterwards [P 1 |... |Pn] PG + channels interleaving, shared variables, synchronous and asyn- chronous message passing T 1 ⊗ T 2 TS no interleaving, pure lock-step synchronisation