Writing specifications for object-oriented programs K. Rustan M. Leino Microsoft Research, Redmond, WA, USA 21 Jan 2005 Invited talk, AIOOL 2005 Paris,

Slides:



Advertisements
Similar presentations
1 Lecture 5 Towards a Verifying Compiler: Multithreading Wolfram Schulte Microsoft Research Formal Methods 2006 Race Conditions, Locks, Deadlocks, Invariants,
Advertisements

Hoare-style program verification K. Rustan M. Leino Guest lecturer Rob DeLines CSE 503, Software Engineering University of Washington 26 Apr 2004.
Verification of object-oriented programs with invariants Mike Barnett, Robert DeLine, Manuel Fahndrich, K. Rustan M. Leino, Wolfram Schulte Formal techniques.
Advanced programming tools at Microsoft
Joint work with Mike Barnett, Robert DeLine, Manuel Fahndrich, and Wolfram Schulte Verifying invariants in object-oriented programs K. Rustan M. Leino.
Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 18 Program Correctness To treat programming.
The Spec# programming system K. Rustan M. Leino Microsoft Research, Redmond, WA, USA Lunch seminar, Praxis Bath, UK 6 Dec 2005 joint work with Mike Barnett,
Bor-Yuh Evan Chang Daan Leijen Peter Müller David A. Naumann The Spec# programming system Mike Barnett Rob DeLine Manuel Fähndrich Bart Jacobs K. Rustan.
Demand-driven inference of loop invariants in a theorem prover
Object Invariants in Specification and Verification K. Rustan M. Leino Microsoft Research, Redmond, WA Joint work with: Mike Barnett, Ádám Darvas, Manuel.
1 Towards a Verifying Compiler: The Spec# Approach Wolfram Schulte Microsoft Research Formal Methods 2006 Joint work with Rustan Leino, Mike Barnett, Manuel.
Program Verification Using the Spec# Programming System ETAPS Tutorial K. Rustan M. Leino, Microsoft Research, Redmond Rosemary Monahan, NUIM Maynooth.
Technologies for finding errors in object-oriented software K. Rustan M. Leino Microsoft Research, Redmond, WA Lecture 3 Summer school on Formal Models.
Technologies for finding errors in object-oriented software K. Rustan M. Leino Microsoft Research, Redmond, WA Lecture 1 Summer school on Formal Models.
Technologies for finding errors in object-oriented software K. Rustan M. Leino Microsoft Research, Redmond, WA Lecture 0 Summer school on Formal Models.
Spec# K. Rustan M. Leino Senior Researcher Programming Languages and Methods Microsoft Research, Redmond, WA, USA Microsoft Research faculty summit, Redmond,
Automated Theorem Proving Lecture 1. Program verification is undecidable! Given program P and specification S, does P satisfy S?
Lecture 4 Towards a Verifying Compiler: Data Abstraction Wolfram Schulte Microsoft Research Formal Methods 2006 Purity, Model fields, Inconsistency _____________.
Challenges in increasing tool support for programming K. Rustan M. Leino Microsoft Research, Redmond, WA, USA 23 Sep 2004 ICTAC Guiyang, Guizhou, PRC joint.
Rigorous Software Development CSCI-GA Instructor: Thomas Wies Spring 2012 Lecture 8.
Modular and Verified Automatic Program Repair Francesco Logozzo, Thomas Ball RiSE - Microsoft Research Redmond.
K. Rustan M. Leino Research in Software Engineering (RiSE) Microsoft Research, Redmond, WA, USA 15 January 2009 Séminaire Digiteo Orsay, France.
An Abstract Interpretation Framework for Refactoring P. Cousot, NYU, ENS, CNRS, INRIA R. Cousot, ENS, CNRS, INRIA F. Logozzo, M. Barnett, Microsoft Research.
The Spec# programming system K. Rustan M. Leino Microsoft Research, Redmond, WA, USA Distinguished Lecture Series Max Planck Institute for Software Systems.
Automated Software Verification with a Permission-Based Logic 20 th June 2014, Zürich Malte Schwerhoff, ETH Zürich.
Verification of Multithreaded Object- Oriented Programs with Invariants Bart Jacobs, K. Rustan M. Leino, Wolfram Schulte.
A simple sequential reasoning approach for sound modular verification of mainstream multithreaded programs Wolfram Schulte & Bart Jacobs Microsoft Research.
K. Rustan M. Leino Research in Software Engineering (RiSE) Microsoft Research, Redmond, WA, USA Invited talk Informatics Education in Europe (IEE III’08)
K. Rustan M. Leino Research in Software Engineering (RiSE) Microsoft Research, Redmond, WA, USA 3 December 2008 U. Lugano Lugano, Switzerland.
Inferring Object Invariants Bor-Yuh Evan ChangK. Rustan M. Leino University of California, BerkeleyMicrosoft Research January 21, 2005 AIOOL 2005 Paris,
Formal Methods of Systems Specification Logical Specification of Hard- and Software Prof. Dr. Holger Schlingloff Institut für Informatik der Humboldt.
ECI 2007: Specification and Verification of Object-Oriented Programs Lecture 2 Courtesy: K. Rustan M. Leino and Wolfram Schulte.
Lecture 2 Towards a Verifying Compiler: Logic of Object oriented Programs Wolfram Schulte Microsoft Research Formal Methods 2006 Objects, references, heaps,
Software Testing and Quality Assurance
Using and Building an Automatic Program Verifier K. Rustan M. Leino Research in Software Engineering (RiSE) Microsoft Research, Redmond Lecture 1 LASER.
ECI 2007: Specification and Verification of Object-Oriented Programs Lecture 3 Courtesy: K. Rustan M. Leino and Wolfram Schulte.
Building a program verifier K. Rustan M. Leino Microsoft Research, Redmond, WA 10 May 2006 Guest lecture, Shaz Qadeer’s cse599f, Formal Verification of.
K. Rustan M. Leino Microsoft Research, Redmond NUI Maynooth Maynooth, Ireland 8 June 2007.
K. Rustan M. Leino Microsoft Research, Redmond, WA, USA with Mike Barnett, Robert DeLine, Manuel Fahndrich, and Wolfram Schulte Toward enforceable contracts.
Well-cooked Spaghetti: Weakest-Precondition of Unstructured Programs Mike Barnett and Rustan Leino Microsoft Research Redmond, WA, USA.
K. Rustan M. Leino Research in Software Engineering (RiSE) Microsoft Research, Redmond Caltech Pasadena, CA 12 November 2009.
Spec# K. Rustan M. Leino Senior Researcher Programming Languages and Methods Microsoft Corporation Joint work with: Mike Barnett, Robert DeLine, Manuel.
K. Rustan M. Leino RiSE, Microsoft Research, Redmond joint work with Peter Müller and Jan Smans Lecture 0 1 September 2009 FOSAD 2009, Bertinoro, Italy.
Computer Science 340 Software Design & Testing Design By Contract.
Ranga Rodrigo. Class is central to object oriented programming.
Software Engineering Prof. Dr. Bertrand Meyer March 2007 – June 2007 Chair of Software Engineering Static program checking and verification Slides: Based.
Using and Building an Automatic Program Verifier K. Rustan M. Leino Research in Software Engineering (RiSE) Microsoft Research, Redmond Lecture 3 Marktoberdorf.
Reasoning about object structures with Spec# K. Rustan M. Leino Microsoft Research, Redmond, WA Joint work with: Mike Barnett, Ádám Darvas, Manuel Fähndrich,
Program Verification K. Rustan M. Leino Research in Software Engineering (RiSE) Microsoft Research, Redmond University of Washington CSE P January.
Verification of Programs with Inspector Methods Bart Jacobs and Frank Piessens Dept. CS, K.U.Leuven, Belgium.
K. Rustan M. Leino Microsoft Research, Redmond, WA, USA with Mike Barnett, Robert DeLine, Manuel Fahndrich, and Wolfram Schulte Spec# Writing and checking.
Using and Building an Automatic Program Verifier K. Rustan M. Leino Research in Software Engineering (RiSE) Microsoft Research, Redmond Lecture 0 Marktoberdorf.
Spec# Andreas Vida. Motivation Correct and maintainable software Correct and maintainable software Cost effective software production Cost effective software.
K. Rustan M. Leino Principal Researcher Microsoft Research, Redmond, WA, USA 14 Nov 2007 Øredev Malmö, Sweden.
K. Rustan M. Leino and Wolfram Schulte Microsoft Research, Redmond ESOP 2007 Braga, Portugal 28 March 2007.
Specifying and verifying programs in Spec# K. Rustan M. Leino Microsoft Research, Redmond, WA, USA Invited talk, PSI 2006 Novosibirsk, Russia 27 June 2006.
SWE 4743 Abstract Data Types Richard Gesick. SWE Abstract Data Types Object-oriented design is based on the theory of abstract data types Domain.
PROGRAMMING PRE- AND POSTCONDITIONS, INVARIANTS AND METHOD CONTRACTS B MODULE 2: SOFTWARE SYSTEMS 13 NOVEMBER 2013.
K. Rustan M. Leino Microsoft Research, Redmond, WA, USA 15 Nov 2007 Chalmers Göteborg, Sweden.
Spec# John Lefor Program Manager Developer Division, Microsoft.
Extended Static Checking for Java Cormac Flanagan Joint work with: Rustan Leino, Mark Lillibridge, Greg Nelson, Jim Saxe, and Raymie Stata Compaq Systems.
1 Verification of object-oriented programs with invariants Mike Barnett, Robert DeLine, Manuel Fahndrich, K. Rustan M. Leino, Wolfram Schulte ECOOP 2003.
Reasoning about object structures with Spec# K. Rustan M. Leino Microsoft Research, Redmond, WA Joint work with: Mike Barnett, Ádám Darvas, Manuel Fähndrich,
Extended Static Checking for Java
Specification techniques for verifying object-oriented software
Class-local object invariants
Spec# Writing and checking contracts in a .NET language
Hoare-style program verification
Aditya Mangipudi Niharika Pamu Srikanth Polisetty
Assertions References: internet notes; Bertrand Meyer, Object-Oriented Software Construction; 4/25/2019.
Presentation transcript:

Writing specifications for object-oriented programs K. Rustan M. Leino Microsoft Research, Redmond, WA, USA 21 Jan 2005 Invited talk, AIOOL 2005 Paris, France joint work with Mike Barnett, Robert DeLine, Manuel Fähndrich, Wolfram Schulte, Herman Venter, Peter Müller, David A. Naumann, Bor-Yuh Evan Chang, Bart Jacobs, Xinming Ou, and Qi Sun

Software engineering problem Building and maintaining large systems that are correct

Using tools to program better Specifications record design decisions –bridge intent and code Tools amplify human effort –manage details –find inconsistencies –ensure quality

Spec# Experimental mix of contracts and tool support Aimed at real programmers, real programs Superset of C# –non-null types –enhanced exception support –pre- and postconditions –object invariants Tool support –more type checking –compiler-emitted run-time checks –static program verification

Spec# demo

Basic architecture of a program verifier verification condition generator theorem prover verification condition program with specifications correct or list of errors

Spec# program verifier architecture V.C. generator automatic theorem prover verification condition Spec# correct or list of errors Spec# compiler MSIL translator intermediate program representation abstract interpreter Spec# program verifier

Modular verification Dont assume we have the entire program –for example, we want to be able to verify a library without having access to its clients Verify a given portion of the program (e.g., a class) against its specifications, assuming that the rest of the program lives up to its specifications Analogous to separate compilation

Specification challenges Object invariants Frame conditions (modifies clauses) Class initialization and class variables Model fields Delegates …

Objects have invariants class C { private int x; private int y; public void M() { int t := 100 / (y – x); x := x + 1; P(t); y := y + 1; } … } division by zero

Pre/post are not enough class C { private int x; private int y; public void M() requires x < y; { int t := 100 / (y – x); x := x + 1; P(t); y := y + 1; } … }

Object invariants class C { private int x; private int y; invariant x < y; public void M() { int t := 100 / (y – x); x := x + 1; P(t); y := y + 1; } … }

When do object invariants hold? class C { private int x; private int y; invariant x < y; public C() { x := 0; y := 1; } public void M() { int t := 100 / (y – x); x := x + 1; P(t); y := y + 1; } … } invariant assumed to hold on entry to method invariant checked to hold on exit from method invariant checked to hold at end of constructor invariant may be temporarily broken here invariant is restored here what if P calls back into M?

Object state: valid vs. mutable introduce a special object field st : { Mutable, Valid } idea: program invariant ( o: C o.st = Mutable Inv C (o)) field updates are allowed only for mutable objects: a field-update statement o.f := E; has the precondition o.st = Mutable holds at every program point! for any o: C, we write Inv C (o) o.x < o.y class C { int x, y; invariant x < y;

pack/unpack statements st is changed by special statements pack and unpack pack o as C –check that Inv C (o) holds –then change o.st from Mutable to Valid unpack o from C –change o.st from Valid to Mutable

Example, with pack/unpack class C { int x, y; invariant x < y; public void M() { int t := 100 / (y – x); unpack this from C; x := x + 1; P(t); y := y + 1; pack this as C; } … } invariant checked to hold here invariant may be temporarily broken here

Specifying methods and constructors class C { int x, y; invariant x < y; public C() ensures this.st = Valid; { x := 0; y := 1; pack this as C; } public void M() requires this.st = Valid; { int t := 100 / (y – x); unpack this from C; x := x + 1; P(t); y := y + 1; pack this as C; } … } Note: if P calls back into M, then P must first make this valid in order to satisfy Ms precondition, so no dangerous reentrancy can occur

Summary, so far invariant … st : { Mutable, Valid } pack, unpack updates of o.f require o.st = Mutable Inv C (o) can mention only the fields of o ( o: C o.st = Mutable Inv C (o))

Aggregate objects class Set { Hashtable h; invariant …; public void Add(Element e) requires this.st = Valid; { unpack this from Set; h.Add(e, e); pack this as Set; } … } class Hashtable { invariant …; public void Add(object key, object val) requires this.st = Valid; … } how do we know h is valid here?

Aggregate objects class Set { Hashtable h; invariant …; public void Add(Element e) requires this.st = Valid; { unpack this from Set; h.Add(e, e); pack this as Set; } public Hashtable Leak() { return h; } } class Hashtable { invariant …; public void Add(object key, object val) requires this.st = Valid; … } how do we know h is valid here? Perhaps it isnt! void Violation(Set s) requires s.st = Valid; { Hashtable g := s.Leak(); unpack g from Hashtable; g.x := …; s.Add(…); }

Ownership class Set { owned Hashtable h; invariant …; public void Add(Element e) requires this.st = Valid; { unpack this from Set; h.Add(e, e); pack this as Set; } … } class Hashtable { invariant …; public void Add(object key, object val) requires this.st = Valid; … } For any s: Set, s uniquely owns s.h validity of s implies validity of s.h ownership of h temporarily relinquished here ownership of h re-obtained here

Object state: mutable, valid, committed st : { Mutable, Valid, Committed } program invariant ( o: C o.st = Mutable Inv C (o)) and for every owned field f ( o: C o.st = Mutable o.f.st = Committed) pack o as C –check that Inv C (o) holds, –check that o.f.st = Valid, –then change o.f.st from Valid to Committed –and change o.st from Mutable to Valid unpack o from C –change o.st from Valid to Mutable, and –change o.f.st from Committed to Valid Committed means valid and owned class C { owned T f; … for every owned field f

Object states: a picture of the heap Mutable Valid Committed s: Set Hashtable ownership h unpack s from Set

Object states: a picture of the heap Mutable Valid Committed s: Set Hashtable ownership h unpack s from Set

Object states: a picture of the heap Mutable Valid Committed s: Set Hashtable ownership h pack s as Set

Object states: a picture of the heap Mutable Valid Committed s: Set Hashtable ownership h pack s as Set

Summary of object invariants invariant … owned T f; st : { Mutable, Valid, Committed } pack, unpack updates of o.f require o.st = Mutable Inv C (o) can mention only the fields of o and the fields of owned fields –example: invariant this.n = this.h.Count; ( o: C o.st = Mutable Inv C (o)) ( o: C o.st = Mutable o.f.st = Committed)

Frame conditions To be useful to the caller, a postcondition must say what goes unchanged –ensures x = old(x) y = old(y) … A modifies clause says what is allowed to change, implicitly indicating what goes unchanged –modifies z

What do modifies clauses mean? modifies M; = modifies Heap; ensures ( o,f Heap[o,f] = old(Heap(o,f)) (o,f) old(M) ¬old(Heap[o,alloc])

Modifying underlying representation Mutable Valid Committed s: Set Hashtable ownership h

Fields of committed objects may change modifies M; = modifies Heap; ensures ( o,f Heap[o,f] = old(Heap(o,f)) (o,f) old(M) ¬old(Heap[o,alloc]) old(Heap[o,Committed]))

So: common modifies clause modifies this.*;

Quirk? void M(T p) requires p.st = Committed p.x = 12; { int y := Math.Sqrt(…); assert p.x = 12; } assertion failure

Default specifications I have showed the building blocks A programming language would use defaults, for example: –a method has precondition o.st=Valid for every parameter o –public method bodies start with unpack and end with pack –a field is owned unless declared as shared

Further research challenges Extend specification methodology to more complicated programming idioms Develop abstract interpretations that work with object-oriented specifications Combine abstract interpretation with theorem proving Use programming system among developers

Conclusions Because of tool support, were ready for programming at the next level of rigor Rigor can be enforced by type checking, by run-time checking, and by static verification Specifications give programmers a way to record their design decisions Methodology is underdeveloped –Can programming theory yet fully explain why real big programs work? –programming theory has not kept up with practice