rely-guarantee - github pagestimeline 3 hoare logic rely-guarantee separation logic concurrent...

35
Rely-Guarantee Lecturer: John Wickerson 1

Upload: others

Post on 25-Mar-2020

4 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Rely-Guarantee - GitHub PagesTimeline 3 Hoare Logic Rely-Guarantee Separation Logic Concurrent Separation Logic RGSep Owicki-Gries method Deny-Guarantee, Concurrent Abstract Predicates,

Rely-Guarantee

Lecturer: John Wickerson

1

Page 2: Rely-Guarantee - GitHub PagesTimeline 3 Hoare Logic Rely-Guarantee Separation Logic Concurrent Separation Logic RGSep Owicki-Gries method Deny-Guarantee, Concurrent Abstract Predicates,

Lecture plan1. Setting the stage

2. Introducing Rely-Guarantee

3. Rely-Guarantee vs. CSL

4. Limitations of Rely-Guarantee

2

Page 3: Rely-Guarantee - GitHub PagesTimeline 3 Hoare Logic Rely-Guarantee Separation Logic Concurrent Separation Logic RGSep Owicki-Gries method Deny-Guarantee, Concurrent Abstract Predicates,

Timeline

3

Hoare Logic

Rely-Guarantee

Separation Logic

ConcurrentSeparation Logic

RGSep

Owicki-Gries method

Deny-Guarantee, Concurrent Abstract Predicates, Local Rely-Guarantee...

next lecture

this lecture

later lectures

Page 4: Rely-Guarantee - GitHub PagesTimeline 3 Hoare Logic Rely-Guarantee Separation Logic Concurrent Separation Logic RGSep Owicki-Gries method Deny-Guarantee, Concurrent Abstract Predicates,

Parallel Rule

4

⊦ {P1} C1 {Q1} ⊦ {P2} C2 {Q2}

C1 doesn’t affect C2’s proofC2 doesn’t affect C1’s proof

⊦ {P1 ∧ P2} C1 ‖ C2 {Q1 ∧ Q2}

Page 5: Rely-Guarantee - GitHub PagesTimeline 3 Hoare Logic Rely-Guarantee Separation Logic Concurrent Separation Logic RGSep Owicki-Gries method Deny-Guarantee, Concurrent Abstract Predicates,

FindFirstPositive

5

while i<min(x,y) do if A[i]>0 then x:=i else i:=i+2 end if end while

while j<min(x,y) do if A[j]>0 then y:=j else j:=j+2 end ifend while

r := min(x,y)

i := 0; j := 1; x := |A|; y := |A|;

Page 6: Rely-Guarantee - GitHub PagesTimeline 3 Hoare Logic Rely-Guarantee Separation Logic Concurrent Separation Logic RGSep Owicki-Gries method Deny-Guarantee, Concurrent Abstract Predicates,

6

{P1}while i<min(x,y) do {P1 ∧ i<x ∧ i<|A|} if A[i]>0 then {P1 ∧ i<x ∧ i<|A| ∧ A[i]>0} x:=i {P1} else {P1 ∧ i<x ∧ i<|A| ∧ A[i]≤0} i:=i+2 {P1} end if {P1}end while {P1 ∧ i≥min(x,y)}

{P2}while j<min(x,y) do {P2 ∧ j<y ∧ j<|A|} if A[j]>0 then {P2 ∧ j<y ∧ j<|A| ∧ A[j]>0} y:=j {P2} else {P2 ∧ j<y ∧ j<|A| ∧ A[j]≤0} j:=j+2 {P2} end if {P2}end while {P2 ∧ j≥min(x,y)}

{P1 ∧ P2}

r := min(x,y)

i := 0; j := 1; x := |A|; y := |A|;

{P1 ∧ P2 ∧ i≥min(x,y) ∧ j≥min(x,y)}

{r≤|A| ∧ (∀k. 0≤k<r ⇒ A[k]≤0) ∧ (r<|A| ⇒ A[r]>0)}

where P1 ≝ x≤|A| ∧ (∀k. 0≤k<i ∧ k even ⇒ A[k]≤0) ∧ i even ∧ (x<|A| ⇒ A[x]>0) and P2 ≝ y≤|A| ∧ (∀k. 0≤k<j ∧ k odd ⇒ A[k]≤0) ∧ j odd ∧ (y<|A| ⇒ A[y]>0)

Page 7: Rely-Guarantee - GitHub PagesTimeline 3 Hoare Logic Rely-Guarantee Separation Logic Concurrent Separation Logic RGSep Owicki-Gries method Deny-Guarantee, Concurrent Abstract Predicates,

Compositionality

7

⊦ {P} C1 {Q}⊦ {Q} C2 {R}

⊦ {P} C1;C2 {R} ⊦ {P} C {Q} mods(C) ∩ fv(R) = ∅⊦ {P ∗ R} C {Q ∗ R}⊦ {P} C1 {Q}[X1]

⊦ {Q} C2 {R}[X2]⊦ {P} C1;C2 {R}[X1 ∪ X2] ⊦ {P} C {Q}[X]

X ∩ fv(R) = ∅⊦ {P ∗ R} C {Q ∗ R}[X]

Page 8: Rely-Guarantee - GitHub PagesTimeline 3 Hoare Logic Rely-Guarantee Separation Logic Concurrent Separation Logic RGSep Owicki-Gries method Deny-Guarantee, Concurrent Abstract Predicates,

Lecture plan1. Setting the stage

2. Introducing Rely-Guarantee

3. Rely-Guarantee vs. CSL

4. Limitations of Rely-Guarantee

8

Page 9: Rely-Guarantee - GitHub PagesTimeline 3 Hoare Logic Rely-Guarantee Separation Logic Concurrent Separation Logic RGSep Owicki-Gries method Deny-Guarantee, Concurrent Abstract Predicates,

Rely-Guarantee

9

R,G ⊦ {P} C {Q}

R{

G

{

P

Q

Q

Q

R{ R{ R{

R{ R{

G

{G

{

Page 10: Rely-Guarantee - GitHub PagesTimeline 3 Hoare Logic Rely-Guarantee Separation Logic Concurrent Separation Logic RGSep Owicki-Gries method Deny-Guarantee, Concurrent Abstract Predicates,

Rely-Guarantee

10

R,G ⊦ {P} C {Q}

IF:(1) the initial state satisfies P, and(2) every state change by another thread is in R,

THEN:(1) every final state satisfies Q, and(2) every state change by C is in G

Page 11: Rely-Guarantee - GitHub PagesTimeline 3 Hoare Logic Rely-Guarantee Separation Logic Concurrent Separation Logic RGSep Owicki-Gries method Deny-Guarantee, Concurrent Abstract Predicates,

Parallel Rule

11

R ∪ G2,G1 ⊦ {P1} C1 {Q1} R ∪ G1,G2 ⊦ {P2} C2 {Q2}

R, G1 ∪ G2 ⊦ {P1 ∧ P2} C1 ‖ C2 {Q1 ∧ Q2}

Page 12: Rely-Guarantee - GitHub PagesTimeline 3 Hoare Logic Rely-Guarantee Separation Logic Concurrent Separation Logic RGSep Owicki-Gries method Deny-Guarantee, Concurrent Abstract Predicates,

Rule of Consequence

12

R ⊆ R’ R’,G’ ⊦ {P} C {Q} G’ ⊆ GR,G ⊦ {P} C {Q}

Page 13: Rely-Guarantee - GitHub PagesTimeline 3 Hoare Logic Rely-Guarantee Separation Logic Concurrent Separation Logic RGSep Owicki-Gries method Deny-Guarantee, Concurrent Abstract Predicates,

Basic commands

13

⊦ {P} c {Q}R,G ⊦ {P} c {Q}

R{G

{R{ R{ R{R{

P QP P Q Q Q

P is stable under R

QP c

the effect of c is contained in GQ is stable under R

∀σ,σ’. P(σ) ∧ (σ,σ’)∈⟦c⟧ ⇒ G(σ,σ’)

∀σ,σ’. P(σ) ∧ R(σ,σ’) ⇒ P(σ’)

Page 14: Rely-Guarantee - GitHub PagesTimeline 3 Hoare Logic Rely-Guarantee Separation Logic Concurrent Separation Logic RGSep Owicki-Gries method Deny-Guarantee, Concurrent Abstract Predicates,

Making assertions stable

14

R,G ⊦ {x=2} x := x+1 {x=3}

x=n ⇝ x=n-1 x=n ⇝ x=n+1

{(σ,σ’) | ∃n. σ(x) = n ∧

σ’=σ[x↦n-1]}

Page 15: Rely-Guarantee - GitHub PagesTimeline 3 Hoare Logic Rely-Guarantee Separation Logic Concurrent Separation Logic RGSep Owicki-Gries method Deny-Guarantee, Concurrent Abstract Predicates,

Making assertions stable

15

R,G ⊦ {x≤2} x := x+1 {x≤3}

x=n ⇝ x=n-1 x=n ⇝ x=n+1

{(σ,σ’) | ∃n. σ(x) = n ∧

σ’=σ[x↦n-1]}

Page 16: Rely-Guarantee - GitHub PagesTimeline 3 Hoare Logic Rely-Guarantee Separation Logic Concurrent Separation Logic RGSep Owicki-Gries method Deny-Guarantee, Concurrent Abstract Predicates,

Quiz

16

Strongest stable weaker assertion

Weakest stable stronger assertion

x=0 x≥0 false

x≠0 true x>0

R ≝ x=n ⇝ x=n+1

Page 17: Rely-Guarantee - GitHub PagesTimeline 3 Hoare Logic Rely-Guarantee Separation Logic Concurrent Separation Logic RGSep Owicki-Gries method Deny-Guarantee, Concurrent Abstract Predicates,

17

Stabilisation

Pwssa(P)

sswa(P)

Page 18: Rely-Guarantee - GitHub PagesTimeline 3 Hoare Logic Rely-Guarantee Separation Logic Concurrent Separation Logic RGSep Owicki-Gries method Deny-Guarantee, Concurrent Abstract Predicates,

18

G2, G1 ⊦ {P1}while i<min(x,y) do {P1 ∧ i<x ∧ i<|A|} if A[i]>0 then {P1 ∧ i<x ∧ i<|A| ∧ A[i]>0} x:=i {P1} else {P1 ∧ i<x ∧ i<|A| ∧ A[i]≤0} i:=i+2 {P1} end if {P1}end while {P1 ∧ i≥min(x,y)}

G1, G2 ⊦ {P2}while j<min(x,y) do {P2 ∧ j<y ∧ j<|A|} if A[j]>0 then {P2 ∧ j<y ∧ j<|A| ∧ A[j]>0} y:=j {P2} else {P2 ∧ j<y ∧ j<|A| ∧ A[j]≤0} j:=j+2 {P2} end if {P2}end while {P2 ∧ j≥min(x,y)}

{P1 ∧ P2}

r := min(x,y)

i := 0; j := 1; x := |A|; y := |A|;

{P1 ∧ P2 ∧ i≥min(x,y) ∧ j≥min(x,y)}

{r≤|A| ∧ (∀k. 0≤k<r ⇒ A[k]≤0) ∧ (r<|A| ⇒ A[r]>0)}

where P1 ≝ x≤|A| ∧ (∀k. 0≤k<i ∧ k even ⇒ A[k]≤0) ∧ i even ∧ (x<|A| ⇒ A[x]>0) and P2 ≝ y≤|A| ∧ (∀k. 0≤k<j ∧ k odd ⇒ A[k]≤0) ∧ j odd ∧ (y<|A| ⇒ A[y]>0) and G1 ≝ {(σ,σ’) | σ’(y) = σ(y) ∧ σ’(j) = σ(j) ∧ σ’(x) ≤ σ(x)} and G2 ≝ {(σ,σ’) | σ’(x) = σ(x) ∧ σ’(i) = σ(i) ∧ σ’(y) ≤ σ(y)}

Page 19: Rely-Guarantee - GitHub PagesTimeline 3 Hoare Logic Rely-Guarantee Separation Logic Concurrent Separation Logic RGSep Owicki-Gries method Deny-Guarantee, Concurrent Abstract Predicates,

Lecture plan1. Setting the stage

2. Introducing Rely-Guarantee

3. Rely-Guarantee vs. CSL

4. Limitations of Rely-Guarantee

19

Page 20: Rely-Guarantee - GitHub PagesTimeline 3 Hoare Logic Rely-Guarantee Separation Logic Concurrent Separation Logic RGSep Owicki-Gries method Deny-Guarantee, Concurrent Abstract Predicates,

20

J ⊧ {P} C {Q}

• initial state satisfies P, and• every state change by

another thread preserves J, ⇓• C doesn’t fault, and• final states satisfy Q, and• every state change by C

preserves J

Comparison

R,G ⊧ {P} C {Q}

• initial state satisfies P, and• every state change by

another thread is in R, ⇓• C doesn’t fault, and• final states satisfy Q, and• every state change by C is

in G

Concurrent Separation Logic Rely-Guarantee

Page 21: Rely-Guarantee - GitHub PagesTimeline 3 Hoare Logic Rely-Guarantee Separation Logic Concurrent Separation Logic RGSep Owicki-Gries method Deny-Guarantee, Concurrent Abstract Predicates,

Verify this...

21

atomic ( [x] := [x]+1 ) atomic ( [x] := [x]+2 ){x↦0}

{x↦3}

Page 22: Rely-Guarantee - GitHub PagesTimeline 3 Hoare Logic Rely-Guarantee Separation Logic Concurrent Separation Logic RGSep Owicki-Gries method Deny-Guarantee, Concurrent Abstract Predicates,

Try CSL...

22

atomic ( [x] := [x]+1 )

atomic (

[x] := [x]+2

)

{emp}

{emp}

{J}

{J}

{emp}

{emp}

{J}

{J}

Page 23: Rely-Guarantee - GitHub PagesTimeline 3 Hoare Logic Rely-Guarantee Separation Logic Concurrent Separation Logic RGSep Owicki-Gries method Deny-Guarantee, Concurrent Abstract Predicates,

Try CSL...

23

atomic ( [x] := [x]+1 )

atomic (

[x] := [x]+2

)

{emp}

{emp}

{emp}

{emp}

{x↦0}

{∃n≥0. x↦n}

{∃n≥0. x↦n}

{∃n≥0. x↦n}

{∃n≥0. x↦n}

{∃n≥0. x↦n}

Page 24: Rely-Guarantee - GitHub PagesTimeline 3 Hoare Logic Rely-Guarantee Separation Logic Concurrent Separation Logic RGSep Owicki-Gries method Deny-Guarantee, Concurrent Abstract Predicates,

Try CSL + auxiliary state...

24

atomic ( [x] := [x]+1; )

atomic (

[x] := [x]+2;

)

{a≐0}

{a≐1}

{b≐0}

{b≐2}

{x↦0}

{x↦a+b ∗ a≐0}

{x↦a+b ∗ a≐1}

{x↦a+b ∗ b≐0}

{x↦a+b ∗ b≐2}

a := 0; b := 0;{x↦a+b ∗ a≐0 ∗ b≐0}

{x↦a+b ∗ a≐1 ∗ b≐2}

a := 1 b := 2

Page 25: Rely-Guarantee - GitHub PagesTimeline 3 Hoare Logic Rely-Guarantee Separation Logic Concurrent Separation Logic RGSep Owicki-Gries method Deny-Guarantee, Concurrent Abstract Predicates,

Try Rely-Guarantee...

25

atomic ( [x] := [x]+1 )

atomic ( [x] := [x]+2 )

{x↦0 ∨ x↦2}

{x↦1 ∨ x↦3}

{x↦0 ∨ x↦1}

{x↦3}

{x↦0}

{x↦2 ∨ x↦3}

G2, G1 ⊦ G1, G2 ⊦

where G1 ≝ (x↦0 ⇝ x↦1) ∪ (x↦2 ⇝ x↦3) and G2 ≝ (x↦0 ⇝ x↦2) ∪ (x↦1 ⇝ x↦3)

Page 26: Rely-Guarantee - GitHub PagesTimeline 3 Hoare Logic Rely-Guarantee Separation Logic Concurrent Separation Logic RGSep Owicki-Gries method Deny-Guarantee, Concurrent Abstract Predicates,

Lecture plan1. Setting the stage

2. Introducing Rely-Guarantee

3. Rely-Guarantee vs. CSL

4. Limitations of Rely-Guarantee

26

Page 27: Rely-Guarantee - GitHub PagesTimeline 3 Hoare Logic Rely-Guarantee Separation Logic Concurrent Separation Logic RGSep Owicki-Gries method Deny-Guarantee, Concurrent Abstract Predicates,

Verify this...

27

atomic ( [x] := [x]+1 ) atomic ( [x] := [x]+1 ){x↦0}

{x↦2}

Page 28: Rely-Guarantee - GitHub PagesTimeline 3 Hoare Logic Rely-Guarantee Separation Logic Concurrent Separation Logic RGSep Owicki-Gries method Deny-Guarantee, Concurrent Abstract Predicates,

Try Rely-Guarantee...

28

atomic ( [x] := [x]+1 )

atomic ( [x] := [x]+1 )

{x↦0 ∨ x↦1}

{x↦1 ∨ x↦2}

{x↦0 ∨ x↦1}

{x↦1 ∨ x↦2}

{x↦0}

{x↦1 ∨ x↦2}

G2, G1 ⊦ G1, G2 ⊦

where G1,G2 ≝ (x↦0 ⇝ x↦1) ∪ (x↦1 ⇝ x↦2)

not stable

Page 29: Rely-Guarantee - GitHub PagesTimeline 3 Hoare Logic Rely-Guarantee Separation Logic Concurrent Separation Logic RGSep Owicki-Gries method Deny-Guarantee, Concurrent Abstract Predicates,

Try Rely-Guarantee...

29

atomic ( [x] := [x]+1 )

atomic ( [x] := [x]+1 )

{∃n≥0. x↦n}

{∃n≥1. x↦n}

{∃n≥0. x↦n}

{∃n≥1. x↦n}

{x↦0}

{∃n≥1. x↦n}

G2, G1 ⊦ G1, G2 ⊦

where G1,G2 ≝ (x↦n ⇝ x↦n+1)

Page 30: Rely-Guarantee - GitHub PagesTimeline 3 Hoare Logic Rely-Guarantee Separation Logic Concurrent Separation Logic RGSep Owicki-Gries method Deny-Guarantee, Concurrent Abstract Predicates,

Abstracting the environmentBy encoding the environment as a rely, we forget:

• the order in which actions can happen;

• which thread performs which action; and

• how many times each action is performed.

30

Page 31: Rely-Guarantee - GitHub PagesTimeline 3 Hoare Logic Rely-Guarantee Separation Logic Concurrent Separation Logic RGSep Owicki-Gries method Deny-Guarantee, Concurrent Abstract Predicates,

Abstracting the environment

31

c1

c2

Thread 1

c3

c4

Thread 2

c5

c6

Thread 3

Page 32: Rely-Guarantee - GitHub PagesTimeline 3 Hoare Logic Rely-Guarantee Separation Logic Concurrent Separation Logic RGSep Owicki-Gries method Deny-Guarantee, Concurrent Abstract Predicates,

Abstracting the environment

32

Page 33: Rely-Guarantee - GitHub PagesTimeline 3 Hoare Logic Rely-Guarantee Separation Logic Concurrent Separation Logic RGSep Owicki-Gries method Deny-Guarantee, Concurrent Abstract Predicates,

Abstracting the environment

33

c1

c2

Thread 1

R*

R*

R*

p stable under R ⟺

⊦ {p} R* {p}

Page 34: Rely-Guarantee - GitHub PagesTimeline 3 Hoare Logic Rely-Guarantee Separation Logic Concurrent Separation Logic RGSep Owicki-Gries method Deny-Guarantee, Concurrent Abstract Predicates,

References• Susan Owicki and David Gries. An Axiomatic Proof Technique

for Parallel Programs. Acta Informatica, 1976. Available from SpringerLink.

• Joey Coleman and Cliff Jones. A structural proof of the soundness of rely/guarantee rules. Journal of Logic and Computation, 2007. Available from: http://homepages.cs.ncl.ac.uk/j.w.coleman/papers/colemanjones-rg-soundness.pdf

• Viktor Vafeiadis. Modular fine-grained concurrency verification. PhD thesis, University of Cambridge, 2007. Available from:http://www.cl.cam.ac.uk/techreports/UCAM-CL-TR-726.html

34

Contains proof of FindFirstPositive

in RG.

Contains proof of FindFirstPositive using Owicki-Gries

method.

Clear and comprehensive

introduction to Rely-Guarantee

Page 35: Rely-Guarantee - GitHub PagesTimeline 3 Hoare Logic Rely-Guarantee Separation Logic Concurrent Separation Logic RGSep Owicki-Gries method Deny-Guarantee, Concurrent Abstract Predicates,

Summary• Owicki-Gries method

• Rely-Guarantee

• Stability

• Relating Rely-Guarantee with CSL

• Limitations of Rely-Guarantee

• NEXT LECTURE: RGSep...

35