ECI 2007: Specification and Verification of Object-Oriented Programs Lecture 3 Courtesy: K. Rustan M. Leino and Wolfram Schulte.

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

Verification of object-oriented programs with invariants Mike Barnett, Robert DeLine, Manuel Fahndrich, K. Rustan M. Leino, Wolfram Schulte Formal techniques.
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.
Checking correctness properties of object-oriented programs K. Rustan M. Leino Microsoft Research, Redmond, WA Lecture 4 EEF summer school on Specification,
Object Invariants in Specification and Verification K. Rustan M. Leino Microsoft Research, Redmond, WA Joint work with: Mike Barnett, Ádám Darvas, Manuel.
Writing specifications for object-oriented programs K. Rustan M. Leino Microsoft Research, Redmond, WA, USA 21 Jan 2005 Invited talk, AIOOL 2005 Paris,
1 Towards a Verifying Compiler: The Spec# Approach Wolfram Schulte Microsoft Research Formal Methods 2006 Joint work with Rustan Leino, Mike Barnett, Manuel.
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.
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.
Verifying Executable Object-Oriented Specifications with Separation Logic Stephan van Staden, Cristiano Calcagno, Bertrand Meyer.
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.
Data Abstraction II SWE 619 Software Construction Last Modified, Spring 2009 Paul Ammann.
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,
Introduction to Spec# Programming System Yossi Peery Advanced SW Tools Seminar TAU Nov 2006.
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.
Modular Specification of Frame Properties in JML Peter Müller Arnd Poetszch Heffter Gary T. Leavens Encapsulation Seminar Tel Aviv University 2005 Presented.
Building a program verifier K. Rustan M. Leino Microsoft Research, Redmond, WA 10 May 2006 Guest lecture, Shaz Qadeer’s cse599f, Formal Verification of.
Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 18 Program Correctness To treat programming.
K. Rustan M. Leino Microsoft Research, Redmond, WA, USA with Mike Barnett, Robert DeLine, Manuel Fahndrich, and Wolfram Schulte Toward enforceable contracts.
Going beyond a basic ownership system in Spec# K. Rustan M. Leino Microsoft Research, Redmond, WA Joint work with: Peter Müller Angela Wallenburg ESF workshop.
Data Abstraction and Object- Oriented Programming CS351 – Programming Paradigms.
Declaring and Checking Non-null Types in an Object-Oriented Language Authors: Manuel Fahndrich K. Rustan M. Leino OOPSLA’03 Presenter: Alexander Landau.
Using data groups to specify and check side effects K. Rustan M. Leino Microsoft Research Arnd Poetzsch-Heffter Universität Kaiserslautern Yunhong Zhou.
Spec# K. Rustan M. Leino Senior Researcher Programming Languages and Methods Microsoft Corporation Joint work with: Mike Barnett, Robert DeLine, Manuel.
Crowfoot: a verifier for higher order store programs Billiejoe (Nathaniel) Charlton Ben Horsfall Bernhard Reus University of Sussex VMCAI 2012.
“is a”  Define a new class DerivedClass which extends BaseClass class BaseClass { // class contents } class DerivedClass : BaseClass { // class.
Software Engineering Prof. Dr. Bertrand Meyer March 2007 – June 2007 Chair of Software Engineering Static program checking and verification Slides: Based.
1 Abstraction  Identify important aspects and ignore the details  Permeates software development programming languages are abstractions built on hardware.
Cs205: engineering software university of virginia fall 2006 Data Abstraction David Evans
Model Programs for Preserving Composite Invariants SAVCBS 2008 Challenge Problem Solution by Steve Shaner, Hridesh Rajan, Gary T. Leavens Iowa State and.
K. Rustan M. Leino RiSE, Microsoft Research 1 Dec 2008 Invited talk, working group meeting COST Action IC0701, Formal Verification of Object-Oriented Software.
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.
A Universe-Type-Based Verification Technique for Mutable Static Fields and Methods Alexander J Summers Sophia Drossopoulou Imperial College London Peter.
CSC 480 Software Engineering Design by Contract. Detail Design Road Map Begin with architectural models  Class model: domain classes  Overall state.
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 and Wolfram Schulte Microsoft Research, Redmond ESOP 2007 Braga, Portugal 28 March 2007.
Static Techniques for V&V. Hierarchy of V&V techniques Static Analysis V&V Dynamic Techniques Model Checking Simulation Symbolic Execution Testing Informal.
CSSE501 Object-Oriented Development. Chapter 10: Subclasses and Subtypes  In this chapter we will explore the relationships between the two concepts.
FEN 2014UCN Teknologi/act2learn1 Object-Oriented Programming “ The Three Pillars of OOP”: Encapsulation Inheritance Polymorphism The Substitution Principle.
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.
DBC NOTES. Design By Contract l A contract carries mutual obligations and benefits. l The client should only call a routine when the routine’s pre-condition.
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,
Object Invariants in Dynamic Contexts K.R.M. Leino and P. Muller : Objects and Aspects Presented by Jonathan Aldrich.
Specification techniques for verifying object-oriented software
Class-local object invariants
Spec# Writing and checking contracts in a .NET language
Lecture 5 Floyd-Hoare Style Verification
Hoare-style program verification
Method Verification CS/SWE 332 Paul Ammann.
Program correctness Axiomatic semantics
A Considerate Specification of the Composite Pattern
Method Verification Paul Ammann.
Presentation transcript:

ECI 2007: Specification and Verification of Object-Oriented Programs Lecture 3 Courtesy: K. Rustan M. Leino and Wolfram Schulte

2 Pre- and Postconditions are not Enough Contracts can break abstraction class C{ private int a, z; public void M() requires a!=0; {z = 100/a;} } We need invariants class C{ private int a, z; invariant a!=0; public void M() {z = 100/a;} }

3 Dealing with Invariants Basic Methodology Object-based Ownership Object-oriented Ownership Visibility based Ownership

4 Problem: Reentrancy How can we prevent that current object is re-entered in an inconsistent state? class Meeting { int day; int time; invariant 0 ≤ day<7 ∧ day==6  1200<= time; void Reschedule(int d ) requires 0 ≤ d < 7; { day = d; X.P(this); if ( day==6 ) time = 1200; } }

5 Program Model for Object Invariants Objects can be valid or mutable –inv  { valid, mutable } is a new ghost field in each object Mutable objects need not satisfy their invariants o.inv indicates whether the invariant of o, Inv(o), may be broken  o: o.inv  mutable  Inv(o) Remark: Quantifier ranges over allocated, non-null objects

6 Field Updates Only fields of mutable objects can be updated Tr[[o.f = e]] = assert o  null ∧ o.inv=mutable; o.f := e

7 Pack and Unpack inv is changed by special source commands unpack(o) to make o mutable pack(o) to re-establish invariant and make o valid Tr[[unpack o]] = assert o.inv = valid; o.inv := mutable Tr[[pack o]] = assert o.inv = mutable; assert Inv(o); o.inv := valid

8 Pack and Unpack Example void Reschedule( int d ) requires inv==valid  0  d<7; { unpack(this); day = d; if ( day==6 ) time = 1200; pack(this); } void Reschedule( int d ) requires inv==valid  0  d<7; { expose(this){ day = d; if ( day==6 ) time = 1200; } Spec# uses expose, defined by expose(o) s; = unpack o; s; pack o; :Meeting x.P(this); Valid Mutable

9 Program Invariant Theorem (Soundness) ∀o: o.inv  mutable  Inv( o ) Admissible invariants contain only field accesses of the form this.f Proof sketch –new: new object is initially mutable –o.f := E; can only affect invariant of o, asserts o.inv = mutable –unpack(o): changes o.inv to mutable –pack(o): asserts Inv(o)

10 Dealing with Invariants Basic Methodology Object-based Ownership Object-oriented Ownership Visibility based Ownership

11 Problem: Object Structures Can we have relax the admissibility condition? How can we find out that Reschedule might break Person’s invariant? class Person { int freeDay; Meeting next; invariant this.next != null  this.next.day != freeDay; } class Meeting { int day; invariant 0 ≤ day<7; void Reschedule(int d ) requires inv==valid; { expose(this){ day = d; } } }

12 Invariants in the Presence of Aliasing? : Meeting : Person next next.day != freeDay : Foo bar bar.day >= 5 call reschedule(4) call re- schedule(4) owner Valid Mutable Committed inv =

13 Ownership-Based Invariants : Meeting : Person owner Establish hierarchy (ownership) on objects Ownership rule: When an object is mutable, so are its (transitive) owners An object o may only depend on –the fields of o and –the fields of objects (transitively) owned by o

14 Dynamic Ownership Each object has a special ghost field, owner, that points to its owner object rep(resentation) declarations lead to implicit owner invariants inv  {committed, valid, mutable} An object is committed, if –its invariant is known to hold –the owner is not mutable class Person { int freeDay; Meeting next; /*implicit*/ invariant next  null  next.owner = this; … } rep

15 Pack and Unpack with Ownership Tr[[unpack o]] = assert o.inv = valid; o.inv := mutable; foreach (c  c.owner = o) { c.inv := valid; } Tr[[ pack o]] = assert o.inv = mutable; assert  c: c.owner = o  c.inv = valid; foreach (c  c.owner = o) { c.inv := committed; } assert Inv( o ); o.inv := valid unpack(o) and pack(o) and change inv for o and o's rep objects

16 Program Invariant with Ownership Theorem (Soundness)  o: o.inv  mutable  Inv(o)  (  c: c.owner = o  c.inv = committed)) Admissible invariants contain only field accesses of the form this.f 1. ….f n where f 1 ….f n-1 must be rep fields

17 Method Framing Revisited Allow methods to modify also committed objects Example. Given class A{ rep B b;} class B{ rep C c;} the method static void m(A a) requires a.inv == valid; modifies a.*; is allowed to modify … the fields of a.b and a.b.c This addresses the transitivity problem of modifies clauses

18 Method Framing Revisited Allow methods to modify also committed objects The Post condition for a Spec# modifies W clause Tr [[W]] = (∀o : ref, f: field :: old(Heap[o,allocated])  (o,f)  old(W)  old(Heap[o,f]) = Heap[o,f])  old(Heap[o,inv]) = committed

19 Example Revisited class Person { int freeDay; rep Meeting next; invariant next  null  next.day  freeDay; int doTravel(int td) requires inv==valid; modifies this.*; { expose(this) { freeDay = td; if (next!=null) { next.reschedule((td+1)%7); }; } class Meeting { int day; void reschedule( int d ) requires inv==valid; { expose(this) day = d; } } : Meeting : Person owned by : Person : Meeting : Person : Meeting

20 Dealing with Invariants Basic Methodology Object-based Ownership Object-oriented Ownership Visibility based Ownership

21 Inheritance Each subtype defines one frame with its variables Single inheritance results in a sequence of frames Example class Cell { int x; invariant x>=0;...} class BackupCell: Cell { rep History h; invariant h.last>=x; Objects of type Cell have 2 frames: [Cell, object] type B have 3 frames: [BackupCell, Cell, object] Subtypes are allowed to strengthen invariants invariant x>10;

22 Refined Representation Idea: Reduce inheritance to ownership of frames. If B is a direct subtype of A, then B has a rep field A. But we only have one inv field, so: o.inv now represents the most derived frame for o which is valid, i.e. –o.inv <: T means Inv(o) holds for the frame T and all its super frames –o.inv == typeof(o) means Inv(o) holds for all of o’s frames o.owner is now a pair (p,T); –p is the owner, –T the frame that contains the rep field that points to o.

23 Refined Representation owned by the BackupCell frame : Cell (frame) inv : BackupCell (frame): History (frame) packed as BackupCell Commiting c to p means then c.commited  let (p,T) = c.owner in p.inv <: T packed as Cell Valid Mutable Committed

24 Refined Representation rep fields f in class T give rise to implicit invariants invariant this.f!= null  let (p,T’) = this.f.owner in p== this ∧ T = T’

25 Pack and Unpack with Inheritance Tr[[unpack(o from T)]] = assert o.inv = T; assert !o.committed; o.inv := S foreach (c  c.owner==(o,T)) { c.committed := false } Tr[[ pack o as T]] = assert o.inv = S; assert Inv T ( o ); assert  c: c.owner==(o,T)  c.inv = typeof(r); foreach (c  c.owner==(o,T)) { c.committed := true } o.inv := T Given class T:S and o of type T. Then pack(o as T) claims every object that has o as its owner and its rep field declared in T

26 Inheritance Precondition Problem virtual void Cell.Set(int x) requires … modifies this.*; { unpack(this from Cell); this.x = x; pack(this to Cell); } override void BackupCell.Set(int x) //requires … { unpack(this from BackupCell); this.b = this.x; base.Set(x); pack(this to BackupCell); } void M(Cell c) { c.Set(23); } How can we verify the dynamically dispatched c.Set call?

27 Dynamic Dispatch and Preconditions For virtual methods m, we allow to write the pre: this.inv == 1 For each frame in which m is defined we generate 2 procs m.C is used for statically bound calls; its pre: Heap[o,inv] = C m.C.Virtual is used for dynamically dispatched calls, its pre: Heap[o,inv] = typeof(o) Only m.C contains the translated code

28 Inheritance Precondition Example virtual void Cell.Set(int x) 1 requires this.inv == 1; modifies this.*; { unpack(this from Cell); this.x = x; pack(this to Cell); } void M(Cell c) requires c.inv == typeof(Cell); { c.Set(23); }

29 Dealing with Invariants Basic Methodology Object-based Ownership Object-oriented Ownership Visibility based Ownership

30 Rep & Peer Objects Rep fields are used to build hierarchical abstractions. Peer fields are used to group sibling objects.

31 Rep and Peer Objects class List { rep Node! head; invariant (  Node n | n.owner=this  next!=null  next.prev = this  … } class Node{ peer Node next, prev; } class List { rep Node! head; } class Node{ peer Node next, prev; invariant next!=null  next.prev = this  … } Peer fields give rise to additional invariants, for Nodes e.g. next!=null  owner = next.owner  prev!=null  owner = prev.owner But how can we reason locally?

32 Mutually Recursive Object Structures Objects are mutually dependent Objects cannot own each other class Person { Person spouse; invariant this.spouse  null  this.spouse.spouse = this; … } : Person spouse: : Person spouse:

33 Admissible Visibility-Based Invariant The invariant declared in class C may contain a field access R.f iff –R is “this” or –R is “this.g 1. ….g n.” and g 1..g n are rep or peer fields, and C is mentioned in the dependent clause of f class Person { Person spouse dependent Person; invariant this.spouse  null  this.spouse.spouse = this; … }

34 Proof Obligation for Field Updates For a field update o.f := E; we have to prove –That o is non-null and mutable (as before) –That all other objects whose invariant depends on o.f are mutable Tr[[o.spouse := E; ]] assert o  null  o.inv = mutable; assert  Person t: t.spouse = o  t.inv = mutable; The other objects are determined by inspecting the invariants of the all “friend” classes mentioned in the dependent clause (see next slide)

35 Marriage is Never Easy… class Person { Person spouse dependent Person; invariant this.spouse  null  this.spouse.spouse = this; void marry( Person p ) requires p  null  p  this  this.inv = valid  p.inv = valid  this.spouse = null  p.spouse = null ; { expose(this) expose(p) { this.spouse := p; p.spouse := this; } this.inv = mutable  this.spouse = null p.inv = mutable  p.spouse = null this.spouse.spouse=this  p.spouse.spouse=p

36 Summary: Object Invariants The methodology solves the problems of –Re-entrance (through the explicit inv field) –Object structures (through ownership or visibility) It can handle –Complex object structures including (mutual) recursion –Ownership transfer (not shown in this talk) The methodology is modular and sound Aliasing is not restricted