13
Correct Concurrency with Chalice Rustan Leino RiSE, Microsoft Research, Redmond MIT 5 June 2009 Joint work with: Peter Müller, ETH Zurich Jan Smans, KU Leuven

Rustan Leino RiSE, Microsoft Research, Redmond MIT 5 June 2009 Joint work with: Peter Müller, ETH Zurich Jan Smans, KU Leuven

Embed Size (px)

Citation preview

Page 1: Rustan Leino RiSE, Microsoft Research, Redmond MIT 5 June 2009 Joint work with: Peter Müller, ETH Zurich Jan Smans, KU Leuven

Correct Concurrency with Chalice

Rustan LeinoRiSE, Microsoft Research, Redmond

MIT5 June 2009

Joint work with:Peter Müller, ETH ZurichJan Smans, KU Leuven

Page 2: Rustan Leino RiSE, Microsoft Research, Redmond MIT 5 June 2009 Joint work with: Peter Müller, ETH Zurich Jan Smans, KU Leuven

Some textbook conceptsAtomicity

Sequential reasoning within atomic sections

Monitor invariantsAssumed when monitor is acquiredChecked when monitor is released

Locking orderDeadlock prevention

Rely-guarantee reasoningThread interference

class Cell { int val; invariant val > 0; void Set( int v ) { lock( this ) { val :=

v; } } void Swap( Cell c ) { lock( this )

{ lock( c ) { int t := val; val := c.val;

c.val := t; } } }}

Page 3: Rustan Leino RiSE, Microsoft Research, Redmond MIT 5 June 2009 Joint work with: Peter Müller, ETH Zurich Jan Smans, KU Leuven

ChallengesClient-side locking

One monitor protects lots of state

Fine-grained lockingOne field protected by several monitors

Thread-local and shared objects

Transitions in both directions

Dynamic changes of locking order

class Node { int val; Node next; invariant next null

val next.val;

… }

class List { Node head; void Reverse( ) { … } … }

Page 4: Rustan Leino RiSE, Microsoft Research, Redmond MIT 5 June 2009 Joint work with: Peter Müller, ETH Zurich Jan Smans, KU Leuven

ChaliceExperimental language with focus on:

Share-memory concurrencyStatic verification

Key featuresMemory access governed by a model of permissionsSharing via locks with monitor invariantsDeadlock checking, dynamic lock re-ordering

Other featuresClasses; Mutual exclusion and readers/writers locks;Fractional permissions;Two-state monitor invariants;Asynchronous method calls; Memory leak checking;Logic predicates and functions; Ghost and prophecy variables

Page 5: Rustan Leino RiSE, Microsoft Research, Redmond MIT 5 June 2009 Joint work with: Peter Müller, ETH Zurich Jan Smans, KU Leuven

PermissionsEvery memory location has an associated permission

A memory location is an (object, field) pair

Permissions can be held by activation records

An activation record is a particular invocation of a method

Permissions can be transferred dynamically

ExhaleInhale

Page 6: Rustan Leino RiSE, Microsoft Research, Redmond MIT 5 June 2009 Joint work with: Peter Müller, ETH Zurich Jan Smans, KU Leuven

SharingObjects can be shared

thread local

shared,availabl

e

shared,locked

new

share

acquire

release

unshare

free

Page 7: Rustan Leino RiSE, Microsoft Research, Redmond MIT 5 June 2009 Joint work with: Peter Müller, ETH Zurich Jan Smans, KU Leuven

MonitorsAn available object can hold permissionsA monitor invariant describes the state of an available object

thread local

shared,availabl

e

shared,locked

new

share

acquire

release

unshare

free

monitor invariant is checked here

Page 8: Rustan Leino RiSE, Microsoft Research, Redmond MIT 5 June 2009 Joint work with: Peter Müller, ETH Zurich Jan Smans, KU Leuven

Locking orderEvery shared object o is associated with a value o.mu in the locking orderThe locking order is a dense lattice, where << denotes its strict partial orderLocks have to be acquired in ascending ordero.mu is set by the share statemento.mu can be changed by the reorder statement

Page 9: Rustan Leino RiSE, Microsoft Research, Redmond MIT 5 June 2009 Joint work with: Peter Müller, ETH Zurich Jan Smans, KU Leuven

ThreadsFork/join provide asynchronous callsRoughly:

call o.M()Exhale Pre; Inhale Post

fork o.M()Exhale Pre

join o.M()Inhale Post

Page 10: Rustan Leino RiSE, Microsoft Research, Redmond MIT 5 June 2009 Joint work with: Peter Müller, ETH Zurich Jan Smans, KU Leuven

AbstractionPredicates provide abstractionPredicates can also hold permissionsPredicates are opened and closed, usually automatically

Page 11: Rustan Leino RiSE, Microsoft Research, Redmond MIT 5 June 2009 Joint work with: Peter Müller, ETH Zurich Jan Smans, KU Leuven

Fractional permissionsOwicki-Gries example

solution due to Bart Jacobs

Page 12: Rustan Leino RiSE, Microsoft Research, Redmond MIT 5 June 2009 Joint work with: Peter Müller, ETH Zurich Jan Smans, KU Leuven

Example:

Hand-over-hand locking

:List

:Node :Node :Node :Node

head

tailcurrent

Page 13: Rustan Leino RiSE, Microsoft Research, Redmond MIT 5 June 2009 Joint work with: Peter Müller, ETH Zurich Jan Smans, KU Leuven

ConclusionChalice has many features forshared-memory concurrencyVerification via BoogiePermissions are flexible, but hard to debug with current interface