Software Quality: Testing and Verification II. 2 1.A failure is an unacceptable behaviour exhibited by a system — The frequency of failures measures software.

Slides:



Advertisements
Similar presentations
Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 18 Program Correctness To treat programming.
Advertisements

Automated Theorem Proving Lecture 1. Program verification is undecidable! Given program P and specification S, does P satisfy S?
Copyright W. Howden1 Programming by Contract CSE 111 6/4/2014.
Rigorous Software Development CSCI-GA Instructor: Thomas Wies Spring 2012 Lecture 10.
Rigorous Software Development CSCI-GA Instructor: Thomas Wies Spring 2012 Lecture 8.
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.
Introducing Formal Methods, Module 1, Version 1.1, Oct., Formal Specification and Analytical Verification L 5.
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.
Chapter 7 User-Defined Methods. Chapter Objectives  Understand how methods are used in Java programming  Learn about standard (predefined) methods and.
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.
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.
CS 355 – Programming Languages
Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 18 Program Correctness To treat programming.
An overview of JML tools and applications Lilian Burdy Gemplus Yoonsik Cheon, Gary Leavens Iowa Univ. David Cok Kodak Michael Ernst MIT Rustan Leino Microsoft.
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.
JML and Class Specifications Class invariant JML definitions Queue example Running JML in Eclipse.
OOP #10: Correctness Fritz Henglein. Wrap-up: Types A type is a collection of objects with common behavior (operations and properties). (Abstract) types.
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.
CS 330 Programming Languages 09 / 16 / 2008 Instructor: Michael Eckmann.
Describing Syntax and Semantics
Ranga Rodrigo. Class is central to object oriented programming.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 18 Exception Handling.
1 Debugging and Testing Overview Defensive Programming The goal is to prevent failures Debugging The goal is to find cause of failures and fix it Testing.
1 Program Correctness CIS 375 Bruce R. Maxim UM-Dearborn.
Chapter 3 (Part 3): Mathematical Reasoning, Induction & Recursion  Recursive Algorithms (3.5)  Program Correctness (3.6)
© by Kenneth H. Rosen, Discrete Mathematics & its Applications, Sixth Edition, Mc Graw-Hill, 2007 Chapter 4 (Part 3): Mathematical Reasoning, Induction.
07 Coding Conventions. 2 Demonstrate Developing Local Variables Describe Separating Public and Private Members during Declaration Explore Using System.exit.
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.
CS 363 Comparative Programming Languages Semantics.
Program Correctness. 2 Program Verification An object is a finite state machine: –Its attribute values are its state. –Its methods optionally: Transition.
Mr. Dave Clausen1 La Cañada High School Chapter 6: Repetition Statements.
Computer Science School of Computing Clemson University Discrete Math and Reasoning about Software Correctness Joseph E. Hollingsworth
Chapter 5: Sequences, Mathematical Induction, and Recursion 5.5 Application: Correctness of Algorithms 1 [P]rogramming reliability – must be an activity.
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.
Exceptions and Assertions Chapter 15 – CSCI 1302.
Principle of Programming Lanugages 3: Compilation of statements Statements in C Assertion Hoare logic Department of Information Science and Engineering.
PROGRAMMING PRE- AND POSTCONDITIONS, INVARIANTS AND METHOD CONTRACTS B MODULE 2: SOFTWARE SYSTEMS 13 NOVEMBER 2013.
Static Techniques for V&V. Hierarchy of V&V techniques Static Analysis V&V Dynamic Techniques Model Checking Simulation Symbolic Execution Testing Informal.
CSC3315 (Spring 2009)1 CSC 3315 Languages & Compilers Hamid Harroud School of Science and Engineering, Akhawayn University
C HAPTER 3 Describing Syntax and Semantics. D YNAMIC S EMANTICS Describing syntax is relatively simple There is no single widely acceptable notation or.
1 Exceptions When the Contract is Broken. 2 Definitions A routine call succeeds if it terminates its execution in a state satisfying its contract A routine.
Chapter 1 The Phases of Software Development. Software Development Phases ● Specification of the task ● Design of a solution ● Implementation of solution.
Chapter 4 (Part 3): Mathematical Reasoning, Induction & Recursion
Accessible Formal Methods A Study of the Java Modeling Language
Axiomatic semantics Points to discuss: The assignment statement
Programming Languages and Compilers (CS 421)
Programming Languages 2nd edition Tucker and Noonan
Hoare-style program verification
Semantics In Text: Chapter 3.
Java Modeling Language (JML)
The Zoo of Software Security Techniques
CSE 1020:Software Development
Programming Languages and Compilers (CS 421)
Programming Languages 2nd edition Tucker and Noonan
Programming Languages 2nd edition Tucker and Noonan
COP4020 Programming Languages
Presentation transcript:

Software Quality: Testing and Verification II

2 1.A failure is an unacceptable behaviour exhibited by a system — The frequency of failures measures software reliability Low failure rate = high reliability — Failures result from violation of a requirement 2.A defect is a flaw that contributes to a failure — It might take several defects to cause one failure 3.An error is a software decision that leads to a defect Software Flaws are identified at three levels

3 Eliminating Failures: Testing vs Verification Testing = running the program with a set of inputs to gain confidence that the software has few defects Goal: reduce the frequency of failures When done: after the programming is complete Methodology: develop test cases; run the program with each test case Verification = formally proving that the software has no defects (in this case, the program is said to be “correct”) Goal: eliminate failures When done: before, during and after the programming is complete Methodology: write separate specifications for the code; prove that the code and the specifications are mathematically equivalent

4 Program Correctness The correctness of a program is based on a specific standard. That standard is called a specification. int max (int a, int b) { int m; if (a >= b) m = a; else m = b; return m; } E.g., a specification for the above program might be that it “finds the maximum value of any two integers.”

5 Formalizing a Specification A Formal specification is written as a logical expression called an assertion. An assertion describes the state of the program’s variables. Two key assertions are the program’s precondition and its postcondition. int max (int a, int b) { int m; if (a >= b) m = a; else m = b; return m; } A domain is a set of values over which a variable is well defined. The primitive types (int, float, boolean, etc.) and standard Java classes (String, Vector, HashMap, etc.) provide domains for reasoning about programs. Postcondition Q = what’s ensured to be true when the program finishes Precondition P = what’s required for the program to begin its run.

6 Pre- and Postconditions Postconditions describes what it will compute. For max, a postcondition is Q = m = max(a, b), where max is a mathematical function that delivers the larger of two integers. Preconditions describe minimum requirements for the program to run. For max, a and b can be any integers, so the precondition is P = true. Before proving a program’s correctness, we first write its specifications: {true} int max (int a, int b) { int m; if (a >= b) m = a; else m = b; return m; } {m = max(a, b)}

7 Another Example – computing a factorial (n!) {n > 1} int Factorial (int n) { int f = 1; int i = 1; while (i < n) { i = i + 1; f = f * i; } return f; } {f = n!} This example raises two issues. What happens if: 1. the program has a loop that never terminates? 2. The program terminates abnormally (e.g., an exception is raised)? Precondition P Postcondition Q

8 Defining away the problem: “partial correctness” Let’s assume that nothing bad ever happens… programs always terminate after a finite number of steps, and termination is always normal. Then such a program is partially correct if, for every set of input values that satisfies precondition P, the program computes a result that satisfies postcondition Q. E.g., Factorial is partially correct if for every value of n that satisfies n > 1, it computes f =n!

9 (Partial) Correctness Proofs Let’s think generally about any program or sequence of statements s, whose pre- and postconditions are P and Q. A “Hoare triple” is a predicate of the form {P} s {Q} which asserts that “execution of statements s, beginning in a state that satisfies P, results in a state that satisfies Q.” If we can prove that this Hoare triple is valid, (i.e., it is true for all assignments of values to variables in P, Q, and s) then the program s is said to be correct. But how can we construct such a proof?

10 Constructing a Correctness Proof If the program is a series of statements: s 1 ; s 2 ; …; s n We start with the Hoare triple {P} s 1 ; s 2 ; …; s n {Q}, and use inference rules for programs to derive an intermediate triple {P i } s i {P i+1 } for every statement s i in this sequence. When done, we also ensure that Note: This process is similar to a direct proof in logic, where we use inference rules to derive a series of assertions that logically link the premises to the conclusion. So, what are the inference rules for programs?

11 Inference Rules are tied to program statement types

12 Formal methods and real programs Major question: Where do formal methods fit in the software process? That is, 1.How do we integrate them into object oriented programs? 2.How do we write pre- and postconditions P and Q for –methods? –classes? –systems? 3.Once written, how are these used to ensure that software is correct?

13 Recall the spiral model for software design Requirements Specifications (use cases) Design Coding/ Integration Testing and Verification Deployment (v 1.0)Review Prototype v 1.1 v 1.0

14 Formal methods in the object oriented design process In OO design, we focus on classes and objects Methods and messages are subordinate Collections of objects have state, which is the set of all active objects and the values of their variables at any moment of run time. Formal specifications P and Q are therefore logical expressions about each object’s state. Tools for the formal design process Specifications : Java Modeling Language (JML) Design: Unified Modeling Language (UML and JML) Coding: Java and JML Verification: JML

15 Correctness of OO systems Where? Method level: pre- and post-conditions, loop invariants Class level: class invariant (class state) System level: intra-class invariants (system state) When? Specification and design phases: Write specifications for all classes and methods (UML/JML) Coding phase: Develop code from the specifications (UML/JML/Java) Validation phase: Prove (mathematically) that specifications and code are logically equivalent (JML Java)

16 What is JML? ( History –Emerged in early 2000s out of ESC/Java2 Goals —Integration of formal methods throughout the software process —Formal specification accessible to programmers —Direct support for design by contract —Integration with a real language (Java) JML allows us to mix specifications directly with the Java code —Preconditions —Postconditions —Loop invariants —Class invariants

17 JML Basics JML specifications are special comments in a Java program: for one-liners for multiple-liners The Hoare triple {P} s 1 ; s 2 ; …; s n {Q} is written in JML/Java as ( P and Q are written as Java boolean expressions, and use parameters, local, and class variables as arguments.) requires P ; ensures Q type method (parameters) { local variables s 1 ; s 2 ; …; s n }

18 JML Language Summary Note: p and e are also good old fashioned Java boolean expressions, possibly augmented by JML-specific operations.

19 Here’s a simple example, first as a Hoare triple: {n > 1} P (precondition) int Factorial (int n) { int f = 1; int i = 1; {1 < i  i < n  f = i!} R (loop invariant) while (i < n) { i = i + 1; f = f * i; } return f; } {f = n!} Q (postcondition)

20 And again with JML expressions for P, Q, and R: requires 1 <= n ; ensures \result == (\product int i; 1<=i && i<=n; i) static int Factorial (int n) { int f = 1; int i = 1; loop_invariant i <= n && f == (\product int j; 1 <= j && j <= i; while (i < n) { i = i + 1; f = f * i; } return f; } PQR

21 JML-based software tools (1 and 3 are built into Eclipse) 1. Compiling (use jmlc instead of javac ) Does syntactic and type checking, and byte code generation for all JML assertions and Java code 2. Static checking (ESC/Java2) 3. Runtime assertion checking (use jmlrac instead of java ) Checks truth of precondition P at entry to every call Checks truth of postcondition Q at exit from every call Checks truth of loop invariant R before every iteration Issues a Java Exception when any of these is not true Note: this is not formal verification (Checking truth for one instance of a call is not the same as checking truth for all instances. The latter is proof of correctness!) 4. Proof assistance tools (Daikon, LOOP)

22 JML Eclipse Environment 3. Runtime assertion checking 1. Compiling

23 Continuing our JML example, let’s wrap Factorial inside a simple class: public class myFactorial { requires 1 <= n; ensures \result == (\product int i; 1<=i && i<=n; static int Factorial (int n) { … } public static void main(String[] args) { int n = Integer.parseInt(args[0]); System.out.println("Factorial of " + n + " = " + Factorial(n)); }

24 … compile it with jmlc, and run it twice with jmlrac: % jmlc -Q myFactorial.java % jmlrac myFactorial 3 Factorial of 3 = 6 % jmlrac myFactorial -5 Exception in thread "main” org.jmlspecs.jmlrac.runtime.JMLEntryPreconditionError: by method myFactorial.Factorial regarding specifications at File "myFactorial.java", line 3, character 15 when 'n' is -5 at myFactorial.checkPre$Factorial$myFactorial(myFactorial.java:240) at myFactorial.Factorial(myFactorial.java:382) at myFactorial.main(myFactorial.java:24) normal run abnormal run (throws a JML exception)

25 JML Exceptions

26 JML helps identify errors Example 1: Suppose we change the while loop from while (i < n) to while (i <= n) so that n! will be computed incorrectly. Here’s the result: % jmlrac myFactorial 3 Exception in thread "main" org.jmlspecs.jmlrac.runtime.JMLLoopInvariantError: LOOP INVARIANT: by method myFactorial.Factorial regarding specifications at File "myFactorial.java", line 9, character 24 when 'n' is 3 at myFactorial.internal$Factorial(myFactorial.java:102) at myFactorial.Factorial(myFactorial.java:575) at myFactorial.main(myFactorial.java:211) invariant not satisfied

27 JML Example 2 Suppose we change the while loop from while (i < n) to while (i <=n ) and also remove the JML loop invariant. Now we get: % jmlrac myFactorial 3 Exception in thread "main" org.jmlspecs.jmlrac.runtime.JMLNormalPostconditionError: by method myFactorial.Factorial regarding specifications at File "myFactorial.java", line 4, character 23 when 'n' is 3 '\result' is 24 at myFactorial.checkPost$Factorial$myFactorial(myFactorial.java:321) at myFactorial.Factorial(myFactorial.java:392) at myFactorial.main(myFactorial.java:24) postcondition not satisfied

28 JML Example 3 Disagreement between a JML specification and a program may signal an error in the specification. E.g., if the loop invariant had specified j <= i rather than j < i the following outcome would occur: % jmlrac myFactorial 3 Exception in thread "main" org.jmlspecs.jmlrac.runtime.JMLLoopInvariantError: LOOP INVARIANT: by method myFactorial.Factorial regarding specifications at File "myFactorial.java", line 9, character 24 when 'n' is 3 at myFactorial.internal$Factorial(myFactorial.java:101) at myFactorial.Factorial(myFactorial.java:573) at myFactorial.main(myFactorial.java:209) invariant not satisfied

29 But beware… JML is no panacea jmlrac doesn’t trap all errors… here are two “normal” runs: % jmlrac myFactorial 21 Factorial of 21 = % jmlrac myFactorial 32 Factorial of 32 = Recall: (1) Java has no ArithmeticOverflow exception, but (2) Factorial(n) for n > 12 should give a result > Note: jmlrac computes the same wrong result when it checks the postcondition as the Factorial method computes, so that this error goes undetected. Conclusion: the program and its specifications are both wrong. wrong results!

30 Exception Handling in JML requires P ; ensures Q ; signals (exception) type method (parameters) { locals s 1 ; s 2 ; …; s n (includes “throw new exception ;”) } We can throw Java Exceptions, and then validate their circumstances in JML whenever they occur. whenever this happens JML executes this Two outcomes: 1) expression is true and normal Java exception handling proceeds, or 2) expression is false and JMLExceptionalPostconditionError is reported.

31 JML Example 4: Throwing and Checking Exceptions requires 1 <= n; ensures \result == (\product int i; 1<=i && i<=n; i); signals (ArithmeticException) n > static int Factorial (int n) { if (n > 12) throw new ArithmeticException(); else { … % jmlrac myFactorial 22 Exception in thread "main" java.lang.ArithmeticException at myFactorial.internal$Factorial(myFactorial.java:9) at myFactorial.Factorial(myFactorial.java:610) at myFactorial.main(myFactorial.java:213) Normal Java exception handling occurs, since n > 12 is true.

32 Additional Points about JML 1. We can sometimes avoid a signals clause by strengthening the precondition. E.g., for Factorial, we could have said: requires 1 <= n && n < 13 ; 2. Specifications are always declarative; they never affect the state of the program. 3. Runtime assertion checking is not proof, but it does provide a rigorous framework for debugging. 4. JML provides a language in which formal methods and Java programs can be integrated. 5. There’s a lot more to JML. We are particularly interested in: a. class level specifications? b. Tools for static checking of specifications? c. Tools for proving correctness?

33 Readings (at the course web site) Hoare, An axiomatic basis for computer programming, Communications of the ACM 12(10): An axiomatic basis for computer programming Leino, Hoare-Style Program Verification I Hoare-Style Program Verification I Leino, Hoare-Style Program Verification II Hoare-Style Program Verification II Gordon, Specification and Verification I Specification and Verification I Dwyer et al., Software SpecificationsSoftware Specifications Leavens and Cheon, Design by Contract with JML Design by Contract with JML Poll, Kiniry, and Cok, Introduction to JML Introduction to JML Burdy et al., Overview of JML Tools Overview of JML Tools JML Reference ManualReference Manual