A marriage of rely/guarantee & separation logic
Viktor V afeiadis MPI-SWS
A marriage of rely/guarantee & separation logic Viktor V - - PowerPoint PPT Presentation
A marriage of rely/guarantee & separation logic Viktor V afeiadis MPI - SWS Coarse - grain locking 2 3 5 7 11 13 Coarse - grain locking 2 3 5 7 11 13 Fine - grain locking Pessimistic: lock - coupling 2 3 5 7 11 13 Fine -
Viktor V afeiadis MPI-SWS
2 3 5 7 11 13
2 3 5 7 11 13
2 3 5 7 11 13
Pessimistic: lock-coupling
2 3 5 7 11 13
Pessimistic: lock-coupling
2 3 5 7 11 13
Pessimistic: lock-coupling
2 3 5 7 11 13
Pessimistic: lock-coupling
2 3 5 7 11 13
Pessimistic: lock-coupling
2 3 5 7 11 13
Pessimistic: lock-coupling
2 3 5 7 11 13
Pessimistic: lock-coupling
2 3 5 7 11 13
Pessimistic: lock-coupling
2 3 5 7 11 13
Optimistic traversal
2 3 5 7 11 13
Optimistic traversal
2 3 5 7 11 13
Optimistic traversal Re-traverse the list OR deletions in two steps Leaks memory: cannot dispose deleted nodes.
Lock Unlock Pessimistic algorithm Add node Delete node
Optimistic algorithm Lock Unlock Add node Delete node
Aim: T ractable reasoning, manual & automatic
[Reynolds, O’Hearn, et al. 2001+]
e1 e2
empty heap ... a logic for describing the heap
P Q
single cell separation
lseg(x, y)
def
= (x = y ∧ emp) ∨ (∃z v. x → v, z ∗ lseg(z, y)) P, Q ::= e = e′ | P ∧ Q | P ∨ Q | ¬P | ∃x. P | ∀x. P | emp | e → e1, . . . , en | P ∗ Q | P − ∗ Q e :
{precondition} program {postcondition}
... a program logic
P Q C R R P Q C
If then {P} C {Q} {P ∗ R} C {Q ∗ R}
... disjoint parallelism If then
P1 Q1 C1 P2 Q2 C2 P1 Q1 P2 Q2 C1C2
and [+ resource invariants] {P1} C1 {Q1} {P2} C2 {Q2} {P1 ∗ P2} C1C2 {Q1 ∗ Q2}
2 3 5 7 11 13
Shared Local
6
Describe how the shared state changes: (i) by the program & (ii) by its environment Semantics: An action changes the part of the shared state that matches the LHS to something that matches the RHS. The rest of the shared state is not touched. Lock Unlock
they specify what an atomic block does to the shared state They also adjust the boundary between local and shared state
the shared state
2 3 5 7 11 13
Shared Local
6 Lock
2 3 5 7 11 13
Shared Local
6 Lock 2 3 5 7 11 13
Shared Local
6
2 3 5 7 11 13
Shared Local
6 Lock 2 3 5 7 11 13
Shared Local
6
2 3 5 7 11 13
Shared Local
6 Lock 2 3 5 7 11 13
Shared Local
6
2 3 5 7 11 13
Shared Local
6 Lock 2 3 5 7 11 13
Shared Local
6
2 3 5 7 11 13
Shared Local
6 Lock 2 3 5 7 11 13
Shared Local
6
2 3 5 7 11 13
Shared Local
6 Lock 2 3 5 7 11 13
Shared Local
6
Where did this node come from? Where did this node go? Add node Delete node
2 3 5 7 11 13
Shared Local
6
Add node
Pessimistic algorithm
2 3 5 7 11 13
Shared
6
Local
Add node
Pessimistic algorithm
2 3 5 7 11 13
Shared
6
Local
Lock node
Pessimistic algorithm
2 3 5 7 11 13
Shared
6
Local
Lock node
Pessimistic algorithm
2 3 5 7 11 13
Shared
6
Local
Delete node
Pessimistic algorithm
2 3 5 7 11 13
Shared Local
6
Delete node
Pessimistic algorithm
2 3 5 7 11 13
Shared Local
6
Now, the node is local; we can safely dispose it. Pessimistic algorithm
2 3 5 7 11 13
Shared Local Now, the node is local; we can safely dispose it. Pessimistic algorithm
Shared Local
13 2 3 5 7 11 6
Optimistic algorithm
Delete node
Shared Local
13 2 3 5 7 11 6
Optimistic algorithm
Delete node
2 7 11 13 5
A A
3
2 7 11 13 5
A A
2 7 11 13 5
12
A A
2 7 11 13 5
12
A A
7 5
A A
7 5
A A
Lock
B
Unlock
B
7 5
A A
Delete node
B B B
Add node
B B
Separation Logic Extended logic P, Q ::= false | emp | e = e′ | e → e′ | ∃x. P | P ⇒ Q | P ∗ Q | P − ⊛ Q h SL P − ⊛ Q ⇐ ⇒ h SL ¬(P − ∗ ¬Q) ⇐ ⇒ ∃h′. (h′ SL P) ∧ (h ⊎ h′ SL Q) p, q ::= P | P | p ∗ q | p ∧ q | p ∨ q | ∃x. p | ∀x. p local shared
Split local state; share global state. l, s P ⇐ ⇒ l SL P l, s P ⇐ ⇒ l = ∅ ∧ (s SL P) l, s p1 ∗ p2 ⇐ ⇒ ∃l1, l2. (l = l1 ⊎ l2) ∧ (l1, s p1) ∧ (l2, s p2)
x → tid, v, t
x → 0, v, t
x → tid, v, t
∗ y → 0, v′, t x → tid, v, y ∗ y → tid, v′, t x → tid, v, t x → tid, v, y ∗ y → tid, v′, t x → tid, v, t ∗ y → tid, v′, t
(precondition, rely, guarantee, postcondition)
Splits local state; Shares global state. ⊢ C1 sat (p1, R ∪ G2, G1, q1) ⊢ C2 sat (p2, R ∪ G1, G2, q2) ⊢ (C1C2) sat (p1 ∗ p2, R, G1 ∪ G2, q1 ∗ q2)
Shared state Local state P2, Q2 precise (P2 Q2) ∈ G ⊢ C sat (P1 ∗ P2, ∅, ∅, Q1 ∗ Q2) ⊢ (atomic C) sat
⊢ (atomic C) sat (p, ∅, G, q) ⊢ (atomic C) sat (p, R, G, q)
iff S stable under P Q ((P − ⊛ S) ∗ Q) ⇒ S