3 COMP 1 5 9 3 Algorithmic Verification SPIN and Promela Dr. - - PowerPoint PPT Presentation

3 comp 1 5 9 3 algorithmic verification
SMART_READER_LITE
LIVE PREVIEW

3 COMP 1 5 9 3 Algorithmic Verification SPIN and Promela Dr. - - PowerPoint PPT Presentation

<latexit


slide-1
SLIDE 1

Promela Synchronisation Model Checking with SPIN Channels

COMP 3 9 1 5 3 Algorithmic Verification

<latexit sha1_base64="P4jUJHo6g1yopyZBD74hiv3LdI=">AIZHicjVRb9NIFD6kXEKW6l4QEhoCBalIa4JYJqVcTSF14QRaIFqanQ2D5xRpnYZjxpG6L8Cn7d/oH9EfvEmWPnRgy7juw5/ubMd75zif1Uq8w2m39fqKxcvHT5SvVq7Y9r12/cvLV6+yhLBibAwyDRifnsywy1ivHQKqvxc2pQ9n2Nn/zevtv/dIomU0n80Q5TPOnLKFYdFUhL0JfVyve2j5GKR1b1vqUqsAOD41o7TkI8zqy0WO8orfd8PcAH2826ULGySuoTsfHV2xSjtsVz63fyNeuM9t+/OxiPx38uMxgMmUD6ySkKo6KuFUln76tHVHqnhGqnOdUJRotU/mok7OfqXZLqHbLqSKDGJeJ0ruOqSw/r5xpiJqkMNWMhOW0Skha5STBUMZzeTmKVl0GAaZWxRGX6fzXdQqNPMubgmGEXIgydGeGuqoXqDeH7paiM4bWHNqa8Z5vFikJacWGV9/yGtNWkMq/dJQYZbt9FYgjNMJZO0Yhwvz9+XWerPR5EsG15hrENxHSrlTa0IYQEAhAHxBisGRrkJDR7xg8aEJK2AmMCDNkKd5HGEONzg7IC8lDEtqjZ0RvxwUa07vjzPh0QFE03YZOCnhc+IRkdxjNVxdfzPn+KsaIuZ3GIa1+wdkn1EKX0P86N/H8v+dcTpYUvuRcFOlMGXFZBgsZdWjV9G5Jv3sOyRPJCumUISsgTBOaIy6GoTWvq8u8y3W7Idk/S4Xd6oH3zj+xGeCaVp9ZjIUzeU9YE15rhLqdCal96yI7mrv8op4x50yhJ3Rvu0zBhzqOMyGsLXrH3K9gj1OmVFNdV8gnj+l28Xo8BYJ3cIFdMCKpFm7f5767majRvfyLaT+cVjPjPFz9keJ70IAW2f1p5IwrH5Bnh+5FBX3OVdHq5n6Zz2lxWZ7R0/VYsJ3MacgonoB97lfM/RAcyXkgc7jpzKvfWMoI4ZS7O2SdlnuC8Iw8M54ZzTmNWK/intf5H6JoR9NOxJ0dwgPKuUkdmuXsVM9nmnKlU45mp3WY/OeQJ6oLQtyd6SB7vtaLyvO6WZ7dfM4Rzum5B4/o7dFvlEw8c0SyJUlZyioM+yZFrfMTuSJTeM5mpgbuW+f9/GVbNo62G97zxvMP2+uv3xRfvSrcg4ewQfPyAl7DWziAQwgq/6Ilc2Vp1f+qV6rlXv5K6VC8WZNVi4qvd/AB3w9Tw=</latexit>

SPIN and Promela

  • Dr. Liam O’Connor

CSE, UNSW (for now) Term 1 2020

1

slide-2
SLIDE 2

Promela Synchronisation Model Checking with SPIN Channels

SPIN http://www.spinroot.com

Programs are modelled in the Promela language.

2

slide-3
SLIDE 3

Promela Synchronisation Model Checking with SPIN Channels

Promela in brief

A kind of weird hybrid of C and Guarded Command Language.

3

slide-4
SLIDE 4

Promela Synchronisation Model Checking with SPIN Channels

Promela in brief

A kind of weird hybrid of C and Guarded Command Language. Models consist of multiple processes which may be non-deterministic, and may include guards.

4

slide-5
SLIDE 5

Promela Synchronisation Model Checking with SPIN Channels

Promela in brief

A kind of weird hybrid of C and Guarded Command Language. Models consist of multiple processes which may be non-deterministic, and may include guards. Supports structured control using special if and do blocks, as well as goto.

5

slide-6
SLIDE 6

Promela Synchronisation Model Checking with SPIN Channels

Promela in brief

A kind of weird hybrid of C and Guarded Command Language. Models consist of multiple processes which may be non-deterministic, and may include guards. Supports structured control using special if and do blocks, as well as goto. Variables are either global or process-local. No other scopes exist.

6

slide-7
SLIDE 7

Promela Synchronisation Model Checking with SPIN Channels

Promela in brief

A kind of weird hybrid of C and Guarded Command Language. Models consist of multiple processes which may be non-deterministic, and may include guards. Supports structured control using special if and do blocks, as well as goto. Variables are either global or process-local. No other scopes exist. Variables can be of several types: bit, byte, int and so on, as well as channels.

7

slide-8
SLIDE 8

Promela Synchronisation Model Checking with SPIN Channels

Promela in brief

A kind of weird hybrid of C and Guarded Command Language. Models consist of multiple processes which may be non-deterministic, and may include guards. Supports structured control using special if and do blocks, as well as goto. Variables are either global or process-local. No other scopes exist. Variables can be of several types: bit, byte, int and so on, as well as channels. Enumerations can be approximated with mtype keyword.

8

slide-9
SLIDE 9

Promela Synchronisation Model Checking with SPIN Channels

Promela in brief

A kind of weird hybrid of C and Guarded Command Language. Models consist of multiple processes which may be non-deterministic, and may include guards. Supports structured control using special if and do blocks, as well as goto. Variables are either global or process-local. No other scopes exist. Variables can be of several types: bit, byte, int and so on, as well as channels. Enumerations can be approximated with mtype keyword. Correctness claims can be expressed in many different ways.

9

slide-10
SLIDE 10

Promela Synchronisation Model Checking with SPIN Channels

Promela in brief

A kind of weird hybrid of C and Guarded Command Language. Models consist of multiple processes which may be non-deterministic, and may include guards. Supports structured control using special if and do blocks, as well as goto. Variables are either global or process-local. No other scopes exist. Variables can be of several types: bit, byte, int and so on, as well as channels. Enumerations can be approximated with mtype keyword. Correctness claims can be expressed in many different ways. Warning Variables of non-fixed size like int are of machine determined size, like C.

10

slide-11
SLIDE 11

Promela Synchronisation Model Checking with SPIN Channels

Example 1: Hello World

Liam will demonstrate the basics of proctype and run using some simple examples.

11

slide-12
SLIDE 12

Promela Synchronisation Model Checking with SPIN Channels

Example 1: Hello World

Liam will demonstrate the basics of proctype and run using some simple examples. Take-away You can use SPIN to randomly simulate Promela programs as well as model check them.

12

slide-13
SLIDE 13

Promela Synchronisation Model Checking with SPIN Channels

Sequential vs Concurrent

We could consider a sequential program as a sequence (or total

  • rder) of actions:
  • · · ·

13

slide-14
SLIDE 14

Promela Synchronisation Model Checking with SPIN Channels

Sequential vs Concurrent

We could consider a sequential program as a sequence (or total

  • rder) of actions:
  • · · ·

The ordering here is “happens before”. For example, processor instructions: LD R0,X LDI R1,5 ADD R0,R1 ST X,R0

14

slide-15
SLIDE 15

Promela Synchronisation Model Checking with SPIN Channels

Sequential vs Concurrent

We could consider a sequential program as a sequence (or total

  • rder) of actions:
  • · · ·

The ordering here is “happens before”. For example, processor instructions: LD R0,X LDI R1,5 ADD R0,R1 ST X,R0 A concurrent program is not a total order but a partial order.

  • · · ·
  • · · ·

This means that there are now multiple possible interleavings of these actions — our program is non-deterministic where the interleaving is selected by the scheduler.

15

slide-16
SLIDE 16

Promela Synchronisation Model Checking with SPIN Channels

Example 2: Counters

Liam will demonstrate a program that exhibits non-deterministic behaviour due to scheduling.

16

slide-17
SLIDE 17

Promela Synchronisation Model Checking with SPIN Channels

Example 2: Counters

Liam will demonstrate a program that exhibits non-deterministic behaviour due to scheduling. Explicit non-determinism You can also add explicit non-determinism using if and do blocks: if :: (n % 2 != 0) -> n = 1; :: (n >= 0) -> n = n - 2; :: (n % 3 == 0) -> n = 3; :: else -> skip; fi

17

slide-18
SLIDE 18

Promela Synchronisation Model Checking with SPIN Channels

Example 2: Counters

Liam will demonstrate a program that exhibits non-deterministic behaviour due to scheduling. Explicit non-determinism You can also add explicit non-determinism using if and do blocks: if :: (n % 2 != 0) -> n = 1; :: (n >= 0) -> n = n - 2; :: (n % 3 == 0) -> n = 3; :: else -> skip; fi What would happen without the else line?

18

slide-19
SLIDE 19

Promela Synchronisation Model Checking with SPIN Channels

Guards

The arrows in the previous slide are just sugar for semicolons: if :: (n % 2 != 0); n = 1; :: (n >= 0); n = n - 2; :: (n % 3 == 0); n = 3; fi A boolean expression by itself forms a guard. Execution can only progress past a guard if the boolean expression evaluates to true (non-zero). Recall a state with no outgoing transitions is called deadlock. SPIN can detect deadlock in Promela programs.

19

slide-20
SLIDE 20

Promela Synchronisation Model Checking with SPIN Channels

mtype and Looping

mtype = {RED, YELLOW, GREEN}; active proctype TrafficLight() { mtype state = GREEN; do :: (state == GREEN) -> state = YELLOW; :: (state == YELLOW) -> state = RED; :: (state == RED) -> state = GREEN;

  • d

} Non-determinism can be avoided by making guards mutually

  • exclusive. Exit loops with break.

20

slide-21
SLIDE 21

Promela Synchronisation Model Checking with SPIN Channels

State Space Explosion

a1 a2 b1 b2 a1 a2 b1 b2 The number of concurrent interleavings gets very large the more processes we add.

21

slide-22
SLIDE 22

Promela Synchronisation Model Checking with SPIN Channels

Synchronisation

In order to reduce the number of possible interleavings, we must allow processes to synchronise their behaviour, ensuring more

  • rderings (and thus fewer interleavings).
  • · · ·
  • · · ·

The red arrows are synchronisations. The most common synchronisation problem is the critical section problem, which we will discuss later. Promela includes some synchronisation primitives, however.

22

slide-23
SLIDE 23

Promela Synchronisation Model Checking with SPIN Channels

atomic and d step

Grouping statements in Promela with atomic prevents them from being interrupted. a1 a2 b1 b2 a1 a2 b1 atomic If a statement in an atomic block is blocked, atomicity is temporarily suspended and another process may run.

23

slide-24
SLIDE 24

Promela Synchronisation Model Checking with SPIN Channels

atomic and d step

Grouping statements with d step is more efficient than atomic, as it groups them all into one transition. a1 a2 b1 b2 a1; a2 b1 d step Non-determinism (if,do) is not allowed in d step. If a statement in the block blocks, a runtime error is raised.

24

slide-25
SLIDE 25

Promela Synchronisation Model Checking with SPIN Channels

Critical Section Problems

In the Real WorldTM, we don’t have the luxury of atomic and d step blocks. To solve this for real systems, we need solutions to the critical section problem.

25

slide-26
SLIDE 26

Promela Synchronisation Model Checking with SPIN Channels

Critical Section Problems

In the Real WorldTM, we don’t have the luxury of atomic and d step blocks. To solve this for real systems, we need solutions to the critical section problem. A sketch of the problem can be outlined as follows: forever do forever do non-critical section non-critical section pre-protocol pre-protocol critical section critical section post-protocol post-protocol

26

slide-27
SLIDE 27

Promela Synchronisation Model Checking with SPIN Channels

Critical Section Problems

In the Real WorldTM, we don’t have the luxury of atomic and d step blocks. To solve this for real systems, we need solutions to the critical section problem. A sketch of the problem can be outlined as follows: forever do forever do non-critical section non-critical section pre-protocol pre-protocol critical section critical section post-protocol post-protocol The non-critical section models the possibility that a process may do something else. It can take any amount of time (even infinite).

27

slide-28
SLIDE 28

Promela Synchronisation Model Checking with SPIN Channels

Critical Section Problems

In the Real WorldTM, we don’t have the luxury of atomic and d step blocks. To solve this for real systems, we need solutions to the critical section problem. A sketch of the problem can be outlined as follows: forever do forever do non-critical section non-critical section pre-protocol pre-protocol critical section critical section post-protocol post-protocol The non-critical section models the possibility that a process may do something else. It can take any amount of time (even infinite). Our task is to find a pre- and post-protocol such that certain atomicity properties are satisfied.

28

slide-29
SLIDE 29

Promela Synchronisation Model Checking with SPIN Channels

Desiderata

We want to ensure two main properties: Mutual Exclusion No two processes are in their critical section at the same time. Eventual Entry (or starvation-freedom) Once it enters its pre-protocol, a process will eventually be able to execute its critical section. Question Which is safety and which is liveness?

29

slide-30
SLIDE 30

Promela Synchronisation Model Checking with SPIN Channels

Desiderata

We want to ensure two main properties: Mutual Exclusion No two processes are in their critical section at the same time. Eventual Entry (or starvation-freedom) Once it enters its pre-protocol, a process will eventually be able to execute its critical section. Question Which is safety and which is liveness? Mutex is safety, Eventual Entry is liveness. Let’s use SPIN verify our solutions!

30

slide-31
SLIDE 31

Promela Synchronisation Model Checking with SPIN Channels

Assertions

We can make boolean assertions with assert, just like in C, but the SPIN verifier can check our assertions for us. Combined with a monitor process, it is a useful way to check safety properties. Liam will use this method to verify a simple mutual exclusion property for a critical section solution.

31

slide-32
SLIDE 32

Promela Synchronisation Model Checking with SPIN Channels

Labels

You can label lines of code with a label name followed by a colon, as in C. Labels can also be used in boolean expressions, where P@label evaluates to true iff process P is at the label marked label.

Liam: Demonstrate using labels on the critical section solution. 32

slide-33
SLIDE 33

Promela Synchronisation Model Checking with SPIN Channels

Labels

You can label lines of code with a label name followed by a colon, as in C. Labels can also be used in boolean expressions, where P@label evaluates to true iff process P is at the label marked label.

Liam: Demonstrate using labels on the critical section solution.

There are also special labels used to denote special states for model checking:

33

slide-34
SLIDE 34

Promela Synchronisation Model Checking with SPIN Channels

Labels

You can label lines of code with a label name followed by a colon, as in C. Labels can also be used in boolean expressions, where P@label evaluates to true iff process P is at the label marked label.

Liam: Demonstrate using labels on the critical section solution.

There are also special labels used to denote special states for model checking: End states occur automatically at the end of processes or on states labelled with labels beginning with end. Terminating in these states is not considered as deadlock.

34

slide-35
SLIDE 35

Promela Synchronisation Model Checking with SPIN Channels

Labels

You can label lines of code with a label name followed by a colon, as in C. Labels can also be used in boolean expressions, where P@label evaluates to true iff process P is at the label marked label.

Liam: Demonstrate using labels on the critical section solution.

There are also special labels used to denote special states for model checking: End states occur automatically at the end of processes or on states labelled with labels beginning with end. Terminating in these states is not considered as deadlock. Progress states must be visited infinitely often (liveness). Labels start with progress.

35

slide-36
SLIDE 36

Promela Synchronisation Model Checking with SPIN Channels

Labels

You can label lines of code with a label name followed by a colon, as in C. Labels can also be used in boolean expressions, where P@label evaluates to true iff process P is at the label marked label.

Liam: Demonstrate using labels on the critical section solution.

There are also special labels used to denote special states for model checking: End states occur automatically at the end of processes or on states labelled with labels beginning with end. Terminating in these states is not considered as deadlock. Progress states must be visited infinitely often (liveness). Labels start with progress. Acceptance states must not be visited infinitely often. Labels start with accept.

36

slide-37
SLIDE 37

Promela Synchronisation Model Checking with SPIN Channels

Labels

You can label lines of code with a label name followed by a colon, as in C. Labels can also be used in boolean expressions, where P@label evaluates to true iff process P is at the label marked label.

Liam: Demonstrate using labels on the critical section solution.

There are also special labels used to denote special states for model checking: End states occur automatically at the end of processes or on states labelled with labels beginning with end. Terminating in these states is not considered as deadlock. Progress states must be visited infinitely often (liveness). Labels start with progress. Acceptance states must not be visited infinitely often. Labels start with accept. In general these states are for use in never claims.

37

slide-38
SLIDE 38

Promela Synchronisation Model Checking with SPIN Channels

Never Claims

A process defined in a never block runs in lock-step with all other processes. Never Claim execution Each transition of a regular process must be matched with a transition of the never process.

38

slide-39
SLIDE 39

Promela Synchronisation Model Checking with SPIN Channels

Never Claims

A process defined in a never block runs in lock-step with all other processes. Never Claim execution Each transition of a regular process must be matched with a transition of the never process. This can be used to restrict the search space to ones where certain properties hold, or to check invariants.

Liam will demonstrate checking Mutual Exclusion with a never claim.

39

slide-40
SLIDE 40

Promela Synchronisation Model Checking with SPIN Channels

Never Claims

A process defined in a never block runs in lock-step with all other processes. Never Claim execution Each transition of a regular process must be matched with a transition of the never process. This can be used to restrict the search space to ones where certain properties hold, or to check invariants.

Liam will demonstrate checking Mutual Exclusion with a never claim.

Question What would the following never claim do? never { true; }

40

slide-41
SLIDE 41

Promela Synchronisation Model Checking with SPIN Channels

LTL formulae

Spin can translate LTL formulae to Never claims automatically. Liam will demonstrate verifying safety (mutex) and liveness (eventual entry) using LTL claims.

41

slide-42
SLIDE 42

Promela Synchronisation Model Checking with SPIN Channels

Eventual Entry!

Our naive solution doesn’t satisfy eventual entry, even with weak fairness. Liam will try a few more examples, ultimately leading up to Peterson’s algorithm.

42

slide-43
SLIDE 43

Promela Synchronisation Model Checking with SPIN Channels

Channels

A channel is a FIFO queue of messages (which can consist of multiple pieces of data) that is shared between processes. chan c = [10] of { mtype, byte, int } Creates a channel of size 10 with messages consisting of an mtype, a byte and an int. c ? v1, v2, v3; Removes a message from a channel, storing the data in the variables v1,v2,v3. If the channel is empty, it blocks. You can also put literal values in place of variables to pattern match. c ! RED, 255, 32767; Sends a message into the channel. If the channel is full, block.

43

slide-44
SLIDE 44

Promela Synchronisation Model Checking with SPIN Channels

Synchronous communication

If channel size is zero, the channel is synchronous. This means that each send must be paired with a matching receive, and they both execute together. Liam will demonstrate modelling a lock using: synchronous message passing, and an asynchronous channel with an atomicity token.

44

slide-45
SLIDE 45

Promela Synchronisation Model Checking with SPIN Channels

Bibliography

M.Ben-Ari, Principles of Concurrent and Distributed Programming http://spinroot.com

45