Further with Hoare Logic Sections 6.12, 6.10, 6.13

Slides:



Advertisements
Similar presentations
Technologies for finding errors in object-oriented software K. Rustan M. Leino Microsoft Research, Redmond, WA Lecture 1 Summer school on Formal Models.
Advertisements

Technologies for finding errors in object-oriented software K. Rustan M. Leino Microsoft Research, Redmond, WA Lecture 0 Summer school on Formal Models.
Automated Theorem Proving Lecture 1. Program verification is undecidable! Given program P and specification S, does P satisfy S?
In this episode of The Verification Corner, Rustan Leino talks about Loop Invariants. He gives a brief summary of the theoretical foundations and shows.
Semantics Static semantics Dynamic semantics attribute grammars
ICE1341 Programming Languages Spring 2005 Lecture #6 Lecture #6 In-Young Ko iko.AT. icu.ac.kr iko.AT. icu.ac.kr Information and Communications University.
Hoare’s Correctness Triplets Dijkstra’s Predicate Transformers
Rigorous Software Development CSCI-GA Instructor: Thomas Wies Spring 2012 Lecture 11.
1 University of Toronto Department of Computer Science © 2001, Steve Easterbrook Lecture 10: Formal Verification Formal Methods Basics of Logic first order.
David Evans CS655: Programming Languages University of Virginia Computer Science Lecture 19: Minding Ps & Qs: Axiomatic.
Partial correctness © Marcelo d’Amorim 2010.
Copyright © 2006 Addison-Wesley. All rights reserved.1-1 ICS 410: Programming Languages Chapter 3 : Describing Syntax and Semantics Axiomatic Semantics.
ISBN Chapter 3 Describing Syntax and Semantics.
Predicate Transformers
Program Proving Notes Ellen L. Walker.
1/22 Programs : Semantics and Verification Charngki PSWLAB Programs: Semantics and Verification Mordechai Ben-Ari Mathematical Logic for Computer.
Wishnu Prasetya Hoare Logic and Predicate Transformers.
ESC Java. Static Analysis Spectrum Power Cost Type checking Data-flow analysis Model checking Program verification AutomatedManual ESC.
Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 18 Program Correctness To treat programming.
ESC Java. Static Analysis Spectrum Power Cost Type checking Data-flow analysis Model checking Program verification AutomatedManual ESC.
Operational Semantics Semantics with Applications Chapter 2 H. Nielson and F. Nielson
Describing Syntax and Semantics
Floyd Hoare Logic. Semantics A programming language specification consists of a syntactic description and a semantic description. Syntactic description:symbols.
Software Engineering Prof. Dr. Bertrand Meyer March 2007 – June 2007 Chair of Software Engineering Static program checking and verification Slides: Based.
CSE 755, part3 Axiomatic Semantics Will consider axiomatic semantics (A.S.) of IMP: ::=skip | | | | ; | | Only integer vars; no procedures/fns; vars declared.
Reading and Writing Mathematical Proofs
1 Inference Rules and Proofs (Z); Program Specification and Verification Inference Rules and Proofs (Z); Program Specification and Verification.
Extended Static Checking for Java  ESC/Java finds common errors in Java programs: null dereferences, array index bounds errors, type cast errors, race.
Propositional Calculus CS 270: Mathematical Foundations of Computer Science Jeremy Johnson.
Program Analysis and Verification Spring 2014 Program Analysis and Verification Lecture 4: Axiomatic Semantics I Roman Manevich Ben-Gurion University.
Chapter 3 Part II Describing Syntax and Semantics.
From Hoare Logic to Matching Logic Reachability Grigore Rosu and Andrei Stefanescu University of Illinois, USA.
Principle of Programming Lanugages 3: Compilation of statements Statements in C Assertion Hoare logic Department of Information Science and Engineering.
Static Techniques for V&V. Hierarchy of V&V techniques Static Analysis V&V Dynamic Techniques Model Checking Simulation Symbolic Execution Testing Informal.
This Week Lecture on relational semantics Exercises on logic and relations Labs on using Isabelle to do proofs.
Cs7100(Prasad)L18-9WP1 Axiomatic Semantics Predicate Transformers.
Program Analysis and Verification
Wishnu Prasetya Predicate-transformer–based Verification (LN Chapter 2)
CORRECTNESS ISSUES AND LOOP INVARIANTS Lecture 8 CS2110 – Fall 2014.
Hoare Logic LN chapter 5, 6 but without 6.8, 6.12, 6.13 (to be discussed later) Hoare Logic is used to reason about the correctness of programs. In the.
Chapter 3 of Programming Languages by Ravi Sethi
Weakest Precondition of Unstructured Programs
Correctness issues and Loop invariants
Compilers Principles, Techniques, & Tools Taught by Jing Zhang
Matching Logic An Alternative to Hoare/Floyd Logic
Lecture 14: Iteration and Recursion (Section 6.5 – 6.6)
Reasoning About Code.
Reasoning about code CSE 331 University of Washington.
Revisiting Predicate Logic LN chapters 3,4
Hoare Logic LN chapter 5, 6 but without 6. 8, 6. 12, 6
Copyright © Cengage Learning. All rights reserved.
Axiomatic semantics Points to discuss: The assignment statement
Programming Languages and Compilers (CS 421)
Programming Languages 2nd edition Tucker and Noonan
Hoare Logic LN chapter 5, 6 Hoare Logic is used to reason about the correctness of programs. In the end, it reduces a program and its specification to.
Some heuristics for deriving invariant LN sections 6.7 , 6.8
MA/CSSE 474 More Math Review Theory of Computation
Predicate Transformers
Formal Methods in software development
Axiomatic Semantics Will consider axiomatic semantics (A.S.) of IMP:
Language-Independent Verification Framework
The Zoo of Software Security Techniques
Program correctness Axiomatic semantics
Program Verification with Hoare Logic
Lecture 2: Axiomatic semantics
CSC1401 Manipulating Pictures 2
Programming Languages and Compilers (CS 421)
Programming Languages 2nd edition Tucker and Noonan
Logic for Program Call chapter 7
Hoare Logic LN chapter 5, 6, 9.3 Hoare Logic is used to reason about the correctness of programs. In the end, it reduces a program and its specification.
Presentation transcript:

Further with Hoare Logic Sections 6.12, 6.10, 6.13

Overview Assignment to composite structures Reasoning about more exotic imperative constructs Motivation: tool support for verification array assignment, block, exception, expr with side effect, etc 2

About computer support for verification We can implement Hoare logic Syntax driven! Use e.g. theorem prover or SAT solver at the back-end to prove the resulting verification conditions. Big-logic architecture: The Hoare logic is defined to work natively on e.g. Java Complex logic; maintenance (of the logic) is an issue Example: LOOP Small-logic architecture: Translate Java to a much simpler intermediate language (e.g. Boogie PL); Hoare logic operates on this intermediate language Feedback is an issue Example : SPEC#, Esc/Java

Assignment to array Recall : wp (x:=e) Q = Q[e/x] {* Q[e/x] *} x:=e {* Q *} Does these “rules” extend to a[e1] := e2 ? {* y = 10 *} a[i] := y {* a[i] = 10 *} // valid {* a[0] = 10 *} a[i] := y {* a[0] = 10 *} // not valid!! {* (i=0  y | a[0]) = 10 *}

The formalism Introduce : a (i repby e) : the same array as a, except its i-th element, which is e. Define it indirectly via : a (i repby e) [k] = i=k  e | a[k] Treat assignment : a[i] := 0  a := a(i repby 0)

Now we can derive the “rule” So : wp (a[e1] := e2) Q = wp (a := a(e1 repby e2)) Q = { this is now an ordinary assignment ! } Q [ a(e1 repby e2) / a ]

Example Prove: {* ik *} a[k]:= 0 ; a[i]:=1 {* a[k] = 0 *} By calculating wp: {* i=k  1 | 0 = 0 *} // def. repby {* ... a(k repby 0)[k] .... *} // wp a[k]:=0 // a := a(k repby 0) {* i=k  1 | a[k] = 0 *} // def. repby {* a(i repby 1)[k] = 0 *} // wp a[i]:=1 // a := a(i repby 1) {* a[k] = 0 *}

Record Can be handled in a way analogous to array : r.fn := e  r := r(fn repby e) But since field names are static, we can immediately collapse the resulting repby expression: r (fn repby e) . fn = e r (fn repby e) . otherfield = r.otherfield See Lec. Notes.

“Predicate transformer” semantics we can abstractly define the meaning/semantic of a statement S by defining how to calculate its wp. wp (assert P) Q = P /\ Q wp (assume P) Q = P  Q

Now we can dealing with actual arrays Array expressions e.g. a[i] has to be checked that the index i is within a’s actual domain. Well, we can adapt uPL logic. Alternatively, keep the logic, but use a pre-processor to translate, e.g.: a[i] := a[k]  assert 0i<#a ; assert 0k<#a ; a[i] := a[k] What if we use assume instead of assert ?

Expressing non-determinism S [] T On a begin state, non-deterministically choose between S and T, and execute it. Define it via wp: wp (S [] T) Q = wp S Q /\ wp T Q

Non-deterministic IF We can now have a non-deterministic IF; define : if g1  S1 g2  S2 = (encoded by) { assume g1 ; S1 } [] { assume g2 ; S2 } Can’t be implemented literally like that; but with respect to their Hoare triple specs, these are equivalent. So, what happens if it is executed on a state not satisfying one of the guards? 

Non-deterministic loop while g1 do S1 g2 do S2 If g1 true, do S1, if g2 true, do S2. If both are true, choose one non-deterministically. Repeat the above. Terminate when g1 and g2 are false. while g1 \/ g2 do if g1  S1 g2  S2

Example Prove that eventually the bag will be empty. By proving this specification: {* reds = 100 /\ blues = 100 *} while reds>0 do reds,blues := reds-1 , blues+2 blues>0 do blues := blues - 1 {* reds = 0 /\ blues = 0 *} Take as invariant: r>=0 /\ w>=0 On termination we have ~g1 /\ ~g2 = r<=0 /\ b<=0, which with inv implies r=b=0. Termination metric: 3r + b Note that for the specification to be valid, we have to take into account all the non-deterministic choices made by the loop.

Block { var x:T ; S } Introduce a local, uninitialized variable x (means its value can be anything of the type T), then does S. The scope of x is limited within the block. Variant, with initialization: { var x = e ; S }  { var x ; x := e ; S } But we can also have a non-deterministic intialization: { var x ; assume x>9 ; S }

Block Define the semantics via wp; first some examples: {* true *} {* y=0 *} { var x ; x:=0 ; y:=x } {* y=x *} {* 0=0 *} // no! {* 0=x *}

Block {* y>x *} x>0  x > x’ y > x’ We may need to protect and restore variable names We may have to quantify { var x ; assume x>0 ; y:=x } {* y>x *} x>0  x > x’ y > x’ // rename x to x’ (x:: x>0  x > x’) (@x:: @x>0  @x > x’) // rename bound var (@x:: @x>0  @x > x) // restore x’ to x Equivalent with 0  x

Block So: wp {var x ; S } Q = restorex (x :: wp S (protectx Q)) where protectx Q = Q[x’/x] // x’ fresh var restorex (x:: R) = (@x:: R[@x/x][x/x’]) // @x fresh var The wp of {var x=e ; S } can be simplified  home work 

Non-deterministic assignment x,y  P Change the values of x and y to arbitrary values satisfying P; other variables stay unchanged. For example, x  x>0 can be encoded by: {var x’ ; assume x’>0 ; x:=x’ } // assuming x’ fresh Theorem: wp (x  P) Q = (x:: P  Q) = (z :: (P  Q)[z/x]) // z fresh Wp {var x’; assume x’>0; x=x’} Q = (forall x’ :: x’>0 ==> Q[x’ / x]) = (forall x :: x>0 ==> Q)

Exception raise Cause the program to enter an exception state. There is only one exception sort. But our concept of Hoare triple does not understand “exceptional state”. Extend “triple” to “quadruple” : {* P *} S {* Q , E *} If S is executed on a state satisfying P it will terminate. If it terminates in a normal state, then the state should satisfy Q. If it is an exceptional state, it should satisfy E.

Exception But now we have to extend all our Hoare logic rules and our defs for wp {* E *} raise {* Q, E *} {* P *} S1 {* Q, E *} , {* Q *} S2 {* R, E *} ---------------------------------------------------------------------- {*P*} S1 ; S2 {* R, E *} wp (x:=e) (Q,E) = Q[e/x] wp (S1 ; S2) (R,E) = wp S1 (wp S2 (R,E), E) wp raise (Q,E) = E

Catching exception try S catch T Execute S, if it jumps to an exception state, continue with T. The logic: {* P *} S {* R, Q *} , {* Q *} T {* R, E *} ---------------------------------------------------------------------- {*P*} try S catch T {* R, E *} wp (try S catch T) (R,E) = wp S (R, wp T (R,E)) { P } S { Q, R } , { R } T {Q, E} -------------------------------------------------- { P } try S catch T { Q, E }

Expression with side effect Assume this simple syntax of expression <expr> ::= <constant> | <var> | <expr> "+" <expr> | <expr> "=" <expr> | <var> "++“ Define a function flat that transform such an expression to statement, such that e.g. : x := e  … ; x:=@0 flat @0 e

Expression with side effect flat @0 (e1 + e2) = flat @1 e1 ; flat @2 e2 ; @0 := @1 + @2 flat @0 var = @0 := var flat @0 var++ = @0 := var ; var:=var+1 … All the @i vars are fresh. Example: x := x++ + y  @1:=x ; x:=x+1 ; @2:=y ; @0:=@1 + @2 ; x := @0 Can be handled by usual uPL logic.