Reasoning about code CSE 331 University of Washington.

Slides:



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

Copyright W. Howden1 Programming by Contract CSE 111 6/4/2014.
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.
Reasoning About Code; Hoare Logic, continued
Hoare’s Correctness Triplets Dijkstra’s Predicate Transformers
David Evans CS655: Programming Languages University of Virginia Computer Science Lecture 19: Minding Ps & Qs: Axiomatic.
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.
1 Design by Contract Building Reliable Software. 2 Software Correctness Correctness is a relative notion  A program is correct with respect to its specification.
Predicate Transformers
Program Proving Notes Ellen L. Walker.
Fall Semantics Juan Carlos Guzmán CS 3123 Programming Languages Concepts Southern Polytechnic State University.
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.
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.
4/17/2017 Section 3.6 Program Correctness ch3.6.
Operational Semantics Semantics with Applications Chapter 2 H. Nielson and F. Nielson
Describing Syntax and Semantics
Proving Program Correctness The Axiomatic Approach.
Proving Program Correctness The Axiomatic Approach.
Computer Science School of Computing Clemson University Discrete Math and Reasoning about Software Correctness Murali Sitaraman
Reading and Writing Mathematical Proofs
1 Program Correctness CIS 375 Bruce R. Maxim UM-Dearborn.
CSI 3125, Axiomatic Semantics, page 1 Axiomatic semantics The assignment statement Statement composition The "if-then-else" statement The "while" statement.
CS 363 Comparative Programming Languages Semantics.
Computer Science School of Computing Clemson University Discrete Math and Reasoning about Software Correctness Joseph E. Hollingsworth
Reading and Writing Mathematical Proofs Spring 2015 Lecture 4: Beyond Basic Induction.
Reasoning about programs March CSE 403, Winter 2011, Brun.
Semantics In Text: Chapter 3.
COP4020 Programming Languages Introduction to Axiomatic Semantics Prof. Robert van Engelen.
Understanding ADTs CSE 331 University of Washington.
Static Techniques for V&V. Hierarchy of V&V techniques Static Analysis V&V Dynamic Techniques Model Checking Simulation Symbolic Execution Testing Informal.
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.
C HAPTER 3 Describing Syntax and Semantics. D YNAMIC S EMANTICS Describing syntax is relatively simple There is no single widely acceptable notation or.
Zach Tatlock / Winter 2016 CSE 331 Software Design and Implementation Lecture 2 Formal Reasoning.
Reasoning and Design (and Assertions). How to Design Your Code The hard way: Just start coding. When something doesn’t work, code some more! The easier.
CORRECTNESS ISSUES AND LOOP INVARIANTS Lecture 8 CS2110 – Fall 2014.
CSE 374 Programming Concepts & Tools Hal Perkins Fall 2015 Lecture 17 – Specifications, error checking & assert.
Insertion sort Loop invariants Dynamic memory
Math/CSE 1019C: Discrete Mathematics for Computer Science Fall 2012
Correctness issues and Loop invariants
Loop Structures.
Reasoning About Code.
Proving Loops Testing debugging and verification
CSE 331 Software Design & Implementation
CSE 331 Software Design and Implementation
Looping and Repetition
Copyright © Cengage Learning. All rights reserved.
Reasoning About Code; Hoare Logic
Lecture 5 Floyd-Hoare Style Verification
Axiomatic semantics Points to discuss: The assignment statement
Programming Languages and Compilers (CS 421)
Programming Languages 2nd edition Tucker and Noonan
Semantics In Text: Chapter 3.
Chapter 6: Repetition Statements
Predicate Transformers
Axiomatic Semantics Will consider axiomatic semantics (A.S.) of IMP:
Output Variables {true} S {i = j} i := j; or j := i;
The Zoo of Software Security Techniques
Program correctness Axiomatic semantics
Program Verification with Hoare Logic
Programming Languages and Compilers (CS 421)
Programming Languages 2nd edition Tucker and Noonan
COP4020 Programming Languages
Generics, Lambdas and Reflection
Program Correctness an introduction.
Presentation transcript:

Reasoning about code CSE 331 University of Washington

Reasoning about code Determine what facts are true during execution for all nodes n: n.next.previous == n array a is sorted x + y == z if x != null, then x.a > x.b Applications: Ensure code is correct (via reasoning or testing) Understand why code is incorrect

Forward reasoning You know what is true before running the code What is true after running the code? Given a precondition, what is the postcondition? Example: // precondition: x is even x = x + 3; y = 2x; x = 5; // postcondition: ?? Application: Rep invariant holds before running code Does it still hold after running code?

Backward reasoning You know what you want to be true after running the code What must be true beforehand in order to ensure that? Given a postcondition, what is the corresponding precondition? Example: // precondition: ?? x = x + 3; y = 2x; x = 5; // postcondition: y > x Application: (Re-)establish rep invariant at method exit: what requires? Reproduce a bug: what must the input have been? Exploit a bug

SQL injection attack Server code bug: SQL query constructed using unfiltered user input query = “SELECT * FROM users ” + “WHERE name=‘” + userInput + “’;”; User inputs: a’ or ‘1’=‘1 Result: query ⇒ SELECT * FROM users WHERE name=‘a’ or ‘1’=‘1’; Query returns information about all users Program logic is supposed to scrub user inputs Does it? http://xkcd.com/327/

Forward vs. backward reasoning Forward reasoning is more intuitive for most people Helps you understand what will happen (simulates the code) Introduces facts that may be irrelevant to goal Set of current facts may get large Takes longer to realize that the task is hopeless Backward reasoning is usually more helpful Helps you understand what should happen Given a specific goal, indicates how to achieve it Given an error, gives a test case that exposes it

Reasoning about code statements Goal: Convert assertions about programs into logic General plan Eliminate code a statement at a time Rely on knowledge of logic and types There is a (forward and backward) rule for each statement in the programming language Loops have no rule: you have to guess a loop invariant Jargon: P {code} Q P and Q are logical statements (about program values) code is Java code “P {code} Q” means “if P is true and you execute code, then Q is true afterward” Is this notation good for forward or for backward reasoning?

Reasoning: putting together statements assert x >= 0; // x ≥ 0 z = 0; // x ≥ 0 & z = 0 if (i != 0) { // x > 0 & z = 0 z = x; // x > 0 & z = x } else { // x = 0 & z = 0 z = z + 1; // x = 0 & z = 1 } // x ≥ 0 & z > 0 assert z > 0;

Forward reasoning with a loop assert x >= 0; // x ≥ 0 i = x; // x ≥ 0 & i = x z = 0; // x ≥ 0 & i = x & z = 0 while (i != 0) { // ??? z = z + 1; i = i - 1; // ??? } // x ≥ 0 & i = 0 & z = x assert x == z; Infinite number of paths through this code How do you know that the overall conclusion is correct? Induction on the length of the computation

Assignment Backward reasoning: // precondition: ?? x = e; // postcondition: Q Precondition = Q with all (free) occurrences of x replaced by e Examples: // assert: ?? // assert: ?? y = x + 1; z = z + 1; // assert y > 0 // assert z > 0 Precondition = (x+1) > 0 Precondition = (z+1) > 0 Notation: wp for “weakest precondition” wp(“x=e;”, Q) = Q with x replaced by e

Method calls // precondition: ?? x = foo(); // postcondition: Q If the method has no side effects: just like ordinary assignment // precondition: ?? (y = 22 or y = -22) and (x = anything) x = Math.abs(y); // postcondition: x = 22 If it has side effects: an assignment to every var in modifies Use the method specification to determine the new value // precondition: ?? z+1 = 22 incrementZ(); // spec: zpost = zpre + 1 // postcondition: z = 22

Composition (statement sequences; blocks) // precondition: ?? S1; // some statement S2; // another statement // postcondition: Q Work from back to front Precondition = wp(“S1; S2;”, Q) = wp(“S1;”, wp(“S2;”, Q)) Example: // precondition: ?? x = 0; y = x+1; // postcondition: y > 0

If statements Do case analysis: // precondition: ?? if (b) S1 else S2 // postcondition: Q Do case analysis: wp(“if (b) S1 else S2”, Q) = ( b ⇒ wp(“S1”, Q) ∧ ¬b ⇒ wp(“S2”, Q) ) = ( b ∧ wp(“S1”, Q) ∨ ¬b ∧ wp(“S2”, Q) (Note no substitution in the condition.)

If statement example Precondition if (x < 5) { x = x*x; } else { x = x+1; } // postcondition: x ≥ 9 Precondition = wp(“if (x<5) {x = x*x;} else {x = x+1}”, x ≥ 9) = (x < 5 ∧ wp("x=x*x", x ≥ 9)) ∨ (x ≥ 5 ∧ wp("x=x+1", x ≥ 9)) = (-4< 5 ∧ x*x ≥ 9) ∨ (x ≥ 5 ∧ x+1 ≥ 9) = (x ≤ -3) ∨ (x ≥ 3 ∧ x < 5) ∨ (x ≥ 8) -4 -3 -2 -1 1 2 3 4 5 6 7 8 9

Reasoning about loops A loop represents an unknown number of paths Case analysis is problematic Recursion presents the same problem as loops Cannot enumerate all paths This is what makes testing and reasoning hard

Reasoning about loops: values and termination // assert x ≥ 0 & y = 0 while (x != y) { y = y + 1; } // assert x = y Does this loop terminate? 1) Pre-assertion guarantees that x ≥ y 2) Every time through loop x ≥ y holds - and if body is entered, x > y y is incremented by 1 x is unchanged Therefore, y is closer to x (but x ≥ y still holds) 3) Since there are only a finite number of integers between x and y, y will eventually equal x 4) Execution exits the loop as soon as x = y

Understanding loops by induction We just made an inductive argument Inducting over the number of iterations Computation induction Show that conjecture holds if zero iterations Show that it holds after n+1 iterations (assuming that it holds after n iterations) Two things to prove Some property is preserved (known as “partial correctness”) Loop invariant is preserved by each iteration The loop completes (known as “termination”) The “decrementing function” is reduced by each iteration and cannot be reduced forever

How to choose a loop invariant, LI // assert P while (b) S; // assert Q Find an invariant, LI, such that 1. P ⇒ LI // true initially 2. LI & b {S} LI // true if the loop executes once 3. (LI & ¬b) ⇒ Q // establishes the postcondition It is sufficient to know that if loop terminates, Q will hold. Finding the invariant is the key to reasoning about loops. Inductive assertions is a complete method of proof: If a loop satisfies pre/post conditions, then there exists an invariant sufficient to prove it

Loop invariant for the example //assert x ≥ 0 & y = 0 while (x != y) { y = y + 1; } //assert x = y A suitable invariant: LI = x ≥ y 1. x ≥ 0 & y = 0 ⇒ LI // true initially 2. LI & x ≠ y {y = y+1;} LI // true if the loop executes once 3. (LI & ¬(x ≠ y)) ⇒ x = y // establishes the postcondition

Total correctness via well-ordered sets We have not established that the loop terminates Suppose that the loop always reduces some variable’s value. Does the loop terminate if the variable is a - Natural number? - Integer? - Non-negative real number? - Boolean? - ArrayList? The loop terminates if the variable values are (a subset of) a well-ordered set - Ordered set - Every non-empty subset has least element

Decrementing function Decrementing function D(X) Maps state (program variables) to some well-ordered set Tip: always use the natural numbers This greatly simplifies reasoning about termination Consider: while (b) S; We seek D(X), where X is the state, such that 1. An execution of the loop reduces the function’s value: LI & b {S} D(Xpost) < D(Xpre) 2. If the function’s value is minimal, the loop terminates: (LI & D(X) = minVal) ⇒ ¬b

Proving termination // assert x ≥ 0 & y = 0 // Loop invariant: x ≥ y // Loop decrements: (x-y) while (x != y) { y = y + 1; } // assert x = y Is this a good decrementing function? 1. Does the loop reduce the decrementing function’s value? // assert (y ≠ x); let dpre = (x-y) y = y + 1; // assert (xpost - ypost) < dpre 2. If the function has minimum value, does the loop exit? (x ≥ y & x - y = 0) ⇒ (x = y)

Choosing loop invariants For straight-line code, the wp (weakest precondition) function gives us the appropriate property For loops, you have to guess: The loop invariant The decrementing function Then, use reasoning techniques to prove the goal property If the proof doesn't work: Maybe you chose a bad invariant or decrementing function Choose another and try again Maybe the loop is incorrect Fix the code Automatically choosing loop invariants is a research topic

When to use code proofs for loops Most of your loops need no proofs for (String name : friends) { ... } Write loop invariants and decrementing functions when you are unsure about a loop If a loop is not working: Add invariant and decrementing function if missing Write code to check them Understand why the code doesn't work Reason to ensure that no similar bugs remain