Mechanised Owicki-Gries Proofs for C11 Brijesh Dongol University of - - PDF document

mechanised owicki gries proofs for c11
SMART_READER_LITE
LIVE PREVIEW

Mechanised Owicki-Gries Proofs for C11 Brijesh Dongol University of - - PDF document

Mechanised Owicki-Gries Proofs for C11 Brijesh Dongol University of Surrey Joint work with Sadegh Dalvandi (University of Surrey) Simon Doherty (University of Sheffield) Heike Wehrheim (University of Paderborn) John Derrick (University of


slide-1
SLIDE 1

Mechanised Owicki-Gries Proofs for C11

Brijesh Dongol University of Surrey

Joint work with

Sadegh Dalvandi (University of Surrey) Simon Doherty (University of Sheffield) Heike Wehrheim (University of Paderborn) John Derrick (University of Sheffield)

slide-2
SLIDE 2

A weak memory talk

{talk = weak memory} reaction := listen(talk) {reaction = ∨ reaction = }

slide-3
SLIDE 3

A weak memory talk

{talk = weak memory} reaction := listen(talk) this(talk) {reaction = ∨ reaction = } { reaction = }

slide-4
SLIDE 4

A weak memory talk

{talk = weak memory} reaction := listen(talk) this(talk) {reaction = ∨ reaction = } { reaction = } Turning into — relate weak memory semantics to Hoare logic and Owicki-Gries style proof rules

slide-5
SLIDE 5

Outline

C11 Axiomatic Semantics C11 Operational Semantics C11 Owicki-Gries Proofs in Isabelle

slide-6
SLIDE 6

C11 Axiomatic Semantics

slide-7
SLIDE 7

Axiomatic C11 semantics

Example (Message Passing). Init: f := 0; d := 0; thread 1 thread 2 d := 5; do r1 f f := 1; until r1 = 1; r2 d;

slide-8
SLIDE 8

Axiomatic C11 semantics

Example (Message Passing). Init: f := 0; d := 0; thread 1 thread 2 d := 5; do r1 f f := 1; until r1 = 1; r2 d; In C11, r2 can have a final value 0 — the execution below is allowed

wr1(d, 5) wr1(f, 1) rd2(f, 1) rd2(d, 0) wr(f, 0), wr(d, 0) sb sb sb sb

slide-9
SLIDE 9

Axiomatic C11 semantics

Example (Message Passing). Init: f := 0; d := 0; thread 1 thread 2 d := 5; do r1 f f := 1; until r1 = 1; r2 d; In C11, r2 can have a final value 0 — the execution below is allowed

wr1(d, 5) wr1(f, 1) rd2(f, 1) rd2(d, 0) wr(f, 0), wr(d, 0) sb sb sb sb mo mo rf rf fr

slide-10
SLIDE 10

Axiomatic C11 semantics

Example (Message Passing). Init: f := 0; d := 0; thread 1 thread 2 d := 5; do r1 f f := 1; until r1 = 1; r2 d; In C11, r2 can have a final value 0 — the execution below is allowed

wr1(d, 5) wr1(f, 1) rd2(f, 1) rd2(d, 0) wr(f, 0), wr(d, 0) sb sb sb sb mo mo rf rf fr

Corrected Message Passing. Init: f := 0; d := 0; thread 1 thread 2 d := 5; do r1 A f f :=R 1; until r1 = 1; r2 d;

slide-11
SLIDE 11

Axiomatic C11 semantics

Example (Message Passing). Init: f := 0; d := 0; thread 1 thread 2 d := 5; do r1 f f := 1; until r1 = 1; r2 d; In C11, r2 can have a final value 0 — the execution below is allowed

wr1(d, 5) wr1(f, 1) rd2(f, 1) rd2(d, 0) wr(f, 0), wr(d, 0) sb sb sb sb mo mo rf rf fr

Corrected Message Passing. Init: f := 0; d := 0; thread 1 thread 2 d := 5; do r1 A f f :=R 1; until r1 = 1; r2 d; The following execution is now disallowed

wr1(d, 5) wrR

1(f, 1)

rdA

2(f, 1)

rd2(d, 0) wr(f, 0), wr(d, 0) sb sb sb sb

slide-12
SLIDE 12

Axiomatic C11 semantics

Example (Message Passing). Init: f := 0; d := 0; thread 1 thread 2 d := 5; do r1 f f := 1; until r1 = 1; r2 d; In C11, r2 can have a final value 0 — the execution below is allowed

wr1(d, 5) wr1(f, 1) rd2(f, 1) rd2(d, 0) wr(f, 0), wr(d, 0) sb sb sb sb mo mo rf rf fr

Corrected Message Passing. Init: f := 0; d := 0; thread 1 thread 2 d := 5; do r1 A f f :=R 1; until r1 = 1; r2 d; The following execution is now disallowed

wr1(d, 5) wrR

1(f, 1)

rdA

2(f, 1)

rd2(d, 0) wr(f, 0), wr(d, 0) sb sb sb sb mo mo rf sw fr

slide-13
SLIDE 13

Axiomatic C11 semantics

Example (Message Passing). Init: f := 0; d := 0; thread 1 thread 2 d := 5; do r1 f f := 1; until r1 = 1; r2 d; In C11, r2 can have a final value 0 — the execution below is allowed

wr1(d, 5) wr1(f, 1) rd2(f, 1) rd2(d, 0) wr(f, 0), wr(d, 0) sb sb sb sb mo mo rf rf fr

Corrected Message Passing. Init: f := 0; d := 0; thread 1 thread 2 d := 5; do r1 A f f :=R 1; until r1 = 1; r2 d; The following execution is now disallowed

wr1(d, 5) wrR

1(f, 1)

rdA

2(f, 1)

rd2(d, 0) wr(f, 0), wr(d, 0) sb sb sb sb mo mo rf sw fr

slide-14
SLIDE 14

What about verification?

I Axiomatic semantics useful for certain forms of verification, e.g., SMT, BMC, ... I But how can we link with existing works — Hoare Logic, Owicki/Gries, Rely/Guarantee ? We need an operational semantics for C11

slide-15
SLIDE 15

C11 Operational Semantics

slide-16
SLIDE 16

Point of departure

I Start with operational semantics by Doherty et al (2019) — proved sound and complete with respect to RC11 I For the experts: restrict attention to a fragment of C11 I All operations are either relaxed, write-releasing, or read-acquiring I Do not model fences or release-sequences I Assume no-thin-air, i.e., sb [ rf acyclic I Strategy: construct valid C11 graphs by stepping through program in thread order (without consulting axioms) I Brings us back to well understood (programmer friendly) notion Concurrency = Interleaving of threads I What’s different? I More non-determinism in choosing the next C11 state I Both reads and writes may change state configuration

slide-17
SLIDE 17

Observing a C11 state

Key point. I Each thread has its own observable set of writes I Observable writes can be determined from the current C11 state

slide-18
SLIDE 18

Observing a C11 state

Key point. I Each thread has its own observable set of writes I Observable writes can be determined from the current C11 state

  • Example. Restricting mo [ rf [ fr to a single variable, we have:

w1 w2 w3 w4 w5 r1 r0

1

r00

1

r2 r0

2

r3 r4 r0

4

mo mo mo rf fr rf fr mo rf fr rf fr t1 Hidden t2 Hidden t3 Hidden

I Thread t1 can observe w3, w4, w5 I Thread t2 can observe w2, w3, w4, w5 I Thread t3 can observe w5

slide-19
SLIDE 19

Observing a C11 state

Key point. I Each thread has its own observable set of writes I Observable writes can be determined from the current C11 state

  • Example. Restricting mo [ rf [ fr to a single variable, we have:

w1 w2 w3 w4 w5 r1 r0

1

r00

1

r2 r0

2

r3 r4 r0

4

mo mo mo rf fr rf fr mo rf fr rf fr t1 Hidden t2 Hidden t3 Hidden

I Thread t1 can observe w3, w4, w5 I Thread t2 can observe w2, w3, w4, w5 I Thread t3 can observe w5 Observable set changes as threads interact with the C11 state

slide-20
SLIDE 20

Message passing with “bad” transition

Init: f := 0; d := 0 thread 1 thread 2 d := 5; do r1 f until r1 = 1; f := 1; r2 d; Pre-state

wr1(d, 5) wr1(f, 1) rd2(f, 1) wr(f, 0), wr(d, 0) sb sb sb mo mo rf

slide-21
SLIDE 21

Message passing with “bad” transition

Init: f := 0; d := 0 thread 1 thread 2 d := 5; do r1 f until r1 = 1; f := 1; r2 d; Pre-state

wr1(d, 5) wr1(f, 1) rd2(f, 1) wr(f, 0), wr(d, 0) sb sb sb mo mo rf

Thread 2 can observe both writes to d

slide-22
SLIDE 22

Message passing with “bad” transition

Init: f := 0; d := 0 thread 1 thread 2 d := 5; do r1 f until r1 = 1; f := 1; r2 d; Pre-state

wr1(d, 5) wr1(f, 1) rd2(f, 1) wr(f, 0), wr(d, 0) sb sb sb mo mo rf

Thread 2 can observe both writes to d Possible post-state

wr1(d, 5) wr1(f, 1) rd2(f, 1) rd2(d, 0) wr(f, 0), wr(d, 0) sb sb sb sb mo mo rf rf fr

“Bad” transition with read from wr(d, 0) is possible

slide-23
SLIDE 23

Message passing with release/acquire annotations

Init: f := 0; d := 0 thread 1 thread 2 d := 5; do r1 A 1 until r1 = 1; f :=R 1; r2 := d; Pre-state

wr1(d, 5) wrR

1(f, 1)

rdA

2(f, 1)

wr(f, 0), wr(d, 0) sb sb sb mo mo sw

slide-24
SLIDE 24

Message passing with release/acquire annotations

Init: f := 0; d := 0 thread 1 thread 2 d := 5; do r1 A 1 until r1 = 1; f :=R 1; r2 := d; Pre-state

wr1(d, 5) wrR

1(f, 1)

rdA

2(f, 1)

wr(f, 0), wr(d, 0) sb sb sb mo mo sw

Thread 2 can only

  • bserve wr1(d, 5)
slide-25
SLIDE 25

Message passing with release/acquire annotations

Init: f := 0; d := 0 thread 1 thread 2 d := 5; do r1 A 1 until r1 = 1; f :=R 1; r2 := d; Pre-state

wr1(d, 5) wrR

1(f, 1)

rdA

2(f, 1)

wr(f, 0), wr(d, 0) sb sb sb mo mo sw

Thread 2 can only

  • bserve wr1(d, 5)

Only possible post-state

wr1(d, 5) wrR

1(f, 1)

rdA

2(f, 1)

rd2(d, 5) wr(f, 0), wr(d, 0) sb sb sb sb mo mo rf sw

Only the “good” transition is available

slide-26
SLIDE 26

C11 Owicki-Gries Proofs in Isabelle

slide-27
SLIDE 27

Proof outline for message passing

Init: d := 0; f := 0; d := 5; do r1 A f until r1 = 1; f :=R 1; r2 d;

slide-28
SLIDE 28

Proof outline for message passing

Init: d := 0; f := 0; {d =1 0 ^ d =2 0 ^ f =1 0 ^ f =2 0} {¬(f ⇡2 1) ^ d =1 0} {[f = 1]2(d = 5)} d := 5; do r1 A f until r1 = 1; {¬(f ⇡2 1) ^ d =1 5} {d =2 5} f :=R 1; r2 d; {true} {r2 = 5} {r2 = 5}

slide-29
SLIDE 29

Proof outline for message passing

Init: d := 0; f := 0; {d =1 0 ^ d =2 0 ^ f =1 0 ^ f =2 0} {¬(f ⇡2 1) ^ d =1 0} {[f = 1]2(d = 5)} d := 5; do r1 A f until r1 = 1; {¬(f ⇡2 1) ^ d =1 5} {d =2 5} f :=R 1; r2 d; {true} {r2 = 5} {r2 = 5} Recall the Owicki-Gries technique: ` {P1}C1{Q1}k{P2}C2{Q2} P ) P1 ^ P2 Q1 ^ Q2 ) Q ` {P} ({P1}C1{Q1}k{P2}C2{Q2}) {Q} ` {P1}C1{Q1} {P1}C1{Q1} is interference free wrt C2 ` {P2}C2{Q2} {P2}C2{Q2} is interference free wrt C1 ` {P1}C1{Q1}k{P2}C2{Q2}

slide-30
SLIDE 30

Proof outline for message passing

Init: d := 0; f := 0; {d =1 0 ^ d =2 0 ^ f =1 0 ^ f =2 0} {¬(f ⇡2 1) ^ d =1 0} {[f = 1]2(d = 5)} d := 5; do r1 A f until r1 = 1; {¬(f ⇡2 1) ^ d =1 5} {d =2 5} f :=R 1; r2 d; {true} {r2 = 5} {r2 = 5} I The C11 state is a special implicit variable in the program I Assertions are predicates over program states (including the C11 states)

slide-31
SLIDE 31

Proof outline for message passing

Init: d := 0; f := 0; {d =1 0 ^ d =2 0 ^ f =1 0 ^ f =2 0} {¬(f ⇡2 1) ^ d =1 0} {[f = 1]2(d = 5)} d := 5; do r1 A f until r1 = 1; {¬(f ⇡2 1) ^ d =1 5} {d =2 5} f :=R 1; r2 d; {true} {r2 = 5} {r2 = 5} I The C11 state is a special implicit variable in the program I Assertions are predicates over program states (including the C11 states) I We define special assertions on C11 state: x ⇡t v $ Thread t possibly observes value v for x x =t v $ Thread t definitely observes value v for x [x = u]t(y = v) $ If thread t observes x = u then it will definitely observe y = v

slide-32
SLIDE 32

Hoare-style axioms

I Rules for compound statements are exactly as in Hoare logic I But have a new set of basic axioms for (atomic) reads and writes (76 at last count), e.g., d obs WrX set {x =t u} [x := v]t {x =t v} not pobs RdA pres {¬(x ⇡t u)} [v A y]t0 {¬(x ⇡t u)} c obs WrR pres z 6= y z 6= x x 6= y {[x = u]t(y = v)} [z :=R w]t {[x = u]t(y = v)}

slide-33
SLIDE 33

Hoare-style axioms

I Rules for compound statements are exactly as in Hoare logic I But have a new set of basic axioms for (atomic) reads and writes (76 at last count), e.g., d obs WrX set {x =t u} [x := v]t {x =t v} not pobs RdA pres {¬(x ⇡t u)} [v A y]t0 {¬(x ⇡t u)} c obs WrR pres z 6= y z 6= x x 6= y {[x = u]t(y = v)} [z :=R w]t {[x = u]t(y = v)} I All basic axioms verified in Isabelle, e.g., corollary d_obs_RdX_other: "wfs σ = ) x 6= y = ) [x =t u] σ = ) σ [v y]t σ’ = ) [x =t u] σ’" by (metis RdX_def avar.simps(1) d_obs_other)

slide-34
SLIDE 34

C11 Owicki-Gries in Isabelle

I Owicki-Gries theory is included in standard Isabelle distribution (Nieto and Nipkow, 2002)

slide-35
SLIDE 35

C11 Owicki-Gries in Isabelle

I Owicki-Gries theory is included in standard Isabelle distribution (Nieto and Nipkow, 2002) I We have extended Nieto-Nipkow’s WHILE language with relaxed / release-acquire statements I C11 state is embedded in the standard state, e.g., for message passing record MP = d :: V f :: V r1 :: V r2 :: V σ :: C11_state I C11 states updated w.r.t. our operational semantics

slide-36
SLIDE 36

Proof of message passing in Isabelle

lemma MessagePassing: "k- { | (wfs ´σ ´f ´d) ^ [ ´d =1 0 ]´σ ^ [ ´d =2 0 ]´σ ^ [ ´f =1 0 ]´σ ^ [ ´f =2 0 ]´σ | } COBEGIN { | (wfs ´σ ´f ´d) ^ ¬[ ´f ⇡2 1 ]´σ ^ [ ´d =1 0 ]´σ | } <´d [´σ] :=1 5> ;; { | (wfs ´σ ´f ´d) ^ ¬[ ´f ⇡2 1 ]´σ ^ [ ´d =1 5 ]´σ | } <´f [´σ]R :=1 1> { | [ ´d =1 5]´σ | } k { | (wfs ´σ ´f ´d) ^ [ ´f = 1 ]2( | ´d = 5 | )´σ | } DO { | (wfs ´σ ´f ´d) ^ [ ´f = 1 ]2( | ´d = 5 | )´σ | } <´r1 [´σ]A 2 ´f> UNTIL ´r1 = 1 INV { | (wfs ´σ ´f ´d) ^ [ ´f = 1 ]2( | ´d = 5 | )´σ ^ (´r1 = 1 ! [ ´d =2 5 ]´σ) | } OD;; { | (wfs ´σ ´f ´d) ^ [ ´d =2 5 ]´σ | } <´r2 [´σ] 2 ´d> { | ´r2 = 5 | } COEND { | ´r2 = 5 | }" apply oghoare apply auto using d_obs_diff_false zero_neq_numeral by blast+

slide-37
SLIDE 37

Case study 2: Peterson’s mutual exclusion

Init: flag1 := false; flag2 := false; turn = 1 thread 1 thread 2 flag1 := true; flag2 := true; swapRA(turn, 2); swapRA(turn, 1); do do r1 A flag2; r3 A flag1; r2 turn; r4 turn; until ¬r1 _ r2 = 1; until ¬r3 _ r4 = 2; //CS1; //CS2; flag1 :=R false; flag2 :=R false; I Encoded and verified in Isabelle I Requires new types of assertions describing the C11 state I Same auxiliary variable as proof in sequentially consistent setting (Apt and Olderog, 2009) I However, proof requires more work beyond oghoare and auto — currently investigating ways to speed this up

slide-38
SLIDE 38

Conclusions

I Operational semantics by Doherty et al (2019) makes deductive verification possible for (a realistic fragment of) C11 I Verification based on well-understood Owicki-Gries theory I Straightforward extension of Nieto and Nipkow’s mechanisations of Owicki-Gries in Isabelle I Paper describing these works is forthcoming I Currently investigating links with distributed correctness (with Philippa Gardner) I Any questions, please e-mail: b.dongol@surrey.ac.uk