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
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.
Filipe Militão1,2 Jonathan Aldrich1 Luís Caires2
1 Carnegie Mellon University, Pittsburgh, USA 2 Universidade Nova de Lisboa, Lisboa, Portugal
ECOOP 2014
state avoids a class of state-related errors.
properties challenging.
2
3
x.open(...); x.write(...); x.write(...);
4
x.open(...); x.write(...); x.write(...);
Opened
x.open(...); x.write(...); x.write(...);
5
Opened
y.close()
Closed
6
y.close() x.open(...); x.write(...); x.write(...);
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.
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
references (and immutable records, tagged sums, ...).
usability and extended with new constructs, and our sharing mechanism.
9
[Ahmed, Fluet, and Morrisett. L3: A linear language with
10
ref A
10
ref A
reference with contents of type A
10
ref A
reference with contents of type A
rw p A
ref p
10
ref A
reference with contents of type A
rw p A
ref p
duplicable reference to location p
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
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
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
11
let y = x in delete y; x := false end x : ref p
rw p string
x := 1;
12
let y = x in x := 1; delete y; x := false end x : ref p
rw p string
y : ref p
13
let y = x in delete y; x := false end x : ref p
rw p int
y : ref p x := 1;
14
let y = x in delete y; x := false end x : ref p y : ref p x := 1;
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;
Capabilities are linear (a.k.a. “unique”)!
15
Capabilities are linear (a.k.a. “unique”)!
15
A capability is split into rely-guarantee protocols to safely coordinate access to the shared state.
16
A capability is split into rely-guarantee protocols to safely coordinate access to the shared state.
16
17
A * B
[Dinsdale-Young, et al. Concurrent Abstract Predicates.
(ECOOP’10), and other works].
18
A * B
shared
[Dinsdale-Young, et al. Concurrent Abstract Predicates.
(ECOOP’10), and other works].
Consider all possible interleaved uses of aliases and how they may change the shared state.
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
20
x := 1; doSomething(); !x // what do we get?
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?
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?
held invariant, invariant-based sharing.
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
held invariant, invariant-based sharing.
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
held invariant, invariant-based sharing.
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
A Rely-Guarantee Protocol models the shared state interaction from the alias’ own view/perspective:
what the protocol specifies/allows.
that may appear when inspecting the shared
aliases.
24
25
( see the paper for more technical details )
26
27
28
29
30
31
32
33
34
35
36
The shared state satisfies A, and requires the alias to obey the guarantee P.
37
Requires the client to establish (guarantee) that the shared state satisfies A before continuing the use of the protocol as P.
38
39
Shared by two aliases interacting via a common buffer, here modeled as a singly linked list.
close the pipe.
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.
40
41
42
Producer Protocol Consumer Protocol
43
Producer
tail : Empty ⇒ ( Filled ⊕ Closed ) ; none
44
Producer
tail : Empty ⇒ ( Filled ⊕ Closed ) ; none
45
Producer
tail : none
46
Producer
tail : Empty ⇒ ( Filled ⊕ Closed ) ; none
Consumer
47
Producer
tail : Empty ⇒ ( Filled ⊕ Closed ) ; none head : rec X.( ( Empty ⇒ Empty ; X ) ⊕ ( Filled ⇒ none ; none ) ⊕ ( Closed ⇒ none ; none ) )
Consumer
48
Producer
tail : Empty ⇒ ( Filled ⊕ Closed ) ; none head : rec X.( ( Empty ⇒ Empty ; X ) ⊕ ( Filled ⇒ none ; none ) ⊕ ( Closed ⇒ none ; none ) )
Consumer
49
Producer
tail : Empty ⇒ ( Filled ⊕ Closed ) ; none head : none
Consumer
50
Producer
tail : Empty ⇒ ( Filled ⊕ Closed ) ; none head : rec X.( ( Empty ⇒ Empty ; X ) ⊕ ( Filled ⇒ none ; none ) ⊕ ( Closed ⇒ none ; none ) )
51
Producer Protocol: Consumer Protocol: head : rec X.( ( Empty ⇒ Empty ; X ) ⊕ ( Filled ⇒ none ; none ) ⊕ ( Closed ⇒ none ; none ) ) tail : Empty ⇒ ( Filled ⊕ Closed ) ; none
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:
53
∃P.∃C.( ![ put : !( ( !int :: P ) ⊸ ( ![] :: P ) ) , close : !( ( ![] :: P ) ⊸ ![] ) , tryTake : !( ( ![] :: C ) ⊸ Depleted#![] + NoResult#(![] :: C) + Result#(!int :: C) ) ] :: ( C * P ) )
53
∃P.∃C.( ![ put : !( ( !int :: P ) ⊸ ( ![] :: P ) ) , close : !( ( ![] :: P ) ⊸ ![] ) , tryTake : !( ( ![] :: C ) ⊸ Depleted#![] + NoResult#(![] :: C) + Result#(!int :: C) ) ] :: ( C * P ) )
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])
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])
Consider all possible interleaved uses of aliases and how they may change the shared state.
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
55
56
57
defocus constructs.
a) Hide private changes from the other aliases of that shared state. b) Advance the step of the protocol, by
58
59
focus Empty ... defocus
59
focus Empty ... defocus
Empty ⇒ Filled ; Next
59
focus Empty ... defocus
Empty ⇒ Filled ; Next
focus Empty ... defocus
Empty ⇒ Filled ; Next
60
defocus-guarantee focused state
Empty ⇒ Filled ; Next
61
focus Empty ... defocus
Empty ⇒ Filled ; Next
61
Empty , Filled ; Next
focus Empty ... defocus
Empty ⇒ Filled ; Next
62
focus Empty ... defocus
PartiallyFilled , Filled ; Next Empty , Filled ; Next
63
Filled , Filled ; Next Empty ⇒ Filled ; Next Empty , Filled ; Next
focus Empty ... defocus
64
Filled , Filled ; Next Empty ⇒ Filled ; Next
focus Empty ... defocus
Empty , Filled ; Next
64
Filled , Filled ; Next Empty ⇒ Filled ; Next
focus Empty ... defocus
Empty , Filled ; Next
64
Filled , Filled ; Next Empty ⇒ Filled ; Next Next
focus Empty ... defocus
Empty , Filled ; Next
65
Empty ⇒ Filled ; Next , Δ Empty, Filled ; Next▷ Δ Filled, Filled; Next▷ Δ Next , Δ
private changes
focus Empty ... defocus
65
Empty ⇒ Filled ; Next , Δ Empty, Filled ; Next▷ Δ Filled, Filled; Next▷ Δ Next , Δ
private changes
focus Empty ... 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
Consider all possible interleaved uses of aliases and how they may change the shared state.
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
67
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)”
existing protocol.
68
possible states that may appear due to the “interleaving” of other aliases of that shared state.
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.
69
share E as rec X.( E ⇒ E;X ⊕ N ⇒ none ⊕ C ⇒ none ) || E ⇒ ( N ⊕ C )
Producer Consumer
69
share E as rec X.( E ⇒ E;X ⊕ N ⇒ none ⊕ C ⇒ none ) || E ⇒ ( N ⊕ C )
70
P C E P C P C P C P C
Initial state.
possible interleaving
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.
71
E
none
Producer Consumer
C
N
E E ⇒ C
N ⊕
E
none
Configurations: State:
71
E
none
Producer Consumer
C
N
E E ⇒ C
N ⊕
E
none
Configurations: State:
71
E
none
Producer Consumer
C
N
E E ⇒ C
N ⊕
E
none
Configurations: State:
71
E
none
Producer Consumer
C
N
E E ⇒ C
N ⊕
E
none
Configurations: State:
72
E
none
Producer Consumer
C
N
E E ⇒ C
N ⊕
E
none
Configurations: State:
C
N ⊕
72
E
none
Producer Consumer
C
N
E E ⇒ C
N ⊕
E
none
Configurations: State:
C
N ⊕
72
E
none
Producer Consumer
C
N
E E ⇒ C
N ⊕
E
none
Configurations: State:
C
N ⊕
72
E
none
Producer Consumer
C
N
E E ⇒ C
N ⊕
none
Configurations: State:
C
N ⊕
72
E
none
Producer Consumer
C
N
E E ⇒ C
N ⊕
none
Configurations: State:
C
N ⊕
73
E
none
Producer Consumer
C
N
E
none
Configurations: State:
C
N
73
E
none
Producer Consumer
C
N
E
none
Configurations: State:
C
N
73
E
none
Producer Consumer
C
N
E
none
Configurations: State:
none
74
Producer Consumer Configurations: State:
none
Krishnaswami, Turon, Dreyer, Garg. Superficially Substructural Types. ICFP 2012. Dinsdale-Young, Birkedal, Gardner, Parkinson,
reasoning for concurrent programs. POPL 2013.
monoids) that enables expressive and precise descriptions of sharing. Gordon, Ernst, Grossman. Rely-Guarantee References for Refinement Types over Aliased Mutable Data. PLDI 2013.
guarantee relations to handle sharing of state.
75
( Paper includes additional Related Work. )
Krishnaswami, Turon, Dreyer, Garg. Superficially Substructural Types. ICFP 2012. Dinsdale-Young, Birkedal, Gardner, Parkinson,
reasoning for concurrent programs. POPL 2013.
monoids) that enables expressive and precise descriptions of sharing. Gordon, Ernst, Grossman. Rely-Guarantee References for Refinement Types over Aliased Mutable Data. PLDI 2013.
guarantee relations to handle sharing of state.
75
( Paper includes additional Related Work. )
+ 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.
Rely-Guarantee Protocols, to control sharing of state mutable by statically disconnected variables.
Experimental Prototype Implementation:
76