Professional Documents
Culture Documents
Spin Tutorial 2004
Spin Tutorial 2004
Spin Tutorial 2004
Theo C. Ruys
University of Twente
Formal Methods & Tools group
http://www.cs.utwente.nl/~ruys
Credits should go to
Gerard Holzmann (Bell Laboratories)
Developer of SPIN, Basic SPIN Manual.
Advanced SPIN
intended audience:
people at least at the level of Basic SPIN
Contents
Emphasis is on using SPIN not on technical details.
In fact, we almost regard SPIN as a black box.
We just want to
press-the-button.
Thursday 11-Apr-2002
Livelock, starvation
Underspecification
unexpected reception
of messages
Overspecification
Dead code
Violations of constraints
Buffer overruns
Array bounds violations
System Development
System
Engineering
Classic
Model Checking
Analysis
Design
Modern
Model Checking
Code
Testing
Thursday 11-Apr-2002
Maintenance
Model
Checker
refinement
techniques
Implementation
Thursday 11-Apr-2002
Model
Checker
Thursday 11-Apr-2002
Thursday 11-Apr-2002
Program suggestions
Some presentations at ETAPS/SPIN 2002 somehow
related to this tutorial:
Dennis Dams
Abstraction in Software Model Checking
Friday April 12th 10.45-13.00
SPIN Applications
Saturday April 13th 11.00-12.30
Modern model checking approach.
Thursday 11-Apr-2002
10
Basic SPIN
Gentle introduction to SPIN and Promela
SPIN Background
Promela processes
Promela statements
Promela communication primitives
Architecture of (X)Spin
Some demos: SPIN and Xspin
hello world
Windows 2000: OK, but
mutual exclusion
SPIN runs more smoothly
alternating bit protocol
under Unix/Linux.
Cookie for the break
Thursday 11-Apr-2002
SPIN - Introduction
11
(1)
+ features
from CSP
12
SPIN - Introduction
(2)
Major versions:
1.0
2.0
3.0
4.0
Jan 1991
Jan 1995
Apr 1997
late 2002
13
Documentation on SPIN
SPINs starting page:
http://netlib.bell-labs.com/netlib/spin/whatispin.html
14
Promela Model
Promela model consist of:
type declarations
channel declarations
variable declarations
process declarations
[init process]
proctype Sender() {
...
process body
}
no unbounded data
no unbounded channels
no unbounded processes
no unbounded process creation
Thursday 11-Apr-2002
proctype Receiver() {
...
}
init {
...
}
creates processes
Processes
15
(1)
a name
a list of formal parameters
local variable declarations
body
name
formal parameters
16
Processes
(2)
A process
is defined by a proctype definition
executes concurrently with all other processes,
independent of speed of behaviour
communicate with other processes
using global (shared) variables
using channels
There may be several processes of the same type.
Each process has its own local state:
process counter (location within the proctype)
contents of the local variables
Thursday 11-Apr-2002
Processes
Process are created using
the run statement (which
returns the process id).
Processes can be created
at any point in the execution
(within any process).
Processes start executing
after the run statement.
Processes can also be
created by adding active
in front of the proctype
declaration.
Thursday 11-Apr-2002
(3)
proctype Foo(byte x) {
...
}
init {
int pid2 = run Foo(2);
run Foo(27);
}
number of procs. (opt.)
active[3] proctype Bar() {
...
}
parameters will be
initialised to 0
17
18
DEMO
Hello World!
/* A "Hello World" Promela model for SPIN. */
active proctype Hello() {
printf("Hello process, my pid is: %d\n", _pid);
}
init {
int lastpid;
printf("init process, my pid is: %d\n", _pid);
lastpid = run Hello();
printf("last pid was: %d\n", lastpid);
}
random seed
$ spin -n2 hello.pr
running SPIN in
init process, my pid is: 1
random simulation mode
last pid was: 2
Hello process, my pid is: 0
Hello process, my pid is: 2
3 processes created
Thursday 11-Apr-2002
19
(1)
Basic types
bit
bool
byte
short
int
Arrays
Records (structs)
[0..1]
[0..1]
[0..255]
[-216-1.. 216 1]
[-232-1.. 232 1]
turn=1;
flag;
counter;
s;
msg;
Arrays
array
indicing
start at 0
byte a[27];
bit flags[4];
}
Record rr;
rr.f1 = ..
Thursday 11-Apr-2002
variable
declaration
20
10
Variables should be
declared.
Variables can be given a
value by:
assignment
argument passing
message passing
(see communication)
(2)
assignment =
bb=1;
ii=2;
short s=-1;
typedef Foo {
bit bb;
int ii;
};
Foo f;
f.bb = 0;
f.ii = -2;
declaration +
initialisation
equal test ==
ii*s+27 == 23;
printf(value: %d, s*s);
Statements
21
(1)
always executable
only executable if value of x is smaller 27
executable if x is not equal to 3
Theo C. Ruys - SPIN Beginners' Tutorial
22
11
Statements
(2)
Statements are
separated by a
semi-colon: ;.
Statements
23
(3)
assert(<expr>);
The assert-statement is always executable.
If <expr> evaluates to zero, SPIN will exit with an error, as
the <expr> has been violated.
The assert-statement is often used within Promela models,
to check whether certain properties are valid in a state.
proctype monitor() {
assert(n <= 3);
}
proctype receiver() {
...
toReceiver ? msg;
assert(msg != ERROR);
...
}
Thursday 11-Apr-2002
24
12
Interleaving Semantics
Promela processes execute concurrently.
Non-deterministic scheduling of the processes.
Processes are interleaved (statements of different
processes do not occur at the same time).
exception: rendez-vous communication.
deadlocks
safety properties
liveness properties
(X)SPIN Architecture
SPIN
25
M |=
random
guided
interactive
LTL
Translator
Simulator
Promela
model M
Xspin
editing window
simulation options
verification options
MSC simulation window
Thursday 11-Apr-2002
spin.exe
Verifier
Generator
C program
counter
example
false
checker
pan.exe
|=
pan.*
26
13
Xspin in a nutshell
Xspin allows the user to
edit Promela models (+ syntax check)
simulate Promela models
random
interactive
guided
additional features
Thursday 11-Apr-2002
DEMO
Mutual Exclusion
bit flag;
byte mutex;
27
WRONG!
(1)
proctype P(bit i) {
flag != 1;
models:
flag = 1;
while (flag == 1) /* wait */;
mutex++;
printf("MSC: P(%d) has entered section.\n", i);
mutex--;
flag = 0;
Problem: assertion violation!
}
Both processes can pass the
proctype monitor() {
flag != 1 at the same time,
assert(mutex != 2);
i.e. before flag is set to 1.
}
init {
atomic { run P(0); run P(1); run monitor(); }
}
starts two instances of process P
Thursday 11-Apr-2002
28
14
DEMO
Mutual Exclusion
bit x, y;
byte mutex;
WRONG!
(2)
*/
*/
DEMO
Mutual Exclusion
bit x, y;
byte mutex;
byte turn;
29
Dekker [1962]
(3)
Thursday 11-Apr-2002
*/
*/
*/
30
15
DEMO
Mutual Exclusion
byte turn[2];
byte mutex;
Bakery
(4)
proctype P(bit i) {
Problem (in Promela/SPIN):
do
turn[i] will overrun after 255.
:: turn[i] = 1;
turn[i] = turn[1-i] + 1;
(turn[1-i] == 0) || (turn[i] < turn[1-i]);
mutex++;
mutex--;
turn[i] = 0;
More mutual exclusion algorithms
od
in (good-old) [Ben-Ari 1990].
}
proctype monitor() { assert(mutex != 2); }
init { atomic {run P(0); run P(1); run monitor()}}
Thursday 11-Apr-2002
if-statement
(1)
31
inspired by:
Dijkstras guarded
command language
if
:: choice1 -> stat1.1; stat1.2; stat1.3;
:: choice2 -> stat2.1; stat2.2; stat2.3;
::
:: choicen -> statn.1; statn.2; statn.3;
fi;
If there is at least one choicei (guard) executable, the ifstatement is executable and SPIN non-deterministically
chooses one of the executable choices.
If no choicei is executable, the if-statement is blocked.
The operator -> is equivalent to ;. By convention, it is used
within if-statements to separate the guards from the
statements that follow the guards.
Thursday 11-Apr-2002
32
16
if-statement
if
::
::
::
::
fi
(n % 2 != 0) -> n=1
(n >= 0)
-> n=n-2
(n % 3 == 0) -> n=3
else
-> skip
skip
skip
skip
skip
->
->
->
->
non-deterministic branching
(2)
n=0
n=1
n=2
n=3
do-statement
33
(1)
do
:: choice1 -> stat1.1; stat1.2; stat1.3;
:: choice2 -> stat2.1; stat2.2; stat2.3;
::
:: choicen -> statn.1; statn.2; statn.3;
od;
34
17
do-statement
(2)
if- and do-statements
are ordinary Promela
statements; so they can
be nested.
Thursday 11-Apr-2002
Communication
Sender
35
(1)
s2r
Receiver
r2s
s2r!MSG
MSG
s2r?MSG
ACK
r2s!ACK
r2s?ACK
! is sending
? is receiving
Thursday 11-Apr-2002
36
18
Communication
(2)
chan c
= [1] of {bit};
chan toR
= [2] of {mtype, bit};
chan line[2] = [1] of {mtype, Record};
Thursday 11-Apr-2002
array of
channels
Communication
37
(3)
<var> +
<const>
can be
mixed
message passing
If the channel is not empty, the message is fetched from the channel
and the individual parts of the message are stored into the <vari>s.
ch ? <const1>, <const2>, <constn>; message testing
If the channel is not empty and the message at the front of the
channel evaluates to the individual <consti>, the statement is
executable and the message is removed from the channel.
Thursday 11-Apr-2002
38
19
Communication
(4)
Rendez-vous communication
<dim> == 0
P wants to do
ch ! 1, 3+7
Q wants to do
ch ? 1, x
Then after the communication, x will have the value 10.
Thursday 11-Apr-2002
DEMO
39
(1)
Thursday 11-Apr-2002
40
20
DEMO
channel
length of 2
chan toS = [2] of {mtype, bit};
chan toR = [2] of {mtype, bit};
Thursday 11-Apr-2002
(2)
init
{
run Sender(toS, toR);
run Receiver(toR, toS);
}
Alternative notation:
ch ! MSG(par1, )
ch ? MSG(par1, )
41
Germany
..
..
10
..
20
<= 60 min?
..
Holland
holes
coffee
shop
25
<=
2 pers
Thursday 11-Apr-2002
42
21
unsafe
<= 60 min?
safe
10 20 25
<=
2 pers
Thursday 11-Apr-2002
43
Advanced SPIN
Towards effective modelling in Promela
Some left-over Promela statements
Properties that can be verified with SPIN
Introduction to SPIN validation algorithms
SPINs reduction algorithms
Extreme modelling: the art of modelling
Beyond Xspin: managing the verification trajectory
Concluding remarks
Summary
Thursday 11-Apr-2002
44
22
Basic SPIN
Promela Model
A Promela model consist of:
mtype, typedefs,
constants
type declarations
channel declarations
global variable declarations
process declarations
[init process]
can be accessed
by all processes
Thursday 11-Apr-2002
Promela statements
always executable
skip
45
Basic SPIN
assignment
always executable
if
do
break
send (ch!)
receive (ch?)
Thursday 11-Apr-2002
46
23
atomic
atomic { stat1; stat2; ... statn }
can be used to group statements into an atomic sequence;
all statements are executed in a single step
(no interleaving with statements of other processes)
no pure atomicity
is executable if stat1 is executable
if a stati (with i>1) is blocked, the atomicity token is
(temporarily) lost and other processes may do a step
47
d_step
d_step { stat1; stat2; ... statn }
more efficient version of atomic: no intermediate states are
generated and stored
may only contain deterministic steps
it is a run-time error if stati (i>1) blocks.
d_step is especially
useful to perform
intermediate computations
in a single transition
::
48
24
No atomicity
P1
P2
0
t1a
t1a (0,1)
t2b
t1c (2,0) t2a
t1b (1,1)
t2b t1a (0,2)
(3,0)
t2c
t2a t1c (2,1)
t2b t1b (1,2)
t2c t1a (0,3)
(3,1)
t2b t1c (2,2)
t2c t1b (1,3)
(3,2)
t2c t1c (2,3)
1
t1b
t2b
2
t1c
t2c
t2a
t2a
(0,0)
t1a
(3,3)
atomic
(0,0)
(1,0)
(2,0)
(3,0)
(0,1)
(1,1)
(2,1)
(3,1)
(0,2)
(1,2)
(2,2)
(3,2)
(0,3)
(1,3)
(2,3)
(3,3)
Thursday 11-Apr-2002
49
50
25
d_step
(0,0)
(1,0)
(2,0)
(3,0)
(0,1)
(1,1)
(2,1)
(3,1)
(0,2)
(1,2)
(2,2)
(3,2)
(0,3)
(1,3)
(2,3)
No intermediate states will
be constructed.
(3,3)
Thursday 11-Apr-2002
51
bit aflag;
aflag=1;
stat2
...
[]!aflag
e.g.
statn
aflag=0;
}
52
26
timeout
(1)
Thursday 11-Apr-2002
timeout
53
(2)
54
27
DEMO
abp-1.pr
perfect lines
abp-2.pr
(3)
only
three!
stealing daemon (models lossy channels)
how do we know that the protocol works correctly?
abp-3.pr
Thursday 11-Apr-2002
55
goto
goto label
transfers execution to label
each Promela statement might be labelled
quite useful in modelling communication protocols
wait_ack:
Timeout modelled by a channel.
if
:: B?ACK -> ab=1-ab ; goto success
:: ChunkTimeout?SHAKE ->
if
:: (rc < MAX) -> rc++; F!(i==1),(i==n),ab,d[i];
goto wait_ack
:: (rc >= MAX) -> goto error
fi
fi ;
Part of model of BRP
Thursday 11-Apr-2002
56
28
unless
{ <stats> } unless { guard; <stats> }
57
macros
#define RESET_ARRAY(a) \
d_step { a[0]=0; a[1]=0; a[2]=0; a[3]=0; }
#ifdef LOSSY
active proctype Daemon() { /* steal messages */ }
#endif
Thursday 11-Apr-2002
58
29
Properties
59
(1)
60
30
Properties
safety property
(2)
Historical
Classification
liveness property
invariant
x is always less than 5
termination
the system will eventually
terminate
deadlock freedom
the system never reaches a
state where no actions are
possible
SPIN: find a trace leading to
the bad thing. If there is not
such a trace, the property is
satisfied.
Thursday 11-Apr-2002
response
if action X occurs then
eventually action Y will occur
SPIN: find a (infinite) loop in
which the good thing does not
happen. If there is not such a
loop, the property is satisfied.
Properties
61
(3)
always P
eventually P
P is true until Q becomes true
Xspin contains a special
LTL Manager to edit,
save and load LTL properties.
invariance
response
precedence
objective
Thursday 11-Apr-2002
[]
[]
[]
[]
(p)
((p) -> (<> (q)))
((p) -> ((q) U (r)))
((p) -> <>((q) || (r)))
Theo C. Ruys - SPIN Beginners' Tutorial
62
31
Properties
(4)
DEMO
63
(1)
64
32
(2)
proctype Timer()
{
end:
do
:: stopwatch ? 0 -> atomic { time=time+5 ; MSCTIME }
:: stopwatch ? 1 -> atomic { time=time+10; MSCTIME }
:: stopwatch ? 2 -> atomic { time=time+20; MSCTIME }
:: stopwatch ? 3 -> atomic { time=time+25; MSCTIME }
od
}
init {
atomic { run Germany(); run Holland(); run Timer(); }
}
Thursday 11-Apr-2002
65
s
Thursday 11-Apr-2002
act
66
33
Verification Algorithm
(1)
Verification Algorithm
P1
P2
Pn
67
(2)
interleaving
product
translation
Buchi
Automaton
language
intersection
Buchi
Automaton
accepts
words
X should be empty.
Search for an accepting state in the intersection,
which is reachable from itself. In SPIN this
Based on
is implemented by two basic DFS procedures.
[Vardi & Wolper 1986].
See [Holzmann 1996 et. al. DFS] for details.
Thursday 11-Apr-2002
68
34
State vector
A state vector is the information to uniquely identify a
system state; it contains:
global variables
contents of the channels
for each process in the system:
local variables
process counter of the process
69
addresses to
linked-list of states
s
s
hash(s)
all states are explicitly stored
lookup is fast due to hash function
memory needed:
n*m bytes + hash table
computes address/index
in the hash table
h-1
hash table
Thursday 11-Apr-2002
70
35
- (not selected)
+
- (disabled by -DSAFETY)
+
property was
satisfied
of states
(i.e. the state space)
Reduction Algorithms
71
(1)
72
36
Reduction Algorithms
Partial Order Reduction
enabled
by default
(2)
Reduction Algorithms
73
(3)
(0,0)
t1a
t1b (1,0)
t1c (2,0)
(3,0)
(0,1)
(1,1)
(2,1)
t2a
(3,1)
t2b
(0,2)
(1,2)
(2,2)
(3,2)
t2c
(0,3)
(1,3)
(2,3)
(3,3)
Thursday 11-Apr-2002
74
37
Reduction Algorithms
Bit-state hashing
approximation
(3)
Hash-compaction
Reduction Algorithms
Bit-state hashing
(cont.)
hash(s)
0 0
1
0
1
1
0
0
0
0
1
0
0
1
0
h-1 1
75
(4)
hash table
Thursday 11-Apr-2002
76
38
Reduction Algorithms
State compression
(5)
Minimised automaton
77
10000
1980: pan
1000
100
10
memory requirements
to (fully) verify Tpc
1
1980
1987
1995
1999
7 days
Thursday 11-Apr-2002
2000
7 secs
Theo C. Ruys - SPIN Beginners' Tutorial
78
39
BRP 2002
state vector
104 bytes
96 bytes
# states
1,799,340
169,208
116.399
14.354
Memory (Mb)
Both verified
with SPIN 3.4.x
79
Lossy channels
Multicast Protocols
Macros
Atomicity
Invariance
Randomness
Still in the pipeline
Bitvectors
Subranges
Scheduling algorithms
Thursday 11-Apr-2002
80
40
[]P
Invariance
[]P where P is a state property
safety property
invariance global universality or global absence
[Dwyer et. al. 1999]:
25% of the properties that are being checked with model
checkers are invariance properties
BTW, 48% of the properties are response properties
examples:
[] !aflag
[] mutex != 2
Thursday 11-Apr-2002
variant
81
(single assert)
[]P
1
assert(P)
2
-end0
Two variations:
1. monitor process is created first
2. monitor process is created last
Thursday 11-Apr-2002
82
41
variant
[]P
Thursday 11-Apr-2002
variant
4 - monitor process
83
[]P
(do assert)
assert(P)
Thursday 11-Apr-2002
84
42
variant
5 - never claim
[]P
(do assert)
Thursday 11-Apr-2002
variant
85
[]P
6 - LTL property
Thursday 11-Apr-2002
86
43
variant
[]P
Discussion
+
+
PII 300Mhz
128 Mb
SPIN 3.3.10
Linux 2.2.12
87
[]P
Invariance experiments
-DNOREDUCE - memory (Mb)
NO partial order reduction
60
50
40
1. monitor first
2. monitor last
3. guarded monitor
4. monitor do assert
5. never do assert
6. LTL property
7. unless
30
20
10
0
brp
Thursday 11-Apr-2002
philo
pftp
88
44
[]P
Invariance experiments
-DNOREDUCE - time (sec)
40
1. monitor first
2. monitor last
3. guarded monitor
4. monitor do assert
5. never do assert
6. LTL property
7. unless
35
30
25
20
15
10
5
0
brp
Thursday 11-Apr-2002
philo
pftp
89
[]P
Invariance experiments
default settings - memory (Mb)
1. monitor first
2. monitor last
3. guarded monitor
4. monitor do assert
5. never do assert
6. LTL property
40
35
30
25
20
15
10
5
0
brp
Thursday 11-Apr-2002
philo
pftp
seems
attractive...
90
45
[]P
Invariance experiments
default settings - time (sec)
35
1. monitor first
2. monitor last
3. guarded monitor
4. monitor do assert
5. never do assert
6. LTL property
30
25
20
15
10
5
0
brp
Thursday 11-Apr-2002
philo
pftp
91
[]P
Invariance - Conclusions
The methods 1 and 2 monitor process with single
assert performed worst on all experiments.
When checking invariance, these methods should be avoided.
92
46
2. Partial check
Properties:
1. deadlock
2. assertions
3. invariance
4. liveness (LTL)
3. Exhaustive check
If this fails, SPIN supports several options to proceed:
1. Compression (of state vector)
2. Optimisations (SPIN-options or manually)
3. Abstractions (manually, guided by SPINs slicing algorithm)
4. Bitstate hashing
Thursday 11-Apr-2002
93
94
47
Beyond Xspin
models
options
results
retrieve
Promela model
options
spin
shell script to
automatically run
spin, gcc & pan
runspin
gcc
pan
pan results
runspin data
Verification results
obtained using a
verification tool
should always be
reproducible.
ppr
LaTeX file
Thursday 11-Apr-2002
.csv file
to analyse in
spreadsheet
95
ppr
parse pan results: recognises 49 items in verification report
Perl script (600 loc)
output to LaTeX or CSV (general spreadsheet format)
Thursday 11-Apr-2002
96
48
Further reading
[Holzmann 2000 Mdorf]
Thursday 11-Apr-2002
97
(1)
98
49
(2)
Atomicity
Enclose statements that do not have to be interleaved
within an atomic / d_step clause
Beware: the behaviour of the processes may change!
Beware of infinite loops.
Computations
use d_step clauses to make the computation a single
transition
reset temporary variables to 0 at the end of a d_step
Processes
sometimes the behaviour of two processes can be
combined into one; this is usually more effective.
Thursday 11-Apr-2002
99
Summary
Basic SPIN
Promela basics
Overview of Xspin
Several Xspin demos
Advanced SPIN
100
50