JML. Outline. Métodos Formais em Engenharia de Software. MI, Braga these slides were prepared by adopting/adapting teaching material

Similar documents
Design by Contract and JML: concepts and tools

Introduction to JML David Cok, Joe Kiniry, and Erik Poll Eastman Kodak Company, University College Dublin, and Radboud University Nijmegen

JML tool-supported specification for Java Erik Poll Radboud University Nijmegen

JML. Java Modeling Language

Java Modelling Language (JML) References

Programming with Contracts. Juan Pablo Galeotti, Alessandra Gorla Saarland University, Germany

Formal Methods for Java

Program Verification (6EC version only)

Assertions & Design-by-Contract using JML Erik Poll University of Nijmegen

Advances in Programming Languages

ESC/Java extended static checking for Java Erik Poll, Joe Kiniry, David Cok University of Nijmegen; Eastman Kodak Company

ESC/Java2 extended static checking for Java Erik Poll Radboud University Nijmegen

Formale Entwicklung objektorientierter Software

The JML Tool. Faculty of Engineering Pontificia Universidad Javeriana. The JML Tool p.1/23

Formal Specification and Verification

ESC/Java2 Use and Features

ESC/Java2 Use and Features

Advances in Programming Languages

Overview The Java Modeling Language (Part 1) Related Work

ESC/Java2 Use and Features David Cok, Joe Kiniry, Erik Poll Eastman Kodak Company, University College Dublin, and Radboud University Nijmegen

The Java Modeling Language JML

Advanced JML. and more tips and pitfalls. David Cok, Joe Kiniry, and Erik Poll

Static program checking and verification

ESC/Java2 Warnings David Cok, Joe Kiniry, and Erik Poll Eastman Kodak Company, University College Dublin, and Radboud University Nijmegen

The Java Modeling Language (Part 1)

The Use of JML in Embedded Real-Time Systems

OOP Design by Contract. Carsten Schuermann Kasper Østerbye IT University Copenhagen

Specification tips and pitfalls

Lecture 10 Design by Contract

Advances in Programming Languages

Design by Contract with JML

Verification Condition Generation

Introduction to the Java Modeling Language

Java Modeling Language (JML)

Integrating verification in programming languages

Java Modelling Language (JML) References

Advances in Programming Languages

Verifying JML specifications with model fields

UC Santa Barbara. CS189A - Capstone. Christopher Kruegel Department of Computer Science UC Santa Barbara

Verifying Java Programs. Verifying Java Programs. The Krakatoa/Why Tool Suite

Verifying Java Programs

Formal Specification and Verification

Canica: An IDE for the Java Modeling Language

Checking Program Properties with ESC/Java

An overview of JML tools and applications

JML s Rich, Inherited Specifications for Behavioral Subtypes

Analysis of Software Artifacts

Advanced JML Erik Poll Radboud University Nijmegen

III lecture: Contracts and Abstract Data Types. An obligation from last lecture... An obligation from last lecture... (2) January/February 2013

Testing, Debugging, and Verification

Assertions, pre/postconditions

Software Engineering Testing and Debugging Testing

An Aspect-Oriented Approach. Henrique Rebêlo Informatics Center

Formal Specification with the Java Modeling Language

Contents. Program 1. Java s Integral Types in PVS (p.4 of 37)

How the Design of JML Accommodates Both Runtime Assertion Checking and Formal Verification

EXAMINATIONS 2009 MID-TERM TEST. COMP 202 / SWEN 202 Formal Methods of Computer Science / Formal Foundations of Software Engineering WITH ANSWERS

Combining Design by Contract and Inference Rules of Programming Logic towards Software Reliability

Software Engineering

A Small Survey of Java Specification Languages *

Design by Contract: An Overview

JML and ESC/Java2 Homework Exercises

Homework #1, on the class web pages later today

A Fitness Function to Find Feasible Sequences of Method Calls for Evolutionary Testing of Object-Oriented Programs

Fundamentals of Software Engineering

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

ESC/Java 2. Checker for Java 2. Extended. Static. B y K ats man Andrey S oftware E ngineering S em inar

Verifying Java Programs Verifying Java Programs with KeY

Announcements. Specifications. Outline. Specifications. HW1 is due Thursday at 1:59:59 pm

Karsten Sohr Center for Computing Technologies Universität Bremen, Germany

Assertions. Assertions - Example

Specification and Verification of Garbage Collector by Java Modeling Language

The Java Modeling Language (Part 2)

Preliminary Design of JML:

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

A Library-Based Approach to Translating OCL Constraints to JML Assertions for Runtime Checking

Verifying Java Programs Verifying Java Programs with KeY

Ambientes de Desenvolvimento Avançados

Lecture Notes on Contracts

A runtime assertion checker for the Java Modeling Language

References: internet notes; Bertrand Meyer, Object-Oriented Software Construction; 10/14/2004 1

Specification of a transacted memory for smart cards in Java and JML

JML Class Specifications The Java Modeling Language (Part 2) A Java Class

Formal Methods for Java

Formal Methods for Software Development

Preliminary Design of JML:

Reasoning about programs

Last time. Reasoning about programs. Coming up. Project Final Presentations. This Thursday, Nov 30: 4 th in-class exercise

Verifying Java Programs with KeY

Testing Library Specifications by Verifying Conformance Tests

Formal Methods for Java

Self-checking software insert specifications about the intent of a system

Formal Methods for Java

Java: advanced object-oriented features

Asserting Expectations. Your Submissions. Oral Exams

Formal methods What are they? Uses Tools Application to software development

JML in Feature-Oriented Programming

Softwaretechnik. Lecture 08: Testing and Debugging Overview. Peter Thiemann SS University of Freiburg, Germany

a correct statement? You need to know what the statement is supposed to do.

Reasoning about Object Structures Using Ownership

Transcription:

Métodos Formais em Engenharia de Software JML José Carlos Bacelar Almeida Departamento de Informática Universidade do Minho MI, Braga 2008 Outline Design by Contract and JML Design by Contract Java Modeling Language Tool support jmlc/jmlrac ESC/Java2 Small Demo Hands on (exercises) these slides were prepared by adopting/adapting teaching material from the JML and ESC/Java2 sites. 1 2

Design by Contract (DBC) Design by Contract Introduced by Bertrand Meyer (Eiffel language) (influenced by VDM, Larch, ) As a way of: - Recording details of method responsibilities and assumptions; - Document intention (specification) of software components (object invariants; methods; etc.); - Avoiding constantly checking arguments; - Assigning blame across interfaces. Goals: - work out application design by writing contracts rather than code; - express design at multiple levels (UML, JML, ); - refine design by refining contracts; - write code once when architecture is stable. 3 4

Contract the Design (J. Kiniry) In practice, in many situations we need to address the opposite direction (e.g. software maintenance, certification, ) a body of code exists and must be annotated - the architecture is typically ill-specified; - the code is typically poorly documented; - the number and quality of unit tests is typically very poor; - the goal of annotation is typically unclear; Goals: - improve understanding of architecture with high-level specifications; - improve quality of subsystems with medium-level specifications; - realize and test against critical design constraints using specification-driven code and architecture evaluation; - evaluate system quality through rigorous testing or verification of key subsystems. Contracts in Software /*@ requires x >= 0.0; @ ensures Math.abs(\result*\result - x) < e; @*/ public static double sqrt(double x) { } Obligations Rights Client Passes non-negative number Gets square root approximation Implementor Computes and returns square root Assumes argument is non-negative 5 6

Pre and Postconditions A method s precondition says: - Implementor perspective: what is expected (assumed) from the environment (in particular, the method arguments); - Client perspective: what should be accomplished to use the method. A method s postcondition says: - Implementor perspective: what is intended with the method; - Client perspective: what is legitimate to assume from the method call. Advantages of DBC Contracts are: - more abstract than code; - not necessarily constructive (e.g. quantified over infinite types); - but often machine checkable (so can help with debugging and testing); - and contracts can always be up-to-date. A contract can be satisfied in many ways. E.g. for square root: - Linear search - Binary search - Newton s method - These will have varying non-functional properties - Efficiency - Memory usage So, a contract abstracts from all these implementation details. 7 8

More advantages of DBC Blame assignment. Who is to blame if: - Precondition doesn t hold? - Postcondition doesn t hold? Avoids inefficient defensive checks //@ requires a!=null && x!=null; //@ requires (* a is sorted *); public static int binarysearch(thing[] a, Thing x) { } Modularity of Reasoning source.close(); dest.close(); getfile().setlastmodified(loc.modtime().gettime()); In order to understand this code - read these methods contracts - instead of look at all the code JML 9 10

Java Modeling Language (JML) A formal specification language for Java (Gary T. Leavens et al.) - to specify behaviour of Java classes - to record design&implementation decisions by adding assertions to Java source code, eg. - preconditions - postconditiions - invariants JML syntax is well integrated with Java: - JML assertions are added as comments in.java file, between /*@ @*/, or after //@ ; - Properties are specified as Java boolean expressions, extended with some operators (\old, \forall, \result, ), - and some keywords (requires, ensures, signals, assignable, pure, invariant, non_null, ). Pre and Postconditions Pre and postconditions for methods are established through the requires and ensures clauses: /*@ requires amount >= 0; @ ensures balance == \old(balance)-amount; @ ensures \result == balance; @*/ public int debit(int amount) { } where - \old(balance) refers to the value of balance before the execution of the method; - the multiple ensures clauses are equivalent to the conjunction of their properties; - \result refers to the outcome of the method (return value). 11 12

JML properties JML properties are boolean Java expressions with the proviso that their evaluation is side-effect free (i.e. does not changes the state). A method without side-effects is called pure. Programmers might signal methods as pure: public /*@ pure @*/ int getbalance(){} Directory /*@ pure non_null @*/ getparent(){} The non_null clause signals that the result of getparent() can t be null (can also be used in arguments and instance variables). JML property language is extended with the binding operators: \forall, \exists, \sum, \product, \max, \min, E.g. (\forall int i ; 0<=i && i<n ; a[i]==null) 13 14

Invariants Invariants (aka class invariants) are properties that must be maintained by all methods. public class Wallet { public static final short MAX_BAL = 1000; private short /*@ spec_public @*/ balance; /*@ invariant 0 <= balance && balance <= MAX_BAL; @*/ } spec_public turns visibility of balance public for specification purposes. Invariants are implicitly included in all pre- and postconditions. Invariants must also be preserved if an exception is thrown! (they must hold whenever the control is outside object s methods) Invariants allow you to define: - acceptable states of an object (helps in understand the code), - and consistency of an object s state (valuable for testing/ debugging). Frame conditions Frame conditions (assignable clause) restrict possible side-effects of the methods (i.e. where the method is allowed to make changes) /*@ requires amount >= 0; assignable balance; ensures balance == \old(balance)-amount; @*/ public void debit(int amount) { balance = balance - amount; } They are a crucial ingredient when we are trying to reason about a program // let us assume that, at this point, name!=null; debit(50); // can we still be sure that name!=null??? Default assignable clause: assignable \everything. Pure method are implicitly assignable \nothing. 15 16

assert and assume clauses JML assert and assume clauses allow to attach a property to a given program location. int x; //@ assert x>=0; x = f(x); //@ assume x<0; The distinction is purely informative: - in an assert clauses, we take responsible for validating the property; - in assume, the property should follow from others guaranties (e.g. preconditions or methods postcontitions). In short, it specifies who should be blamed if the property does not hold. DBC and JML DBC can roughly be seen as an expantion of pre and postconditions as assert and assume clauses. //@ requires x >= 0.0; //@ ensures Math.abs(\result*\result - x) < e; public static double sqrt(double x) { } b = sqrt(a); Should be expanded into (performed by JML tools): public static double sqrt(double x) { //@ assume x>=0.0; //@ assert Math.abs(r*r - x) < e; return r; } //@ assert a>=0; b = sqrt(a); //@ assume Math.abs(b*b - a) < e; 17 18

Loop Invariants When reasoning about cycles, we need to annotate them with invariants (to establish what is their outcome) and variants (to establish their termination). int f = 1 ; int i = 1 ; /*@ loop_invariant i <= n && f == (\product int j ; 1 <= j && j <= i ; j ) ; decreases n-i; @*/ while ( i < n ) { i = i + 1 ; f = f i ; } A loop_invariant express a property that is valid when the control reaches the loop, and is preserved by it; The decreases clause expects an integer quantity (that decreses during the loop) --- the loop variant. JML tools 19 20

Web pages Unit tests jmldoc jmlunit JML Annotated Java } public class ArrayOps { private /*@ spec_public @*/ Object[] a; //@ public invariant 0 < a.length; /*@ requires 0 < arr.length; @ ensures this.a == arr; @*/ public void init(object[] arr) { this.a = arr; } ESC/Java2 Daikon Warnings Data trace file XVP Class file jmlc Bogor Model checking JACK, Jive, Krakatoa, KeY, LOOP Correctness proof Runtime Assertion Checking (jmlc/jmlrac) jmlrac compiler by Gary Leavens, Yoonsik Cheon, et al. (at Iowa State Univ.) translates JML assertions into runtime checks: during execution, all assertions are tested and any violation of an assertion produces an error. jmlrac even checks \forall if the domain of quantification is finite. jmlrac can generate complicated test-code for free. Usage: $ jmlc -Q -e Prog.java $ jmlrac Prog Very powerful when combined with unit testing - cheap & easy to do as part of existing testing practice - better testing and better feedback, because more properties are tested, at more places in the code 21 22

Extended Static Checking (ESC/Java2) ESC/Java was originally developed by Rustan Leino (DEC SRC), and extended by David Cok and Joe Kirini (Eastman Kodak Company, University College Dublin). Extended static checking = fully automated program verification, with some compromises to achieve full automation. It verifies the code at compile time: - generates proof-obligations from the annotated code; - uses an automated prover (Simplify) to check if generated conditions are provable. But, since it is intended to be run in a fully automated manner, has some shortcomings: - it is not complete - ESC/Java may warn of errors that are impossible; - it is not sound - ESC/Java may miss an error that is actually present. but finds lots of potential bugs quickly (good at proving absence of runtime exceptions and verifying relatively simple properties). Using ESC/Java2 ESC/Java2 can be used: - as a stand-alone tool; $ escjava2 Prog.java Prog: Prog() [0.033 s 17264696 bytes] passed [1.723 s 17264696 bytes total] 1 warning - as an eclipse plugin (real-time verification) Possible problems detected during analysis are always referred as warnings --- the programmer should judge their pertinence (real problem, lack of capability to derive the property, ) obs.: default loop treatment is very primitive (escjava unfolds its definition a small number of times). 23 24

Static Checking vs. Runtime Checking ESC/Java2 checks specs at compile-time, jmlrac checks specs at run-time. ESC/Java2 proves correctness of specs, jml only tests correctness of specs. Hence: - ESC/Java2 is independent of any test suite, results of runtime testing are only as good as the test suite; - ESC/Java2 provides higher degree of confidence. But, as soon as we depend on complex properties, ESC/Java2 is no longer able to deal with them. Jmlrac can (maybe with a greater perfomance penalty, but that is something admissible in a testing phase). Tool Download and Instalation Both tools are available for the major operating systems (macosx, linux, windows, ) JML toolset: - http://www.jml-specs.org (Download section) ESC/Java2 standalone tool: - http://kind.ucd.ie/products/opensource/escjava2/ ESC/Java2 Eclipse plugin (eclipse update site): - http://kind.ucd.ie/products/opensource/escjava2/escjava-eclipse/updates 25 26

Demo 27