目录
This article is P12 of BUAA OO course, which integrates the work of us in JML section.
Firstly, we will introduce JML’s theory basement and tool chain. After that, we will show how we deploy and use the JML. Thirdly, we will analyze our project and its bugs. Finally, we will give a brief note of JML Syntax.
This section introduce JML’s theory basement and tool chain.
Design by contract (DBC) is a method for developing software 1. The principal idea behind DBC is that a class and its clients have a “contract” with each other. The client must guarantee certain conditions before calling a method de?ned by the class, and in return the class guarantees certain properties that will hold after the call. The use of such pre- and postconditions to specify software dates back to Hoare’s 1969 paper on formal veri?cation 2. What is novel about DBC is that it makes these contracts executable. The contracts are de?ned by program code in the programming language itself, and are translated into executable code by the compiler. Thus, any violation of the contract that occurs while the program is running can be detected immediately3
Behavioral interface speci?cation languages provide formal code-level annotations, such as preconditions, postconditions, invariants, and assertions that allow programmers to express the intended behavior of program modules. Such speci?cations are useful for precisely documenting program behavior, for guiding implementation, and for facilitating agreement between teams of programmers in modular development of software. When used in conjunction with automated analysis and program veri?cation tools, such speci?cations can support detection of common code vulnerabilities, capture of light-weight application-speci?c semantic properties, generation of test cases and test oracles, and full formal program veri?cation.4
The Java Modeling Language (JML) is a behavioral interface specification language that can be used to specify the behavior of Java modules.
JML combines the design by contract approach of Eiffel and the model-based specification approach of the Larch family of interface specification languages, with some elements of the refinement calculus.5
The command jml-launcher starts a graphical user interface that can launch other JML tools. These include the JML checker (jml), the runtime assertion checking compiler (jmlc), the documentation generator (jmldoc), the specification skeleton generator (jmlspec), and the unit testing file generation tool (jmlunit).
The launcher can also be run by opening (double clicking) the file bin/jml-release.jar that is part of the JML release. The jar file and the script both run the Java class org.jmlspecs.launcher.JmlLauncher.
The command jml and the graphical user interface version jml-gui both do type checking of Java Modeling Language (JML) specifications. JML is a behavioral interface specification language for Java(TM).
The jml script sets the CLASSPATH and then runs the Java class org.jmlspecs.checker.Main, which does both syntax and type checking of Java and JML files. That is it checks both Java source code and the JML annotations in source code and JML files.
The jml-gui script is similar, but just runs the Java class org.jmlspecs.checker.JmlGUI, which brings up a graphical user-interface to do the checking.
The command jmlc and the graphical user interface jmlc-gui compile Java(TM) files (and JML specification files) with their JML annotations turned into runtime checks. It thus acts as a Java compiler, but with the additional effect of enabling automatic checks of assertions when the code is executed. The output is a .class file, which can be treated in much the same way as the output of any java compiler.
The command jmldoc and the graphical user interface version jmldoc-gui generate HTML (web) pages from JML annotated Java(TM) and JML files. This is similar to Javadoc, but jmldoc understands JML specifications, and includes this information in the generated HTML pages. The program also type checks the JML specifications to some extent and combines annotations across refinement files. The output is a set of HTML files that can be browsed using a web browser.
OpenJML is a program verification tool for Java programs that allows you to check the specifications of programs annotated in the Java Modeling Language.10
OpenJML is capable of checking Java programs annotated with specifications in the Java Modeling Language and provides robust support for many of JML‘s features.
The goal of the OpenJML project is to implement a full tool for JML and current Java that is easy for practioners and students to use to specify and verify Java programs. In addition, the project seeks to advance the theory and experience of software verification in practical, industrially-relevant contexts.
OpenJML is not a theorem prover or model checker itself. OpenJML translates JML specifications into SMT-LIB format and passes the proof problems implied by the Java+JML program to backend SMT solvers. OpenJML supports major SMT solvers such as Z3, CVC4 and Yices. The success of the proofs will depend on the capability of the SMT solver (e.g., which logics it supports), the particular logical encoding of the code+specifications, and the complexity and style in which the code and specifications are written.
JML Editing extends the Java development tools (JDT) with support for JML to make writing JML specifications as convenient as writing Java source code.11
The AspectJML tool is able to specify and do runtime assertion checking for both Java and AspectJ programs. Ajmlc uses aspect-oriented compilation techniques to provide significantly improved runtime assertion violation messages, and also works with JavaME.12
The jml4c tool is a JML compiler built on the Eclipse Java compiler. It translates a significant subset of JML specifications into runtime checks.13
JML4c is a new JML compiler built upon the Eclipse JDT open-source platform; JML is a formal interface specification language for Java to document the behavior of Java classes and interfaces. It translates a significant subset of JML specifications into runtime checks.
Some of notable features of JML4c include
Using JML4c, therefore, one can now verify Java 5 classes annotated with JML specifications.
Sireum/Kiasan for Java is a JML contract-based automatic verification and test case generation tool-set for Java program units.14
JMLEclipse is a pre-alpha version of the JML tool-suite developed on top of Eclipse‘s JDT compiler infrastructure.15
JMLUnitNG is an automated unit test generation tool for JML-annotated Java code, including code using Java 1.5+ features such as generics, enumerated types, and enhanced for loops. Like the original JMLUnit, it uses JML assertions as test oracles. It improves upon the original JMLUnit by allowing easy customization of data to be used for each method parameter of a class under test, as well as by using Java reflection to automatically generate test data of non-primitive types.16
JMLOK is a tool that uses random tests to check Java code against JML specifications and suggest likely causes for non-conformance problems it finds.17
AutoJML is a tool for the generation of JML specifications from state diagrams in various formats (including UML diagrams) and from security protocol specifications.18
AutoJML is a JML specification generator. It generates specifications based on other, higher level, specification formalisms such as UML state diagrams, or security protocol specifications. The output is a combination of Java skeleton code and JML class and method specifications.
We deploy and use the SMT Solver to analyse our project.
To Do (Unsolvable problem occurred during deploy SMT Solver.)
Because the specification of out project seems to be not compatible to the JMLUnit and OpenJML, we take a simple Java program as an example.
package daniel.jml.example;
// Refine Statement
//@ refine "<FileDir>"
// Import Statement
//@ model import org.jmlspecs.models.<JMLModel>;
import ...
interface MyInterface
{
// Instance and Initially Statement
/*@ public model instance <JMLModel> jmlModel;
@ public initially <JMLExpression>;
@*/
// Invariant Statement
/*@ public instance invariant <JMLExpression>;
// Behavior Statement
/*@ public normal_behavior
@ requires <JMLExpression>;
@ assignable <Variable>[, <Variable>]*;
@ ensures <JMLExpression>;
@ also
@ public exceptional_behavior
@ requires <JMLExpression>;
@ signals_only <ExceptionClass>;
@*/
/*@ pure @*/ ReturnType method1(ArguType argu);
// Behavior Statement
/*@ public normal_behavior
@ requires <JMLExpression>;
@ assignable <Variable>[, <Variable>]*;
@ ensures <JMLExpression>;
@ also
@ public exceptional_behavior
@ requires <JMLExpression>;
@ signals_only <ExceptionClass>;
@*/
ReturnType method2(ArguType argu);
}
also
来连接本类中重写方法的规格与原有规格class Class implements Interface
{
/*@ also
@ public normal_behavior
@ ...
@*/
<ReturnType> method(<Argument>)
{
...
}
}
//@ <Statement>
@
开始到行末的部分将被识别为 JML/*@ <Statement> @*/
@
之间的部分将被识别为 JML/*@ <Statement>[
@ <Statement>]*
@*/
- 注释块中每行从`@`开始到行末的部分将被识别为 JML
//@ instance <Type> <Field>;
<Field>
of type <Type>
//@ instance <Statement>;
<Statement>
become effective in the instance space, a imaginative space<Statement>
is a JML Statement//@ instance <Type> <Field>;
//@ initially <JMLExpression>;
<JMLExpression>
initially//@ invariant <JMLExpression>;
<JMLExpression>
must be true
all the timepackage ...
import ...
/*@ <Module Specification>[
@ <Module Specification>]*
@*/
//@ refine "<RefinedFileDirectory>";
<RefinedFileDirectory>
<RefinedFileDirectory>
, by imposing additional constraints on <RefinedFileDirectory>
/*@ normal_behavior
@ requires <JMLExpression>;
@ assignable <Object>[, <Object>]*;
@ ensures <JMLExpression>;
@*/
/*@ exceptional_behavior
@ requires <JMLExpression>;
@ signals_only <ExceptionClass>;
/*@ <BehaviorStatement>
@ also
@ <BehaviorStatement>
@Override
/*@ also
@ normal_behavior
@ ...
@*/
<ReturnType> method(<Argument>)
{
...
}
//@ old <Type> <Variable> = ...;
//@ requires <JMLExpression>;
<JMLExpression>
<JMLExpression>
is satisfied by a call//@ assignable <Variable>[, <Variable>]*;
<Variable>
\nothing
//@ ensures <JMLExpression>;
<JMLExpression>
<JMLExpression>
//@ ensures_redundantly <JMLExpression>;
<JMLExpression>
//@ signals_only <ExceptionClass>;
<JMLExpression>
<ExceptionClass>
//@ import <FullClassName>;
model
statement
import ...
model
修饰符model
修饰符/*@ pure @*/ <ReturnType> <Method>(<Argument>)
{
...
}
- 要求方法是纯函数, 没有任何副作用
- 不对任何外部变量赋值
/*@ pure @*/ class <Class>
{
...
}
- 定义 Pure Type
<RetuenType> method(/*@ nullable @*/ <Type> <Parameter>...)
{
...
}
- 当不使用`nullable`修饰符时, JML 会自动为所有behavior添加参数值非`null`的前置条件
- JML 默认认为参数值不会为`null`
- JML 默认不考虑参数值为`null`的情况
- e.g.
/*@ public normal_behavior
@ requires ...
@ ...
@ also
@ public normal_behavior
@ requires ...
@ ...
@*/
<ReturnType> method(<Type> arg1, <Type> arg2)
{
...
}
equivalent to:
/*@ public normal_behavior
@ requires arg1 != null && arg2 != null;
@ requires ...
@ ...
@ also
@ public normal_behavior
@ requires arg1 != null && arg2 != null;
@ requires ...
@ ...
@*/
<ReturnType> method(<Type> arg1, <Type> arg2)
{
...
}
instance
statement, the Expression can access objects in instance space\forall <Type> <Variable>; <Range>; <Condition>
<Type>
type variable <Variable>
that satisfies first <JMLExpression>
, it satisfies the second <JMLExpression>
too, then return true
; otherwise, return false
.
<Type>
type variable <Variable>
, <JMLExpression> -> <JMLExpression>
is true, return true
; otherwise, return false
.\exists <Type> <Variable>; <JMLExpression>; <JMLExpression>
\exists <Type> <Variable>; <Range>; <Condition>
<Type>
type variable <Variable>
that satisfies first <JMLExpression>
, there exists a variable that satisfies the second <JMLExpression>
too<JMLExpression> <==> <JMLExpression>
==
for boolean: <BooleanValue1> == <BooleanValue2>
equivalent to <BooleanValue1> <==> <BooleanValue2>
\result
\old(<JMLExpression>)
<JMLExpression>
in pre-state of method\not_modified(<JMLExpression>)
org.jmlspecs.models
pure
modifier.jml
fileJMLType
interface (?)pure
pure
modifierorg.jmlspecs.models.JMLType
clone()
/*@ also
@ public normal_behavior
@ ensures \result instanceof JMLType
@ && ((JMLType) \result).equals(this);
@*/
public /*@ pure @*/ Object clone();
- `equals(Object o)`
org.jmlspecs.models
JMLType
objects..jml
fileBertrand Meyer. Applying “design by contract”. Computer, 25(10):40–51, October 1992.?
C. A. R. Hoare. An axiomatic basis for computer programming. Communications of the ACM, 12(10):576–583, October 1969.?
Gary T. Leavens, Yoonsik Cheon. Design by Contract with JML.?
Chalin, Patrice, et al. "Beyond assertions: Advanced specification and verification with JML and ESC/Java2." International Symposium on Formal Methods for Components and Objects. Springer, Berlin, Heidelberg, 2005.?
The Java Modeling Language (JML) Home Page, http://www.eecs.ucf.edu/~leavens/JML/index.shtml.?
JML-Launcher Doc, http://www.eecs.ucf.edu/~leavens/JML-release/docs/man/jml-launcher.html.?
JML Doc, http://www.eecs.ucf.edu/~leavens/JML-release/docs/man/jml.html.?
JMLC Doc, http://www.eecs.ucf.edu/~leavens/JML-release/docs/man/jmlc.html.?
JMLDoc Doc, http://www.eecs.ucf.edu/~leavens/JML-release/docs/man/jmldoc.html?
OpenJML Home Page, http://www.openjml.org/.?
JMLEditing Home Page, http://i12www.ira.uka.de/key/eclipse/JMLEditing/index.html.?
AspectJML Home Page, http://www.cin.ufpe.br/~hemr/aspectjml/.?
JML4C Home Page, http://www.cs.utep.edu/cheon/download/jml4c/index.php.?
Sireum Home Page, http://sireum.org/.?
JMLEclipse Home Page, https://sourceforge.net/apps/trac/jmlspecs/browser/JmlEclipse.?
JMLUnitNG Home Page, http://formalmethods.insttech.washington.edu/software/jmlunitng/.?
JMLOK Home Page, http://massoni.computacao.ufcg.edu.br/home/jmlok.?
AutoJML Home Page, http://autojml.sourceforge.net/.?
Gary T. Leavens, Erik Poll, Curtis Clifton, Yoonsik Cheon, Clyde Ruby, David Cok, Peter Müller, Joseph Kiniry, Patrice Chalin, and Daniel M. Zimmerman. JML Reference Manual (DRAFT), May, 2013.?
原文:https://www.cnblogs.com/daniel10001/p/10908448.html