Outline Introduction The Spec# language Running Spec# Tutorials on Spec# Carl Leonardsson 2/

Similar documents
Program Verification Using the Spec# Programming System

Lecture 10 Design by Contract

Testing, Debugging, and Verification

Formal Systems II: Applications

Fundamentals of Software Engineering

CS 161 Computer Security

Reminder of the last lecture. Aliasing Issues: Call by reference, Pointer programs. Introducing Aliasing Issues. Home Work from previous lecture

Program Verification Using the Spec# Programming System

Formal Specification and Verification

Checking Program Properties with ESC/Java

Motivation. Correct and maintainable software Cost effective software production Implicit assumptions easily broken

The Java Modeling Language JML

Go Bears! IE170: Algorithms in Systems Engineering: Lecture 4

Part II. Hoare Logic and Program Verification. Why specify programs? Specification and Verification. Code Verification. Why verify programs?

Runtime Checking for Program Verification Systems

Spark verification features

Verification Condition Generation

Assertions & Design-by-Contract using JML Erik Poll University of Nijmegen

Specification tips and pitfalls

Overview The Java Modeling Language (Part 1) Related Work

Hoare Logic: Proving Programs Correct

Program Verification using the Spec# Programming System

Formal Methods for Java

Self-checking software insert specifications about the intent of a system

Program Verification (6EC version only)

Review: Hoare Logic Rules

An Annotated Language

Lecture Notes: Hoare Logic

Type checking. Jianguo Lu. November 27, slides adapted from Sean Treichler and Alex Aiken s. Jianguo Lu November 27, / 39

Advances in Programming Languages

Introduction to JML David Cok, Joe Kiniry, and Erik Poll Eastman Kodak Company, University College Dublin, and Radboud University Nijmegen

BOOGIE. Presentation by Itsik Hefez A MODULAR REUSABLE VERIFIER FOR OBJECT-ORIENTED PROGRAMS MICROSOFT RESEARCH

Announcements. Specifications. Outline. Specifications. HW1 is due Thursday at 1:59:59 pm

Hardware versus software

Verifying Java Programs Verifying Java Programs with KeY

Chapter 19 Verification of Counting Sort and Radix Sort

Rethinking Automated Theorem Provers?

Mutual Summaries: Unifying Program Comparison Techniques

JML. Outline. Métodos Formais em Engenharia de Software. MI, Braga these slides were prepared by adopting/adapting teaching material

Static program checking and verification

Java Modelling Language (JML) References

The Java Modeling Language (Part 1)

Formal Specification and Verification

Assertions, pre/postconditions

assertion: A statement that is either true or false.

Deductive Verification in Frama-C and SPARK2014: Past, Present and Future

Code Contracts. Pavel Parízek. CHARLES UNIVERSITY IN PRAGUE faculty of mathematics and physics

Practical introduction to Frama-C (without Mathematical notations ;-) )

Advances in Programming Languages

The Prototype Verification System PVS

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

n Specifying what each method does q Specify it in a comment before method's header n Precondition q Caller obligation n Postcondition

CSC Advanced Object Oriented Programming, Spring Specification

Advances in Programming Languages

A short manual for the tool Accumulator

Verifying Java Programs. Verifying Java Programs. The Krakatoa/Why Tool Suite

Viper A Verification Infrastructure for Permission-Based Reasoning

Homework #1, on the class web pages later today

Incremental Proof Development in Dafny

Verifying Java Programs

Exercise 3 Subtyping and Behavioral Subtyping October 13, 2017

ESC/Java2 vs. JMLForge. Juan Pablo Galeotti, Alessandra Gorla, Andreas Rau Saarland University, Germany

Outline. Introduction. 2 Proof of Correctness. 3 Final Notes. Precondition P 1 : Inputs include

Semantics. There is no single widely acceptable notation or formalism for describing semantics Operational Semantics

Overview. Verification with Functions and Pointers. IMP with assertions and assumptions. Proof rules for Assert and Assume. IMP+: IMP with functions

Verifying Java Programs Verifying Java Programs with KeY

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

Frama-C WP Tutorial. Virgile Prevosto, Nikolay Kosmatov and Julien Signoles. June 11 th, 2013

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

Assertions & Verification & Example Loop Invariants Example Exam Questions

ESC/Java2 extended static checking for Java Erik Poll Radboud University Nijmegen

CSC313 High Integrity Systems/CSCM13 Critical Systems. CSC313/CSCM13 Chapter 2 1/ 221

Formale Entwicklung objektorientierter Software

Lecture 5. Towards a Verifying Compiler: Multithreading

Some notes about Event-B and Rodin

FreePascal changes: user documentation

ELEC 377 C Programming Tutorial. ELEC Operating Systems

linaye/gl.html

Testing Library Specifications by Verifying Conformance Tests

Softwaretechnik. Program verification. Albert-Ludwigs-Universität Freiburg. June 28, Softwaretechnik June 28, / 24

FAKULTÄT FÜR INFORMATIK

Assertions & Verification Example Exam Questions

Software Security: Vulnerability Analysis

Frama-C A Collaborative Framework for C Code Verification

To be or not programmable Dimitri Papadimitriou, Bernard Sales Alcatel-Lucent April 2013 COPYRIGHT 2011 ALCATEL-LUCENT. ALL RIGHTS RESERVED.

The Contract Pattern. Design by contract

Lecture 5: Methods CS2301

Verifying Java Programs with KeY

An Operational and Axiomatic Semantics for Non-determinism and Sequence Points in C

Harvard School of Engineering and Applied Sciences CS 152: Programming Languages

Incremental Reasoning for Multiple Inheritance

CodeContracts & Clousot. Francesco Logozzo - Microsoft Mehdi Bouaziz ENS

ESC/Java extended static checking for Java Erik Poll, Joe Kiniry, David Cok University of Nijmegen; Eastman Kodak Company

CSI33 Data Structures

JML Class Specifications The Java Modeling Language (Part 2) A Java Class

Inspector Methods for State Abstraction

INITIALISING POINTER VARIABLES; DYNAMIC VARIABLES; OPERATIONS ON POINTERS

5 Exercise Formal Specification winter term 2010/11

Formal Methods for Java

CSE 307: Principles of Programming Languages

Transcription:

Tutorials on Spec# Carl Leonardsson 2/11-2011

So far in the course: We have been looking at Hoare Logic. Specifying contracts: {Pre}Program{Post} Manually computing proof-obligations Manually proving proof obligations Showing partial/total correctness Now: Enter Spec# Integration with actual compilable program code Automation of computation of proof-obligations Automation of proof

So far in the course: We have been looking at Hoare Logic. Specifying contracts: {Pre}Program{Post} Manually computing proof-obligations Manually proving proof obligations Showing partial/total correctness Now: Enter Spec# Integration with actual compilable program code Automation of computation of proof-obligations Automation of proof

1 Outline 2 3 4 RiSE4fun Visual Studio Commandline

What is Spec#? Language for software development with formal verification Extension of C# Developed by Microsoft Research Components Compiler (ssc) Verifier (boogie) Visual Studio plugin

What is Spec#? Language for software development with formal verification Extension of C# Developed by Microsoft Research Components Compiler (ssc) Verifier (boogie) Visual Studio plugin

What does Spec# provide? (Pre- and post conditions for functions.) Verified asserts Verified bounds checks for array indexing Verified freedom of null-derefencing

What does Spec# provide? (Pre- and post conditions for functions.) Verified asserts Verified bounds checks for array indexing Verified freedom of null-derefencing

What does Spec# provide? (Pre- and post conditions for functions.) Verified asserts Verified bounds checks for array indexing Verified freedom of null-derefencing

What does Spec# provide? (Pre- and post conditions for functions.) Verified asserts Verified bounds checks for array indexing Verified freedom of null-derefencing

What does Spec# provide? (Pre- and post conditions for functions.) Verified asserts Verified bounds checks for array indexing Verified freedom of null-derefencing

What does Spec# provide? Also... Not covered in these tutorials (and not in the labs): Class invariants for object oriented programming Support for ownership hierarchies

Specifying pre- and post conditions {Pre}Program{Post} Spec# example static int increase(int x, int delta) { return x + delta; }

Specifying pre- and post conditions {Pre}Program{Post} Spec# example static int increase(int x, int delta) ensures result > x; { return x + delta; }

Specifying pre- and post conditions {Pre}Program{Post} Spec# example static int increase(int x, int delta) requires delta > 0; ensures result > x; { return x + delta; }

requires e; Specifies pre condition e is a C# boolean expression over variables in scope May also contain quantifiers (more later) ensures e; Specifies post condition e is a C# boolean expression over variables in scope Use special variable result for return value

Functions with side-effects Example static void dbl(int[] a) { a[0] = a[0]*2; }

Functions with side-effects Example static void dbl(int[] a) ensures a[0] == 2*old(a[0]); { a[0] = a[0]*2; }

Functions with side-effects Example static void dbl(int[] a) requires a.length > 0; modifies a[*]; ensures a[0] == 2*old(a[0]); { a[0] = a[0]*2; }

old(v) Refers to the value of variable v at the start of the function call. Can be used only in ensures clause. modifies v; Functions may not modify values which are not either class members or specified in the modifies clause. Framing Spec# will produce proof obligations for array bounds checking

References in Spec# can be declared Null or Non-null. string! s; Non-null string string? s; String which may be a null reference string![]? a; a is either null or a string array All elements in the array are non-null. Dereferencing a possibly null reference requires a proof that it is in fact not null. Assigning a possibly null reference r to a non-null reference requires proof that r null.

References in Spec# can be declared Null or Non-null. string! s; Non-null string string? s; String which may be a null reference string![]? a; a is either null or a string array All elements in the array are non-null. Dereferencing a possibly null reference requires a proof that it is in fact not null. Assigning a possibly null reference r to a non-null reference requires proof that r null.

References in Spec# can be declared Null or Non-null. string! s; Non-null string string? s; String which may be a null reference string![]? a; a is either null or a string array All elements in the array are non-null. Dereferencing a possibly null reference requires a proof that it is in fact not null. Assigning a possibly null reference r to a non-null reference requires proof that r null.

References in Spec# can be declared Null or Non-null. string! s; Non-null string string? s; String which may be a null reference string![]? a; a is either null or a string array All elements in the array are non-null. Dereferencing a possibly null reference requires a proof that it is in fact not null. Assigning a possibly null reference r to a non-null reference requires proof that r null.

Example static void dbl(int[]? a) requires a.length > 0; modifies a[*]; ensures a[0] == 2*old(a[0]); { a[0] = a[0]*2; }

Example static void dbl(int[]? a) /* Note order of require clauses */ requires a!= null; requires a.length > 0; modifies a[*]; ensures a[0] == 2*old(a[0]); { a[0] = a[0]*2; }

Example assert e - assert a boolean expression Verified at compile time! Usage: Making sure that the code does what it should Use during development to understand your code Give hints to the automatic verifier static int foo(int x, int y) requires x >= 2 && y >= x-1; { assert y*2 >= x; return y*2; }

Example assert e - assert a boolean expression Verified at compile time! Usage: Making sure that the code does what it should Use during development to understand your code Give hints to the automatic verifier static int foo(int x, int y) requires x >= 2 && y >= x-1; { assert y*2 >= x; return y*2; }

Example assert e - assert a boolean expression Verified at compile time! Usage: Making sure that the code does what it should Use during development to understand your code Give hints to the automatic verifier static int foo(int x, int y) requires x >= 2 && y >= x-1; { assert y*2 >= x; return y*2; }

Example assert e - assert a boolean expression Verified at compile time! Usage: Making sure that the code does what it should Use during development to understand your code Give hints to the automatic verifier static int foo(int x, int y) requires x >= 2 && y >= x-1; { assert y*2 >= x; return y*2; }

Example assert e - assert a boolean expression Verified at compile time! Usage: Making sure that the code does what it should Use during development to understand your code Give hints to the automatic verifier static int foo(int x, int y) requires x >= 2 && y >= x-1; { assert y*2 >= x; return y*2; }

assume e - assume a boolean expression Assumed true by the verifier - Not verified Usage: Introducing lemmas which can not be automatically proven Figuring out what is necessary to get a proof to go through For the labs... Assume statements are useful, but dangerous since they allow verification of completely erroneous programs. In the labs you may use assume statements (sparingly, as necessary) for the last exercise. But not for the other exercises.

assume e - assume a boolean expression Assumed true by the verifier - Not verified Usage: Introducing lemmas which can not be automatically proven Figuring out what is necessary to get a proof to go through For the labs... Assume statements are useful, but dangerous since they allow verification of completely erroneous programs. In the labs you may use assume statements (sparingly, as necessary) for the last exercise. But not for the other exercises.

assume e - assume a boolean expression Assumed true by the verifier - Not verified Usage: Introducing lemmas which can not be automatically proven Figuring out what is necessary to get a proof to go through For the labs... Assume statements are useful, but dangerous since they allow verification of completely erroneous programs. In the labs you may use assume statements (sparingly, as necessary) for the last exercise. But not for the other exercises.

assume e - assume a boolean expression Assumed true by the verifier - Not verified Usage: Introducing lemmas which can not be automatically proven Figuring out what is necessary to get a proof to go through For the labs... Assume statements are useful, but dangerous since they allow verification of completely erroneous programs. In the labs you may use assume statements (sparingly, as necessary) for the last exercise. But not for the other exercises.

In specifications we can use quantifiers forall{int i in (a:b); bexpr} i.a i < b bexpr exists{int i in (a:b); bexpr} i.a i < b bexpr sum{int i in (a:b); iexpr} a i<b iexpr product{int i in (a:b); iexpr} a i<b iexpr min{int i in (a:b); iexpr} min{iexpr a i < b} max{int i in (a:b); iexpr} max{iexpr a i < b} count{int i in (a:b); bexpr} {i a i < b bexpr}

In loops, we may specify invariants Invariants need to be proven to hold initially... and proven to be maintained by the loop body. Simple invariants and framing can be automatically inferred But don t count on it...

In loops, we may specify invariants Invariants need to be proven to hold initially... and proven to be maintained by the loop body. Simple invariants and framing can be automatically inferred But don t count on it...

In loops, we may specify invariants Invariants need to be proven to hold initially... and proven to be maintained by the loop body. Simple invariants and framing can be automatically inferred But don t count on it...

Example static int asum(int[] a) ensures result == sum{int i in (0:a.Length); a[i]}; { int i = 0; int s = 0; while(i < a.length) { s += a[i]; i++; } return s; }

Example static int asum(int[] a) ensures result == sum{int i in (0:a.Length); a[i]}; { int i = 0; int s = 0; while(i < a.length) invariant s == sum{int j in (0:i); a[j]}; { s += a[i]; i++; } return s; }

Example static int asum(int[] a) ensures result == sum{int i in (0:a.Length); a[i]}; { int i = 0; int s = 0; while(i < a.length) invariant 0 <= i && i <= a.length; invariant s == sum{int j in (0:i); a[j]}; { s += a[i]; i++; } return s; }

Proof of Total Correctness is not natively supported by Spec# We want variants! How to hack a proof of total correctness: 1 Declare a loop-local program variable variant 2 Initialise variant = expr; at the start of the loop body 3 At the end of loop body assert assert variant >= 0; and 4 assert expr < variant; for the same expr as above.

Proof of Total Correctness is not natively supported by Spec# We want variants! How to hack a proof of total correctness: 1 Declare a loop-local program variable variant 2 Initialise variant = expr; at the start of the loop body 3 At the end of loop body assert assert variant >= 0; and 4 assert expr < variant; for the same expr as above.

Proof of Total Correctness is not natively supported by Spec# We want variants! How to hack a proof of total correctness: 1 Declare a loop-local program variable variant 2 Initialise variant = expr; at the start of the loop body 3 At the end of loop body assert assert variant >= 0; and 4 assert expr < variant; for the same expr as above.

Example while(i < a.length) invariant 0 <= i && i <= a.length; invariant s == sum{int j in (0:i); a[j]}; { int variant = a.length - i; s += a[i]; i++; assert variant >= 0; assert a.length - i < variant; }

Can we use any boolean expression in specifications? No. Needs to be side-effect free. To be able to use your own functions in specifications they need to be declared [Pure]. Requirements for [Pure]: Example Side-effect free Well-defined (terminating) [Pure] int avg(int a, int b) ensures result == (a+b)/2; { return (a + b) / 2; }

Can we use any boolean expression in specifications? No. Needs to be side-effect free. To be able to use your own functions in specifications they need to be declared [Pure]. Requirements for [Pure]: Example Side-effect free Well-defined (terminating) [Pure] int avg(int a, int b) ensures result == (a+b)/2; { return (a + b) / 2; }

Can we use any boolean expression in specifications? No. Needs to be side-effect free. To be able to use your own functions in specifications they need to be declared [Pure]. Requirements for [Pure]: Example Side-effect free Well-defined (terminating) [Pure] int avg(int a, int b) ensures result == (a+b)/2; { return (a + b) / 2; }

Can we use any boolean expression in specifications? No. Needs to be side-effect free. To be able to use your own functions in specifications they need to be declared [Pure]. Requirements for [Pure]: Example Side-effect free Well-defined (terminating) [Pure] int avg(int a, int b) ensures result == (a+b)/2; { return (a + b) / 2; }

Can we use any boolean expression in specifications? No. Needs to be side-effect free. To be able to use your own functions in specifications they need to be declared [Pure]. Requirements for [Pure]: Example Side-effect free Well-defined (terminating) [Pure] int avg(int a, int b) ensures result == (a+b)/2; { return (a + b) / 2; }

Summary of commands requires ensures modifies old result assert assume invariant count{int i in (a:b); bexpr} sum{int i in (a:b); iexpr} product{int i in (a:b); iexpr} min{int i in (a:b); iexpr} max{int i in (a:b); iexpr} exists{int i in (a:b); bexpr} forall{int i in (a:b); bexpr} Pure

RiSE4fun Visual Studio Commandline RiSE4fun An easy way of trying out Spec# is the web interface at http://rise4fun.com/specsharp.

RiSE4fun Visual Studio Commandline Create a Spec# project by File New Project Spec# Projects Console Application Important: In the project properties Configuration Properties, make sure to enable RunProgramVerifier and Treat Warnings as Errors. The Spec# plugin for Visual Studio is a bit unstable, so if unsure about the results double-check using the commandline method described next.

RiSE4fun Visual Studio Commandline Need to set Path environment variable. (See lab instructions.) Compile by Verify by > ssc /t:library /debug file.ssc > boogie file.dll