Rely-Guarantee Protocols Filipe Milito 1,2 Jonathan Aldrich 1 Lus - - PowerPoint PPT Presentation

rely guarantee protocols
SMART_READER_LITE
LIVE PREVIEW

Rely-Guarantee Protocols Filipe Milito 1,2 Jonathan Aldrich 1 Lus - - PowerPoint PPT Presentation

ECOOP 2014 Rely-Guarantee Protocols Filipe Milito 1,2 Jonathan Aldrich 1 Lus Caires 2 1 Carnegie Mellon University, Pittsburgh, USA 2 Universidade Nova de Lisboa, Lisboa, Portugal Motivation Mutable state can be useful in certain cases.


slide-1
SLIDE 1

Filipe Militão1,2 Jonathan Aldrich1 Luís Caires2

1 Carnegie Mellon University, Pittsburgh, USA 2 Universidade Nova de Lisboa, Lisboa, Portugal

Rely-Guarantee Protocols

ECOOP 2014

slide-2
SLIDE 2

Motivation

  • Mutable state can be useful in certain cases.
  • Precisely tracking the properties of mutable

state avoids a class of state-related errors.

  • However, aliasing makes tracking such

properties challenging.

2

slide-3
SLIDE 3

3

x.open(...); x.write(...); x.write(...);

slide-4
SLIDE 4

4

x.open(...); x.write(...); x.write(...);

Opened

slide-5
SLIDE 5

x.open(...); x.write(...); x.write(...);

5

Opened

y.close()

slide-6
SLIDE 6

Closed

6

y.close() x.open(...); x.write(...); x.write(...);

slide-7
SLIDE 7

x.open(...); x.write(...); x.write(...);

Closed

7

The assumption that x was pointing to an Opened file can be invalidated due to the interference caused by y.

slide-8
SLIDE 8

A novel interference-control mechanism, Rely-Guarantee Protocols, to statically handle interference in the use of mutable state that is shared by aliases through statically disconnected variables.

8

Contribution

slide-9
SLIDE 9

Language

  • Polymorphic λ-calculus with mutable

references (and immutable records, tagged sums, ...).

  • Technically, we use a variant of L3 adapted for

usability and extended with new constructs, and our sharing mechanism.

9

[Ahmed, Fluet, and Morrisett. L3: A linear language with

  • locations. Fundam. Inform. 2007.]
slide-10
SLIDE 10

State as a Linear Resource

10

ref A

slide-11
SLIDE 11

State as a Linear Resource

10

ref A

reference with contents of type A

slide-12
SLIDE 12

State as a Linear Resource

10

ref A

reference with contents of type A

rw p A

{

ref p

slide-13
SLIDE 13

State as a Linear Resource

10

ref A

reference with contents of type A

rw p A

{

ref p

duplicable reference to location p

slide-14
SLIDE 14

State as a Linear Resource

10

ref A

reference with contents of type A

rw p A

{

ref p

duplicable reference to location p

ref p ref p ref p ref p ref p ref p

slide-15
SLIDE 15

State as a Linear Resource

10

ref A

reference with contents of type A

rw p A

{

ref p

duplicable reference to location p linear (“unique”)

read+write capability of location p with contents of type A

ref p ref p ref p ref p ref p ref p

slide-16
SLIDE 16

State as a Linear Resource

10

ref A

reference with contents of type A

rw p A

{

ref p

duplicable reference to location p linear (“unique”)

read+write capability of location p with contents of type A

p links ref to

capability

ref p ref p ref p ref p ref p ref p

slide-17
SLIDE 17

11

let y = x in delete y; x := false end x : ref p

rw p string

x := 1;

slide-18
SLIDE 18

12

let y = x in x := 1; delete y; x := false end x : ref p

rw p string

y : ref p

slide-19
SLIDE 19

13

let y = x in delete y; x := false end x : ref p

rw p int

y : ref p x := 1;

slide-20
SLIDE 20

14

let y = x in delete y; x := false end x : ref p y : ref p x := 1;

slide-21
SLIDE 21

14

let y = x in delete y; x := false end x : ref p y : ref p Type Error: Missing capability to location p. x := 1;

slide-22
SLIDE 22

Why sharing?

Capabilities are linear (a.k.a. “unique”)!

15

slide-23
SLIDE 23

Why sharing?

Capabilities are linear (a.k.a. “unique”)!

15

slide-24
SLIDE 24

Sharing

A capability is split into rely-guarantee protocols to safely coordinate access to the shared state.

16

slide-25
SLIDE 25

Sharing

A capability is split into rely-guarantee protocols to safely coordinate access to the shared state.

16

slide-26
SLIDE 26

Disjoint

17

A * B

  • Linearity ensured disjointness.
  • Sharing causes fictional disjointness.

[Dinsdale-Young, et al. Concurrent Abstract Predicates.

(ECOOP’10), and other works].

slide-27
SLIDE 27

Fictionally

18

A * B

shared

Disjoint

[Dinsdale-Young, et al. Concurrent Abstract Predicates.

(ECOOP’10), and other works].

  • Linearity ensured disjointness.
  • Sharing causes fictional disjointness.
slide-28
SLIDE 28

Problems of Sharing

  • 1. Account for interference (public changes).

Consider all possible interleaved uses of aliases and how they may change the shared state.

  • 2. Handle private changes.

Making sure other aliases do not see any intermediate or inconsistent states of the shared state (which may appear due to type changing assignments like int to string, etc.).

19

slide-29
SLIDE 29

20

x := 1; doSomething(); !x // what do we get?

Alias Interleaving

slide-30
SLIDE 30

21

fun().1 fun().x := false fun().delete x

doSomething interleave zero or more aliases to the same state as referenced by x.

x := 1; doSomething(); !x // what do we get?

Alias Interleaving

slide-31
SLIDE 31

22

If doSomething did change the same state as aliased by x (i.e. interfered), what change occurred?

x := 1; doSomething(); !x // what do we get?

Alias Interleaving

slide-32
SLIDE 32

Handling Interference

  • One solution is to ensure that each alias obeys an initially

held invariant, invariant-based sharing.

  • Instead, we adapt the spirit of rely-guarantee reasoning to a

state-centric model by generalizing the specification of shared state interactions. By individually constraining the actions of each alias, we can make stronger (as in more precise) assumptions how interference may change the shared state.

23

I I R G

⇒ ⇒

slide-33
SLIDE 33

Handling Interference

  • One solution is to ensure that each alias obeys an initially

held invariant, invariant-based sharing.

  • Instead, we adapt the spirit of rely-guarantee reasoning to a

state-centric model by generalizing the specification of shared state interactions. By individually constraining the actions of each alias, we can make stronger (as in more precise) assumptions how interference may change the shared state.

23

I I R G

⇒ ⇒

Relied state

slide-34
SLIDE 34

Handling Interference

  • One solution is to ensure that each alias obeys an initially

held invariant, invariant-based sharing.

  • Instead, we adapt the spirit of rely-guarantee reasoning to a

state-centric model by generalizing the specification of shared state interactions. By individually constraining the actions of each alias, we can make stronger (as in more precise) assumptions how interference may change the shared state.

23

I I R G

⇒ ⇒

Relied state Guaranteed state

slide-35
SLIDE 35

Modeling Interference

A Rely-Guarantee Protocol models the shared state interaction from the alias’ own view/perspective:

  • The alias’ actions are constrained to fit within

what the protocol specifies/allows.

  • Interference is observed through new state(s)

that may appear when inspecting the shared

  • state. Thus, the protocol may specify actions
  • ver states that can only be produced by other

aliases.

24

slide-36
SLIDE 36

Rely-Guarantee Protocols

  • An interference-control mechanism.
  • I will focus on presenting the following:
  • 1. Protocol Specification (“public changes”)
  • 2. Protocol Use (“private changes”)
  • 3. Protocol Conformance (“alias interleaving”)

25

( see the paper for more technical details )

slide-37
SLIDE 37

26

Types

slide-38
SLIDE 38

27

Types

slide-39
SLIDE 39

28

Types

slide-40
SLIDE 40

29

Types

slide-41
SLIDE 41

30

Types

slide-42
SLIDE 42

31

Types

slide-43
SLIDE 43
  • 1. Protocol

Specification

32

slide-44
SLIDE 44
  • 1. Protocol

Specification

33

slide-45
SLIDE 45
  • 1. Protocol

Specification

34

slide-46
SLIDE 46
  • 1. Protocol

Specification

35

slide-47
SLIDE 47
  • 1. Protocol

Specification

36

The shared state satisfies A, and requires the alias to obey the guarantee P.

slide-48
SLIDE 48
  • 1. Protocol

Specification

37

Requires the client to establish (guarantee) that the shared state satisfies A before continuing the use of the protocol as P.

slide-49
SLIDE 49
  • 1. Protocol

Specification

38

slide-50
SLIDE 50

Shared Pipe

39

Shared by two aliases interacting via a common buffer, here modeled as a singly linked list.

  • 1. The Producer alias may put new elements in or

close the pipe.

  • 2. The Consumer alias may only tryTake elements

from the buffer. The result of tryTake is one of the following states: either there was some Result, or NoResult, or the pipe is fully Depleted.

slide-51
SLIDE 51

40

Pipe

slide-52
SLIDE 52

41

Consumer Producer

slide-53
SLIDE 53

42

Consumer Producer Shared Buffer

Producer Protocol Consumer Protocol

slide-54
SLIDE 54

43

Producer

tail : Empty ⇒ ( Filled ⊕ Closed ) ; none

slide-55
SLIDE 55

44

Producer

tail : Empty ⇒ ( Filled ⊕ Closed ) ; none

slide-56
SLIDE 56

45

Producer

tail : none

slide-57
SLIDE 57

46

Producer

tail : Empty ⇒ ( Filled ⊕ Closed ) ; none

slide-58
SLIDE 58

Consumer

47

Producer

tail : Empty ⇒ ( Filled ⊕ Closed ) ; none head : rec X.( ( Empty ⇒ Empty ; X ) ⊕ ( Filled ⇒ none ; none ) ⊕ ( Closed ⇒ none ; none ) )

slide-59
SLIDE 59

Consumer

48

Producer

tail : Empty ⇒ ( Filled ⊕ Closed ) ; none head : rec X.( ( Empty ⇒ Empty ; X ) ⊕ ( Filled ⇒ none ; none ) ⊕ ( Closed ⇒ none ; none ) )

slide-60
SLIDE 60

Consumer

49

Producer

tail : Empty ⇒ ( Filled ⊕ Closed ) ; none head : none

slide-61
SLIDE 61

Consumer

50

Producer

tail : Empty ⇒ ( Filled ⊕ Closed ) ; none head : rec X.( ( Empty ⇒ Empty ; X ) ⊕ ( Filled ⇒ none ; none ) ⊕ ( Closed ⇒ none ; none ) )

slide-62
SLIDE 62

51

Producer Protocol: Consumer Protocol: head : rec X.( ( Empty ⇒ Empty ; X ) ⊕ ( Filled ⇒ none ; none ) ⊕ ( Closed ⇒ none ; none ) ) tail : Empty ⇒ ( Filled ⊕ Closed ) ; none

slide-63
SLIDE 63

52

T[t] = rw t Empty#[] ⇒ ( (rw t Node#[...]) ⊕ (rw t Closed#[]) ); none H[t] = rec X.(

( rw t Empty#[] ⇒ rw t Empty#[] ; X ) ⊕ ( rw t Node#[...] ⇒ none ; none ) ⊕ ( rw t Closed#[] ⇒ none ; none ) )

Producer Protocol: Consumer Protocol:

slide-64
SLIDE 64

Pipe Typestate

53

∃P.∃C.( ![ put : !( ( !int :: P ) ⊸ ( ![] :: P ) ) , close : !( ( ![] :: P ) ⊸ ![] ) , tryTake : !( ( ![] :: C ) ⊸ Depleted#![] + NoResult#(![] :: C) + Result#(!int :: C) ) ] :: ( C * P ) )

slide-65
SLIDE 65

Pipe Typestate

53

∃P.∃C.( ![ put : !( ( !int :: P ) ⊸ ( ![] :: P ) ) , close : !( ( ![] :: P ) ⊸ ![] ) , tryTake : !( ( ![] :: C ) ⊸ Depleted#![] + NoResult#(![] :: C) + Result#(!int :: C) ) ] :: ( C * P ) )

slide-66
SLIDE 66

Pipe Typestate

53

∃P.∃C.( ![ put : !( ( !int :: P ) ⊸ ( ![] :: P ) ) , close : !( ( ![] :: P ) ⊸ ![] ) , tryTake : !( ( ![] :: C ) ⊸ Depleted#![] + NoResult#(![] :: C) + Result#(!int :: C) ) ] :: ( C * P ) )

rw p ∃p.(ref p :: T[p])

slide-67
SLIDE 67

Pipe Typestate

53

∃P.∃C.( ![ put : !( ( !int :: P ) ⊸ ( ![] :: P ) ) , close : !( ( ![] :: P ) ⊸ ![] ) , tryTake : !( ( ![] :: C ) ⊸ Depleted#![] + NoResult#(![] :: C) + Result#(!int :: C) ) ] :: ( C * P ) )

rw p ∃p.(ref p :: T[p]) rw c ∃p.(ref p :: H[p])

slide-68
SLIDE 68

Problems of Sharing

  • 1. Account for interference (public changes).

Consider all possible interleaved uses of aliases and how they may change the shared state.

  • 2. Handle private changes.

Making sure other aliases do not see any intermediate or inconsistent states of the shared state (which may appear due to type changing assignments like int to string, etc.).

54

slide-69
SLIDE 69

Syntax

55

slide-70
SLIDE 70

Syntax

56

slide-71
SLIDE 71

Syntax

57

slide-72
SLIDE 72
  • 2. Protocol Use
  • Protocols are used through focus and

defocus constructs.

  • They serve two purposes:

a) Hide private changes from the other aliases of that shared state. b) Advance the step of the protocol, by

  • beying the constraints on public changes.

58

slide-73
SLIDE 73

Focus / Defocus

59

focus Empty ... defocus

slide-74
SLIDE 74

Focus / Defocus

59

focus Empty ... defocus

Empty ⇒ Filled ; Next

slide-75
SLIDE 75

Focus / Defocus

59

focus Empty ... defocus

Empty ⇒ Filled ; Next

slide-76
SLIDE 76

focus Empty ... defocus

Empty ⇒ Filled ; Next

Focus / Defocus

60

defocus-guarantee focused state

slide-77
SLIDE 77

Empty ⇒ Filled ; Next

Focus / Defocus

61

focus Empty ... defocus

slide-78
SLIDE 78

Empty ⇒ Filled ; Next

Focus / Defocus

61

Empty , Filled ; Next

focus Empty ... defocus

slide-79
SLIDE 79

Empty ⇒ Filled ; Next

Focus / Defocus

62

focus Empty ... defocus

PartiallyFilled , Filled ; Next Empty , Filled ; Next

slide-80
SLIDE 80

Focus / Defocus

63

Filled , Filled ; Next Empty ⇒ Filled ; Next Empty , Filled ; Next

focus Empty ... defocus

slide-81
SLIDE 81

Focus / Defocus

64

Filled , Filled ; Next Empty ⇒ Filled ; Next

focus Empty ... defocus

Empty , Filled ; Next

slide-82
SLIDE 82

Focus / Defocus

64

Filled , Filled ; Next Empty ⇒ Filled ; Next

focus Empty ... defocus

Empty , Filled ; Next

slide-83
SLIDE 83

Focus / Defocus

64

Filled , Filled ; Next Empty ⇒ Filled ; Next Next

focus Empty ... defocus

Empty , Filled ; Next

slide-84
SLIDE 84

Focus / Defocus

65

Empty ⇒ Filled ; Next , Δ Empty, Filled ; Next▷ Δ Filled, Filled; Next▷ Δ Next , Δ

{

private changes

focus Empty ... defocus

slide-85
SLIDE 85

Focus / Defocus

65

Empty ⇒ Filled ; Next , Δ Empty, Filled ; Next▷ Δ Filled, Filled; Next▷ Δ Next , Δ

{

private changes

focus Empty ... defocus

slide-86
SLIDE 86

Focus / Defocus

65

Empty ⇒ Filled ; Next , Δ Empty, Filled ; Next▷ Δ Filled, Filled; Next▷ Δ Next , Δ

{

private changes

focus Empty ... defocus

hides any state that may allow reentrant accesses to focused state

slide-87
SLIDE 87

Problems of Sharing

  • 1. Account for interference (public changes).

Consider all possible interleaved uses of aliases and how they may change the shared state.

  • 2. Handle private changes.

Making sure other aliases do not see any intermediate or inconsistent states of the shared state (which may appear due to type changing assignments like int to string, etc.).

66

slide-88
SLIDE 88

67

  • Protocols are introduced explicitly, in pairs, through

the share construct: share A as B || C “type A (either a capability or an existing protocol) can be safely split in types B and C (two protocols)”

  • Arbitrary aliasing is possible by continuing to split an

existing protocol.

  • 3. Protocol

Conformance

slide-89
SLIDE 89

68

  • We must check that a protocol is aware of all

possible states that may appear due to the “interleaving” of other aliases of that shared state.

  • Checking a split is built from two components:

a) a stepping relation, that “simulates” a single use of focus-defocus (i.e. a step of the protocol). b) a protocol conformance definition that ensures the protocol considers all possible alias interleaving.

Checking share

slide-90
SLIDE 90

Protocol Conformance Example

69

share E as rec X.( E ⇒ E;X ⊕ N ⇒ none ⊕ C ⇒ none ) || E ⇒ ( N ⊕ C )

slide-91
SLIDE 91

Producer Consumer

Protocol Conformance Example

69

share E as rec X.( E ⇒ E;X ⊕ N ⇒ none ⊕ C ⇒ none ) || E ⇒ ( N ⊕ C )

slide-92
SLIDE 92

70

P C E P C P C P C P C

Initial state.

{

possible interleaving

slide-93
SLIDE 93

70

P C E P C P C P C P C

Initial state.

{

possible interleaving

However, our protocols can only list a finite number of distinct states, and each protocol lists a finite number of distinct protocol steps. This will ensure that there is finite number of distinct configurations, each representing one possible alias interleaving in the use of the state that is being shared by the protocols.

slide-94
SLIDE 94

71

E

none

⇒ ⊕

Producer Consumer

C

N

⇒ ⇒

E E ⇒ C

N ⊕

E

none

Configurations: State:

slide-95
SLIDE 95

71

E

none

⇒ ⊕

Producer Consumer

C

N

⇒ ⇒

E E ⇒ C

N ⊕

E

none

Configurations: State:

slide-96
SLIDE 96

71

E

none

⇒ ⊕

Producer Consumer

C

N

⇒ ⇒

E E ⇒ C

N ⊕

E

none

Configurations: State:

slide-97
SLIDE 97

71

E

none

⇒ ⊕

Producer Consumer

C

N

⇒ ⇒

E E ⇒ C

N ⊕

E

none

Configurations: State:

slide-98
SLIDE 98

72

E

none

⇒ ⊕

Producer Consumer

C

N

⇒ ⇒

E E ⇒ C

N ⊕

E

none

Configurations: State:

C

N ⊕

slide-99
SLIDE 99

72

E

none

⇒ ⊕

Producer Consumer

C

N

⇒ ⇒

E E ⇒ C

N ⊕

E

none

Configurations: State:

C

N ⊕

slide-100
SLIDE 100

72

E

none

⇒ ⊕

Producer Consumer

C

N

⇒ ⇒

E E ⇒ C

N ⊕

E

none

Configurations: State:

C

N ⊕

slide-101
SLIDE 101

72

E

none

⇒ ⊕

Producer Consumer

C

N

⇒ ⇒

E E ⇒ C

N ⊕

none

Configurations: State:

C

N ⊕

slide-102
SLIDE 102

72

E

none

⇒ ⊕

Producer Consumer

C

N

⇒ ⇒

E E ⇒ C

N ⊕

none

Configurations: State:

C

N ⊕

slide-103
SLIDE 103

73

E

none

⇒ ⊕

Producer Consumer

C

N

⇒ ⇒

E

none

Configurations: State:

C

N

none

slide-104
SLIDE 104

73

E

none

⇒ ⊕

Producer Consumer

C

N

⇒ ⇒

E

none

Configurations: State:

C

N

none

slide-105
SLIDE 105

73

E

none

⇒ ⊕

Producer Consumer

C

N

⇒ ⇒

E

none

Configurations: State:

none

none

slide-106
SLIDE 106

74

Producer Consumer Configurations: State:

none

none

none

slide-107
SLIDE 107

Related Work

Krishnaswami, Turon, Dreyer, Garg. Superficially Substructural Types. ICFP 2012. Dinsdale-Young, Birkedal, Gardner, Parkinson,

  • Yang. Views: compositional

reasoning for concurrent programs. POPL 2013.

  • Powerful generalization of split and merge operations (using commutative

monoids) that enables expressive and precise descriptions of sharing. Gordon, Ernst, Grossman. Rely-Guarantee References for Refinement Types over Aliased Mutable Data. PLDI 2013.

  • References extended with predicate (for expressing local knowledge), rely and

guarantee relations to handle sharing of state.

75

( Paper includes additional Related Work. )

slide-108
SLIDE 108

Related Work

Krishnaswami, Turon, Dreyer, Garg. Superficially Substructural Types. ICFP 2012. Dinsdale-Young, Birkedal, Gardner, Parkinson,

  • Yang. Views: compositional

reasoning for concurrent programs. POPL 2013.

  • Powerful generalization of split and merge operations (using commutative

monoids) that enables expressive and precise descriptions of sharing. Gordon, Ernst, Grossman. Rely-Guarantee References for Refinement Types over Aliased Mutable Data. PLDI 2013.

  • References extended with predicate (for expressing local knowledge), rely and

guarantee relations to handle sharing of state.

75

( Paper includes additional Related Work. )

  • We are limited to finite state representations, i.e. typestates.

+ Protocols can express changes over time (“temporal sharing”), without requiring the use of auxiliary variables to distinguish steps. + Sharing is a typing artifact and is not tied to a module. + Can be type checked without manual intervention.

slide-109
SLIDE 109

Summary

  • Contribution: novel interference-control mechanism,

Rely-Guarantee Protocols, to control sharing of state mutable by statically disconnected variables.

  • Topics Covered: (more details in the paper)
  • 1. Protocol Specification (“public changes”)
  • 2. Protocol Use (“private changes”)
  • 3. Protocol Conformance (“alias interleaving”)

Experimental Prototype Implementation:

  • http://deaf-parrot.googlecode.com

76