Lecture 5 Floyd-Hoare Style Verification

Slides:



Advertisements
Similar presentations
Automated Theorem Proving Lecture 1. Program verification is undecidable! Given program P and specification S, does P satisfy S?
Advertisements

Program verification: flowchart programs Book: chapter 7.
Rigorous Software Development CSCI-GA Instructor: Thomas Wies Spring 2012 Lecture 12.
Semantics Static semantics Dynamic semantics attribute grammars
Copyright , Doron Peled and Cesare Tinelli. These notes are based on a set of lecture notes originally developed by Doron Peled at the University.
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.
Formal Semantics of Programming Languages 虞慧群 Topic 5: Axiomatic Semantics.
David Evans CS655: Programming Languages University of Virginia Computer Science Lecture 19: Minding Ps & Qs: Axiomatic.
Axiomatic Verification I Prepared by Stephen M. Thebaut, Ph.D. University of Florida Software Testing and Verification Lecture 17.
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.
Predicate Transformers
Program Proving Notes Ellen L. Walker.
1 Semantic Description of Programming languages. 2 Static versus Dynamic Semantics n Static Semantics represents legal forms of programs that cannot be.
1/22 Programs : Semantics and Verification Charngki PSWLAB Programs: Semantics and Verification Mordechai Ben-Ari Mathematical Logic for Computer.
CS 355 – Programming Languages
Programming Language Semantics Axiomatic Semantics Chapter 6.
Axiomatic Semantics Dr. M Al-Mulhem ICS
CS 330 Programming Languages 09 / 18 / 2007 Instructor: Michael Eckmann.
Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 18 Program Correctness To treat programming.
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
Programming Language Semantics Denotational Semantics Chapter 5 Part III Based on a lecture by Martin Abadi.
Proving Program Correctness The Axiomatic Approach.
Reading and Writing Mathematical Proofs
1 Inference Rules and Proofs (Z); Program Specification and Verification Inference Rules and Proofs (Z); Program Specification and Verification.
Chapter 25 Formal Methods Formal methods Specify program using math Develop program using math Prove program matches specification using.
ISBN Chapter 3 Describing Semantics -Attribute Grammars -Dynamic Semantics.
CS 363 Comparative Programming Languages Semantics.
Reading and Writing Mathematical Proofs Spring 2015 Lecture 4: Beyond Basic Induction.
Program Analysis and Verification Spring 2014 Program Analysis and Verification Lecture 4: Axiomatic Semantics I Roman Manevich Ben-Gurion University.
Semantics In Text: Chapter 3.
COP4020 Programming Languages Introduction to Axiomatic Semantics Prof. Robert van Engelen.
From Hoare Logic to Matching Logic Reachability Grigore Rosu and Andrei Stefanescu University of Illinois, USA.
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.
Principle of Programming Lanugages 3: Compilation of statements Statements in C Assertion Hoare logic Department of Information Science and Engineering.
Cs7100(Prasad)L18-9WP1 Axiomatic Semantics Predicate Transformers.
CSC3315 (Spring 2009)1 CSC 3315 Languages & Compilers Hamid Harroud School of Science and Engineering, Akhawayn University
Reasoning About Code.
Reasoning about code CSE 331 University of Washington.
Formal Methods in Software Engineering 1
Mathematical Structures for Computer Science Chapter 1
Lecture 2: Axiomatic semantics
Axiomatic semantics Points to discuss: The assignment statement
Programming Languages and Compilers (CS 421)
Programming Languages 2nd edition Tucker and Noonan
Logic for Computer Security Protocols
Semantics In Text: Chapter 3.
Axiomatic Verification I
Formal Methods in software development
Predicate Transformers
Formal Methods in software development
Axiomatic Verification I
The Zoo of Software Security Techniques
Program correctness Axiomatic semantics
Program Verification with Hoare Logic
Lecture 2: Axiomatic semantics
Programming Languages and Compilers (CS 421)
Programming Languages 2nd edition Tucker and Noonan
COP4020 Programming Languages
Presentation transcript:

Lecture 5 Floyd-Hoare Style Verification Xiaokang Qiu

Motivation Consider the following program: z = 0; i = x; while (i) { z = z + y; i = i – 1; }

Motivation The techniques we learned in 573 are insufficient. Easy to argue that a given input will produce a given output. Easy to argue that a property always holds at a single program point Also easy to argue that all constructs in the language will preserve some property (like when we proved type soundness). Much harder to prove general properties of the behavior of a program on all inputs. Types allow us to design custom checkers to verify specific properties. Very good at reasoning about properties of the data pointed at by particular variables.

Axiomatic Semantics (AKA program logics) A system for proving properties about programs Key idea: We can define the semantics of a construct by describing its effect on assertions about the program state. Two components A language for stating assertions (“the assertion logic”) Can be First-Order Logic (FOL), a specialized logic such as separation logic, or Higher-Order Logic (HOL), which can encode the others. Many specialized languages developed over the years: Z, Larch, JML, Spec# Deductive rules (“the program logic”) for establishing the truth of such assertions

A little history Early years: Unbridled optimism The middle ages Heavily endorsed by the likes of Hoare and Dijkstra If you can prove programs correct, bugs will be a thing of the past. You won’t even have to test your programs! The middle ages 1979 paper by DeMillo, Lipton and Perlis “Proofs in math only work because there is a social process in place to get people to argue them and internalize them.” “Program proofs are too boring for social process to form around them.” “Programs change too fast and proofs are too brittle.” The renaissance New generation of automated reasoning tools A handful of success stories Better appreciation of costs, benefits and limitations?

The basics Hoare triple This is a partial correctness assertion. {A} stmt {B} Precondition Postcondition Hoare triple If the precondition holds before stmt and stmt terminates, postcondition will hold afterward. This is a partial correctness assertion. We sometimes use the notation to denote a total correctness assertion which means you also have to prove termination. [A] stmt [B]

What do assertions mean? We first need to introduce a language. For today we will be using Winskel’s IMP: e := n | x | e1 + e2 | e1 - e2 c:= x := e | c1; c2 | if e then c1 else c2 | while e do c Big Step Semantics have two kinds of judgments: expressions result in values; commands change the state

Semantics of IMP Commands mutate the state: What about loops?

Semantics of IMP The definition for loops must be recursive.

What do assertions mean? The language of assertions: A := true | false | e1 = e2 | e1 ≥ e2 | A1 ∧ A2 | ¬A | ∀x. A Notation 𝜎⊨𝐴 means that the assertion holds on state 𝜎 . 𝐴 is interpreted inductively over state 𝜎 as a FO structure. Ex. 𝜎⊨𝐴∧𝐵 iff. 𝜎⊨𝐴 and 𝜎⊨𝐵 Partial Correctness can then be defined in terms of OS: {A} c {B} iff. If the program state before execution satisfies A, and the execution of c terminates, the program state after execution satisfies B

Defining axiomatic semantics Establishing the truth of a Hoare triple in terms of the operational semantics is impractical. The real power of AS is the ability to establish the validity of a Hoare triple by using deduction rules. means we can deduce the triple from a set of basic axioms.

Derivation Rules Derivation rules for each language construct Can be combined together with the rule of consequence

Example The following program purports to compute the square of a given integer n (not necessarily positive). int i, j; i := 1; j := 1; while (j != n) { i := i + 2*j + 1; j := j+1; } return i;

Example {true} int i, j; i := 1; j := 1; while (j != n) { i := i + 2*j + 1; j := j+1; } return i; {i = n*n}

Example {true} int i, j; {??} i := 1; j := 1; while (j != n) { i := i + 2*j + 1; j := j+1; } return i; {i = n*n}

Example {true} int i, j; {true} //strongest postcondition i := 1; {i=1} //strongest postcondition j := 1; {i=1 ∧ j=1} //strongest postcondition {??} //loop invariant while (j != n) { i := i + 2*j + 1; j := j+1; } {i = n*n} //weakest postcondition return i; {i = n*n}

Example {true} int i, j; {true} //strongest postcondition i := 1; {i=1} //strongest postcondition j := 1; {i=1 ∧ j=1} //strongest postcondition {i = j*j} //loop invariant while (j != n) { i := i + 2*j + 1; j := j+1; } {i = n*n} //weakest postcondition return i; {i = n*n}

Example {true} int i, j; {true} //strongest postcondition i := 1; {i=1} //strongest postcondition j := 1; {i=1 ∧ j=1} //strongest postcondition {i = j*j} //loop invariant while (j != n) { {i = j*j ∧ j != n} i := i + 2*j + 1; {i = (j+1)*(j+1) ∧ j != n} j := j+1; {i = j*j ∧ j-1 != n} } {i = n*n} //weakest postcondition return i; {i = n*n}

Soundness and Completeness What does it mean for our deduction rules to be sound? What does it mean for them to be complete? So are they complete? {true} x:=x {p} {true} c {false} Relative Completeness in the sense of Cook (1974) Expressible enough to express intermediate assertions, e.g., loop invariants

Total Correctness Definition: a well-ordered set is a set 𝑆 with with a total order > such that every non-empty subset of 𝑆 has a least element. E.g., ℕ,> is a w.o. set, ℤ,> is not ℕ 2 ,> where 𝑎,𝑏 > 𝑎 ′ , 𝑏 ′ if 𝑎>𝑎′, or 𝑎=𝑎′ and 𝑏>𝑏′

Total Correctness Termination: 1. find a ranking function 𝑟𝑎𝑛𝑘:𝑃𝑟𝑜𝑔𝑆𝑡𝑎𝑡𝑒𝑠→(ℕ,>) 2. find a set cutpoints (program points) to cut the program 3. prove for any cutpoint 𝑝𝑐, and any two program states 𝑆 1 , 𝑆 2 , if ( 𝑆 1 ,𝑝𝑐) reaches ( 𝑆 2 ,𝑝𝑐) in a execution sequence, then 𝑟𝑎𝑛𝑘 𝑆 1 >𝑟𝑎𝑛𝑘( 𝑆 2 ) Example: while (x>5) x:=x-1;

Total Correctness Example: int i, j; i := 1; j := 1; while (j != n) { i := i + 2*j + 1; j := j+1; } return i;