Axiomatic Methods for Software Verification

25
Axiomatic Methods for Software Verification Hongseok Yang

description

Axiomatic Methods for Software Verification. Hongseok Yang. - PowerPoint PPT Presentation

Transcript of Axiomatic Methods for Software Verification

Page 1: Axiomatic Methods for Software Verification

Axiomatic Methods for Software Verification

Hongseok Yang

Page 2: Axiomatic Methods for Software Verification

Things like even software verification, this has been the Holy Grail of computer science for many decades but now in some very key areas, for example, driver verification we’re building tools that can do actual proof about the software and how it works in order to guarantee the reliability." Bill Gates, April 18, 2002. Keynote address at WinHec 2002

Page 3: Axiomatic Methods for Software Verification

Verification Tools

• Tools for software verification:– Compaq: ESC/Java– Microsoft: SLAM– KSU, NASA: Bandera

• Axiomatic methods play a crucial role in those tools.

Page 4: Axiomatic Methods for Software Verification

Axiomatic Methods

• Hoare triple {P}C{Q}– P, Q: assertions such as (x==3)&&(y==z)– C: imperative programs– e.g. {x==4}y=x;{y%2==0}

• Weakest precondition WP(C,Q)– WP(C,Q): the weakest P s.t. {P}C{Q}– WP(y=x;, y%2==0) = (x%2==0)

Page 5: Axiomatic Methods for Software Verification

History

• Naur66, Floyd67:– used assertions to specify/verify flowchart program

s

• Hoare69:– developed the proof system for Hoare triples

• Reynolds00, Ishtiaq&O’Hearn01– extended Hoare’s proof system using separating c

onnectives to handle pointers

Page 6: Axiomatic Methods for Software Verification

Fibonacci Numbers

• n’th Fibonacci number fib(n):– fib(0) = 0, fib(1) = 1– fib(n+2) = fib(n+1) + fib(n)

Page 7: Axiomatic Methods for Software Verification

Implementation in C

if (n==0) { a=0; }else {

i=1; p=0; a=1;while (i != n) {

t=p; p=a; a=p+t; i=i+1;}

}

• Does this program calculate “fib(n)” and store the result in “a”?

Page 8: Axiomatic Methods for Software Verification

Specification

• Spec: {true}FIB{a==fib(n)}

Page 9: Axiomatic Methods for Software Verification

Specification

• Spec: {true}FIB{a==fib(n)}• FIB does not satisfy the spec: when

n<0, fib(n) is not even defined!!

Page 10: Axiomatic Methods for Software Verification

Specification

• Spec: {true}FIB{a==fib(n)}• FIB does not satisfy the spec: when

n<0, fib(n) is not even defined!!• New spec: {n>=0}FIB{a==fib(n)}• But, how can we be sure that the

new spec holds?

Page 11: Axiomatic Methods for Software Verification

Hoare Logic

• Hoare logic consists of inference rules for proving valid Hoare triples.

• So, we can use these rules to show that {n>=0}FIB{a==fib(n)} holds.

Page 12: Axiomatic Methods for Software Verification

Rule for Conditional

• So, {n>=0}FIB{a==fib(n)} holds if FIB satisfies:

if (n==0) { {n>=0&&n==0}C1{a==fib(n)} }

else { {n>=0&&!(n==0)}C2{a==fib(n)}}

Page 13: Axiomatic Methods for Software Verification

Rule for Assignment

• So, {n==0}a=0;{a==fib(n)} because n==0 implies 0==fib(n).

• It suffices to show the correctness of C2:

if (n==0) { {n>=0&&n==0}a=0;{a==fib(n)} }

else { {n>=0&&!(n==0)}C2{a==fib(n)} }

Page 14: Axiomatic Methods for Software Verification

Rule for Sequencing

• So, it suffices to show:if (n==0) { {n>=0&&n==0}a=0;{a==fib(n)} }else { {n>=0&&!(n==0)}

i=1;p=0;a=1;{a==fib(i)&&p==fib(i-1)&&i<=n}while (I!=n) { t=p;p=a; a=p+t;i=i+1; }

{a==fib(n)}

}

We focus on this step

Page 15: Axiomatic Methods for Software Verification

Rule for Loop

• So, we have:{a==fib(i)&&p==fib(i-1)&&i<=n}while(i!=n) {

{a==fib(i)&&p==fib(i-1)&&i<=n&&i!=n} t=p; p=a; a=p+t; i=i+1;{a==fib(i)&&p==fib(i-1)&&i<=n}

}{a==fib(i)&&p==fib(i-1)&&i<=n&&!(i!=n)}

We prove this in the next slide

Page 16: Axiomatic Methods for Software Verification

Preservation of the Loop Invariant

{a==fib(i)&&p==fib(i-1)&&i<=n&&i!=n}{(a+p)==fib(i+1)&&a==fib(i+1-1)&&(i+1)<=n}

t=p; {(a+t)==fib(i+1)&&a==fib(i+1-1)&&(i+1)<=n}

p=a; {(p+t)==fib(i+1)&&p==fib(i+1-1)&&(i+1)<=n}

a=p+t;{a==fib(i+1)&&p==fib(i+1-1)&&(i+1)<=n}

i=i+1;{a==fib(i)&&p==fib(i-1)&&i<=n}

Page 17: Axiomatic Methods for Software Verification

Consequence

• Since a==fib(i)&&!(i!=n) implies a==fib(n), we have:

{a==fib(i)&&p==fib(i-1)&&i<=n}while(i!=n) { t=p; p=a; a=p+t; i=i+1; }{a==fib(i)&&p==fib(i-1)&&i<=n&&!(i!

=n)} {a==fib(n)}

Page 18: Axiomatic Methods for Software Verification

Simple Twistvoid PFIB(int *n, int *a){ int t,i,p;

if (*n==0) { *a=0; }else {

i=1; p=0; *a=1;while (i != *n) { t=p; p=*a; *a=p+t; i=i+1; }

}}

• Does the same reasoning prove {*n>=0}PFIB(n,a){*a==fib(*n)}?

Page 19: Axiomatic Methods for Software Verification

Pointers cause a problem!

• Not quite!! What if a=n? The problem is that the following rule is not sound.

• Two Solutions:– Morris’s solution: modify subsitution using dynamic

aliasing checks in the above rule– Reynolds’s solution: use separating conjunction “*

*’’ in assertions.

Page 20: Axiomatic Methods for Software Verification

Semantics of Assertions

• Semantic Domains– s 2 Stacks = Vars !fin Ints

– h 2 Heaps = Nats !fin Ints– (s,h) 2 States = Stacks x Heaps

• “(s,h)²P”: P holds for the state (s,h).– (s,h)²P&&Q iff (s,h)²P and (s,h)²Q– (s,h)²(xE) iff dom(h)={s(x)} and h(s(x))=«E¬

Page 21: Axiomatic Methods for Software Verification

Separating Conjunction

• #,* for heaps:– h1#h2 iff dom(h1)\dom(h2) = ;

– When h1#h2, h1*h2=h1[h2

• (s,h)²P**Q iff there exist h1,h2 such that

– h1*h2=h; and

– (s,h1)²P and (s,h2)²Q.

• e.g. (nn0)**true, (nn0)**(afib(n0))

Page 22: Axiomatic Methods for Software Verification

Rule for Pointer Swing

• By this rule we can prove:

{(nn0)**(afib(i))**(t==fib(i-1)&&p==fib(i)&&i+1<=n0)}

*a=p+t; {(nn0)**(ap+t)**(t==fib(i-1)&&p==fib(i)&&i+1<=n0)}

Page 23: Axiomatic Methods for Software Verification

Correctness of PFIB(n,a)

• Spec:{(nn0)**(a-)**(n0>=0)}

PFIB(n,a){(n n0)**(a fib(n0))**true}

• Loop Invariant:(n n0)**(a fib(i))**(p==fib(i-1)&& i<=n0)

Page 24: Axiomatic Methods for Software Verification

Preservation of Loop Invariant

{(n n0)**(afib(i))**(p==fib(i-1)&& i<=n0&&i!=n0)}{(n n0)**(afib(i))**(p==fib(i-1)&& i+1<=n0)}

t=p; {(n n0)**(afib(i))**(t==fib(i-1)&& i+1<=n0)}

p=*a; {(n n0)**(afib(i))**(t==fib(i-1)&&p==fib(i)&& i+1<=n0)}

*a=p+t; {(n n0)**(ap+t)**(t==fib(i-1)&&p==fib(i)&&i+1<=n0)}{(n n0)**(afib(i+1))**(p==fib(i)&&i+1<=n0)}

i=i+1;

{(n n0)**(a fib(i))**(p==fib(i-1)&&i<=n0)}

Page 25: Axiomatic Methods for Software Verification

Concluding Remarks

• Why don’t you verify your C program using Hoare logic?

• Well, even if you are lazy, you still might want to play with verification tools. Look at: http://research.microsoft.com/SLAM