Spring 2014 Program Analysis and Verification Lecture 4: Axiomatic Semantics I

50
Spring 2014 Program Analysis and Verification Lecture 4: Axiomatic Semantics I Roman Manevich Ben-Gurion University

description

Spring 2014 Program Analysis and Verification Lecture 4: Axiomatic Semantics I. Roman Manevich Ben-Gurion University. Syllabus. Today. Basic concepts of correctness Axiomatic semantics (pages 175-183) Hoare Logic Properties of the semantics Weakest precondition. program correctness. - PowerPoint PPT Presentation

Transcript of Spring 2014 Program Analysis and Verification Lecture 4: Axiomatic Semantics I

Page 1: Spring 2014 Program Analysis and Verification Lecture 4: Axiomatic Semantics  I

Spring 2014Program Analysis and Verification

Lecture 4: Axiomatic Semantics I

Roman ManevichBen-Gurion University

Page 2: Spring 2014 Program Analysis and Verification Lecture 4: Axiomatic Semantics  I

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 4: Axiomatic Semantics  I

3

Today

• Basic concepts of correctness• Axiomatic semantics (pages 175-183)– Hoare Logic– Properties of the semantics– Weakest precondition

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

4

program correctness

Page 5: Spring 2014 Program Analysis and Verification Lecture 4: Axiomatic Semantics  I

5

Program correctness concepts• Property = a certain relationship between initial state

and final state

• Partial correctness = properties that holdif program terminates

• Termination = program always terminates– i.e., for every input state

partial correctness + termination = total correctness

Other correctness concepts exist: liveness, resource usage, …

Main focus of this course

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

6

Factorial example

• Factorial partial correctness property =if the statement terminates then the final value of y will be the factorial of the initial value of x– What if x < 0?

• Formally, using natural semantics: …?

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

Sfac , ’ implies ’ y = ( x)!

Page 7: Spring 2014 Program Analysis and Verification Lecture 4: Axiomatic Semantics  I

7

Verifying factorial with natural semantics

Page 8: Spring 2014 Program Analysis and Verification Lecture 4: Axiomatic Semantics  I

8

Natural semantics for While x := a, [x Aa][assns]

skip, [skipns]

S1, ’, S2, ’ ’’S1; S2, ’’ [compns]

S1, ’ if b then S1 else S2, ’

if B b = tt[ifttns]

S2, ’ if b then S1 else S2, ’

if B b = ff[ifffns]

while b do S, if B b = ff[whileffns]

S, ’, while b do S, ’ ’’while b do S, ’’

if B b = tt[whilettns]

Page 9: Spring 2014 Program Analysis and Verification Lecture 4: Axiomatic Semantics  I

9

Staged proof

Page 10: Spring 2014 Program Analysis and Verification Lecture 4: Axiomatic Semantics  I

10

Stages

y := 1; while (x=1) do (y := y*x; x := x–1)s s’

s’ y = (s x)! s x > 0

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

y := y*x; x := x–1s s’’

s y (s x)! = s’’ y (s’’ x)! s x > 0

s s’’

s y (s x)! = s’’ y (s’’ x)! s’’x = 1 s x > 0

Page 11: Spring 2014 Program Analysis and Verification Lecture 4: Axiomatic Semantics  I

11

Inductive proof over iterations

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

(y := y*x; x := x–1)

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

s s’’s y (s x)! = s’’ y (s’’ x)! s’’x = 1 s x > 0

s s’s’ s’’

s’ y (s’ x)! = s’’ y (s’’ x)! s’’x = 1 s’ x > 0

s y (s x)! = s’ y (s’ x)! s x > 0

Page 12: Spring 2014 Program Analysis and Verification Lecture 4: Axiomatic Semantics  I

12

First stage

Page 13: Spring 2014 Program Analysis and Verification Lecture 4: Axiomatic Semantics  I

Second stage

13

Page 14: Spring 2014 Program Analysis and Verification Lecture 4: Axiomatic Semantics  I

while (x=1) do (y := y*x; x := x–1), s s’

14

Page 15: Spring 2014 Program Analysis and Verification Lecture 4: Axiomatic Semantics  I

Third stage

15

Page 16: Spring 2014 Program Analysis and Verification Lecture 4: Axiomatic Semantics  I

16

How easy was that?

• Proof is very laborious– Need to connect all transitions and argues about

relationships between their states– Reason: too closely connected to semantics of

programming language• Proof is long– Makes it hard to find possible mistakes

• How did we know to find this proof?– Is there a methodology?

Page 17: Spring 2014 Program Analysis and Verification Lecture 4: Axiomatic Semantics  I

17

Can you prove my program correct?

I’ll use operational semantics

Better use axiomatic verification

Page 18: Spring 2014 Program Analysis and Verification Lecture 4: Axiomatic Semantics  I

18

A systematic approachto program verification

Page 19: Spring 2014 Program Analysis and Verification Lecture 4: Axiomatic Semantics  I

19

Axiomatic verification approach• What do we need in order to prove that the

program does what it supposed to do?• Specify the required behavior • Compare the behavior with the one obtained by the

operational semantics• Develop a proof system for showing that the program

satisfies a requirement• Mechanically use the proof system to show correctness

Page 20: Spring 2014 Program Analysis and Verification Lecture 4: Axiomatic Semantics  I

Axiomatic semantics contributors

C.A.R. HoareRobert Floyd Edsger W. Dijkstra

20

1967: use assertionsas foundationfor static correctness proofs

1969: use Floyd’s ideasto define axiomatic semantics“An axiomatic basis for computer programming”

Predicate transformersemantics: weakest precondition and strongest postcondition

Page 21: Spring 2014 Program Analysis and Verification Lecture 4: Axiomatic Semantics  I

21

Assertions, a.k.a Hoare triples

• P and Q are state predicates– Example: x>0

• If P holds in the initial state, andif execution of C terminates on that state,then Q will hold in the state in which C halts

• C is not required to always terminate {true} while true do skip {false}

{ P } C { Q }precondition postcondition

statementa.k.a command

Page 22: Spring 2014 Program Analysis and Verification Lecture 4: Axiomatic Semantics  I

22

Total correctness assertions

• If P holds in the initial state,execution of C must terminate on that state,and Q will hold in the state in which C halts

[ P ] C [ Q ]

Page 23: Spring 2014 Program Analysis and Verification Lecture 4: Axiomatic Semantics  I

23

Specifying correctnessof factorial

Page 24: Spring 2014 Program Analysis and Verification Lecture 4: Axiomatic Semantics  I

24

Factorial example:specify precondition/postcondition

{ ? } y := 1; while (x=1) do (y := y*x; x := x–1)

{ ? }

Page 25: Spring 2014 Program Analysis and Verification Lecture 4: Axiomatic Semantics  I

25

First attempt

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

{ y=x! }

Holds only for value of x at state after execution finishes

We need a way to “remember” value of x before execution

Page 26: Spring 2014 Program Analysis and Verification Lecture 4: Axiomatic Semantics  I

26

Fixed assertion

{ x=n } y := 1; while (x=1) do (y := y*x; x := x–1)

{ y=n! n>0 }

A logical variable, must not appear in statement - immutable

Page 27: Spring 2014 Program Analysis and Verification Lecture 4: Axiomatic Semantics  I

27

The proof outline

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

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

{n!*(n+1) = (n+1)! }

Backgroundaxiom

Page 28: Spring 2014 Program Analysis and Verification Lecture 4: Axiomatic Semantics  I

28

Formalizing partial correctness via hoare logic

Page 29: Spring 2014 Program Analysis and Verification Lecture 4: Axiomatic Semantics  I

29

States and predicates• – program states

– undefined• A state predicate P is a (possibly

infinite) setof states

• P– P holds in state

P

Page 30: Spring 2014 Program Analysis and Verification Lecture 4: Axiomatic Semantics  I

30

Formalizing Hoare triples

• { P } C { Q }– , ’ . (P C, ’) ’Q

alternatively– . (P SnsC ) SnsC Q– Convention: P for all P

. P SnsC Q

P C(P)Q

’C

Why did we choose natural semantics?

Sns C =

’ if C, ’ else

Page 31: Spring 2014 Program Analysis and Verification Lecture 4: Axiomatic Semantics  I

31

Formalizing Hoare triples

• { P } C { Q }– , ’ . (P C, *’) ’Q

alternatively– . (P SsosC ) SsosC Q– Convention: P for all P

. P SsosC Q

P C(P)Q

’C

Sns C =

’ if C, ’ else

Page 32: Spring 2014 Program Analysis and Verification Lecture 4: Axiomatic Semantics  I

32

How do we express predicates?

• Extensional approach– Abstract mathematical functions

P : State {tt, ff}

• Intensional approach– via language of formulae

Page 33: Spring 2014 Program Analysis and Verification Lecture 4: Axiomatic Semantics  I

33

An assertion language• Bexp is not expressive enough to express

predicates needed for many proofs– Extend Bexp

• Allow quantification– z. … – z. …

• z. z = kn

• Import well known mathematical concepts– n! n (n-1) 2 1

Page 34: Spring 2014 Program Analysis and Verification Lecture 4: Axiomatic Semantics  I

34

An assertion language

a ::= n | x | a1 + a2 | a1 a2 | a1 – a2

A ::= true | false| a1 = a2 | a1 a2 | A | A1 A2 | A1 A2| A1 A2 | z. A | z. A

Either a program variablesor a logical variable

Page 35: Spring 2014 Program Analysis and Verification Lecture 4: Axiomatic Semantics  I

35

SomeFO logic

definitionsbefore we get to the rules

Page 36: Spring 2014 Program Analysis and Verification Lecture 4: Axiomatic Semantics  I

36

Free/bound variables

• A variable is said to be bound in a formula when it occurs in the scope of a quantifier. Otherwise it is said to be free– i. k=im– (i+10077)i. j+1=i+3)

• FV(A) the free variables of A• Defined inductively on the abstract syntax tree

of A

Page 37: Spring 2014 Program Analysis and Verification Lecture 4: Axiomatic Semantics  I

37

Free variables

FV(n) {}FV(x) {x}FV(a1+a2) FV(a1a2) FV(a1-a2) FV(a1) FV(a2)FV(true) FV(false) {}FV(a1=a2) FV(a1a2) FV(a1) FV(a2)FV(A) FV(A)FV(A1 A2) FV(A1 A2) FV(A1 A2)

FV(a1) FV(a2) FV(z. A) FV(z. A) FV(A) \ {z}

Page 38: Spring 2014 Program Analysis and Verification Lecture 4: Axiomatic Semantics  I

38

Substitution

• An expression t is pure (a term) if it does not contain quantifiers

• A[t/z] denotes the assertion A’ which is the same as A, except that all instances of the free variable z are replaced by t

• A i. k=imA[5/k] = …? A[5/i] = …?

What if t is not pure?

Page 39: Spring 2014 Program Analysis and Verification Lecture 4: Axiomatic Semantics  I

39

Calculating substitutions

n[t/z] = nx[t/z] = xx[t/x] = t

(a1 + a2)[t/z] = a1[t/z] + a2[t/z](a1 a2)[t/z] = a1[t/z] a2[t/z](a1 - a2)[t/z] = a1[t/z] - a2[t/z]

Page 40: Spring 2014 Program Analysis and Verification Lecture 4: Axiomatic Semantics  I

40

Calculating substitutionstrue[t/x] = truefalse[t/x] = false(a1 = a2)[t/z] = a1[t/z] = a2[t/z] (a1 a2)[t/z] = a1[t/z] a2[t/z] (A)[t/z] = (A[t/z])(A1 A2)[t/z]= A1[t/z] A2[t/z](A1 A2)[t/z] = A1[t/z] A2[t/z] (A1 A2)[t/z] = A1[t/z] A2[t/z]

(z. A)[t/z] = z. A(z. A)[t/y] = z. A[t/y]( z. A)[t/z] = z. A( z. A)[t/y] = z. A[t/y]

Page 41: Spring 2014 Program Analysis and Verification Lecture 4: Axiomatic Semantics  I

41

and now…the rules

six are completely

enough

Page 42: Spring 2014 Program Analysis and Verification Lecture 4: Axiomatic Semantics  I

42

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

Notice similarity to natural semantics rules

What’s different about this rule?

Page 43: Spring 2014 Program Analysis and Verification Lecture 4: Axiomatic Semantics  I

43

Assignment rule

• A “backwards” rule• x := a always finishes• Why is this true?– Recall operational semantics:

• Example: {y*z<9} x:=y*z {x<9}What about {y*z<9w=5} x:=y*z {w=5}?

x := a, [xAa][assns]

[xAa] P

Page 44: Spring 2014 Program Analysis and Verification Lecture 4: Axiomatic Semantics  I

44

skip rule

skip, [skipns]

Page 45: Spring 2014 Program Analysis and Verification Lecture 4: Axiomatic Semantics  I

45

Composition rule

• Holds when S1 terminates in every state where P holds and then Q holdsand S2 terminates in every state where Q holds and then R holds

S1, ’, S2, ’ ’’S1; S2, ’’ [compns]

Page 46: Spring 2014 Program Analysis and Verification Lecture 4: Axiomatic Semantics  I

46

Condition rule

S1, ’ if b then S1 else S2, ’

if B b = tt[ifttns]

S2, ’ if b then S1 else S2, ’

if B b = ff[ifffns]

Page 47: Spring 2014 Program Analysis and Verification Lecture 4: Axiomatic Semantics  I

47

Loop rule

• Here P is called an invariant for the loop– Holds before and after each loop iteration– Finding loop invariants – most challenging part of proofs

• When loop finishes, b is false

while b do S, if B b = ff[whileffns]

S, ’, while b do S, ’ ’’while b do S, ’’

if B b = tt[whilettns]

Page 48: Spring 2014 Program Analysis and Verification Lecture 4: Axiomatic Semantics  I

48

Rule of consequence

• Allows strengthening the precondition and weakening the postcondition

• The only rule that is not related to a statement

Page 49: Spring 2014 Program Analysis and Verification Lecture 4: Axiomatic Semantics  I

49

Rule of consequence

• Why do we need it?• Allows the following

{y*z<9} x:=y*z {x<9} {y*z<9w=5} x:=y*z {x<10}

Page 50: Spring 2014 Program Analysis and Verification Lecture 4: Axiomatic Semantics  I

Next lecture:axiomatic semantics

practice and extensions