36
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

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

Embed Size (px)

Citation preview

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

Writing specifications for object-oriented programsK. Rustan M. LeinoMicrosoft Research, Redmond, WA, USA

21 Jan 2005Invited talk, AIOOL 2005Paris, France

joint work withMike 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

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

Software engineering problem

Building and maintaining large systems that are correct

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

Using tools to program better• Specifications record design decisions

– bridge intent and code

• Tools amplify human effort– manage details– find inconsistencies– ensure quality

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

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

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

Spec# demo

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

Basic architecture of a program verifier

verification conditiongenerator

theorem prover

verification condition

program with specifications

“correct” or list of errors

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

Spec# program verifier architecture

V.C. generator

automatictheorem prover

verification condition

Spec#

“correct” or list of errors

Spec# compiler

MSIL

translator

intermediate program representation

abstract interpreter

Spec# program verifier

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

Modular verification

• Don’t 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

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

Specification challenges

• Object invariants• Frame conditions (modifies clauses)• Class initialization and class

variables• Model fields• Delegates• …

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

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

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

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;

}…

}

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

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;

}…

}

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

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 holdon entry to method

invariant checked to holdon exit from method

invariant checked to holdat end of constructor

invariant may betemporarily broken here

invariant is restored here

what if P calls back into M?

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

Object state: valid vs. mutable

• introduce a special object fieldst : { Mutable, Valid }

• idea: program invariant(o: C ・ o.st = Mutable InvC(o))

• field updates are allowed only for mutable objects:a field-update statement

o.f := E;has the precondition o.st = Mutable

holds at everyprogram point!

for any o: C, we writeInvC(o) ≡ o.x < o.y

class C {int x, y;invariant x < y;

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

pack/unpack statements

• st is changed by special statements pack and unpack

• pack o as C– check that InvC(o) holds

– then change o.st from Mutable to Valid

• unpack o from C– change o.st from Valid to Mutable

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

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 checkedto hold here

invariant may betemporarily broken here

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

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 M’s precondition,so no dangerous reentrancy can occur

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

Summary, so far

• invariant …• st : { Mutable, Valid }• pack, unpack• updates of o.f require o.st =

Mutable

• InvC(o) can mention only the fields of o

• (o: C ・ o.st = Mutable InvC(o))

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

Aggregate objectsclass 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 his valid here?

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

Aggregate objectsclass 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 his valid here?

Perhaps it isn’t!

void Violation(Set s)requires s.st = Valid;

{Hashtable g :=

s.Leak();unpack g from

Hashtable;g.x := …;s.Add(…);

}

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

Ownershipclass 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 htemporarily

relinquished here

ownership of hre-obtained here

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

Object state: mutable, valid, committed

• st : { Mutable, Valid, Committed }

• program invariant(o: C ・ o.st = Mutable InvC(o))

• and for every owned field f(o: C ・ o.st = Mutable o.f.st =

Committed)

• pack o as C– check that InvC(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

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

Object states: a picture of the heap

Mutable

Valid

Committed

s: Set

Hashtable

ownershiph

unpack s from Set

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

Object states: a picture of the heap

Mutable

Valid

Committed

s: Set

Hashtable

ownershiph

unpack s from Set

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

Object states: a picture of the heap

Mutable

Valid

Committed

s: Set

Hashtable

ownershiph

pack s as Set

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

Object states: a picture of the heap

Mutable

Valid

Committed

s: Set

Hashtable

ownershiph

pack s as Set

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

Summary of object invariants

• invariant …• owned T f;• st : { Mutable, Valid, Committed }• pack, unpack• updates of o.f require o.st = Mutable• InvC(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 InvC(o))

• (o: C ・ o.st = Mutable o.f.st = Committed)

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

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

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

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])

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

Modifying underlying representation

Mutable

Valid

Committed

s: Set

Hashtable

ownershiph

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

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]))

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

So: common modifies clause• modifies this.*;

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

Quirk?

void M(T p)requires p.st = Committed p.x

= 12;{

int y := Math.Sqrt(…);assert p.x = 12;

}assertion failure

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

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

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

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

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

Conclusions• Because of tool support, we’re 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

http://research.microsoft.com/~leino

http://research.microsoft.com/projects/specsharp