Académique Documents
Professionnel Documents
Culture Documents
Erik Poll
Digital Security Radboud University Nijmegen
JML
formal specification language for sequential Java by Gary Leavens et. al. to specify behaviour of Java classes & interfaces to record detailed design decisions by adding annotations to Java source code in Design-ByContract style, using eg. pre/postconditions and invariants
JML example
public class ePurse{ private int balance; //@ invariant 0 <= balance && balance < 500; //@ requires amount >= 0; //@ ensures balance <= \old(balance); public debit(int amount) { if (amount > balance) { throw (new BankException("No way"));} balance = balance amount; }
or high(er) level
//@ invariant child.parent == this;
Erik Poll, JML introduction - CHARTER meeting - 6
non_null
Lots of invariants and preconditions are about references not being null, eg int[] a; //@ invariant a != null; Therefore there is a shorthand /*@ non_null @*/ int[] a; But, as most references are non-null, JML adopted this as default. So only nullable fields, arguments and return types need to be annotated, eg /*@ nullable @*/ int[] b; JML will move to adopting JSR308 Java tags for this @Nullable int[] b;
Erik Poll, JML introduction - CHARTER meeting - 11
pure
Methods without side-effects that are guaranteed to terminate can be declared as pure /*@ pure @*/ int getBalance (){ return balance; }; Pure methods can be used in JML annotations //@ requires amount < getBalance(); public debit (int amount)
NB this does not follow from the postcondition Assignable clauses are needed for modular verification
Fields can be grouped in Datagroups, so that spec does not have to list concrete fields
Erik Poll, JML introduction - CHARTER meeting - 13
resource usage
Syntax for specifying resource usage /*@ measured_by len; // max recursion depth @ working_space (len*4); // max heap space used @ duration len*24; // max execution time @ ensures \fresh(\result); // freshly allocated @*/ public List(int len) {... }
model state
interface Connection{ //@ model boolean opened; // spec-only field //@ ensures !opened; public Connection(...); //@ requires !opened; //@ ensures opened; public void open (); //@ requires opened; //@ ensures !opened; public void close ();
Erik Poll, JML introduction - CHARTER meeting - 15
pointer trouble
References are the main source of trouble, also in verification Universes are a type system to control aliasing
class //@ /*@ /*@ } A { invariant invA; rep @*/ C c1, c2; rep @*/ B b; a.c1 a.c2
a a.b a.b.d
testing vs verification
verification gives complete coverage all paths, all possible inputs if testing fails, you get a counterexample (trace); if verification fails, you typically don't.... verification can be done before code is complete verification requires many more specs as verification is done on a per method basis incl API specs
Erik Poll, JML introduction - CHARTER meeting - 20
related work
OCL for UML pro: not tied to a specific programming language con: idem less expressive, and semantics less clear Spec# for C# by Rustan Leino & co at Microsoft Research SparkAda for Ada by Praxis High Integrity System Commercially used
Erik Poll, JML introduction - CHARTER meeting - 21
some ideas...
Coping with concurrency Track thread-ownership of objects marking objects are thread-local or shared, to make guarantees about memory-separation between threads. Largely supported by type system Traceability could maybe be supported by naming JML annotations //@ invariant propertyXyz: .... ;
questions?
Copies an array from the specified source array, beginning at the specified position, to the specified position of the destination array.
We don't have to write \old(len) and \old(dest)[\old(dstPos)+1] in the postcondition, because all parameters are implicily \old() in JML postconditions
Erik Poll, JML introduction - CHARTER meeting - 29
can be omitted
Can debit throw a BankException, if precondition holds? YES Can debit throw a NullPointerException, if the precondition holds? NO. Unlike Java, JML only allows method to throw unchecked
is shorthand for
Ruling out exceptions also helps with certified analyses for PCC, as it rules out many execution paths
Erik Poll, JML introduction - CHARTER meeting - 33
Ruling out exceptions, esp. RuntimeExceptions, as much as possible is the natural thing to do and a good bottom line specification
Erik Poll, JML introduction - CHARTER meeting - 37
pure
Methods without side-effects that are guaranteed to terminate can be declared as pure /*@ pure @*/ int getBalance (){ return balance; }; Pure methods can be used in JML annotations //@ requires amount < getBalance(); public debit(int amount)
assignable
The default assignable clause is //@ assignable \everything;
Pure methods are //@ assignable \nothing; Pure constructors are //@ assignable this.*;
Well-typed OO languages already ensure this in a weak form, as soundness of subtyping: "substituting a subclass object for a parent object will not result in 'Method not found' errors at runtime"
behavioural subtyping
Two ways to achieve behavioural subtyping 1. For any method spec in a subclass, prove that it is implies the spec for that method in the parent class ie prove that the precondition is weaker ! and the postcondition is stronger 1. Implicitly conjoin method spec in a subclass with method specs in the parent class called specification inheritance, which is what JML uses this guarantees that resulting precondition is weaker, and the resulting postcondition is stronger
JML invariants
The more general problem: how to cope with invariants that involve multiple (or aggregate) objects still an active research area... one solution is to use some notion of object ownership
Erik Poll, JML introduction - CHARTER meeting - 51
invariants should only depend on owned state an object's invariant may be broken when it invokes methods on sub-objects
Erik Poll, JML introduction - CHARTER meeting - 53