Introduction to JML the Java Modeling Language
Total Page:16
File Type:pdf, Size:1020Kb
Goal: JML should be easy to use for any Java programmer. Outline of this tutorial First • introduction to JML Introduction to JML • overview of tool support for JML, esp. runtime assertion checking (using jmlrac) and extended static David Cok, Joe Kiniry, and Erik Poll checking ESC/Java2 Then Eastman Kodak Company, University College Dublin, • ESC/Java2: Use and Features and Radboud University Nijmegen • ESC/Java2: Warnings • Specification tips and pitfalls • Advanced JML: more tips and pitfalls interspersed with demos. David Cok, Joe Kiniry & Erik Poll - ESC/Java2 & JML Tutorial – p.1/30 David Cok, Joe Kiniry & Erik Poll - ESC/Java2 & JML Tutorial – p.2/30 JML by Gary Leavens et al. Formal specification language for Java • to specify behaviour of Java classes • to record design &implementation decisions The Java Modeling Language by adding assertions to Java source code, eg • preconditions JML • postconditions • invariants www.jmlspecs.org as in Eiffel (Design by Contract), but more expressive. David Cok, Joe Kiniry & Erik Poll - ESC/Java2 & JML Tutorial – p.3/30 David Cok, Joe Kiniry & Erik Poll - ESC/Java2 & JML Tutorial – p.4/30 JML by Gary Leavens et al. JML Formal specification language for Java To make JML easy to use: to specify behaviour of Java classes • • JML assertions are added as comments in .java file, • to record design &implementation decisions between /*@ . @*/, or after //@, by adding assertions to Java source code, eg • Properties are specified as Java boolean expressions, extended with a few operators (nold, nforall, nresult, • preconditions . ). • postconditions • using a few keywords (requires, ensures, • invariants signals, assignable, pure, invariant, non null, . ) as in Eiffel (Design by Contract), but more expressive. Goal: JML should be easy to use for any Java programmer. David Cok, Joe Kiniry & Erik Poll - ESC/Java2 & JML Tutorial – p.4/30 David Cok, Joe Kiniry & Erik Poll - ESC/Java2 & JML Tutorial – p.5/30 requires, ensures requires, ensures Pre- and post-conditions for method can be specified. JML specs can be as strong or as weak as you want. /*@ requires amount >= 0; /*@ requires amount >= 0; ensures balance == nold(balance-amount) && ensures true; nresult == balance; @*/ @*/ public int debit(int amount) f public int debit(int amount) f ... ... g g This default postcondition “ensures true” can be Here nold(balance) refers to the value of balance omitted. before execution of the method. David Cok, Joe Kiniry & Erik Poll - ESC/Java2 & JML Tutorial – p.6/30 David Cok, Joe Kiniry & Erik Poll - ESC/Java2 & JML Tutorial – p.7/30 Design-by-Contract signals Pre- and postconditions define a contract between a class Exceptional postconditions can also be specified. and its clients: • Client must ensure precondition and may assume /*@ requires amount >= 0; postcondition ensures true; • Method may assume precondition and must ensure signals (BankException e) postcondition amount > balance && balance == nold(balance) && Eg, in the example specs for debit, it is the obligation of the client to ensure that amount is positive. The requires e.getReason().equals("Amount too big"); clause makes this explicit. @*/ public int debit(int amount) throws BankException f ... g David Cok, Joe Kiniry & Erik Poll - ESC/Java2 & JML Tutorial – p.8/30 David Cok, Joe Kiniry & Erik Poll - ESC/Java2 & JML Tutorial – p.9/30 signals invariant Exceptions mentioned in throws clause are allowed by Invariants (aka class invariants) are properties that must be default. To change this, there are three options: maintained by all methods, e.g., • To rule out all exceptions, use a normal_behavior public class Wallet f public static final short MAX_BAL = 1000; /*@ normal behavior private short balance; requires ... /*@ invariant 0 <= balance && ensures ... balance <= MAX_BAL; @*/ @*/ • To rule out particular exception E, add ... Invariants are implicitly included in all pre- and signals (E) false; postconditions. • To allow only some exceptions, add Invariants must also be preserved if exception is thrown! signals_only E1, ..., E2; David Cok, Joe Kiniry & Erik Poll - ESC/Java2 & JML Tutorial – p.10/30 David Cok, Joe Kiniry & Erik Poll - ESC/Java2 & JML Tutorial – p.11/30 invariant non_null Invariants document design decisions, e.g., Many invariants, pre- and postconditions are about references not being null. non_null is a convenient public class Directory f short-hand for these. private File[] files; public class Directory f /*@ invariant files != null private /*@ non null @*/ File[] files; && (nforall int i; 0 <= i && i < files.length; void createSubdir(/*@ non null @*/ String name)f ; files[i] != null && ... files[i].getParent() == this); @*/ /*@ non null @*/ Directory getParent()f ... Making them explicit helps in understanding the code. David Cok, Joe Kiniry & Erik Poll - ESC/Java2 & JML Tutorial – p.12/30 David Cok, Joe Kiniry & Erik Poll - ESC/Java2 & JML Tutorial – p.13/30 assert assert An assert clause specifies a property that should hold at JML keyword assert now also in Java (since Java 1.4). some point in the code, e.g., Still, assert in JML is more expressive, for example in if (i <= 0 || j < 0) f ... ... for (n = 0; n < a.length; n++) else if (j < 5) g f if (a[n]==null) break; //@ assert i > 0 && 0 < j && j < 5; /*@ assert (nforall int i; 0 <= i && i < n; ... a[i] != null); g else f @*/ //@ assert i > 0 && j > 5; ... g David Cok, Joe Kiniry & Erik Poll - ESC/Java2 & JML Tutorial – p.14/30 David Cok, Joe Kiniry & Erik Poll - ESC/Java2 & JML Tutorial – p.15/30 assignable pure Frame properties limit possible side-effects of methods. A method without side-effects is called pure. /*@ requires amount >= 0; assignable balance; public /*@ pure @*/ int getBalance()f... ensures balance == nold(balance)-amount; @*/ Directory /*@ pure non null @*/ getParent()f...g public int debit(int amount) f g ... Pure method are implicitly assignable nnothing. E.g., debit can only assign to the field balance. Pure methods, and only pure methods, can be used in NB this does not follow from the post-condition. specifications, eg. Default assignable clause: assignable neverything. //@ invariant 0<=getBalance() && getBalance()<=MAX_BALANCE; David Cok, Joe Kiniry & Erik Poll - ESC/Java2 & JML Tutorial – p.16/30 David Cok, Joe Kiniry & Erik Poll - ESC/Java2 & JML Tutorial – p.17/30 JML recap The JML keywords discussed so far: • requires • ensures • signals • assignable Tools for JML • normal behavior • invariant • non null • pure • nold, nforall, nexists, nresult This is all you need to know to get started! David Cok, Joe Kiniry & Erik Poll - ESC/Java2 & JML Tutorial – p.18/30 David Cok, Joe Kiniry & Erik Poll - ESC/Java2 & JML Tutorial – p.19/30 • runtime assertion checking: test for violations of assertions during execution jmlrac • extended static checking ie. automated program • extended static checking ie. automated program verification: verification: prove that contracts are never violated at compile-time prove that contracts are never violated at compile-time ESC/Java2 ESC/Java2 This is program verification, not just testing. This is program verification, not just 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 Eg, “Invariant violated in line 8000” after 1 minute instead of “NullPointerException in line 2000” after 4 minutes Of course, an assertion violation can be an error in code or an error in specification. The jmlunit tool combines jmlrac and unit testing. tools for JML tools for JML • parsing and typechecking • parsing and typechecking • runtime assertion checking: test for violations of assertions during execution jmlrac David Cok, Joe Kiniry & Erik Poll - ESC/Java2 & JML Tutorial – p.20/30 David Cok, Joe Kiniry & Erik Poll - ESC/Java2 & JML Tutorial – p.20/30 tools for JML runtime assertion checking jmlrac compiler by Gary Leavens, Yoonsik Cheon, et al. at • parsing and typechecking Iowa State Univ. • runtime assertion checking: • translates JML assertions into runtime checks: test for violations of assertions during execution during execution, all assertions are tested and jmlrac any violation of an assertion produces an • extended static checking ie. automated program Error. verification: prove that contracts are never violated at compile-time ESC/Java2 This is program verification, not just testing. David Cok, Joe Kiniry & Erik Poll - ESC/Java2 & JML Tutorial – p.20/30 David Cok, Joe Kiniry & Erik Poll - ESC/Java2 & JML Tutorial – p.21/30 Eg, “Invariant violated in line 8000” after 1 minute instead of “NullPointerException in line 2000” after 4 minutes Of course, an assertion violation can be an error in code or Of course, an assertion violation can be an error in code or an error in specification. an error in specification. The jmlunit tool combines jmlrac and unit testing. The jmlunit tool combines jmlrac and unit testing. The jmlunit tool combines jmlrac and unit testing. runtime assertion checking runtime assertion checking jmlrac compiler by Gary Leavens, Yoonsik Cheon, et al. at jmlrac compiler by Gary Leavens, Yoonsik Cheon, et al. at Iowa State Univ. Iowa State Univ. • translates JML assertions into runtime checks: • translates JML assertions into runtime checks: during execution, all assertions are tested and during execution, all assertions are tested and any violation of an assertion produces an any violation of an assertion produces an Error. Error. • cheap & easy to do as part of existing testing practice • cheap & easy to do as part of existing testing practice • better testing and better feedback, because more • better testing and better feedback, because more properties are tested, at more places in the code properties are tested, at more places in the code Eg, “Invariant violated in line 8000” after 1 minute instead of “NullPointerException in line 2000” after 4 minutes David Cok, Joe Kiniry & Erik Poll - ESC/Java2 & JML Tutorial – p.21/30 David Cok, Joe Kiniry & Erik Poll - ESC/Java2 & JML Tutorial – p.21/30 runtime assertion checking runtime assertion checking jmlrac compiler by Gary Leavens, Yoonsik Cheon, et al. at jmlrac compiler by Gary Leavens, Yoonsik Cheon, et al.