noam rinetzky lecture 7: axiomatic semantics - concurrency

36
Program Analysis and Verification 0368-4479 http://www.cs.tau.ac.il/~maon/teaching/2013-2014/paav/pa av1314b.html Noam Rinetzky Lecture 7: Axiomatic Semantics - Concurrency Slides credit: Roman Manevich, Mooly Sagiv, Eran Yahav

Upload: ama

Post on 24-Feb-2016

58 views

Category:

Documents


0 download

DESCRIPTION

Program Analysis and Verification 0368- 4479 http://www.cs.tau.ac.il/~maon/teaching/2013-2014/paav/paav1314b.html. Noam Rinetzky Lecture 7: Axiomatic Semantics - Concurrency. Slides credit: Roman Manevich , Mooly Sagiv , Eran Yahav. Good manners. Mobiles. Home Work Assignment #1 & #2. - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Noam Rinetzky Lecture 7: Axiomatic Semantics - Concurrency

Program Analysis and Verification

0368-4479http://www.cs.tau.ac.il/~maon/teaching/2013-2014/paav/paav1314b.html

Noam Rinetzky

Lecture 7: Axiomatic Semantics - Concurrency

Slides credit: Roman Manevich, Mooly Sagiv, Eran Yahav

Page 2: Noam Rinetzky Lecture 7: Axiomatic Semantics - Concurrency

Good manners• Mobiles

Page 3: Noam Rinetzky Lecture 7: Axiomatic Semantics - Concurrency

Home Work Assignment #1 & #2

• #1 due today– Modulo justified extensions

• #2 Wednesday– Due lesson 10

Page 4: Noam Rinetzky Lecture 7: Axiomatic Semantics - Concurrency

Axiomatic Semantics (Hoare Logic)• Programming Language– Syntax (skip | S1;S2 | … )– Semantics (e.g., states ∑ + Stmts C, s s)

• Assertions – Syntax ( x = 3 | x < y + a | …)– Semantics ( P ∑ alt. s ⟦ ⟧ ⊆ p P)

• Judgments – {P} C {Q}, [P] C [Q]– p {P} C {Q} : s, s’ . (s p P C, ss’) s’pQ)

• Inference rules• Proofs

Page 5: Noam Rinetzky Lecture 7: Axiomatic Semantics - Concurrency

Axiomatic semantics for While { P[a/x] } x := a { P }[assp]

{ P } skip { P }[skipp]

{ P } S1 { Q }, { Q } S2 { R } { P } S1; S2 { R }[compp]

{ b P } S1 { Q }, { b P } S2 { Q } { P } if b then S1 else S2 { Q }[ifp]

{ b P } S { P } { P } while b do S {b P }[whilep]

{ P’ } S { Q’ } { P } S { Q }[consp] if PP’ and Q’Q

Inference rule for every composed statement

Axiom for every primitive statement

Page 6: Noam Rinetzky Lecture 7: Axiomatic Semantics - Concurrency

Factorial proof

W = while (x1) do (y:=y*x; x:=x–1)

INV = x > 0 (y x! = n! n x)

{ INV[x-1/x][y*x/y] } y:=y*x; x:=x–1 {INV}

{ INV[x-1/x] } x:=x-1 {INV}

{ INV } W {x=1 INV }{ INV[1/y] } y:=1 { INV }

{ INV[x-1/x][y*x/y] } y:=y*x { INV[x-1/x] }

{x1 INV } y:=y*x; x:=x–1 { INV }

[comp]

[cons]

[while]

[cons]{ INV } W { y=n! n>0 }{ x=n } y:=1 { INV }

[cons]

{ x=n } while (x1) do (y:=y*x; x:=x–1) { y=n! n>0 }

[comp]

Goal: { x=n } y:=1; while (x1) do (y:=y*x; x:=x–1) { y=n! n>0 }

Page 7: Noam Rinetzky Lecture 7: Axiomatic Semantics - Concurrency

Soundness

• The inference system is sound:– p { P } C { Q } implies p { P } C { Q }

Page 8: Noam Rinetzky Lecture 7: Axiomatic Semantics - Concurrency

Soundness and completeness

• The inference system is sound:– p { P } C { Q } implies p { P } C { Q }

• The inference system is relatively complete:– p { P } C { Q } implies p { P } C { Q }• ∀A,B. A B ⇒• Assertion language is expressive enough

Page 9: Noam Rinetzky Lecture 7: Axiomatic Semantics - Concurrency

While + ConcurrencyAbstract syntax:a ::= n | x | a1 + a2 | a1 a2 | a1 – a2

b ::= true | false| a1 = a2 | a1 a2 | b | b1 b2

S ::= x := a | skip | S1; S2| if b then S1 else S2

| while b do S| cobegin S1‖ …‖Sn coend

Page 10: Noam Rinetzky Lecture 7: Axiomatic Semantics - Concurrency

Proofs

{P1 Λ P1 } S1 ‖ S2 { Q2 Λ Q2 }

{ P1 } S1 { Q2 } { P2 } S2 { Q2 }

… …

Challenge: Interference

Page 11: Noam Rinetzky Lecture 7: Axiomatic Semantics - Concurrency

Disjoint Parallelism

{P1 Λ P1 } S1 ‖ S2 { Q2 Λ Q2 }

{ P1 } S1 { Q2 } { P2 } S2 { Q2 }

FV(P1,S1,Q1) ∩ FV(P2,S2,Q2) = ∅

Page 12: Noam Rinetzky Lecture 7: Axiomatic Semantics - Concurrency

Global Invariant

I {⊢ P1 Λ P1 } S1 ‖ S2 { Q2 Λ Q2 }

I { ⊢ P1 } S1 { Q2 } I { ⊢ P2 } S2 { Q2 }

Page 13: Noam Rinetzky Lecture 7: Axiomatic Semantics - Concurrency

Global Invariant

I {⊢ P1 Λ P1 } S1 ‖ S2 { Q2 Λ Q2 }

I { ⊢ P1 } S1 { Q2 } I { ⊢ P2 } S2 { Q2 }

I { ⊢ P } S1; S2 { R }

I { ⊢ P } S1 { Q } I { ⊢ Q } S2 { R }

Page 14: Noam Rinetzky Lecture 7: Axiomatic Semantics - Concurrency

Global Invariant

I {⊢ P1 Λ P1 } S1 ‖ S2 { Q2 Λ Q2 }

I { ⊢ P1 } S1 { Q2 } I { ⊢ P2 } S2 { Q2 }

I { ⊢ P } S1; S2 { Q }

I { ⊢ P } S1 { R } I { ⊢ R } S2 { Q }

I { ⊢ P } S { Q }

{ P Λ I } S { Q Λ I }

Page 15: Noam Rinetzky Lecture 7: Axiomatic Semantics - Concurrency

Owicky-Gries• A command C with a precondition pre(C) does not

interfere with the proof of {P} S {Q} if:– {Q pre(C) } C {Q}– For any S’ “∈” S: {pre(S’) pre(C) } C {pre(S’)}

• {P1} C1 {Q1} ... {Pk} Ck {Qk} are interference free if ∀ i j and ∀x:=a “∈” Ci ,

x:=a does not interfere with {Pj} Cj {Qj}

Page 16: Noam Rinetzky Lecture 7: Axiomatic Semantics - Concurrency

Parallel Composition Rule

I {⊢ P1 Λ P1 } S1 ‖ S2 { Q2 Λ Q2 }

I { ⊢ P1 } S1 { Q2 } I { ⊢ P2 } S2 { Q2 }

{P1 }C1 }Q1{ ... }Pk{ Ck }Qk{ a re interference free

Page 17: Noam Rinetzky Lecture 7: Axiomatic Semantics - Concurrency

Owicky-Gries: Limiations• Checking interference can be hard

– Non-compositionality• Until you finished the local proofs cannot check interference• Proofs need to be “saved”

– Hard to handle libraries and missing code

• A non-standard meaning of Hoare triples– Depends on the interference of other threads with the proof– Soundness is non-trivial

• Completeness depends on auxiliary variables

Page 18: Noam Rinetzky Lecture 7: Axiomatic Semantics - Concurrency

Rely / Guarantee

• Aka assume Guarantee

• Cliff Jones

• Main idea: Modular capture of interference– Compositional proofs

Page 19: Noam Rinetzky Lecture 7: Axiomatic Semantics - Concurrency

Commands as relations

• It is convenient to view the meaning of commands as relations between pre-states and post-states

• In {P} C {Q}– P is a one state predicate– Q is a two-state predicate• Recall auxiliary variables

• Example– {true} x := x + 1 {x= x + 1}

Page 20: Noam Rinetzky Lecture 7: Axiomatic Semantics - Concurrency

IntuitionC

G

Hoare: { P } S { Q } ~ {P} {Q} ⇒⇒⇒⇒C

R/G: R,G { ⊢ P } S { Q } ~ {P} ⇒⇒⇒⇒ {Q} ⇒⇒⇒GR R R G G

Page 21: Noam Rinetzky Lecture 7: Axiomatic Semantics - Concurrency

From one- to two-state relations

• p(, ) =p()• p(, ) =p()• A single state predicate p is preserved by a

two-state relation R if– p R p– , : p() R(, ) p()

Page 22: Noam Rinetzky Lecture 7: Axiomatic Semantics - Concurrency

Operations on Relations

• (P;Q)(, )=:P(, ) Q(, )• ID(, )= (=)• R*=IDR (R;R) (R;R;R) …

Page 23: Noam Rinetzky Lecture 7: Axiomatic Semantics - Concurrency

Formulas

• ID(x) = (x = x)• ID(p) =(p p)• Preserve (p)= p p

Page 24: Noam Rinetzky Lecture 7: Axiomatic Semantics - Concurrency

Informal Semantics• c (p, R, G, Q)– For every state such that p:

• Every execution of c on state with (potential) interventions which satisfy R results in a state such that (, ) Q

• The execution of every atomic sub-command of c on any possible intermediate state satisfies G

Page 25: Noam Rinetzky Lecture 7: Axiomatic Semantics - Concurrency

Informal Semantics• c (p, R, G, Q)– For every state such that p:

• Every execution of c on state with (potential) interventions which satisfy R results in a state such that (, ) Q

• The execution of every atomic sub-command of c on any possible intermediate state satisfies G

• c [p, R, G, Q]– For every state such that p:

• Every execution of c on state with (potential) interventions which satisfy R must terminate in a state such that (, ) Q

• The execution of every atomic sub-command of c on any possible intermediate state satisfies G

Page 26: Noam Rinetzky Lecture 7: Axiomatic Semantics - Concurrency

A Formal Semantics• Let CR denotes the set of quadruples <1, 2, 3, 4 > s.t. that when c executes

on 1 with potential interferences by R it yields an intermediate state 2 followed by an intermediate state 3 and a final state 4

– as usual 4= when c does not terminate

• CR = {<1, 2, 3, 4> : : <1, > R ( <C, > * 2 2 = 3= 4 ’, C’: <C, >* <C’, ’ > ( (2= 1 2 = ) (3 = 3=’) 4= ) <’, 2, 3, 4 > C’R )

• c (p, R, G, Q) – For every <1, 2, 3 , 4 > CR such that 1 p

• < 2, 3> G• If 4 : <1, 4 > Q

Page 27: Noam Rinetzky Lecture 7: Axiomatic Semantics - Concurrency

Simple Examples• X := X + 1 (true, X=X, X =X+1X=X, X =X+1)• X := X + 1 (X 0, X X, X>0 X=X, X>0)• X := X + 1 ; Y := Y + 1 (X 0Y 0, X X Y Y, G, X>0 Y>0)

Page 28: Noam Rinetzky Lecture 7: Axiomatic Semantics - Concurrency

Inference Rules

• Define c (p, R, G, Q) by structural induction on c

• Soundness– If c (p, R, G, Q) then c (p, R, G, Q)

Page 29: Noam Rinetzky Lecture 7: Axiomatic Semantics - Concurrency

Atomic Command

{p }c {Q}

atomic {c} (p, preserve(p), QID, Q)

(Atomic)

Page 30: Noam Rinetzky Lecture 7: Axiomatic Semantics - Concurrency

Conditional Critical Section

{pb }c {Q}

await b then c (p, preserve(p), QID, Q)

(Critical)

Page 31: Noam Rinetzky Lecture 7: Axiomatic Semantics - Concurrency

Sequential Composition

c1 (p1, R, G, Q1)

c1 ; c2 (p1, R, G, (Q1; R*; Q2))

(SEQ)

c2 (p2, R, G, Q2)Q1 p2

Page 32: Noam Rinetzky Lecture 7: Axiomatic Semantics - Concurrency

Conditionals

c1 (p b1, R, G, Q) p b R* b1

if atomic {b} then c1 else c2 (p, R, G, Q)

(IF)c2 (p b2, R, G, Q) p b R* b2

Page 33: Noam Rinetzky Lecture 7: Axiomatic Semantics - Concurrency

Loops

c (j b1, R, G, j) j b R* b1

while atomic {b} do c (j, R, G, b j)

(WHILE) R Preserve(j)

Page 34: Noam Rinetzky Lecture 7: Axiomatic Semantics - Concurrency

Refinementc (p, R, G, Q)

c (p’, R’, G’, Q’)

(REFINE)

p’ p Q Q’

R’ R G G ’

Page 35: Noam Rinetzky Lecture 7: Axiomatic Semantics - Concurrency

Parallel Composition

c1 (p1, R1, G1, Q1)

c1 || c2 (p1 p1, (R1 R2), (G1 G2), Q)

where Q= (Q1 ; (R1R2)*; Q2) (Q2 ; (R1R2)*; Q1)

(PAR)

c2 (p2, R2, G2, Q2)G1 R2

G2 R1

Page 36: Noam Rinetzky Lecture 7: Axiomatic Semantics - Concurrency

Issues in R/G

• Total correctness is trickier• Restrict the structure of the proofs– Sometimes global proofs are preferable

• Many design choices– Transitivity and Reflexivity of Rely/Guarantee– No standard set of rules

• Suitable for designs