Symbolic Execution and Verification Conditions. Review of Verification Conditions. Survey from Homework 4. Time spent: 7.1 hrs (mean), 6 hrs (median)

Similar documents
Axiomatic Semantics III --The. Verification Crusade

Axiomatic Semantics. Automated Deduction - George Necula - Lecture 2 1

Axiomatic Semantics. More Semantics. Review - Operational Semantics. Partial Correctness Assertions. Programs Theorems. Axiomatic Semantics

Introduction to Axiomatic Semantics

Lectures 20, 21: Axiomatic Semantics

6. Hoare Logic and Weakest Preconditions

Part II. Hoare Logic and Program Verification. Why specify programs? Specification and Verification. Code Verification. Why verify programs?

Introduction to Axiomatic Semantics (1/2)

Hoare Logic: Proving Programs Correct

Introduction to Axiomatic Semantics (1/2)

AXIOMS OF AN IMPERATIVE LANGUAGE PARTIAL CORRECTNESS WEAK AND STRONG CONDITIONS. THE AXIOM FOR nop

Verification Condition Generation

Program Verification (6EC version only)

Induction and Semantics in Dafny

1 Introduction. 3 Syntax

Chapter 3 (part 3) Describing Syntax and Semantics

A CRASH COURSE IN SEMANTICS

Types for References, Exceptions and Continuations. Review of Subtyping. Γ e:τ τ <:σ Γ e:σ. Annoucements. How s the midterm going?

An Annotated Language

Harvard School of Engineering and Applied Sciences CS 152: Programming Languages

Proof Carrying Code(PCC)

CS 6110 S11 Lecture 25 Typed λ-calculus 6 April 2011

Lecture Notes: Hoare Logic

Softwaretechnik. Program verification. Albert-Ludwigs-Universität Freiburg. June 28, Softwaretechnik June 28, / 24

The Rule of Constancy(Derived Frame Rule)

Reasoning About Imperative Programs. COS 441 Slides 10

Main Goal. Language-independent program verification framework. Derive program properties from operational semantics

Why. an intermediate language for deductive program verification

Lecture 1 Contracts. 1 A Mysterious Program : Principles of Imperative Computation (Spring 2018) Frank Pfenning

Program Static Analysis. Overview

Programming Languages Third Edition

Programming Languages and Compilers Qualifying Examination. Answer 4 of 6 questions.

Hoare triples. Floyd-Hoare Logic, Separation Logic

Hoare logic. A proof system for separation logic. Introduction. Separation logic

Softwaretechnik. Program verification. Software Engineering Albert-Ludwigs-University Freiburg. June 30, 2011

Chapter 3. Describing Syntax and Semantics ISBN

COS 320. Compiling Techniques

Lecture 1 Contracts : Principles of Imperative Computation (Fall 2018) Frank Pfenning

Semantics. There is no single widely acceptable notation or formalism for describing semantics Operational Semantics

Getting Started with AutoCorres

Hoare Logic and Model Checking

CITS5501 Software Testing and Quality Assurance Formal methods

CSE 307: Principles of Programming Languages

Checking Program Properties with ESC/Java

CS4215 Programming Language Implementation. Martin Henz

Hoare logic. WHILE p, a language with pointers. Introduction. Syntax of WHILE p. Lecture 5: Introduction to separation logic

Programming Languages

Hoare logic. Lecture 5: Introduction to separation logic. Jean Pichon-Pharabod University of Cambridge. CST Part II 2017/18

Formal Methods. CITS5501 Software Testing and Quality Assurance

Programming Languages Lecture 15: Recursive Types & Subtyping

4/24/18. Overview. Program Static Analysis. Has anyone done static analysis? What is static analysis? Why static analysis?

Abstract Interpretation

1. true / false By a compiler we mean a program that translates to code that will run natively on some machine.

Assertions, pre/postconditions

Overview. Probabilistic Programming. Dijkstra s guarded command language: Syntax. Elementary pgcl ingredients. Lecture #4: Probabilistic GCL

Programming Languages

Operational Semantics. One-Slide Summary. Lecture Outline

Compilation and Program Analysis (#11) : Hoare triples and shape analysis

Static verification of program running time

Static Program Analysis Part 1 the TIP language

Symbolic Execution and Proof of Properties

In Our Last Exciting Episode

Generalized Verification Support for Magic Wands

Introduction to Denotational Semantics. Brutus Is An Honorable Man. Class Likes/Dislikes Survey. Dueling Semantics

CS 161 Computer Security

Principles of Programming Languages

Verifying Java Programs Verifying Java Programs with KeY

Recursive Types and Subtyping

COMP 507: Computer-Aided Program Design

Lecture 5 - Axiomatic semantics

Simply-Typed Lambda Calculus

Hoare Logic. COMP2600 Formal Methods for Software Engineering. Rajeev Goré

axiomatic semantics involving logical rules for deriving relations between preconditions and postconditions.

Incremental Proof Development in Dafny

MIDTERM EXAMINATION - CS130 - Spring 2005

Having a BLAST with SLAM

Verifying Java Programs Verifying Java Programs with KeY

Having a BLAST with SLAM

Hiding local state in direct style: a higher-order anti-frame rule

Shared Variables and Interference

n Specifying what each method does q Specify it in a comment before method's header n Precondition q Caller obligation n Postcondition

Chapter 1. Introduction

Meeting13:Denotations

Forward Assignment; Strongest Postconditions

Program Verification. Aarti Gupta

Verifying Java Programs with KeY

Hardware versus software

Software Security: Vulnerability Analysis

Overview. Verification with Functions and Pointers. IMP with assertions and assumptions. Proof rules for Assert and Assume. IMP+: IMP with functions

Lecture Notes on Memory Layout

3.7 Denotational Semantics

Lecture 3 Notes Arrays

Assertions. Assertions - Example

Chapter 13: Reference. Why reference Typing Evaluation Store Typings Safety Notes

Formal Systems II: Applications

Outline. Introduction. 2 Proof of Correctness. 3 Final Notes. Precondition P 1 : Inputs include

Static Program Analysis

Chapter 3. Semantics. Topics. Introduction. Introduction. Introduction. Introduction

Arguing for program correctness and writing correct programs

Reasoning about programs

Transcription:

Symbolic Execution and Verification Conditions Survey from Homework 4 Time spent: 7.1 hrs (mean), 6 hrs (median) Meeting 13, CSCI 5535, Spring 2009 2 Survey from Homework 4 Fun facts about your classmates: Born and raised in Indiana Likes animals more than human beings First programming language was Fortran Is a synesthete visual perception of time Review of Verification Conditions 3 Review by Class Participation Axiomatic semantics: The meaning of a program is Hoare triples Weakest Precondition Verification Condition Not Quite Weakest Preconditions Recall what we are trying to do: false Pre(s, B) strong weakest A precondition: WP(c, B) verification condition: VC(c, B) true weak We shall construct a verification condition: VC(c, B) The loops are annotated with loop invariants! VC is guaranteed stronger than WP But hopefully still weaker than A: A VC(c, B) WP(c, B) 5 CS 263 6 1

VCGen for while VC(while Inv b do c, B) = Inv Æ ( x 1 x n. Inv ((b VC(c, Inv)) Æ ( b B))) Inv holds on entry Inv is preserved in an arbitrary iteration B holds when the loop terminates in an arbitrary iteration Inv is the loop invariant (provided externally) x 1,, x n are all the variables modified in c 7 Forward VCGen Traditionally the VC is computed backwards That s how we ve been doing it in class It works well for structured code But it can also be computed forward Works even for unstructured languages (e.g., assembly language) Uses symbolic execution, a technique that has broad applications in program analysis e.g., the PREfix tool (Intrinsa, Microsoft) works this way 8 Plan for Applying VCGen End of Review Questions? Symbolic Execution and Forward VCGen Handling Exponential Blowup Invariants Dropping Paths VCGen For Exceptions (double trouble) VCGen For Memory (McCarthyism) VCGen For Structures (have a field day) VCGen For Semantic Checksum 10 Simple Assembly Language Consider the language of instructions: I ::= x := e f() if e goto L goto L L: return inv e The inv e instruction is an annotation Says that boolean expression e holds at that point Each function f() comes with Pre f and Post f annotations (pre- and postconditions) New Notation (yay!): I k is the instruction at address k 11 Symbolic Execution Symbolic State We set up a symbolic execution state: Σ : Var SymbolicExpressions Σ(x) = the symbolic value of x in state Σ Σ[x:=e] = a new state where x s value is e We use states as substitutions: Σ(e) = e where x replaced by Σ(x) So far, much like operational semantics 12 2

Symbolic Execution Invariant State The symbolic executor keeps track of the encountered invariants A new part of symex state: Inv {1 n} If k Inv then I k is an invariant instr. that we have already executed Basic idea: execute an inv instruction only twice: The first time it is encountered Once more time around an arbitrary iteration 13 Symbolic Execution Rules Define a VC function as an interpreter: VC : Address SymbolicState InvariantState Assertion VC(k, Σ, Inv) = VC(L, Σ, Inv) if I k = goto L if I k = if e goto L if I k = x := e if I k = return if I k = f() 14 Symbolic Execution Rules Define a VC function as an interpreter: VC : Address SymbolicState InvariantState Assertion VC(L, Σ, Inv) e VC(L, Σ, Inv) Æ e VC(k+1, Σ, Inv) VC(k+1, Σ[x:=Σ(e)], Inv) if I k = x := e Σ(Post current-function ) if I k = return VC(k, Σ, Inv) = Σ(Pre f ) Æ a 1..a m.σ (Post f ) VC(k+1, Σ, Inv) if I k = f() (where y 1,, y m are modified by f) and a 1,, a m are fresh parameters and Σ = Σ[y 1 := a 1,, y m := a m ] if I k = goto L if I k = if e goto L 15 Symbolic Execution Invariants Two cases when seeing an invariant instruction: 1. We see the invariant for the first time I k = inv e k Inv (= not in the set of invariants we ve seen ) Let {y 1,, y m } = the variables that could be modified on a path from the invariant back to itself Let a 1,, a m be fresh new symbolic parameters VC(k, Σ, Inv) = Σ(e) Æ a 1 a m. Σ (e) VC(k+1, Σ, Inv {k}]) with Σ = Σ[y 1 := a 1,, y m := a m ] (like a function call) 16 Symbolic Execution Invariants 2. We see the invariant for the second time I k = inv E k Inv VC(k, Σ, Inv) = Σ(e) (like a function return) Some tools take a more simplistic approach Do not require invariants Iterate through the loop a fixed number of times PREfix, versions of ESC (DEC/Compaq/HP SRC) Sacrifice soundness for usability 17 Symbolic Execution Top-Level Let x 1,, x n be the variables and a 1,, a n fresh params Let Σ 0 be the state [x 1 := a 1,,x n :=a n ] Let be the empty Inv set For all functions f in your program, prove: a 1 a n. Σ 0 (Pre f ) VC(f entry, Σ 0, ) If you start the program by invoking any f in a state that satisfies Pre f, then the program will execute such that At all inv e the e holds, and If the function returns then Post f holds Can be proved w.r.t. a real interpreter (operational semantics) Or via a proof technique called co-induction (or, assumeguarantee) 18 3

Forward VCGen Example Precondition: x 0 Loop: inv x 6 For convenience, name fresh parameter x (instead of a ) if x > 5 goto End x := x + 1 goto Loop End: return Postconditon: x = 6 Forward VCGen Example Precondition: x 0 Loop: inv x 6 For convenience, name fresh parameter x (instead of a ) if x > 5 goto End x := x + 1 goto Loop End: return Postconditon: x = 6 x. x 0 x 6 Æ x. (x 6 x > 5 x = 6 Æ x 5 x + 1 6 ) x. x 0 x 6 Æ x. (x 6 x > 5 x = 6 Æ x 5 x + 1 6 ) VC contains both proof obligations and assumptions about the control flow 19 VC contains both proof obligations and assumptions about the control flow 20 VCs Can Be Large Consider the sequence of conditionals (if x < 0 then x := - x); (if x 3 then x += 3) With the postcondition P(x) The VC is x < 0 Æ -x 3 P(-x + 3) Æ x < 0 Æ -x > 3 P(-x) Æ x 0 Æ x 3 P(x + 3) Æ x 0 Æ x > 3 P(x ) There is one conjunct for each path exponential number of paths! Conjuncts for infeasible paths have unsatisfiable guards! Try with P(x) = x 3 22 VCs Can Be Exponential VCs are exponential in the size of the source because they attempt relative completeness: Perhaps the correctness of the program must be argued independently for each path Unlikely that the programmer wrote a program by considering an exponential number of cases But possible. Any examples? Any solutions? VCs Can Be Exponential. Solutions? VCs are exponential in the size of the source because they attempt relative completeness: Perhaps the correctness of the program must be argued independently for each path Unlikely that the programmer wrote a program by considering an exponential number of cases But possible. Any examples? Any solutions? 23 24 4

VCs Can Be Exponential. Solutions? VCs are exponential in the size of the source because they attempt relative completeness: Perhaps the correctness of the program must be argued independently for each path Invariants in Straight-Line Code Purpose: modularize the verification task Add the command after c establish Inv Same semantics as c (Inv is only for VC purposes) VC(after c establish Inv, P) = def Standard Solutions: Allow invariants even in straight-line code And thus do not consider all paths independently! 25 26 Invariants in Straight-Line Code Purpose: modularize the verification task Add the command after c establish Inv Same semantics as c (Inv is only for VC purposes) VC(after c establish Inv, P) = def VC(c,Inv) x i. Inv P where x i are the ModifiedVars(c) Use when c contains many paths after if x < 0 then x := - x establish x 0; if x 3 then x += 3 { P(x) } VC is now: (x < 0 - x 0) Æ (x 0 x 0) Æ x. x 0 (x 3 P(x+3) Æ x > 3 P(x)) 27 Dropping Paths Without annotations, we can drop some paths VC(if E then c 1 else c 2, P) = choose one of E VC(c 1, P) E VC(c 2, P) (drop no paths) E VC(c 1, P) (drops else path!) E VC(c 2, P) (drops then path!) We sacrifice soundness! (we are now unsound) No more guarantees Possibly still a good debugging aid Remarks: A recent trend is to sacrifice soundness to increase usability (e.g., Metal, ESP, even ESC) The PREfix tool considers only 50 non-cyclic paths through a function (almost at random) 28 VCGen for Exceptions Extend the language with exceptions without arguments (cf. HW2): throw throws an exception try c 1 catch c 2 executes c 2 if c 1 throws Problem: We have non-local transfer of control What is VC(throw, P)? Solutions? 30 5

VCGen for Exceptions Extend the language with exceptions without arguments (cf. HW2): throw throws an exception try c 1 catch c 2 executes c 2 if c 1 throws Problem: We have non-local transfer of control What is VC(throw, P)? Standard Solution: use 2 postconditions One for normal termination One for exceptional termination 31 VCGen for Exceptions VC(c, P, Q) is a precondition that makes c either not terminate, or terminate normally with P or throw an exception with Q Rules VC(skip, P, Q) = P VC(c 1 ; c 2, P, Q) = VC(c 1, VC(c 2, P, Q), Q) VC(throw, P, Q) = VC(try c 1 catch c 2, P, Q) = VC(try c 1 finally c 2, P, Q) =? 32 VCGen for Exceptions VC(c, P, Q) is a precondition that makes c either not terminate, or terminate normally with P or throw an exception with Q Rules VC(skip, P, Q) = P VC(c 1 ; c 2, P, Q) = VC(c 1, VC(c 2, P, Q), Q) VC(throw, P, Q) = Q VC(try c 1 catch c 2, P, Q) = VC(c 1, P, VC(c 2, P, Q)) VC(try c 1 finally c 2, P, Q) =? 33 VCGen Try-Finally Given these: VC(c 1 ; c 2, P, Q) = VC(c 1, VC(c 2, P, Q), Q) VC(try c 1 catch c 2, P, Q) = VC(c 1, P, VC(c 2, P, Q)) Finally is somewhat like if : VC(try c 1 finally c 2, P, Q) = VC(c 1, VC(c 2, P, Q), true ) Æ VC(c 1, true, VC(c 2, Q, Q) ) Which reduces to: VC(c 1, VC(c 2, P, Q), VC(c 2, Q, Q ) 34 VCGen Try-Finally Given these: VC(c 1 ; c 2, P, Q) = VC(c 1, VC(c 2, P, Q), Q) VC(try c 1 catch c 2, P, Q) = VC(c 1, P, VC(c 2, P, Q)) Finally is somewhat like if : VC(try c 1 finally c 2, P, Q) = VC(c 1, VC(c 2, P, Q), true) Æ VC(c 1, true, VC(c 2, Q, Q)) Which reduces to: VC(c 1, VC(c 2, P, Q), VC(c 2, Q, Q)) 35 6

Hoare Rules and the Heap When is the following Hoare triple valid? { A } *x := 5 { *x + *y = 10 } A should be *y = 5 or x = y The Hoare rule for assignment would give us: [5/*x](*x + *y = 10) = 5 + *y = 10 = *y = 5 (we lost one case) Handling The Heap We do not yet have a way to talk about memory (the heap, pointers) in assertions Model the state of memory as a symbolic mapping from addresses to values: If A denotes an address and M is a memory state then: sel(m,a) denotes the contents of the memory cell upd(m,a,v) denotes a new memory state obtained from M by writing V at address A Why didn t this work? 37 38 More on Memory Allow variables to range over memory states Can quantify over all possible memory states Use the special pseudo-variable µ (mu) in assertions to refer to the current memory Example: i. i 0 i < 5 sel(µ, A + i) > 0 says that entries 0..4 in array A are positive 39 Hoare Rules: Side-Effects To model writes we use memory expressions A memory write changes the value of memory { B[upd(µ, E 1, E 2 )/µ] } *E 1 := E 2 {B} 40 Hoare Rules: Side-Effects To model writes we use memory expressions A memory write changes the value of memory { B[upd(µ, E 1, E 2 )/µ] } *E 1 := E 2 {B} Important technique: treat memory as a whole And reason later about memory expressions with inference rules such as (McCarthy, ~ 67): sel(upd(m, A 1, V), A 2 ) = V if A 1 = A 2 sel(m, A 2 ) if A 1 A 2 41 Memory Aliasing Consider again: { A } *x := 5 { *x + *y = 10 } We obtain: A = [upd(µ, x, 5)/µ] (*x + *y = 10) = [upd(µ, x, 5)/µ] (sel(µ, x) + sel(µ, y) = 10) (1) = sel(upd(µ, x, 5), x) + sel(upd(µ, x, 5), y) = 10 = 5 + sel(upd(µ, x, 5), y) = 10 = if x = y then 5 + 5 = 10 else 5 + sel(µ, y) = 10 (2) = x = y or *y = 5 Up to (1) is theorem generation From (1) to (2) is theorem proving 42 7

Alternative Handling for Memory Reasoning about aliasing can be expensive It is NP-hard (and/or undecidable) Sometimes completeness is sacrificed with the following (approximate) rule: V if A 1 = (obviously) A 2 sel(upd(m, A 1, V), A 2 ) = sel(m, E 3 ) if A 1 (obviously) A 2 p otherwise (p is a fresh new parameter) The meaning of obvious varies: The addresses of two distinct globals are The address of a global and one of a local are PREfix and GCC use such schemes 43 VCGen Overarching Example Consider the program Precondition: B : bool A : array(bool, L) 1: I := 0 R := B 3: inv I 0 R : bool if I L goto 9 assert saferd(a + I) T := *(A + I) I := I + 1 R := T goto 3 9: return R Postcondition: R : bool 44 VCGen Overarching Example A. B. L. µ B : bool Æ A : array(bool, L) 0 0 Æ B : bool Æ I. R. I 0 Æ R : bool I L R : bool Æ I < L saferd(a + I) Æ I + 1 0 Æ sel(µ, A + I) : bool VC contains both proof obligations and assumptions about the control flow 45 Mutable Records - Two Models Let r : RECORD { f1 : T1; f2 : T2 } END For us, records are reference types Method 1: one memory for each record One index constant for each field r.f1 is sel(r,f1) and r.f1 := E is r := upd(r,f1,e) Method 2: one memory for each field The record address is the index r.f1 is sel(f1,r) and r.f1 := E is f1 := upd(f1,r,e) Only works in strongly-typed languages like Java Fails in C where &r.f2 = &r + sizeof(t1) 47 VC as a Semantic Checksum Weakest preconditions are an expression of the program s semantics: Two equivalent programs have logically equivalent WPs No matter how different their syntax is! VC are almost as powerful 48 8

VC as a Semantic Checksum Consider the assembly language program to the right x := 4 x := (x == 5) assert x : bool x := not x assert x High-level type checking is not appropriate here The VC is: ((4 == 5) : bool) (not (4 == 5)) No confusion from reuse of x with diff. types 49 Invariance of VC Across Optimizations VC is so good at abstracting syntactic details that it is syntactically preserved by many common optimizations Register allocation, instruction scheduling Common subexp elim, constant and copy propagation Dead code elimination We have identical VCs whether or not an optimization has been performed Preserves syntactic form, not just semantic meaning! This can be used to verify correctness of compiler optimizations (Translation Validation) 50 VC Characterize a Safe Interpreter Consider a fictitious safe interpreter As it goes along it performs checks (e.g. safe to read from this memory addr, this is a null-terminated string, I have not already acquired this lock ) Some of these would actually be hard to implement The VC describes all of the checks to be performed Along with their context (assumptions from conditionals) Invariants and pre/postconditions are used to obtain a finite expression (through induction) VC is valid interpreter never fails We enforce same level of correctness But better (static + more powerful checks) VC Big Picture Verification conditions Capture the semantics of code + specifications Language independent Can be computed backward/forward on structured/unstructured code Make Axiomatic Semantics practical 51 52 Invariants Are Not Easy Consider the following code from QuickSort int partition(int *a, int L 0, int H 0, int pivot) { int L = L 0, H = H 0 ; while(l < H) { while(a[l] < pivot) L ++; while(a[h] > pivot) H --; if(l < H) { swap a[l] and a[h] } } return L } Consider verifying only memory safety What is the loop invariant for the outer loop? 53 Questions? 54 9

One-Slide Summary Verification conditions make axiomatic semantics practical. We can compute verification conditions forward for use on unstructured code (= assembly language). This is sometimes called symbolic execution. We can add extra invariants or drop paths (dropping is unsound) to help verification condition generation scale. We can model exceptions, memory operations and data structures using verification condition generation. 55 10