Program Verification using Hoare logic ThanhVu Nguyen CSCE 467 - - PowerPoint PPT Presentation

program verification using hoare logic
SMART_READER_LITE
LIVE PREVIEW

Program Verification using Hoare logic ThanhVu Nguyen CSCE 467 - - PowerPoint PPT Presentation

Program Verification using Hoare logic ThanhVu Nguyen CSCE 467 Adapted from Jonathan Aldrichs Program Analysis slides November 19, 2019 1 Big-Step Operational Semantics E a n E-Assign E x := a E { x n } E-Skip E


slide-1
SLIDE 1

Program Verification using Hoare logic

ThanhVu Nguyen CSCE 467 Adapted from Jonathan Aldrich’s Program Analysis slides November 19, 2019

1

slide-2
SLIDE 2

Big-Step Operational Semantics

E-Assign E ⊢ a ⇓ n E ⊢ x := a ⇓ E{x → n} E-Skip E ⊢ skip ⇓ E E-Seq E ⊢ S1 ⇓ E′ E′ ⊢ S1 ⇓ E′′ E ⊢ S1; S2 ⇓ E′′ E-IfTrue E ⊢ b ⇓ True E ⊢ S1 ⇓ E′ E ⊢ if b then S1 else S2 ⇓ E′ E-IfFalse E ⊢ b ⇓ False E ⊢ S2 ⇓ E′′ E ⊢ if b then S1 else S2 ⇓ E′′ E-While E ⊢ c ⇓ True E ⊢ S; while b do S ⇓ E’ E ⊢ while b do S ⇓ E′ E ⊢ c ⇓ False E ⊢ while b do S ⇓ E

2

slide-3
SLIDE 3

Axiomatic Semantics

Big step semantics: relates intial state to final one,

e.g., if we start the program with the env/state {x → 3, y → 4}, we get the new env {x → 7, y → 2}.

Axiomantic Semantics: instead of single state (e.g., {x → 3, y → 4}, work with a set of states, described by a formula

e.g., if we start the program with variables having values satisfying x >= 0, y >= 0, we get a new state that satisfy x < 100, y = x2.

3

slide-4
SLIDE 4

Hoare Tripple

{P} S {Q}

By Tony Hoare Reasoning about partial program correctness using pre- and post- conditions Hoare Tripple

P: a formula representing the precondition Q: a formula representing the postcondition Read: assume P holds, if S successfully executes, then Q holds P and Q: specifications of the program S

Partial Correctness: assume S terminates Total Correctness: require S terminates

4

slide-5
SLIDE 5

Examples of Hoare Tripples

1 { True } x:=5 { x ≡ 5 } 2 { x ≡ y } x := x + 3 { x≡ y+3 } 3 { x>-1 } x:=2*x + 3 { x > 1 } 4 { x ≡ a } if x < 0 then x := -x { x ≡ |a| } 5 { False } x:=3 { x ≡ 8 } 5

slide-6
SLIDE 6

Examples of Hoare Tripples

1 { True } x:=5 { x ≡ 5 } 2 { x ≡ y } x := x + 3 { x≡ y+3 } 3 { x>-1 } x:=2*x + 3 { x > 1 } 4 { x ≡ a } if x < 0 then x := -x { x ≡ |a| } 5 { False } x:=3 { x ≡ 8 }

In-class Questions: { x≡ y } ??? { x≡ y } { ??? } x:= y - 3 { x ≡ 8 } { x<0 } while(x!=0) x:=x - 1 { ??? }

5

slide-7
SLIDE 7

Examples of Hoare Tripples

1 { True } x:=5 { x ≡ 5 } 2 { x ≡ y } x := x + 3 { x≡ y+3 } 3 { x>-1 } x:=2*x + 3 { x > 1 } 4 { x ≡ a } if x < 0 then x := -x { x ≡ |a| } 5 { False } x:=3 { x ≡ 8 }

In-class Questions: { x≡ y } ??? { x≡ y } { ??? } x:= y - 3 { x ≡ 8 } { x<0 } while(x!=0) x:=x - 1 { ??? }

Not valid for Total Correctess

5

slide-8
SLIDE 8

Strongest Postconditions

Which are valid? { x ≡ 5 } x:=x*2 { true } { x ≡ 5 } x:=x*2 { x >0 } { x ≡ 5 } x:=x*2 { x ≡ 10 ∨ x≡ 5 } { x ≡ 5 } x:=x*2 { x ≡ 10 }

6

slide-9
SLIDE 9

Strongest Postconditions

Which are valid? { x ≡ 5 } x:=x*2 { true } { x ≡ 5 } x:=x*2 { x >0 } { x ≡ 5 } x:=x*2 { x ≡ 10 ∨ x≡ 5 } { x ≡ 5 } x:=x*2 { x ≡ 10 } All are valid, but which one is the most useful?

6

slide-10
SLIDE 10

Strongest Postconditions

Which are valid? { x ≡ 5 } x:=x*2 { true } { x ≡ 5 } x:=x*2 { x >0 } { x ≡ 5 } x:=x*2 { x ≡ 10 ∨ x≡ 5 } { x ≡ 5 } x:=x*2 { x ≡ 10 } All are valid, but which one is the most useful?

x ≡ 10 is the strongest postcondition In general, we want strong postconditions

Definition In { P } S { Q } , Q is the strongest postcondition if ∀Q′.{ P } S { Q’ } , Q ⇒ Q′ Ex: x ≡ 10 is the strongest postcondition

x ≡ 10 ⇒ true x ≡ 10 ⇒ x > 0 x ≡ 10 ⇒ (x ≡ 10 ∨ x ≡ 5) x ≡ 10 ⇒ x ≡ 10

6

slide-11
SLIDE 11

Weakest Preconditions

{ x ≡ 5 ∧ y ≡ 10 } z:=x/y { z<1 } { x < y ∧ y > 0 } z:=x/y { z<1 } { y = 0 ∧ x/y < 1 } z:=x/y { z<1 } All are true, but which one is the most useful?

7

slide-12
SLIDE 12

Weakest Preconditions

{ x ≡ 5 ∧ y ≡ 10 } z:=x/y { z<1 } { x < y ∧ y > 0 } z:=x/y { z<1 } { y = 0 ∧ x/y < 1 } z:=x/y { z<1 } All are true, but which one is the most useful?

y = 0 ∧ x/y < 1 is the weakest precondition In general, we want weak preconditions (allowing us to run the program with fewer assumptions or restrictions)

Definition In { P } S { Q } , P is the weakest precondition if ∀P ′.{ P’ } S { Q’ } , P ′ ⇒ P

7

slide-13
SLIDE 13

Program Verification

Verification using Hoare Triples and Weakest Preconditions To prove { P } S { Q } is valid, we check P ⇒ wp(S, Q) wp: a function returning the weakest precondition allowing the execution of S to achieve Q Need to define wp for different statements in WHILE

8

slide-14
SLIDE 14

WP for Assignment

Find the weakest precondition P { P } x := 3 { x + y ≡ 10 } ?

9

slide-15
SLIDE 15

WP for Assignment

Find the weakest precondition P { P } x := 3 { x + y ≡ 10 } ?

A: y ≡ 7 Check { y≡7 } x := 3 { x + y ≡ 10 }

9

slide-16
SLIDE 16

WP for Assignment

Find the weakest precondition P { P } x := 3 { x + y ≡ 10 } ?

A: y ≡ 7 Check { y≡7 } x := 3 { x + y ≡ 10 }

{ P } x := 3 { x + y > 0 }

9

slide-17
SLIDE 17

WP for Assignment

Find the weakest precondition P { P } x := 3 { x + y ≡ 10 } ?

A: y ≡ 7 Check { y≡7 } x := 3 { x + y ≡ 10 }

{ P } x := 3 { x + y > 0 }

A: 3 + y > 0, (or y > -3) Check { y > -3 } x:=3 { x + y > 0 }

9

slide-18
SLIDE 18

WP for Assignment

Find the weakest precondition P { P } x := 3 { x + y ≡ 10 } ?

A: y ≡ 7 Check { y≡7 } x := 3 { x + y ≡ 10 }

{ P } x := 3 { x + y > 0 }

A: 3 + y > 0, (or y > -3) Check { y > -3 } x:=3 { x + y > 0 }

WP for Assignment

wp(x:= E, Q) = QE

x

9

slide-19
SLIDE 19

WP for Assignment

Find the weakest precondition P { P } x := 3 { x + y ≡ 10 } ?

A: y ≡ 7 Check { y≡7 } x := 3 { x + y ≡ 10 }

{ P } x := 3 { x + y > 0 }

A: 3 + y > 0, (or y > -3) Check { y > -3 } x:=3 { x + y > 0 }

WP for Assignment

wp(x:= E, Q) = QE

x

wp(x:=3, x + y ≡ 10) = (x + y ≡ 10)3

x = 3 + y = 10 = y = 7

wp(x:=3, x + y > 0) = (x + y > 0)3

x = 3 + y > 0

9

slide-20
SLIDE 20

WP for While statements

Statement S wp(S,Q) Assignment x:= e Qe

x

Skip skip Q Sequential S1;S2 wp(S1, wp(S2,Q)) Conditional if b then S1 else S2 b ⇒ wp(S1, Q) ∧ ¬b ⇒ wp(S2, Q)

10

slide-21
SLIDE 21

WP for While statements

Statement S wp(S,Q) Assignment x:= e Qe

x

Skip skip Q Sequential S1;S2 wp(S1, wp(S2,Q)) Conditional if b then S1 else S2 b ⇒ wp(S1, Q) ∧ ¬b ⇒ wp(S2, Q) In-class Exercise Find the weakest preconditions for

1 { ?? } x := x + 3 { x ≡ z } 2 { ?? } x := x + 1; y := y * x { y ≡ 2 * z } 3 { ?? } if (x > 0) then y := x else y := 0 { y > 0 } 10

slide-22
SLIDE 22

Loops

wp(while b do S) = ?? Idea: use loop invariant

holds when the loop is entered preserves after the loop body is executed

11

slide-23
SLIDE 23

Loops

wp(while b do S) = ?? Idea: use loop invariant

holds when the loop is entered preserves after the loop body is executed

Example

{N ≥ 0} i := 0; while (i < N) i := N;

Which ones are loop invariants? For those that are not, explain why

1 i ≡ 0 2 i ≡ N 3 N ≥ 0 4 i ≤ N 11

slide-24
SLIDE 24

WP for Loop

wp(while b do S) = (I) ∧ (I ∧ b ⇒ wp(S, I)) ∧ (I ∧ ¬b ⇒ Q) Find/Guess a loop invariant I: P ⇒ I: initially I is true wrt P (base case) I ∧ b ⇒ I: I is preserved after each execution (inductive case) I ∧ ¬B ⇒ Q: if the loop terminates, the post condition holds (Partial correctness)

{N ≥ 0} i := 0; while (i < N) i := N; {i ≡ N}

Which ones would be good invariant to find the wp?

1 N ≥ 0 2 i ≤ N 12

slide-25
SLIDE 25

WP for Loop

wp(while b do S) = (I) ∧ (I ∧ b ⇒ wp(S, I)) ∧ (I ∧ ¬b ⇒ Q) Find/Guess a loop invariant I: P ⇒ I: initially I is true wrt P (base case) I ∧ b ⇒ I: I is preserved after each execution (inductive case) I ∧ ¬B ⇒ Q: if the loop terminates, the post condition holds (Partial correctness)

{N ≥ 0} i := 0; while (i < N) i := N; {i ≡ N}

Which ones would be good invariant to find the wp?

1 N ≥ 0 2 i ≤ N

Find the wp for the loop Prove the program is correct (show that P ⇒ wp)

12

slide-26
SLIDE 26

In-class Exercise

{x ≤ 10} while x != 10 x := x + 1 {x ≡ 10}

Find an invariant I for the loop Find the wp of the loop Prove the program is correct

13