spring 2014 program analysis and verification lecture 6: axiomatic semantics iii

89
Spring 2014 Program Analysis and Verification Lecture 6: Axiomatic Semantics III Roman Manevich Ben-Gurion University

Upload: mahlah

Post on 22-Feb-2016

68 views

Category:

Documents


0 download

DESCRIPTION

Spring 2014 Program Analysis and Verification Lecture 6: Axiomatic Semantics III. Roman Manevich Ben-Gurion University. Syllabus. Previously. Hoare logic Inference system Annotated programs Soundness and completeness Weakest precondition calculus. Axiomatic semantics for While. - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Spring 2014 Program Analysis and Verification Lecture 6: Axiomatic Semantics  III

Spring 2014Program Analysis and Verification

Lecture 6: Axiomatic Semantics III

Roman ManevichBen-Gurion University

Page 2: Spring 2014 Program Analysis and Verification Lecture 6: Axiomatic Semantics  III

2

Syllabus

Semantics

NaturalSemantics

Structural semantics

AxiomaticVerification

StaticAnalysis

AutomatingHoare Logic

AbstractInterpretation fundamentals

Lattices

Galois Connections

Fixed-Points

Widening/Narrowing

Domain constructors

InterproceduralAnalysis

AnalysisTechniques

Numerical Domains

CEGAR

Alias analysis

ShapeAnalysis

Crafting your own

Soot

From proofs to abstractions

Systematically developing

transformers

Page 3: Spring 2014 Program Analysis and Verification Lecture 6: Axiomatic Semantics  III

3

Previously

• Hoare logic– Inference system– Annotated programs– Soundness and completeness

• Weakest precondition calculus

Page 4: Spring 2014 Program Analysis and Verification Lecture 6: Axiomatic Semantics  III

4

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

Page 5: Spring 2014 Program Analysis and Verification Lecture 6: Axiomatic Semantics  III

5

Weakest precondition calculus

Page 6: Spring 2014 Program Analysis and Verification Lecture 6: Axiomatic Semantics  III

6

Weakest liberal precondition

• A backward-going predicate transformer• The weakest liberal precondition for Q is

wlp(C, Q)if and only if for all states ’if C, ’ then ’ Q

Propositions:1. p { wlp(C, Q) } C { Q }2. If p { P } C { Q } then P wlp(C, Q)

Page 7: Spring 2014 Program Analysis and Verification Lecture 6: Axiomatic Semantics  III

7

Weakest liberal precondition• A backward-going predicate transformer• The weakest liberal precondition for Q is

wlp(C, Q)if and only if for all states ’if C, ’ then ’ Q

P

C(P)

Q

C

wlp(C, Q) C(wlp(C, Q))

Page 8: Spring 2014 Program Analysis and Verification Lecture 6: Axiomatic Semantics  III

8

Strongest postcondition

• A forward-going predicate transformer• The strongest postcondition for P is

’ sp(P, C)if and only if there exists such that P and C, ’

Propositions:1. p { P } C { sp(P, C) }2. If p { P } C { Q } then sp(P, C) Q

Page 9: Spring 2014 Program Analysis and Verification Lecture 6: Axiomatic Semantics  III

9

CalculatingWeakest

preconditionsBy Vadim Plessky (http://svgicons.sourceforge.net/) [see page for license], via Wikimedia Commons

Page 10: Spring 2014 Program Analysis and Verification Lecture 6: Axiomatic Semantics  III

10

Calculating wlp

1. wlp(skip, Q) = Q2. wlp(x := a, Q) = Q[a/x]3. wlp(S1; S2, Q) = wlp(S1, wlp(S2, Q))4. wlp(if b then S1 else S2, Q) =

(b wlp(S1, Q)) (b wlp(S2, Q))

5. wlp(while b do S, Q) = … ?hard to capture

Page 11: Spring 2014 Program Analysis and Verification Lecture 6: Axiomatic Semantics  III

11

Calculating the wlp of a loop

wlp(while b do S, Q) =

Idea: we know the following statements are semantically equivalentwhile b do Sif b do (S; while b do S) else skip

Let’s try to substitute and calculate on

wlp(if b do (S; while b do S) else skip, Q) =

(b wlp(S; while b do S, Q)) (b wlp(skip, Q)) =

(b wlp(S, wlp(while b do S, Q))) (b Q)

LoopInv = (b wlp(S, LoopInv)) (b Q)

Page 12: Spring 2014 Program Analysis and Verification Lecture 6: Axiomatic Semantics  III

12

Another variant for WP of loops

• Parametric in the loop invariant• wlp(while b do {} S, Q) =

where {b } S {}and b Q

Page 13: Spring 2014 Program Analysis and Verification Lecture 6: Axiomatic Semantics  III

13

Variable swap program – specify

{ ? }t := xx := yy := t{ ? }

Page 14: Spring 2014 Program Analysis and Verification Lecture 6: Axiomatic Semantics  III

14

Prove using weakest precondition

{ y=b x=a }t := x{ ? }x := y{ ? } y := t{ x=b y=a }

Page 15: Spring 2014 Program Analysis and Verification Lecture 6: Axiomatic Semantics  III

15

Prove using weakest precondition

{ y=b x=a }t := x{ y=b t=a }x := y{ x=b t=a } y := t{ x=b y=a }

Page 16: Spring 2014 Program Analysis and Verification Lecture 6: Axiomatic Semantics  III

16

Absolute value program

if x<0 then x := -xelse skip

if b then Sis syntactic sugar forif b then S else skipThe latter form is easier to reason about

Page 17: Spring 2014 Program Analysis and Verification Lecture 6: Axiomatic Semantics  III

17

Absolute value program – specify

{ ? }if x<0 then x := -xelse skip{ ? }

Page 18: Spring 2014 Program Analysis and Verification Lecture 6: Axiomatic Semantics  III

18

Absolute value program – specify

{ x=v }if x<0 then x := -xelse skip{ x=|v| }

Page 19: Spring 2014 Program Analysis and Verification Lecture 6: Axiomatic Semantics  III

19

Prove using weakest precondition{ x=v }{ } if x<0 then { } x := -x { }else { } skip { }{x=|v| }

Page 20: Spring 2014 Program Analysis and Verification Lecture 6: Axiomatic Semantics  III

20

Prove using weakest precondition{ x=v }{ (-x=|v| x<0) (x=|v| x0) } if x<0 then { -x=|v| } x := -x { x=|v| }else { x=|v| } skip { x=|v| }{ x=|v| }

Page 21: Spring 2014 Program Analysis and Verification Lecture 6: Axiomatic Semantics  III

21

Making the proof systemmore practical

Page 22: Spring 2014 Program Analysis and Verification Lecture 6: Axiomatic Semantics  III

22

Conjunction rule

• Allows breaking up proofs into smaller, easier to manage, sub-proofs

Page 23: Spring 2014 Program Analysis and Verification Lecture 6: Axiomatic Semantics  III

23

More useful rules{ P } C { Q } { P’ } C { Q’ }

{ P P’ } C {Q Q’ }[disjp]

{ P } C { Q } { v. P } C { v. Q }[existp] vFV(C

)

{ P } C { Q } {v. P } C {v. Q }[univp] vFV(C)

{ F } C { F } Mod(C) FV(F)={}[Invp]• Mod(C) = set of variables assigned to in sub-statements of C• FV(F) = free variables of F

Breaks if C is non-deterministic

Page 24: Spring 2014 Program Analysis and Verification Lecture 6: Axiomatic Semantics  III

24

Invariance + Conjunction = Constancy

• Mod(C) = set of variables assigned to in sub-statements of C• FV(F) = free variables of F

{ P } C { Q } { F P } C { F Q }[constancyp] Mod(C) FV(F)={}

Page 25: Spring 2014 Program Analysis and Verification Lecture 6: Axiomatic Semantics  III

25

Today

• Strongest postcondition• Extension for memory• Proving termination

Page 26: Spring 2014 Program Analysis and Verification Lecture 6: Axiomatic Semantics  III

26

Strongestpostcondition

calculusBy Vadim Plessky (http://svgicons.sourceforge.net/) [see page for license], via Wikimedia Commons

Page 27: Spring 2014 Program Analysis and Verification Lecture 6: Axiomatic Semantics  III

27

Floyd’s strongest postcondition rule

• Example{ z=x } x:=x+1 { ? }

• This rule is often considered problematic because it introduces a quantifier – needs to be eliminated further on

• We will now see a variant of this rule

{ P } x := a { v. x=a[v/x] P[v/x] } where v is a fresh variable

[assFloyd]

The value of x in the pre-state

Page 28: Spring 2014 Program Analysis and Verification Lecture 6: Axiomatic Semantics  III

28

Floyd’s strongest postcondition rule

• Example{ z=x } x:=x+1 { v. x=v+1 z=v }

• This rule is often considered problematic because it introduces a quantifier – needs to be eliminated further on

• We will now see a variant of this rule

{ P } x := a { v. x=a[v/x] P[v/x] } where v is a fresh variable

[assFloyd]

meaning: {x=z+1}

Page 29: Spring 2014 Program Analysis and Verification Lecture 6: Axiomatic Semantics  III

29

“Small” assignment axiom

• Examples:{x=n} x:=5*y {x=5*y}{x=n} x:=x+1 {x=n+1}

{x=n} x:=y+1 {x=y+1}[existp] {n. x=n} x:=y+1 {n. x=y+1} therefore {true} x:=y+1 {x=y+1} [constancyp] {z=9} x:=y+1 {z=9 x=y+1}

{ x=v } x:=a { x=a[v/x] }where vFV(a)

[assfloyd]

First evaluate ain the precondition state(as a may access x)

Then assign the resulting value to x

Create an explicit Skolem variable in precondition

Page 30: Spring 2014 Program Analysis and Verification Lecture 6: Axiomatic Semantics  III

30

“Small” assignment axiom

• Examples:{x=n} x:=5*y {x=5*y}{x=n} x:=x+1 {x=n+1}

{x=n} x:=y+1 {x=y+1}[existp] {n. x=n} x:=y+1 {n. x=y+1} therefore {true} x:=y+1 {x=y+1} [constancyp] {z=9} x:=y+1 {z=9 x=y+1}

{ x=v } x:=a { x=a[v/x] }where vFV(a)

[assfloyd]

Page 31: Spring 2014 Program Analysis and Verification Lecture 6: Axiomatic Semantics  III

31

“Small” assignment axiom

• Examples:{x=n} x:=5*y {x=5*y}{x=n} x:=x+1 {x=n+1}

{x=n} x:=y+1 {x=y+1}[existp] {n. x=n} x:=y+1 {n. x=y+1} therefore {true} x:=y+1 {x=y+1} [constancyp] {z=9} x:=y+1 {z=9 x=y+1}

{ x=v } x:=a { x=a[v/x] }where vFV(a)

[assfloyd]

Page 32: Spring 2014 Program Analysis and Verification Lecture 6: Axiomatic Semantics  III

32

“Small” assignment axiom

• Examples:{x=n} x:=5*y {x=5*y}{x=n} x:=x+1 {x=n+1}

{x=n} x:=y+1 {x=y+1}[existp] {n. x=n} x:=y+1 {n. x=y+1} therefore {true} x:=y+1 {x=y+1} [constancyp] {z=9} x:=y+1 {z=9 x=y+1}

{ x=v } x:=a { x=a[v/x] }where vFV(a)

[assfloyd]

Page 33: Spring 2014 Program Analysis and Verification Lecture 6: Axiomatic Semantics  III

33

Calculating sp

1. sp(skip, P) = P2. sp(x := a, P) = v. x=a[v/x] P[v/x] 3. sp(S1; S2, P) = sp(S2, sp(S1, P))4. sp(if b then S1 else S2, P) =

sp(S1, b P) sp(S2, b P)5. sp(while b do {} S, P) = b

where {b } S {}and P b

Page 34: Spring 2014 Program Analysis and Verification Lecture 6: Axiomatic Semantics  III

34

Prove using strongest postcondition{ x=a y=b }t := x

x := y

y := t

{ x=b y=a }

Page 35: Spring 2014 Program Analysis and Verification Lecture 6: Axiomatic Semantics  III

35

Prove using strongest postcondition{ x=a y=b }t := x{ x=a y=b t=a }

x := y

y := t

{ x=b y=a }

Page 36: Spring 2014 Program Analysis and Verification Lecture 6: Axiomatic Semantics  III

36

Prove using strongest postcondition{ x=a y=b }t := x{ x=a y=b t=a }

x := y{ x=b y=b t=a }

y := t

{ x=b y=a }

Page 37: Spring 2014 Program Analysis and Verification Lecture 6: Axiomatic Semantics  III

37

Prove using strongest postcondition{ x=a y=b }t := x{ x=a y=b t=a }

x := y{ x=b y=b t=a }

y := t{ x=b y=a t=a }{ x=b y=a } // cons

Page 38: Spring 2014 Program Analysis and Verification Lecture 6: Axiomatic Semantics  III

38

Prove using strongest postcondition{ x=v }if x<0 then { x=v x<0 } x := -x { x=-v x>0 }else

{ x=v x0 }skip{ x=v x0 }

{ v<0 x=-v v0 x=v }{ x=|v| }

Page 39: Spring 2014 Program Analysis and Verification Lecture 6: Axiomatic Semantics  III

39

Prove using strongest postcondition{ x=v }if x<0 then { x=v x<0 } x := -x { x=-v x>0 }else

{ x=v x0 }skip{ x=v x0 }

{ v<0 x=-v v0 x=v }{ x=|v| }

Page 40: Spring 2014 Program Analysis and Verification Lecture 6: Axiomatic Semantics  III

40

Sum program – specify• Define Sum(0, n) = 0+1+…+n

{ ? }x := 0res := 0while (x<y) do res := res+x x := x+1 { ? }

{ x=Sum(0, n) } { y=n+1 }

{ x+y=Sum(0, n+1) }

Background axiom

Page 41: Spring 2014 Program Analysis and Verification Lecture 6: Axiomatic Semantics  III

41

Sum program – specify• Define Sum(0, n) = 0+1+…+n

{ y0 }x := 0res := 0while (x<y) do res := res+x x := x+1 { res = Sum(0, y) }

{ x=Sum(0, n) } { y=n+1 }

{ x+y=Sum(0, n+1) }

Background axiom

Page 42: Spring 2014 Program Analysis and Verification Lecture 6: Axiomatic Semantics  III

42

Sum program – prove• Define Sum(0, n) = 0+1+…+n

{ y0 }x := 0

res := 0

Inv = while (x<y) do

res := res+x

x := x+1

{ res = Sum(0, y) }

{ x=Sum(0, n) } { y=n+1 }

{ x+y=Sum(0, n+1) }

Page 43: Spring 2014 Program Analysis and Verification Lecture 6: Axiomatic Semantics  III

43

Sum program – prove• Define Sum(0, n) = 0+1+…+n

{ y0 }x := 0{ y0 x=0 }res := 0

Inv = while (x<y) do res := res+x x := x+1

{ res = Sum(0, y) }

{ x=Sum(0, n) } { y=n+1 }

{ x+y=Sum(0, n+1) }

Page 44: Spring 2014 Program Analysis and Verification Lecture 6: Axiomatic Semantics  III

44

Sum program – prove• Define Sum(0, n) = 0+1+…+n

{ y0 }x := 0{ y0 x=0 }res := 0{ y0 x=0 res=0 }Inv = while (x<y) do res := res+x x := x+1

{ res = Sum(0, y) }

{ x=Sum(0, n) } { y=n+1 }

{ x+y=Sum(0, n+1) }

Page 45: Spring 2014 Program Analysis and Verification Lecture 6: Axiomatic Semantics  III

45

Sum program – prove• Define Sum(0, n) = 0+1+…+n

{ y0 }x := 0{ y0 x=0 }res := 0{ y0 x=0 res=0 }Inv = { y0 res=Sum(0, x) xy }while (x<y) do

res := res+x x := x+1

{ res = Sum(0, y) }

{ x=Sum(0, n) } { y=n+1 }

{ x+y=Sum(0, n+1) }

Page 46: Spring 2014 Program Analysis and Verification Lecture 6: Axiomatic Semantics  III

46

Sum program – prove• Define Sum(0, n) = 0+1+…+n

{ y0 }x := 0{ y0 x=0 }res := 0{ y0 x=0 res=0 }Inv = { y0 res=Sum(0, x) xy }while (x<y) do { y0 res=m x=n ny m=Sum(0, n) x<y } { y0 res=m x=n m=Sum(0, n) n<y } res := res+x x := x+1

{ res = Sum(0, y) }

{ x=Sum(0, n) } { y=n+1 }

{ x+y=Sum(0, n+1) }

Page 47: Spring 2014 Program Analysis and Verification Lecture 6: Axiomatic Semantics  III

47

Sum program – prove• Define Sum(0, n) = 0+1+…+n

{ y0 }x := 0{ y0 x=0 }res := 0{ y0 x=0 res=0 }Inv = { y0 res=Sum(0, x) xy }while (x<y) do { y0 res=m x=n ny m=Sum(0, n) x<y } { y0 res=m x=n m=Sum(0, n) n<y } res := res+x { y0 res=m+x x=n m=Sum(0, n) n<y } x := x+1

{ res = Sum(0, y) }

{ x=Sum(0, n) } { y=n+1 }

{ x+y=Sum(0, n+1) }

Page 48: Spring 2014 Program Analysis and Verification Lecture 6: Axiomatic Semantics  III

48

Sum program – prove• Define Sum(0, n) = 0+1+…+n

{ y0 }x := 0{ y0 x=0 }res := 0{ y0 x=0 res=0 }Inv = { y0 res=Sum(0, x) xy }while (x<y) do { y0 res=m x=n ny m=Sum(0, n) x<y } { y0 res=m x=n m=Sum(0, n) n<y } res := res+x { y0 res=m+x x=n m=Sum(0, n) n<y } x := x+1 { y0 res=m+x x=n+1 m=Sum(0, n) n<y } { y0 res=Sum(0, x) x=n+1 n<y } // sum axiom { y0 res=Sum(0, x) xy } // cons

{ res = Sum(0, y) }

{ x=Sum(0, n) } { y=n+1 }

{ x+y=Sum(0, n+1) }

Page 49: Spring 2014 Program Analysis and Verification Lecture 6: Axiomatic Semantics  III

49

Sum program – prove• Define Sum(0, n) = 0+1+…+n

{ y0 }x := 0{ y0 x=0 }res := 0{ y0 x=0 res=0 }Inv = { y0 res=Sum(0, x) xy }while (x<y) do { y0 res=m x=n ny m=Sum(0, n) x<y } { y0 res=m x=n m=Sum(0, n) n<y } res := res+x { y0 res=m+x x=n m=Sum(0, n) n<y } x := x+1 { y0 res=m+x x=n+1 m=Sum(0, n) n<y } { y0 res=Sum(0, x) x=n+1 n<y } // sum axiom { y0 res=Sum(0, x) xy } // cons{ y0 res=Sum(0, x) xy xy }{ y0 res=Sum(0, y) x=y }{ res = Sum(0, y) }

{ x=Sum(0, n) } { y=n+1 }

{ x+y=Sum(0, n+1) }

Page 50: Spring 2014 Program Analysis and Verification Lecture 6: Axiomatic Semantics  III

50

Buggy sum program{ y0 }x := 0{ y0 x=0 }res := 0{ y0 x=0 res=0 }Inv = { y0 res=Sum(0, x) } = { y0 res=m x=n m=Sum(0, n) } while (xy) do { y0 res=m x=n m=Sum(0, n) xy ny } x := x+1 { y0 res=m x=n+1 m=Sum(0, n) ny} res := res+x { y0 res=m+x x=n+1 m=Sum(0, n) ny} { y0 res-x=Sum(0, x-1) ny} { y0 res=Sum(0, x) }{ y0 res=Sum(0, x) x>y } {res = Sum(0, y) }

Page 51: Spring 2014 Program Analysis and Verification Lecture 6: Axiomatic Semantics  III

51

Handling data structures

Page 52: Spring 2014 Program Analysis and Verification Lecture 6: Axiomatic Semantics  III

52

Problems with Hoare logic and heaps

• { P[a/x] } x := a { P }• Consider the annotated program{ y=5 }y := 5;{ y=5 }x := &y;{ y=5 }*x := 7;{ y=5 }

• Is it correct?

The rule works on a syntactic level unaware of possible aliasing between different terms (y and *x in our case)

Page 53: Spring 2014 Program Analysis and Verification Lecture 6: Axiomatic Semantics  III

53

Problems with Hoare logic and heaps

• { P[a/x] } x := a { P }• {(x=&y z=5) (x&y y=5)}*x = z;{ y=5 }

• What should the precondition be?

Page 54: Spring 2014 Program Analysis and Verification Lecture 6: Axiomatic Semantics  III

54

Problems with Hoare logic and heaps

• { P[a/x] } x := a { P }• {(x=&y z=5) (x&y y=5)}*x = z;{ y=5 }

• We split into cases depending on possible aliasing

Page 55: Spring 2014 Program Analysis and Verification Lecture 6: Axiomatic Semantics  III

55

Problems with Hoare logic and heaps

• { P[a/x] } x := a { P }• {(x=&y z=5) (x&y y=5)}*x = z;{ y=5 }

• What should the precondition be?• Joseph M. Morris:

A General Axiom of Assignment• A different approach: heaps as arrays• Really successful approach for heaps is based on

Separation Logic

Page 56: Spring 2014 Program Analysis and Verification Lecture 6: Axiomatic Semantics  III

56

Axiomatizing data types

• We added a new type of variables – array variables– Model array variable as a function y : Z Z

• We need the two following axioms:

S ::= x := a | x := y[a] | y[a] := x | skip | S1; S2| if b then S1 else S2| while b do S

{ y[xa](x) = a }

{ zx y[xa](z) = y(z) }

Page 57: Spring 2014 Program Analysis and Verification Lecture 6: Axiomatic Semantics  III

57

Array update rules (wlp)

• Treat an array assignment y[a] := x as an update to the array function y– y := y[ax] meaning y’=v. v=a ? x : y(v)

S ::= x := a | x := y[a] | y[a] := x | skip | S1; S2| if b then S1 else S2| while b do S

[array-update] { P[y[ax]/y] } y[a] := x { P }

[array-load] { P[y(a)/x] } x := y[a] { P }

A very general approach – allows handling many data types

Page 58: Spring 2014 Program Analysis and Verification Lecture 6: Axiomatic Semantics  III

58

Array update rules (wlp) example• Treat an array assignment y[a] := x as an

update to the array function y– y := y[ax] meaning y’=v. v=a ? x : y(v)

[array-update] { P[y[ax]/y] } y[a] := x { P }{x=y[i7](i)} y[i]:=7 {x=y(i)}

{x=7} y[i]:=7 {x=y(i)}

[array-load] { P[y(a)/x] } x := y[a] { P }{y(a)=7} x:=y[a] {x=7}

Page 59: Spring 2014 Program Analysis and Verification Lecture 6: Axiomatic Semantics  III

59

Array update rules (sp)

[array-updateF] { x=v y=g a=b } y[a] := x { y=g[bv] }

[array-loadF] { y=g a=b } x := y[a] { x=g(b) }

In both rulesv, g, and b are fresh

Page 60: Spring 2014 Program Analysis and Verification Lecture 6: Axiomatic Semantics  III

60

Example of proving program with arrays

Page 61: Spring 2014 Program Analysis and Verification Lecture 6: Axiomatic Semantics  III

61

Array-max program – specify

nums : arrayN : int // N stands for num’s length { N0 nums=orig_nums } x := 0res := nums[0]while x < N if nums[x] > res then res := nums[x] x := x + 11. { x=N }2. { m. (m0 m<N) nums(m)res }3. { m. m0 m<N nums(m)=res }4. { nums=orig_nums }

Page 62: Spring 2014 Program Analysis and Verification Lecture 6: Axiomatic Semantics  III

62

Array-max program

nums : arrayN : int // N stands for num’s length { N0 nums=orig_nums } x := 0res := nums[0]while x < N if nums[x] > res then res := nums[x] x := x + 1Post1: { x=N }Post2: { nums=orig_nums }Post3: { m. 0m<N nums(m)res }Post4: { m. 0m<N nums(m)=res }

Page 63: Spring 2014 Program Analysis and Verification Lecture 6: Axiomatic Semantics  III

63

Proof strategy• Prove each goal 1, 2, 3, 4 separately and use

conjunction rule to prove them all• After proving

– {N0} C {x=N}– {nums=orig_nums} C {nums=orig_nums}

• We have proved– {N0 nums=orig_nums} C{x=N nums=orig_nums}

• We can refer to assertions from earlier proofs in writing new proofs

Page 64: Spring 2014 Program Analysis and Verification Lecture 6: Axiomatic Semantics  III

64

Array-max example: Post1

nums : arrayN : int // N stands for num’s length { N0 } x := 0{ N0 x=0 }res := nums[0]{ x=0 }Inv = { xN }while x < N { x=k k<N } if nums[x] > res then { x=k k<N } res := nums[x] { x=k k<N } { x=k k<N } x := x + 1 { x=k+1 k<N }{ xN xN }{ x=N }

Page 65: Spring 2014 Program Analysis and Verification Lecture 6: Axiomatic Semantics  III

65

Array-max example: Post2

nums : arrayN : int // N stands for num’s length { nums=orig_nums }x := 0{ nums=orig_nums }res := nums[0]{ nums=orig_nums }Inv = { nums=orig_nums }while x < N { nums=orig_nums x < N } if nums[x] > res then { nums=orig_nums } res := nums[x] { nums=orig_nums } { nums=orig_nums } x := x + 1 { nums=orig_nums }{ nums=orig_nums xN }{ nums=orig_nums }

Page 66: Spring 2014 Program Analysis and Verification Lecture 6: Axiomatic Semantics  III

66

Array-max example: Post3nums : array{ N0 0m<N } // N stands for num’s lengthx := 0{ x=0 }res := nums[0]{ x=0 res=nums(0) }Inv = { 0m<x nums(m)res }while x < N { x=k res=oRes 0m<k nums(m)oRes } if nums[x] > res then { nums(x)>oRes res=oRes x=k 0m<k nums(m)oRes } res := nums[x] { res=nums(x) nums(x)>oRes x=k 0m<k nums(m)oRes } { x=k 0mk nums(m)res } { (x=k 0mk nums(m)res) (oResnums(x) res=oRes x=k res=oRes 0m<k nums(m)oRes)} { x=k 0mk nums(m)res } x := x + 1 { x=k+1 0mk nums(m)res } { 0m<x nums(m)res }{ x=N 0m<x nums(m)res} [univp]{ m. 0m<N nums(m)res }

Page 67: Spring 2014 Program Analysis and Verification Lecture 6: Axiomatic Semantics  III

67

Proving termination

Page 68: Spring 2014 Program Analysis and Verification Lecture 6: Axiomatic Semantics  III

68

Total correctness 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]

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

[whilep][ P(z+1) ] S [ P(z) ]

[ z. P(z) ] while b do S [ P(0) ]P(z+1) bP(0) b

Page 69: Spring 2014 Program Analysis and Verification Lecture 6: Axiomatic Semantics  III

69

Total correctness 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]

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

[whilep][ b P t=k ] S [ P t<k ]

[ P ] while b do S [ b P ] P t0

Rank, orLoop variant

Page 70: Spring 2014 Program Analysis and Verification Lecture 6: Axiomatic Semantics  III

70

Proving termination

• There is a more general rule based on well-founded relations– Partial orders with no infinite strictly decreasing

chains• Exercise: write a rule that proves only that a

program S, started with precondition P terminates[ ] S [ ]

Page 71: Spring 2014 Program Analysis and Verification Lecture 6: Axiomatic Semantics  III

71

Proving termination

• There is a more general rule based on well-founded relations– Partial orders with no infinite strictly decreasing

chains• Exercise: write a rule that proves only that a

program S, started with precondition P terminates[ P ] S [ true ]

Page 72: Spring 2014 Program Analysis and Verification Lecture 6: Axiomatic Semantics  III

72

Array-max – specify terminationnums : arrayN : int // N stands for num’s length x := 0res := nums[0]Variant = [ ? ]while x < N if nums[x] > res then res := nums[x] x := x + 1[ ? ]

Page 73: Spring 2014 Program Analysis and Verification Lecture 6: Axiomatic Semantics  III

73

Array-max – specify terminationnums : arrayN : int // N stands for num’s length x := 0res := nums[0]Variant = [ N-x ]while x < N [ ? ] if nums[x] > res then res := nums[x] x := x + 1 [ ? ][ true ]

Page 74: Spring 2014 Program Analysis and Verification Lecture 6: Axiomatic Semantics  III

74

Array-max – prove loop variantnums : arrayN : int // N stands for num’s length x := 0res := nums[0]Variant = [ t=N-x ]while x < N [ x<N N-x=k N-x0 ] if nums[x] > res then res := nums[x] x := x + 1 // [ N-x<k N-x0 ][ true ]

Page 75: Spring 2014 Program Analysis and Verification Lecture 6: Axiomatic Semantics  III

75

Array-max – prove loop variantnums : arrayN : int // N stands for num’s length x := 0res := nums[0]Variant = [ t=N-x ]while x < N [ x=x0 x0<N N-x0=k N-x00 ] if nums[x] > res then res := nums[x] x := x + 1 // [ N-x<k N-x0 ][ true ]

Capture initial value of x, since it changes in the loop

Page 76: Spring 2014 Program Analysis and Verification Lecture 6: Axiomatic Semantics  III

76

Array-max – prove loop variantnums : arrayN : int // N stands for num’s length x := 0res := nums[0]Variant = [ t=N-x ]while x < N [ x=x0 x0<N N-x0=k N-x00 ] if nums[x] > res then res := nums[x] [ x=x0 x0<N N-x0=k N-x00 ] // Frame x := x + 1 // [ N-x<k N-x0 ][ true ]

Page 77: Spring 2014 Program Analysis and Verification Lecture 6: Axiomatic Semantics  III

77

Array-max – prove loop variantnums : arrayN : int // N stands for num’s length x := 0res := nums[0]Variant = [ t=N-x ]while x < N [ x=x0 x0<N N-x0=k N-x00 ] if nums[x] > res then res := nums[x] [ x=x0 x0<N N-x0=k N-x00 ] // Frame x := x + 1 [ x=x0+1 x0<N N-x0=k N-x00 ] // [ N-x<k N-x0 ][ true ]

Page 78: Spring 2014 Program Analysis and Verification Lecture 6: Axiomatic Semantics  III

78

Array-max – prove loop variantnums : arrayN : int // N stands for num’s length x := 0res := nums[0]Variant = [ t=N-x ]while x < N [ x=x0 x0<N N-x0=k N-x00 ] if nums[x] > res then res := nums[x] [ x=x0 x0<N N-x0=k N-x00 ] // Frame x := x + 1 [ x=x0+1 x0<N N-x0=k N-x00 ] [ N-x<k N-x0 ] // cons[ true ]

Page 79: Spring 2014 Program Analysis and Verification Lecture 6: Axiomatic Semantics  III

Zune calendar bugwhile (days > 365) { if (IsLeapYear(year)) { if (days > 366) { days -= 366; year += 1; }

} else { days -= 365; year += 1; } }

79

Page 80: Spring 2014 Program Analysis and Verification Lecture 6: Axiomatic Semantics  III

Fixed codewhile (days > 365) { if (IsLeapYear(year)) { if (days > 366) { days -= 366; year += 1; } else { break; } } else { days -= 365; year += 1; } }

80

Page 81: Spring 2014 Program Analysis and Verification Lecture 6: Axiomatic Semantics  III

Fixed code – specify termination[ ? ]while (days > 365) { if (IsLeapYear(year)) { if (days > 366) { days -= 366; year += 1; }

else { break; } } else { days -= 365; year += 1; } }[ ? ]

81

Page 82: Spring 2014 Program Analysis and Verification Lecture 6: Axiomatic Semantics  III

Fixed code – specify variant[ true ]Variant = [ ? ]while (days > 365) { if (IsLeapYear(year)) { if (days > 366) { days -= 366; year += 1; } else { break; } } else { days -= 365; year += 1; } [ ? ]}[ true ]

82

Page 83: Spring 2014 Program Analysis and Verification Lecture 6: Axiomatic Semantics  III

Fixed code – proving termination[ true ]Variant = [ t=days ]while (days > 365) { [ days>365 days=k days0 ] if (IsLeapYear(year)) { if (days > 366) { days -= 366; year += 1; } else { break; [ false ] } } else { days -= 365; year += 1; } // [ days0 days<k ]}[ true ]

83

Let’s model break by a small cheat – assume execution never gets past it

Page 84: Spring 2014 Program Analysis and Verification Lecture 6: Axiomatic Semantics  III

Fixed code – proving termination[ true ]Variant = [ t=days ]while (days > 365) { [ days0 k=days days>365 ] -> [ days0 k=days days>365 ] if (IsLeapYear(year)) { [ k=days days>365 ] if (days > 366) { [ k=days days>365 days>366 ] -> [ k=days days>366 ] days -= 366; [ days=k-366 days>0 ] year += 1; [ days=k-366 days>0 ] } else {

[ k=days days>365 days366 ] break; [ false ] } [ (days=k-366 days>0) false ] -> [ days<k days>0 ] } else { [ k=days days>365 ] days -= 365; [ k-365=days days-365>365 ] -> [ k-365=days days0 ] -> [ days<k days0 ] year += 1; [ days<k days0 ] } [ days<k days0 ]}[ true ]

84

Page 85: Spring 2014 Program Analysis and Verification Lecture 6: Axiomatic Semantics  III

85

Challenge: proving non-termination

• Write a rule for proving that a program does not terminate when started with a precondition P

• Prove that the buggy Zune calendar program does not terminate

{ b P } S { b } { P } while b do S { false }[while-ntp]

Page 86: Spring 2014 Program Analysis and Verification Lecture 6: Axiomatic Semantics  III

86

conclusion

Page 87: Spring 2014 Program Analysis and Verification Lecture 6: Axiomatic Semantics  III

87

Extensions to axiomatic semantics

• Assertions for execution time– Exact time– Order of magnitude time

• Assertions for dynamic memory– Separation Logic

• Assertions for parallelism– Owicki-Gries– Concurrent Separation Logic– Rely-guarantee

Page 88: Spring 2014 Program Analysis and Verification Lecture 6: Axiomatic Semantics  III

88

Axiomatic verification conclusion

• Very powerful technique for reasoning about programs– Sound and complete

• Extendible• Static analysis can be used to automatically

synthesize assertions and loop invariants

Page 89: Spring 2014 Program Analysis and Verification Lecture 6: Axiomatic Semantics  III

Next lecture:static analysis