Program Verification with Hoare Logic

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

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.
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.
Reasoning About Code; Hoare Logic, continued
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.
Axiomatic Semantics The meaning of a program is defined by a formal system that allows one to deduce true properties of that program. No specific meaning.
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.
Dynamic semantics Precisely specify the meanings of programs. Why? –programmers need to understand the meanings of programs they read –programmers need.
Copyright © 2006 Addison-Wesley. All rights reserved. 3.5 Dynamic Semantics Meanings of expressions, statements, and program units Static semantics – type.
1 Discrete Structures Lecture 29 Predicates and Programming Read Ch
Predicate Transformers
Announcements We are done with homeworks Second coding exam this week, in recitation –Times will be posted later today –If in doubt, show up for your regular.
CSE 331 Software Design & Implementation Dan Grossman Winter 2014 Lecture 2 – Reasoning About Code With Logic 1CSE 331 Winter 2014.
1/22 Programs : Semantics and Verification Charngki PSWLAB Programs: Semantics and Verification Mordechai Ben-Ari Mathematical Logic for Computer.
CS 355 – Programming Languages
CSE115/ENGR160 Discrete Mathematics 04/12/11 Ming-Hsuan Yang UC Merced 1.
ESC Java. Static Analysis Spectrum Power Cost Type checking Data-flow analysis Model checking Program verification AutomatedManual ESC.
Axiomatic Semantics Dr. M Al-Mulhem ICS
Dr. Muhammed Al-Mulhem 1ICS ICS 535 Design and Implementation of Programming Languages Part 1 Fundamentals (Chapter 4) Axiomatic Semantics ICS 535.
Software Verification Bertrand Meyer Chair of Software Engineering Lecture 2: Axiomatic semantics.
Describing Syntax and Semantics
Floyd Hoare Logic. Semantics A programming language specification consists of a syntactic description and a semantic description. Syntactic description:symbols.
Reading and Writing Mathematical Proofs
1 Inference Rules and Proofs (Z); Program Specification and Verification Inference Rules and Proofs (Z); Program Specification and Verification.
CSI 3125, Axiomatic Semantics, page 1 Axiomatic semantics The assignment statement Statement composition The "if-then-else" statement The "while" statement.
1 Formal Semantics of Programming Languages “Program testing can be used to show the presence of bugs, but never to show their absence!” --Dijkstra.
ISBN Chapter 3 Describing Semantics -Attribute Grammars -Dynamic Semantics.
CS 363 Comparative Programming Languages Semantics.
Reasoning about programs March CSE 403, Winter 2011, Brun.
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.
Semantics In Text: Chapter 3.
COP4020 Programming Languages Introduction to Axiomatic Semantics Prof. Robert van Engelen.
13 Aug 2013 Program Verification. Proofs about Programs Why make you study logic? Why make you do proofs? Because we want to prove properties of programs.
Program Analysis and Verification Spring 2015 Program Analysis and Verification Lecture 4: Axiomatic Semantics I Roman Manevich Ben-Gurion University.
Dr. Naveed Riaz Design and Analysis of Algorithms 1 1 Formal Methods in Software Engineering Lecture # 26.
Cs7100(Prasad)L18-9WP1 Axiomatic Semantics Predicate Transformers.
1 Section 8.2 Program Correctness (for imperative programs) A theory of program correctness needs wffs, axioms, and inference rules. Wffs (called Hoare.
CSC3315 (Spring 2009)1 CSC 3315 Languages & Compilers Hamid Harroud School of Science and Engineering, Akhawayn University
Boolean Programs: A Model and Process For Software Analysis By Thomas Ball and Sriram K. Rajamani Microsoft technical paper MSR-TR Presented by.
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 4 (Part 3): Mathematical Reasoning, Induction & Recursion
Reasoning About Code.
Reasoning about code CSE 331 University of Washington.
CSE 331 Software Design & Implementation
Formal Methods in Software Engineering 1
CSE 331 Software Design and Implementation
Hoare-style program verification
Mathematical Structures for Computer Science Chapter 1
Reasoning About Code; Hoare Logic
Lecture 5 Floyd-Hoare Style Verification
Axiomatic semantics Points to discuss: The assignment statement
Reasoning about Loops, Conclusion
Programming Languages and Compilers (CS 421)
Semantics In Text: Chapter 3.
Section 1: Code Reasoning
Formal Methods in software development
Predicate Transformers
Formal Methods in software development
Program correctness Axiomatic semantics
Lecture 2: Axiomatic semantics
Programming Languages and Compilers (CS 421)
COP4020 Programming Languages
Presentation transcript:

Program Verification with Hoare Logic CS 510/10 Program Verification with Hoare Logic

Program Verification Decides if specific properties hold for a program. Other Approaches Model checking Symbolic : (program + properties) -> CNF -> SAT Explicit state: explore all possible states Problem lies in scalability Static program analysis Conservatively consider all possible executions False positives

Verification through Hoare Logic It is proof based A set of proof rules are available which can be applied to prove a program satisfies certain properties. Semi-automatic Some steps have to involve human intelligence Popular Proof based implies scalability as proof is constructed by looking at the structure of the program and the structure of the formula. Better scalability. Long history. Job opennings. A formal software process: Turn informal requirements to an equivalent formula of some logic write the program Prove the program satisfies the formula.

Intuition

A Sample Hoare Triple Initial informal requirement Computer a number y whose square is less than the input x. Revised informal requirement If the input x is a positive number, compute a number whose square is less than x. The formal specification (( x>0 )) P (( y*y <x )) P is free to do whatever it wants if x<=0 What if x is a negative number

Definition of Hoare Logic The form ((Φ)) P ((Ψ)) is called hoare triple Φ is called the precondition and Ψ the postcondition In the core language, a store or state is a function L that assigns to each variable x an integer For a formula Φ with function symbols – (unary), +, -, and * (binary); and binary predicate symbols < and =, we say a state L satisfies Φ, denoted as L |= Φ, if and only if Φ is evaluated to true with the value assignment given in L.

Examples Assume a state L(x)=-2, L(y)=5, L(z)=-1 L |= ! (x+y<z) ? L |= y-x*z<z ? L |= V u (y<u → y*z < u*z ) ? x, y, z are variables in the program, u is a variable in the formula

Partial Correctness vs. Total Correctness We say a triple ((Φ)) P ((Ψ)) is satisfied under partial correctness if it holds under the condition that P terminates for all states that satisfy Φ. |=par ((Φ)) P ((Ψ)) a weak requirement. While (true) {x=0;} We say a triple ((Φ)) P ((Ψ)) is satisfied under total correctness if it holds and P terminates. |=tot ((Φ)) P ((Ψ)) Seems to be more desirable, but very challenging.

A Core Programming Language E ::= n | x | (-E) | ( E+E) | (E-E) | (E*E) B ::=true | false | (!B) | (B&B) | (B||B) | (E<E) C ::= x=E | C;C | if B {C} else {C} | while B {C} y=1; z=0; while (z!=x) { z=z+1; y=y*z; } Partial correctness vs. total correctness

Proof Rules for Partial Correctness

Composition \eta Eta is called the midcondition

Assignment Has no premises and thus an axiom of our logic Φ[t/x] is to replace x with t in Φ A backward rule and machine friendly

Confusion

Clarification

More Examples (( ?? )) x=2 (( x=2 )) (( ?? )) x=2 (( x=y ))

If-Statements (( T )) if x = 0 then y := 1 else y := a / x (( y==1 || y==a/x ))

While Loops Loop invariant If e if false as soon as embark on the while-statement, then we do not execute C at all. Nothing has happened to change the truth value of phi, so we end the while-statement with phi and !e. If B is true, we execute C again; phi is again set up. No matter how many times we execute C in this way, phi is true at the end of each execution of C. The while terminates if and only if e is false after the loop. The rule is still true.

Implied The proof rules do not always give the desired pre/post condition

Proof Tableaux

Constructing a Proof Tableau

Backwards Derivation

Weakest Precondition The process of obtaining Φi from Ci+1 and Φi+1 is called computing the weakest precondition of Ci+1, given the postcondition Φi+1. The logically weakest formula whose truth at the beginning of the execution of Ci+1 is enough to guarantee Φi+1. x>5 vs x>10 |=par ((y>10)) x=y+1 ((x>6)) |=par ((T)) z=x; z=z+y; u=z; ((u=x+y)) Why do we want the weakest condition instead of the strongest condition? Because we want to maximize the chance of applying the implied rule to prove the target precondition.

WP for If-Statements Push Ψ upwards through C+, resulting in Φ1 Set Φ to be (e→ Φ1) && (!e→Φ2)

An Example

Proving While Loops The requirement Rule at hand

Proving While Loops

Finding an Invariant

Checking the Invariant

Completing the Proof

A Case: Minimal-Sum Section Let a[0],…, a[n-1]be the integer values of an array a. A section of a is a continuous piece a[i],…, a[j], where 0<=i <= j <n. We denote the sum of that section: a[i]+ a[i+1]+ … + a[j] as the Si,j. A minimal sum section is a section that is less than or equal to the sum Si’,j’ for any other 0<=i’ <= j’ <n. [-1, 3, 15, -6, 4, -5]

One Implementation Formally specify the requirements. Prove the following implementation satisfies the requirements. k=1; t=a[0]; s=a[0]; while (k !=n ) { t= min(t+a[k], a[k]); s= min (s,t); k=k+1; }

Requirements ((T)) Min_Sum (( for all i,j, 0<=i <=j < n → s<=Si,j )) ((T)) Min_Sum (( exist i,j, 0<=i <=j < n → s==Si,j ))

Proving the First Property