Rigorous Software Development CSCI-GA 3033-011 Instructor: Thomas Wies Spring 2012 Lecture 8.

Slides:



Advertisements
Similar presentations
Chapter 17 Failures and exceptions. This chapter discusses n Failure. n The meaning of system failure. n Causes of failure. n Handling failure. n Exception.
Advertisements

Advanced programming tools at Microsoft
Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 18 Program Correctness To treat programming.
Writing specifications for object-oriented programs K. Rustan M. Leino Microsoft Research, Redmond, WA, USA 21 Jan 2005 Invited talk, AIOOL 2005 Paris,
Program Verification Using the Spec# Programming System ETAPS Tutorial K. Rustan M. Leino, Microsoft Research, Redmond Rosemary Monahan, NUIM Maynooth.
Automated Theorem Proving Lecture 1. Program verification is undecidable! Given program P and specification S, does P satisfy S?
Challenges in increasing tool support for programming K. Rustan M. Leino Microsoft Research, Redmond, WA, USA 23 Sep 2004 ICTAC Guiyang, Guizhou, PRC joint.
Exceptions CSE301 University of Sunderland Harry Erwin, PhD.
Rigorous Software Development CSCI-GA Instructor: Thomas Wies Spring 2013 Lecture 5 Disclaimer. These notes are derived from notes originally.
Rigorous Software Development CSCI-GA Instructor: Thomas Wies Spring 2012 Lecture 10.
Rigorous Software Development CSCI-GA Instructor: Thomas Wies Spring 2012 Lecture 6 Disclaimer. These notes are derived from notes originally.
Identity and Equality Based on material by Michael Ernst, University of Washington.
Introduction to Linked Lists In your previous programming course, you saw how data is organized and processed sequentially using an array. You probably.
Problem Solving 5 Using Java API for Searching and Sorting Applications ICS-201 Introduction to Computing II Semester 071.
Rigorous Software Development CSCI-GA Instructor: Thomas Wies Spring 2012 Lecture 11.
The Java Modeling Language JML Erik Poll Digital Security Radboud University Nijmegen.
Rigorous Software Development CSCI-GA Instructor: Thomas Wies Spring 2012 Lecture 9.
Data Abstraction II SWE 619 Software Construction Last Modified, Spring 2009 Paul Ammann.
Lecture 23 Input and output with files –(Sections 2.13, 8.7, 8.8) Exceptions and exception handling –(Chapter 17)
JML and ESC/Java2: An Introduction Karl Meinke School of Computer Science and Communication, KTH.
Chapter 8 Designing Classes. Assignment Chapter 9 Review Exercises (Written)  R8.1 – 8.3, 8.5 – 8.7, 8. 10, 8.11, 8.13, 8.15, 8.19, 8.20 Due Friday,
6/10/2015C++ for Java Programmers1 Pointers and References Timothy Budd.
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.
Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 18 Program Correctness To treat programming.
1 Chapter 4 Language Fundamentals. 2 Identifiers Program parts such as packages, classes, and class members have names, which are formally known as identifiers.
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.
Software Quality: Testing and Verification II. 2 1.A failure is an unacceptable behaviour exhibited by a system — The frequency of failures measures software.
Eiffel Language and Design by Contract Contract –An agreement between the client and the supplier Characteristics –Expects some benefits and is prepared.
Computer Science 340 Software Design & Testing Design By Contract.
Ranga Rodrigo. Class is central to object oriented programming.
CMSC 202 Interfaces. 11/20102 Classes and Methods When a class defines its methods as public, it describes how the class user interacts with the method.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 18 Exception Handling.
CISC6795: Spring Object-Oriented Programming: Polymorphism.
Software Engineering Prof. Dr. Bertrand Meyer March 2007 – June 2007 Chair of Software Engineering Static program checking and verification Slides: Based.
Design by Contract in Java Concept and Comparison.
Netprog: Java Intro1 Crash Course in Java. Netprog: Java Intro2 Why Java? Network Programming in Java is very different than in C/C++ –much more language.
Extended Static Checking for Java  ESC/Java finds common errors in Java programs: null dereferences, array index bounds errors, type cast errors, race.
Today’s Agenda  Quick Review  Continue on JML Formal Methods in Software Engineering1.
CIS 771: Software Specifications Lecture 18: Specifying and Checking Partial Properties of Java Code Copyright , Matt Dwyer, John Hatcliff, and.
C# Classes and Inheritance CNS 3260 C#.NET Software Development.
Programming with Assertions © Allan C. Milne v
Pre- and postconditions, Using assertions and exceptions 1 Pre- and postconditions Using assertions and exceptions.
Spec# Andreas Vida. Motivation Correct and maintainable software Correct and maintainable software Cost effective software production Cost effective software.
Verificare şi Validarea Sistemelor Soft Tem ă Laborator 1 ESC/Java2 Extended Static Checker for Java Dat ă primire laborator: Lab 1 Dat ă predare laborator:
Exceptions and Assertions Chapter 15 – CSCI 1302.
SWE 4743 Abstract Data Types Richard Gesick. SWE Abstract Data Types Object-oriented design is based on the theory of abstract data types Domain.
PROGRAMMING PRE- AND POSTCONDITIONS, INVARIANTS AND METHOD CONTRACTS B MODULE 2: SOFTWARE SYSTEMS 13 NOVEMBER 2013.
Institute for Applied Information Processing and Communications (IAIK) – Secure & Correct Systems 1 Static Checking  note for.
1 Exceptions. 2 Syntax Errors, Runtime Errors, and Logic Errors syntax errors, runtime errors, and logic errors You learned that there are three categories.
Extended Static Checking for Java Cormac Flanagan Joint work with: Rustan Leino, Mark Lillibridge, Greg Nelson, Jim Saxe, and Raymie Stata Compaq Systems.
DBC NOTES. Design By Contract l A contract carries mutual obligations and benefits. l The client should only call a routine when the routine’s pre-condition.
David Evans CS201j: Engineering Software University of Virginia Computer Science Lecture 10: Programming Exceptionally.
Preventing bugs with pluggable type-checking Michael Ernst MIT
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.
Agenda Introduction Errors and Exception Exception Hierarchy Classification of Exceptions Built in Exceptions Exception Handling in Java User defined.
CSE 374 Programming Concepts & Tools Hal Perkins Fall 2015 Lecture 17 – Specifications, error checking & assert.
Chapter 6 CS 3370 – C++ Functions.
CSE 374 Programming Concepts & Tools
Testing and Debugging.
Accessible Formal Methods A Study of the Java Modeling Language
CSE 143 Error Handling [Section 2.8] 3/30/98 CSE 143.
CSC 143 Error Handling Kinds of errors: invalid input vs programming bugs How to handle: Bugs: use assert to trap during testing Bad data: should never.
Hoare-style program verification
Managing Errors and Exceptions
Go to pollev.com/cse143.
Java Modeling Language (JML)
Assertions References: internet notes; Bertrand Meyer, Object-Oriented Software Construction; 4/25/2019.
Computer Science 340 Software Design & Testing
Programming Languages 2nd edition Tucker and Noonan
Presentation transcript:

Rigorous Software Development CSCI-GA Instructor: Thomas Wies Spring 2012 Lecture 8

Run-time vs. Static Checking Runtime Assertion Checking (RAC) – finds errors at run time, – tests for violation during execution, – can check most of the JML, – depends on appropriate test cases. (Extended) Static Checking (ESC) – finds errors at compile time, – proves that there is no violation – higher degree of confidence, – can check only parts of the JML, – does not require test cases.

ESC/Java 2 Developed by the DEC Software Research Center (now HP Research) Extended by David Cok and Joe Kiniry (Kind Software) Proves correctness of Java code wrt. JML specifications Is not sound: may approve an incorrect program Is not complete: may complain about a correct program Is useful to find many errors Works with Java 1.5 Homepage: Download link: ESCJava2.0.5

Importance of Specifications ESC/Java checks that each method behaves correctly in all calling contexts admitted by the specification. Programmer needs to provide method contracts and invariants to reduce the number of alarms produced by the tool. The tool emits three types of alarms: – error: program is not well-formed (syntax/type error) – warning: a likely error that might disappear when the user provides a stronger specification – caution: a property has not been checked

Absence of Runtime Exceptions ESC checks that no undeclared runtime exceptions occur. NullPointerException ClassCastException ArrayIndexOutOfBoundsException ArrayStoreException ArithmeticException NegativeArraySizeException other run-time exception, e.g., when calling library functions.

ESC/Java and JML Specifications ESC/Java also checks JML specifications: – ensures clauses at end of a called method, – requires clauses before call of a method, – assert statements, – signals clauses, – invariants (loop invariants and class invariants). ESC/Java assumes that some specifications hold: – requires clauses at entry of a method, – ensures clauses after return of a called method, – assume statements, – invariants (loop invariants and class invariants).

ESC/Java 2 Demo Consider the following code : Object[] a; void m(int i) { a[i] = "Hello"; } Is a a null pointer? ( NullPointerException ) Is i non-negative? ( ArrayIndexOutOfBoundsException ) Is i smaller than the array length? ( ArrayIndexOutOfBoundsException ) Is a an array of Object or String ? ( ArrayStoreException ) ESC/Java warns about these issues.

NullPointerException public void put(Object o) { int hash = o.hashCode();... } ESC/Java reports: Possible null dereference Solutions: Declare o as non_null. Add o != null to precondition. Add throws NullPointerException or add signals (NullPointerException) o == null. Add Java code that handles null pointers: int hash = (o == null ? 0 : o.hashCode());

ClassCastException class Priority implements Comparable { public int compareTo(Object other) { Priority o = (Priority) other;... } ESC/Java reports: Possible type cast error. Solutions: Add throws ClassCastException or add signals (ClassCastException) !(other instanceof Priority)) Add Java code that handles differently typed objects: if (!(other instanceof Priority)) return -other.compareTo(this) Priority o =...

ArrayIndexOutOfBoundsException void byte[] what, int offset, int len) { for (int i = 0; i < len; i++) { write(what[offset + i]); } ESC/Java reports: Possible negative array index Solution: Add offset >= 0 to pre-condition. This results in Array index possibly too large. Add offset + len <= what.length. ESC/Java does not complain but there is still a problem. If offset and len are very large numbers, then offset + len can be negative. The code would throw an A rrayIndexOutOfBoundsException at runtime. The correct pre-condition is: requires offset >= 0 && offset + len >= offset offset + len <=

ArrayStoreException public class Stack { Object[] elems; int top; invariant 0 <= top && top <= requires top < void add(Object o) { elems[top++] = o; } ESC/Java reports: Type of right-hand side possibly not a subtype of array element type (ArrayStore). Solutions: Add an invariant \typeof(elems) == \type(Object[]). Add a precondition \typeof(o) <: \elemtype(\typeof(elems)).

Java Types and JML \typeof gets the runtime type of an expression: \typeof(obj)  » obj.getClass() \elemtype gets the base type of an array type: \elemtype(t1) » t1.getComponentType() \type gets the type representing the given Java type: \type(Foo) » Foo.class <: means is sub-type of: t1 <: t2 » t2.isAssignableFrom(t1)

ArithmeticException class HashTable { Bucket[] buckets; void Object key, Object val) { int hash = key.hashCode() % buckets.length;... } ESC/Java reports: Possible division by zero Solution: Add class invariant buckets.length > 0. Run ESC/Java again to check that this invariant holds. It probably warns about a Possible negative array index.

Exceptions in Library Functions class Bag { Object[] elems; void sort() { java.util.Arrays.sort(elems); } ESC/Java reports: Possible unexpected exception Look in escjava/specs/java/util/Arrays.refines-spec! Array.sort() has pre-condition: elems[i] instanceof Comparable for all i. Solution: Add similar condition as class invariant.

Modular Checking ESC/Java checks each method in each class in isolation. Each method body is transformed into straight-line code with inlined specs, but with all method calls and loops eliminated. Straight-line code is then transformed into logical formulas that are given to an automated theorem prover.

assume and assert The basic specifications in ESC/Java are assume and assert. assume this.next != this.next.prev = this; assert this.next.prev == ESC/Java proves that if the assume statement holds in the pre-state, the assert statement holds in the post- state. Such a triple of specification and code is called Hoare triple.

Checking for Runtime Errors To check for runtime errors ESC/Java automatically inserts appropriate assert statements: a[x] = "Hello"; becomes assert a != null && x >= 0 && x < a.length \typeof("Hello") <: a[x] = "Hello";

Inlining requires and ensures The method specification is just translated into assume and assert: requires n > ensures \result == (int) int m() { body return x; } becomes: assume n > body assert x == (int)

Eliminating Method Calls And if method m is called, the roles of assume and assert are interchanged:... y = m(x);... becomes:... assert x > y = m_x; // m_x is a “fresh” variable assume y == (int)

Handling Loops int a[] = new int[6]; for (int i = 0; i <= 6; i++) { a[i] = i; } > escj -q Loop.java 0 warnings > escj -Loop 7 -q Loop.java Loop.java:5: Warning: Array index possibly too large (IndexTooBig) a[i] = i; ^ 1 warning > escj -LoopSafe -q Loop.java Loop.java:5: Warning: Array index possibly too large (IndexTooBig) a[i] = i; ^ 1 warning

Adding Loop Invariants int a[] = new int[6]; for (int i = 0; i < 6; i++) a[i] = i; > escj -LoopSafe -q Loop.java Loop.java:6: Warning: Possible negative array index (IndexNegative) a[i] = i; ^ 1 warning

Adding Loop Invariants public void m() { int a[] = new int[6]; maintaining i >= 0; for (int i = 0; i < 6; i++) a[i] = i; } > escj -LoopSafe -q Loop.java 0 warnings

Caution with assume Never assume something that is not true, otherwise ESC/Java will be able to prove everything: Object o = null; assume o != Object[] a = new String[-5]; a[-3] = new Integer(2); > escj -q BadAssume.java 0 warnings

ESC/Java is not complete ESC/Java can only do limited reasoning: requires i == 5 && j== ensures \result == int m(int i, int j) { return i*j; } Incomplete.java:7: Warning: Postcondition possibly not established (Post) } ^ Associated declaration is “Incomplete.java", line 3, col ensures \result == 15; Adding a good assumption can help eliminate such warnings, e.g. int m(int i, int j) { assume 15 == 5 * return i*j; } But this is dangerous since assume statements are not checked.

Class Invariants Class invariants are properties that must hold at the entry and exit point of every method They often express properties about the consistency of the internal representation of an object. They are typically transparent to clients of an object. They are sometimes also called object invariants or instance invariants.

The Problem with Class Invariants There are some problems with class invariants: Ownership: invariants can depend on fields of other objects. – For example, the invariant of List accesses Node fields. Callback: invariants can be temporarily violated. – While the invariant is violated, we call a different method that calls back to the same object. Atomicity: invariants can be temporarily violated. – While the invariant is violated, another thread accesses object.

The Problem with Class Invariants ESC/Java checks the highlighted assumes and asserts. This is unsound! public class SomeClass { invariant requires ensures public void doSomething() { assume(P); assume(inv);...code of doSomething... assert(Q); assert(inv); } public class OtherClass { public void caller(SomeClass o) {...some other code... assert(P); o.doSomething(); assume(Q); }

Invariants May Depend on Other Objects Consider a doubly linked list: class Node { Node prev, next; invariant this.prev.next == this && this.next.prev == } class List { private Node first; public void add() { Node newnode = new Node(); newnode.prev = first.prev; newnode.next = first; first.prev.next = newnode; first.prev = newnode; } The invariant of this depends on the fields of this.next and this.prev. Moreover the List.add function changes the fields of the invariants of Node.

List Example First observation: the invariant should be put into the List class: class Node { Node prev, next; } class List { private Node first; private ghost JMLObjectSet invariant (\forall Node n; nodes.has(n); n.prev.next == n && n.next.prev == public void add() { Node newnode = new Node(); newnode.prev = first.prev; newnode.next = first; first.prev.next = newnode; first.prev = newnode; set nodes = nodes.insert(newnode); }

List Example Second observation: Node objects much not be shared between to different lists. class Node { ghost Object Node prev, next; } class List { private Node first; private ghost JMLObjectSet invariant (\forall Node n; nodes.has(n); n.prev.next == n && n.next.prev == n && n.owner == public void add() { Node newnode = new Node(); set newnode.owner = this; newnode.prev = first.prev; newnode.next = first; first.prev.next = newnode; first.prev = newnode; set nodes = nodes.insert(newnode); }

List Example Third observation: One may only change the owned fields. class Node { ghost Object Node prev, next; } class List { private Node first; private ghost JMLObjectSet invariant (\forall Node n; nodes.has(n); n.prev.next == n && n.next.prev == n && n.owner == public void add() { Node newnode = new Node(); set newnode.owner = this; newnode.prev = first.prev; newnode.next = first; assert(first.prev.owner == this) first.prev.next = newnode; assert(first.owner == this) first.prev = newnode; set nodes = nodes.insert(newnode); }

The Owner-As-Modifier Property JML supports a type system for checking the owner-as-modifier property, when invoked as jmlc --universes. The underlying type system is called Universes: The class Object has a ghost field owner. Fields can be declared as rep, peer, readonly. – rep Object x adds an implicit invariant (or requires) x.owner = this. – peer Object x adds an implicit invariant (or requires) x.owner = this.owner. – readonly Object x does not restrict owner, but does not allow modifications of x. The new operation supports rep and peer : – new sets owner field of new node to this. – new sets owner field of new node to this.owner.

List with Universes Type System class Node { Node prev, next; } class List { private Node first; private ghost JMLObjectSet invariant (\forall Node n; nodes.has(n); n.prev.next == n && n.next.prev == n && n.owner == public void add() { Node newnode = new Node(); newnode.prev = first.prev; newnode.next = first; first.prev.next = newnode; first.prev = newnode; set nodes = nodes.insert(newnode); }

The Universes Type System A simple type system can check most issues related to ownership: rep T can be assigned without cast to rep T and readonly T. peer T can be assigned without cast to peer T and readonly T. readonly T can be assigned without cast to readonly T.

The Universes Type System One needs to distinguish between the type of a field peer Node prev and the type of a field expression rep Node first.prev. If obj is a peer type and fld is a peer T field then obj.fld has type peer T. If obj is a rep type and fld is a peer T field then obj.fld has type rep T. If obj = this and fld is a rep T field then this.fld has type rep T. In all other cases obj.fld has type readonly T.

readonly References To prevent changing readonly references, the following restrictions apply: If obj has type readonly T, then – obj.fld = expr is illegal. – obj.method(...) is only allowed if method is a pure method. It is allowed to cast readonly T references to rep T or peer T : – (rep T) expr asserts that expr.owner == this. – (peer T) expr asserts that expr.owner == this.owner.

Modification only by Owner All write accesses to a field of an object obj are in a method of the owner of obj or in a method of an object having the same owner as the object that was invoked (directly or indirectly) by the owner of obj. Invariants that only depend on fields of owned objects can only be invalidated by the owner or methods that the owner invokes.