TRIAL-EXAM Software Engineering using Formal Methods TDA293 (TDA292) / DIT270. Only dictionaries may be used. Other aids are not allowed!

Similar documents
TRIAL EXAM C Software Engineering using Formal Methods TDA293 / DIT270

TRIAL-EXAM Software Engineering using Formal Methods TDA293 (TDA292) / DIT270. Only dictionaries may be used. Other aids are not allowed!

Network Protocol Design and Evaluation

Computer Lab 1: Model Checking and Logic Synthesis using Spin (lab)

The SPIN Model Checker

Logic Model Checking

Copyright 2008 CS655 System Modeling and Analysis. Korea Advanced Institute of Science and Technology

Computer Lab 1: Model Checking and Logic Synthesis using Spin (lab)

Automated Reasoning. Model Checking with SPIN (II)

Tool demonstration: Spin

INF5140: Specification and Verification of Parallel Systems

Applications of Formal Verification

Formal Methods for Software Development

The Spin Model Checker : Part I/II

Applications of Formal Verification

The Spin Model Checker : Part I. Moonzoo Kim KAIST

Applications of Formal Verification

Software Engineering using Formal Methods

The SPIN Model Checker

Software Engineering using Formal Methods

A Tutorial on Model Checker SPIN

Formal Specification and Verification

MP 6 Modeling in Promela and SPIN

Distributed Systems Programming (F21DS1) SPIN: Formal Analysis II

SPIN: Introduction and Examples

Formal Specification and Verification

Model-Checking Concurrent Systems. The Model Checker Spin. The Model Checker Spin. Wolfgang Schreiner

Patrick Trentin Formal Methods Lab Class, March 03, 2017

4/6/2011. Model Checking. Encoding test specifications. Model Checking. Encoding test specifications. Model Checking CS 4271

Design and Analysis of Distributed Interacting Systems

Computer Aided Verification 2015 The SPIN model checker

Distributed Systems Programming (F21DS1) SPIN: Formal Analysis I

Patrick Trentin Formal Methods Lab Class, Feb 26, 2016

Lecture 3 SPIN and Promela

Model-Checking Concurrent Systems

Network Protocol Design and Evaluation

THE MODEL CHECKER SPIN

Programming with Contracts. Juan Pablo Galeotti, Alessandra Gorla Saarland University, Germany

T Parallel and Distributed Systems (4 ECTS)

Formal Methods for Software Development

SPIN part 2. Verification with LTL. Jaime Ramos. Departamento de Matemática, Técnico, ULisboa

EMBEDDED C CODE 17. SPIN Version 4 supports the inclusion of embedded C code into PROMELA models through the following fiv e new primitives:

Distributed Systems Programming F29NM1 2. Promela I. Andrew Ireland. School of Mathematical and Computer Sciences Heriot-Watt University Edinburgh

What is SPIN(Simple Promela Interpreter) Material About SPIN. Elements of Promela. Basic Variables and Types. Typical Structure of Promela Model

CS477 Formal Software Development Methods / 32

LTL Reasoning: How It Works

Testing, Debugging, and Verification

CS477 Formal Software Development Methods / 39

Spin: Overview of PROMELA

What is SPIN(Simple Promela Interpreter) Elements of Promela. Material About SPIN. Basic Variables and Types. Typical Structure of Promela Model

Multi-Threaded System int x, y, r; int *p, *q, *z; int **a; EEC 421/521: Software Engineering. Thread Interleaving SPIN. Model Checking using SPIN

SPIN: Part /614 Bug Catching: Automated Program Verification. Sagar Chaki November 12, Carnegie Mellon University

Model Requirements and JAVA Programs MVP 2 1

The Java Modeling Language JML

FORMAL METHODS IN NETWORKING COMPUTER SCIENCE 598D, SPRING 2010 PRINCETON UNIVERSITY LIGHTWEIGHT MODELING IN PROMELA/SPIN AND ALLOY

Using Spin to Help Teach Concurrent Programming

The Design of a Distributed Model Checking Algorithm for Spin

Design of Internet Protocols:

Distributed Systems Programming (F21DS1) SPIN: Simple Promela INterpreter

Hello World. Slides mostly a reproduction of Theo C. Ruys SPIN Beginners Tutorial April 23, Hello() print "Hello"

Promela and SPIN. Mads Dam Dept. Microelectronics and Information Technology Royal Institute of Technology, KTH. Promela and SPIN

Logic Model Checking

B.V. Patel Institute of Business Management, Computer & Information Technology, Uka Tarsadia University

INF672 Protocol Safety and Verification. Karthik Bhargavan Xavier Rival Thomas Clausen

Fundamentals of Software Engineering

Appendix. Grammar. A.1 Introduction. A.2 Keywords. There is no worse danger for a teacher than to teach words instead of things.

Spin: Overview of PROMELA

Testing, Debugging, and Verification exam DIT082/TDA567. Day: 9 January 2016 Time: Will be published mid February or earlier

Analysis of Software Artifacts

Proving Dekker with SPIN and PROMELA

Rigorous Software Development CSCI-GA

15-819M: Data, Code, Decisions

SPIN. Carla Ferreira.

Lecture 1 Contracts : Principles of Imperative Computation (Fall 2018) Frank Pfenning

A Practical approach on Model checking with Modex and Spin

Model checking Timber program. Paweł Pietrzak

Temporal Logic and Timed Automata

EXAMINATIONS 2009 MID-TERM TEST. COMP 202 / SWEN 202 Formal Methods of Computer Science / Formal Foundations of Software Engineering WITH ANSWERS

SPIN, PETERSON AND BAKERY LOCKS

Lecture 1 Contracts. 1 A Mysterious Program : Principles of Imperative Computation (Spring 2018) Frank Pfenning

T Parallel and Distributed Systems (4 ECTS)

Formal Verification by Model Checking

Formal Specification and Verification

Formal Methods for Software Development

Mapping of UML Diagrams to Extended Petri Nets for Formal Verification

Linear Temporal Logic. Model Checking and. Based on slides developed by Natasha Sharygina. Carnegie Mellon University.

CSE331 Winter 2014, Midterm Examination February 12, 2014

Model Checking with Automata An Overview

SWEN-220 Mathematical Models of Software. Process Synchronization Critical Section & Semaphores

Topic 6: A Quick Intro To C. Reading. "goto Considered Harmful" History

ESC/Java2 Warnings David Cok, Joe Kiniry, and Erik Poll Eastman Kodak Company, University College Dublin, and Radboud University Nijmegen

CS/ECE 374 Fall Homework 1. Due Tuesday, September 6, 2016 at 8pm

formal methods & tools.

Formal Systems II: Applications

Programming, numerics and optimization

The Maude LTL Model Checker and Its Implementation

CS 61C: Great Ideas in Computer Architecture Introduction to C

CS3157: Advanced Programming. Outline

Overview The Java Modeling Language (Part 1) Related Work

CSCI.6962/4962 Software Verification Fundamental Proof Methods in Computer Science (Arkoudas and Musser) Chapter 11 p. 1/38

Transcription:

TRIAL-EXAM Software Engineering using Formal Methods TDA293 (TDA292) / DIT270 Extra aid: Only dictionaries may be used. Other aids are not allowed! Please observe the following: This exam has 14 numbered pages, plus two pages of the Spin Reference Card. Please check immediately that your copy is complete Answers must be given in English Use page numbering on your pages Start every assignment on a fresh page Write clearly; unreadable = wrong! Fewer points are given for unnecessarily complicated solutions Indicate clearly when you make assumptions that are not given in the assignment Good luck! 1

Exam/Tenta SEFM 2 Assignment 1 PROMELA (12p) In this assignment, we model a small part of a wifi network. A number of devices, modelled by the process Device, compete to get access to the network, which however has limited capacity (3 in our example). Consider the following PROMELA model. #define numofdevices 5 #define limit 3 byte numofusers = 0; chan ch = [0] o f { byte, bool ; proctype Device ( byte i) { bool answer ; do (to be filled in by you) od active proctype AccessControl () { byte id; do :: ch? id, _ -> i f :: numofusers < limit - > ch! id, true :: e l s e -> ch! id, f a l s e f i od i n i t { byte i = 0; atomic { do :: (i >= numofdevices ) -> break :: e l s e -> run Device (i); i++ od Note that we do not actually model the network to be accessed. Rather, we only model the competing devices, plus a single AccessControl process which grants or denies access, depending on the number of devices currently accessing the network. A single channel, called ch, is used to communicate access requests (where the second argument does not matter), permissions (true), and denials (false). Your answers to the questions below should remain valid even if the numbers in the definitions of numofdevices and limit change. (For the continuation of this assignment, see next page.)

Exam/Tenta SEFM 3 (a) (b) (c) (d) (e) Solution (a) [8p] Complete the process Device, according to the following instructions. Only the place marked by (to be filled in by you) should be completed, everything else in the above PROMELA model should be left unchanged. After being granted access, device n enters the network by incrementing numofusers, and printing out device n enters network. Devices whose request gets denied print out device n cannot enter now. Those devices which entered the network perform activities therein, here modelled by printing out device n using network, and after that leave the network, by decrementing numofusers, and printing out device n leaves network. In both cases (whether the device was denied access, or granted access and entered-used-left the network), the same device will start over by sending a new (identical) request, and all that infinitely often. Your solution has to ensure that numofusers never exceeds the limit. At the same time, it would be too restrictive to only allow, for instance, that only one device uses the network at once. Instead, your solution has to allow runs with up to limit devices using the network at once. [1p] Explain briefly why your solution guarantees that numofusers never exceeds the limit. [1p] Write a separate process that allows to verify this property with SPIN (without using LTL). [1p] Explain briefly why your solution allows numofusers to reach limit. [1p] Write a separate process that allows to confirm this using SPIN (without using LTL). proctype Device ( byte i) { bool answer ; do :: ch! i, f a l s e ; i f :: atomic { ch? eval (i), true ; numofusers ++; p r i n t f (" device %d enters network \n", i); p r i n t f (" device %d using network \n", i); numofusers - -; p r i n t f (" device %d leaves network \n", i) :: ch? eval (i), f a l s e ; p r i n t f (" device %d cannot enter now \n", i) f i od

Exam/Tenta SEFM 4 Another alternative: proctype Device ( byte i) { bool answer ; do :: ch! i, f a l s e ; atomic { ch? eval (i), answer ; i f :: answer - > numofusers ++; p r i n t f (" device %d enters network \n", i) :: e l s e -> p r i n t f (" device %d cannot enter now \n", i) f i i f :: answer -> p r i n t f (" device %d using network \n", i); numofusers - -; p r i n t f (" device %d leaves network \n", i) :: e l s e f i od (b+d) (The explanation is not given here.) (c) active proctype VerifierB () { assert ( numofusers <= limit ) (e) active proctype VerifierC () { assert ( numofusers!= limit ) If SPIN finds a failing run, that shows that numofusers can reach limit, which is what we wanted to show.

Exam/Tenta SEFM 5 Assignment 2 Linear Temporal Logic (LTL) (10p) Consider the following Promela model: byte x = 0; bool b = f a l s e active proctype P() { do :: x < 20 - > x = 20; b = true :: x >= 0 -> i f :: x < 30 -> x++ :: e l s e -> x = 10 f i od Take your time to understand the behavior of P. Then consider the following properties, each of which might or might not hold: 1. b will be true at some point. 2. x will always be 10. 3. At some point, x will be 10. 4. At some point, x will be 11. 5. From some point on, x will always be 10. 6. x will infinitely often be 11. 7. If b will never be true, then x will infinitely often be 11. (a) (b) Solution (a) 1. <>b [6p] Formulate each of the properties 1. - 7. in Linear Temporal Logic. [4p] For each of the properties 1. - 7., tell whether or not the property is valid in the transition system given by the above Promela model. (You don t need to explain your answer.) 2. [](x >= 10) 3. <>(x == 10)

Exam/Tenta SEFM 6 4. <>(x == 11) 5. <>[](x >= 10) 6. []<>(x == 11) 7. (!<>b) -> []<>(x == 11) (b) 1. invalid 2. invalid 3. valid 4. invalid 5. valid 6. invalid 7. valid

Exam/Tenta SEFM 7 Assignment 3 (Büchi Automata and Model Checking) (8p) (a) (b) (c) [2p] Give the ω expression describing the language accepted by the following Büchi automaton: a Solution (ac b) ω q 0 q 1 b c [3p] Give the ω expression describing the language accepted by the following Büchi automaton: q 0 a c q 1 b b q 2 Solution a(ba + cb) ω [3p] Give a Büchi automaton that accepts exactly those runs satisfying the LTL formula: p (p q) Solution Σ := 2 {p,q {p, q q 0, {q {p, q q 1 {p q 2, {p, {q Σ

Exam/Tenta SEFM 8 Assignment 4 (First-Order Sequent Calculus) (8p) Prove the validity of the following untyped first-order formulas, only using the sequent calculus. You are only allowed to use the rules presented in the SEFM lectures! Provide the name of each rule used in your proof as well as the resulting sequent, and make clear on which sequent you have applied the rule. When applying a quantifier rule, justify that the respective side condition is fulfilled. (a) [4p] (b) [4p] ( x; ( p(x) q(x))) x; (p(x) q(x)) x; (p(x) q(x)) ( x; ( p(x) q(x))) Solution In this document, we only provide the names of the used proof rules. In the exam, you were requested to give more information, including the resulting sequents, for each proof step. See the question text. (a) impright, notleft, allright, x c, c fresh constant, exright with c, orright, andright, gives: 1) notright with c, close for p(c) 2) notright with c, close for q(c). (b) impright, notright, exleft, x c, c fresh constant, allleft with c, andleft, notleft on p(c) and q(c), orleft gives: 1) close on p(c), 2) close on q(c).

Exam/Tenta SEFM 9 Assignment 5 (Java Modeling Language) (10p) Consider the Java classes Interval and IntervalSeq: public class Interval { private final int start, end; public Interval(int start, int end) { this.start = start; this.end = end; public int getstart() { return start; public int getend() { return end; /** * Class to represent sequence of intervals. */ public class IntervalSeq { protected int size = 0; protected Interval[] contents = new Interval[1000]; /** * Insert a new element in the sequence; * it is not specified in which place * the element will be inserted */ public void insert(interval iv) { //... // more methods In the following, observe the usual restrictions under which Java elements can be used in JML specifications. (For the continuation of this assignment, see next page.)

Exam/Tenta SEFM 10 (a) (b) [3p] Augment class Interval with JML specification stating that getend() is always getstart(). [7p] In class IntervalSeq, the field size holds the number of Interval objects which have yet been inserted into the IntervalSeq object. All inserted Interval objects are stored in the beginning of the array. The remaining cells of the array are null. Augment class IntervalSeq with JML specification stating the following: The size field is never negative, and always contents.length. The contents of the array which are stored below index size are never null. If the size is strictly smaller than contents.length, then all of the following must hold: insert terminates normally insert increases size by one After insert(iv), the interval iv is stored in contents at some index i below size. Below index i, the array contents is unchanged. The elements stored in between i and size were shifted one index upwards (as compared to the old contents). If the size has reached contents.length, IndexOutOfBoundsException. insert will throw an Solution (a) Also, add assignable clauses where appropriate. public class Interval { private /* spec_public */ final int start, end; /* public invariant getend() >= getstart(); */ public Interval(int start, int end) { this.start = start; this.end = end; public /* pure */ int getstart() { return start;

Exam/Tenta SEFM 11 public /* pure */ int getend() { return end; (b) Remark: nullable was not required. /** * Class to represent sequence of intervals. */ public class IntervalSeq { // public invariant size >= 0; protected /* spec_public */ int size = 0; // public invariant contents.length >= size; // public invariant (\forall int i; // 0 <= i && i < size; // contents[i]!= null); protected /* nullable spec_public */ Interval[] contents = new Interval[1000]; /** * Insert a new element in the sequence; it is not specified * in which place the element will be inserted */ /* public normal_behavior requires size < contents.length; ensures size == \old(size) + 1; ensures (\exists int i; 0 <= i && i < size; contents[i] == iv && (\forall int j; 0 <= j && j < i; contents[j] == \old(contents[j])) && (\forall int k; i < k && k < size; contents[k] == \old(contents[k-1]))); assignable contents, contents[*], size; also public exceptional_behavior requires contents.length == size; signals_only IndexOutOfBoundsException;

Exam/Tenta SEFM 12 */ public void insert(interval iv) { //... // more methods

Exam/Tenta SEFM 13 Assignment 6 (Loop Invariants) (12p) Consider the following program: /*public invariant (\forall int i; (\forall int j; i>=0 && j>=0 && j<=i && i<arr.length; arr[j]<=arr[i])); */ public int[] arr; /*public normal_behavior requires true; ensures? */ public int f(int x) { int r=0; /* loop_invariant? assignable? decreases? */ while(r<arr.length && arr[r]<x) { r++; return r; (a) [1p] Explain in your own words what f does. (b) [3p] Provide the postcondition for method f. (c) [1p] What fields can f modify? Change the specification of f accordingly. (d) [5p] Provide a loop invariant along with an assignable clause that would be sufficient for proving the postcondition of f. (e) [2p] Provide a decreases clause that would be sufficient for proving termination of f. Solution class Loop{

Exam/Tenta SEFM 14 /* public invariant (\forall int i; (\forall int j; i>=0 && j>=0 && j<=i && i<arr.length; arr[j] <= arr[i])); */ public int [] arr; /* public normal_behaviour requires true; ensures \result >= 0 && \result <= arr.length && (\forall int i; i >= 0 && i < \result; arr[i] < x) && (\forall int j; j >= \result && j < arr.length; arr[j] >= x); */ public int f(int x) { int r = 0; /* loop_invariant r >= 0 && r <= arr.length && (\forall int i; i >= 0 && i < r; arr[i] < x); assignable r; decreases arr.length - r; */ while (r < arr.length && arr[r] < x) { r++; return r; (total 60p)

.............................................................................................................................................................................................................................................................................. Liveness: spin -a file gcc -o pan pan.c pan -a -f or./pan -a -f spin -t -p -l -g -r -s file Spin arguments -a generate verifier and syntax check -i interactive simulation -I display Promela program after preprocessing -nn seed for random simulation -t guided simulation with trail -tn guided simulation with Nth trail -un maximum number of steps is N -f translate an LTL formula into a never claim -F translate an LTL formula in a file into a never claim -N include never claim from a file -l display local variables -g display global variables -p display statements -r display receive events -s display send events Compile arguments -DBFS breadth-first search -DNP enable detection of non-progress cycles -DSAFETY optimize for safety -DBITSTATE bitstate hashing -DCOLLAPSE collapse compression -DHC hash-compact compression -DMA=n minimized DFA with maximum n bytes -DMEMLIM=N use up to N megabytes of memory Pan arguments -a find acceptance cycles -f weak fairness -l find non-progress cycles -cn stop after Nth error -c0 report all errors -e create trails for all errors -i search for shortest path to error -I approximate search for shortest path to error -mn maximum search depth is N -wn 2 N hash table entries -A suppress reporting of assertion violations -E suppress reporting of invalid end states Caveats Expessions must be side-effect free. Local variable declarations always take effect at the beginning of a process. A true guard can always be selected; an else guard is selected only if all others are false. Macros and inline do not create a new scope. Place labels before an if or do, not before a guard. In an if or do statement, interleaving can occur between a guard and the following statement. Processes are activated and die in LIFO order. Atomic propositions in LTL formulas must be identifiers starting with lowerase letters and must be boolean variables or symbols for boolean-valued expressions. Arrays of bit or bool are stored in bytes. The type of a message field of a channel cannot be an array; it can be a typedef that contains an array. The functions empty and full cannot be negated. References G. J. Holzmann. The Spin Model Checker: Primer and Reference Manual, Addison-Wesley, 2004. http://spinroot.com. M. Ben-Ari. Principles of the Spin Model Checker, Springer, 2008. http://www.springer.com/978-1-84628-769-5. Spin Reference Card Mordechai (Moti) Ben-Ari October 1, 2007 Copyright 2007 by Mordechai (Moti) Ben-Ari. This work is licensed under the Creative Commons Attribution-Noncommercial-ShareAlike 3.0 License. To view a copy of this license, visit http://creativecommons.org/licenses/by-nc-sa/3. 0/; or, (b) send a letter to Creative Commons, 543 Howard Street, 5th Floor, San Francisco, California, 94105, USA. Datatypes bit (1 bit) bool (1 bit) byte (8 bits unsigned) short (16 bits signed) int (32 bits signed) unsigned ( 32 bits unsigned) - for a 32-bit machine. pid chan mtype = { name, name,... (8 bits) typedef typename { sequence of declarations Declaration - type var [= initial value] Default initial values are zero. Array declaration - type var[n] [= initial value] Array initial value assigned to all elements. Operators (descending precedence) () [].! ~ ++ -- * / % + - << >> < <= > >= ==!= & ^

&& (... ->... :... ) conditional expression = Predefined Constants - true, false Variables (read-only except ): - write-only hidden scratch variable nr pr - number of processes pid - instantiation number of executing process timeout - no executable statements in the system? Preprocessor #define name (arguments) string #undef, #if, #ifdef, #ifndef, #else, #endif #include file name inline name (arguments) {... Statements Assignment - var = expression, var++, var-- assert(expression) printf, printm - print to standard output %c (character), %d (decimal), %e (mtype), %o (octal), %u (unsigned), %x (hex) scanf - read from standard input in simulation mode skip - no operation break - exit from innermost do loop goto - jump to label Label prefixes with a special meaning: accept - accept cycle end - valid end state progress - non-progress cycle atomic {... - execute without interleaving d step {... - execute deterministically (no jumping in or out; deterministic choice among true guards; only the first statement can block). {... unless {... - exception handling........................................................................................................................................ Guarded commands if :: guard -> statements ::... fi do :: guard -> statements ::... od else guard - executed if all others are false. Processes Declaration - proctype procname (parameters) {... Activate with prefixes - active or active[n] Explicit process activation - run procname (arguments) Initial process - init {... Declaration suffixes: priority - set simulation priority provided (e) - executable only if expression e is true Channels chan ch = [ capacity ] of { type, type,... ch! args send ch!! args sorted send ch? args receive and remove if first message matches ch?? args receive and remove if any message matches ch? <args> receive if first message matches ch?? <args> receive if any message matches ch? [args] poll first message (side-effect free) ch?? [args] poll any message (side-effect free) Matching in a receive statement: constants and mtype symbols must match; variables are assigned the values in the message; eval(expression) forces a match with the current value of the expression. len(ch) - number of messages in a channel empty(ch) / nempty(ch) - is channel empty / not empty? full(ch) / nfull(ch) - is channel full / not full? Channel use assertions: xr ch - channel ch is receive-only in this process xs ch - channel ch is send-only in this process....................................................................................................................................... Temporal logic! not && and or -> implies <-> equivalent to [] always <> eventually X next U strong until V dual of U defined as pvq <->!(!pu!q) Remote references Test the control state or the value of a variable: process-name label-name proctype-name [ expression ] label-name process-name : label-name proctype-name [ expression ] : label-name Never claim never {.... Predefined constructs that can only appear in a never claim: last - last process to execute enabled(p) - is process enabled? np - true if no process is at a progress label pc value(p) - current control state of process remote references See also trace and notrace. Variable declaration prefixes hidden - hide this variable from the system state local - a global variable is accessed only by one process show - track variable in Xspin message sequence charts Verification Safety: spin -a file gcc -DSAFETY -o pan pan.c pan or./pan spin -t -p -l -g -r -s file