ESC/Java2 assignment

Software Security, Spring 2011

Exercise 3: Program verification with ESC/Java2

Purpose of this exercise is to get a taste of how formal program verification can be used to certify properties of code. We use the program verification tool ESC/Java2 to verify (and debug) some pieces of Java code.

It is an individual exercise. It won't be marked, but doing it is obligatory and required to pass the course.

There will be the opportunity to get help doing this assignment on May 27 in HG00.023, 10:30-12:30 and 13:30-15:30. Fabian van den Broek and Wojciech Mostowksi will be there to help. For non-Nijmegen students we will provide user accounts.

NB have a look at the Java code samples and a first stab at the exercise before showing up here, so that you can make the most of the available help. It should then not be a problem to finish the exercise well within 2 hours.

Handing in the assignment

Easiest way is simply to show Fabian or Wojciech that you have completed the exercises at the end of the practicum session.

Otherwise, email your solutions (ie. the annotated Java files) to Fabian van den Broek (f.vandenbroek at cs.ru.nl) with subject [SS] ESC by June 3. NB Provide the annotated files in two attachements, named BagYourName.java and AmountYourName.java, where YourName is your full name, and also put your name in the Java file. Please don't zip or tar these attachments, to save us a lot of unzipping and untarring.

Background: The program verification tool ESC/Java2

ESC/Java2 is an automated program verification tool (aka extended static checker) for Java programs. It implements a Hoare logic for reasoning over Java programs that have been annotated with JML specifications. These specifications express for instance preconditions, postconditions, and invariants, and can be added as assertions to the program code. Essentially, ESC/Java2 computes weakest preconditions of methods, and then sends the resulting proof obligiations to the automated theorem prover Simplify. The user never get to see these proof obligations or the back-end theorem prover; instead, the tool gives feedback over which assertions it could not prove.

Running ESC/Java2

ESC/Java2 is available on the central Linux machines and on the Windows PCs in the terminal rooms. You can also download it on your own machine. Instructions for this:

The assignment

For the assignment, you should specify the two classes
  1. Bag.java
  2. Amount.java
with JML and run ESC/Java2 to verify these specifications. Simply put, you have to run ESC/Java2 on these files, and, if the tool produces some warning, to make the warning go away. You have to use your own best judgement to choose between these two options, but there are some deliberate bugs in the code for you to detect, with the help of the tool.

For Amount.java you must also try to formalise the informal invariant that is discussed in the file in JML, which should reveal some problems in the code.

In the end, ESC/Java2 should run without any complaints on the annotated code. ESC/Java2 complains (among other things) if it thinks a runtime exception may occur, say a NullPointerException, so if ESC/Java2 runs without complaints this means it has verified that no runtime exceptions can occur.

More detailed instructions are given in the Java files. Read these!

The only JML keywords you'll need to use for this are requires, invariant, and ensures. If you want, you can also use non_null as an abbreviation and experiment with other features.

The ESC/Java2 tool has many command line options. The most useful ones are

Some hints to keep you out of trouble with the tool:

Reflection

Some questions to consider
  1. In the end, do you think that you found all problems? Are you certain now that the code is correct?
  2. Can you think of ways in which the tool or the specification language could be improved?
  3. Instead of the tool we used, can you think of other ways (formal or informal, tool-supported or not) to find the problems that the tool found? If so, would these alternatives