Gem #68: Let's SPARK! — Part 1

by Yannick Moy—AdaCore

Let's get started…

With Altran and AdaCore now teaming up to offer an integration of SPARK technology inside GPS (see http://www.adacore.com/home/products/sparkpro/), many GPS users will be interested in trying out the proof capabilities of SPARK on their own Ada programs. Of course it's a little more involved than that. SPARK is not only a set of tools for verifying high-assurance systems, but also incorporates a language that must be learned.

The SPARK language is made up of two parts, one of which is a subset of Ada (whose features will obviously already be familiar to Ada programmers!), meant to facilitate code understanding and proofs, and the other part being a specification language, meant to express properties of programs. Quite conveniently, the SPARK specifications (a.k.a. SPARK annnotations, to distinguish them from Ada specifications) are expressed within stylized Ada comments of the following form:

         <em>--# <some annotation here></em>

so SPARK annotations don't interfere with normal Ada compilation.

This Gem and the next one demonstrate various of the properties you can prove with SPARK, and how these relate to the SPARK annotations written by the user. As a simple example, we will take a procedure which searches linearly for a value in an array, and returns the index, if any, at which the value is found.

Here is the specification file search.ads:

<b>package</b> Search <b>is</b>
  <b>type</b> IntArray <b>is</b> <b>array</b> (Integer <b>range</b> <>) <b>of</b> Integer;
  <b>procedure</b> Linear_Search
    (Table : <b>in</b> IntArray;
     Value : <b>in</b> Integer;
     Found : <b>out</b> Boolean;
     Index : <b>out</b> Integer);
<b>end</b> Search;

And the body file search.adb:

<b>package</b> <b>body</b> Search <b>is</b>
  <b>procedure</b> Linear_Search
    (Table : <b>in</b> IntArray;
     Value : <b>in</b> Integer;
     Found : <b>out</b> Boolean;
     Index : <b>out</b> Integer) 
  <b>is</b>
     I : Integer := 0;
  <b>begin</b>
     Found := False;
     <b>while</b> I <= Table'Last <b>loop</b>
        <b>if</b> Table(I) = Value <b>then</b>
           Found := True;
           Index := I;
           <b>exit</b>;
        <b>end</b> <b>if</b>;
        I := I + 1;
     <b>end</b> <b>loop</b>;
  <b>end</b> Linear_Search;
<b>end</b> Search;

Let's first get set up for using SPARK:

1. Copy the files search.ads and search.adb into a directory named search.
2. Open GPS with a default project in the same directory.
3. Expand file search.adb.
4. Select SPARK/SPARKMake from the menu. This generates a file search.idx.
5. Copy the following code to a file called search.cfg:

  <b>package</b> Standard <b>is</b>
     <b>type</b> Integer <b>is</b> <b>range</b> -2**31 .. 2**31-1;
  <b>end</b> Standard;

6. From the Menu, select Project/Edit Project Properties.
7. Go to the page Switches/Examiner.
8. Select the following options:

  Index File            : search.idx
  Configuration File    : search.cfg
  Analysis              : Data Flow only
  Generate VCs          : yes (check it)

That's it!

Now open search.adb and select SPARK/Examine File. The SPARK Examiner runs and outputs the following messages:

       Flow Error 602 - The undefined initial value <b>of</b> Index may be
                         used <b>in</b> the derivation <b>of</b> Index
       Warning 402 - Default assertion planted to cut the <b>loop</b>
       Note - Information flow analysis <b>not</b> carried <b>out</b>

The Flow Error indicates that, although Index is an out parameter, it is not initialized on all paths through Linear_Search. One could argue that our intent here is to access Index only when Found is set to True. SPARK considers initialization errors too serious to allow such subtleties, and requires that all paths through the procedure must initialize all out parameters. Let's comply and initialize Index:

  <b>begin</b>
     Found := False;
     Index := 0;

After we rerun the Examiner, we are left with a Note that is simply a reminder of our choice of options, and a Warning that we will explain in the next Gem.

Now, we can continue with proving that our procedure is free from run-time errors, such as integer overflow and out-of-bounds array accesses. Select SPARK/Simplify All. The SPARK Simplifier runs. To see the result of this tool's execution, select SPARK/POGS. This opens a file search.sum, which summarizes all the proofs in the following table:

VCs <b>for</b> procedure_linear_search :
<em>----------------------------------------------------------------------------</em>
     |       |                     |  <em>-----Proved In-----  |       |       |</em>
#    | From  | To                  | vcg | siv | plg | prv | False | TO <b>DO</b> |
<em>----------------------------------------------------------------------------</em>
1    | start | rtc check @ 13      |     | YES |     |     |       |       | 
2    | start |    assert @ 15      |     | YES |     |     |       |       | 
3    | 15    |    assert @ 15      |     | YES |     |     |       |       | 
4    | 15    | rtc check @ 16      |     |     |     |     |       |  YES  | 
5    | 15    | rtc check @ 18      |     | YES |     |     |       |       | 
6    | 15    | rtc check @ 21      |     |     |     |     |       |  YES  | 
7    | 15    |    assert @ finish  | YES |     |     |     |       |       | 
8    | 15    |    assert @ finish  | YES |     |     |     |       |       | 
<em>----------------------------------------------------------------------------</em>

Each line corresponds to a Verification Condition (VC), which must be proved in order to guarantee that the program is free from run-time errors. Each column corresponds to the result of the proof attempt. If YES appears in one of the 4 "Proved In" columns, the proof was successful. If YES appears in the "False" column, there is something definitely wrong. If "YES" appears in the "TO DO" column, we don't know: either the program is wrong, or it is too complex to prove.

Since column "TO DO" is not empty here, not all proofs were successful. In the next Gem, we will give you more details about failed proofs. The reason for the failures here is that program Linear_Search is incorrect, meaning that run-time errors can be raised. To see this, just complete the program with the following code in main.adb, and build the executable.

<b>with</b> Search;
<b>use</b> Search;
<b>procedure</b> Main <b>is</b>
   Table : IntArray(1..10) := (<b>others</b> => 0);
   Found : Boolean;
   Index : Integer;
<b>begin</b>
   Linear_Search(Table, 0, Found, Index);
<b>end</b> Main;

Now run the executable, and it raises an exception:

   raised CONSTRAINT_ERROR : search.adb:16 index check failed

This is because we are passing an array to Linear_Search that starts at index 1 in its parameter Table, whereas Linear_Search loop assumes that the array starts at index 0. SPARK correctly assumes that we can pass in such an array to Linear_Search, and thus it fails to prove that Linear_Search is free from run-time errors.

Let's correct the code of Linear_Search:

  <b>procedure</b> Linear_Search
    (Table : <b>in</b> IntArray;
     Value : <b>in</b> Integer;
     Found : <b>out</b> Boolean;
     Index : <b>out</b> Integer) <b>is</b>
  <b>begin</b>
     Found := False;
     Index := 0;
     <b>for</b> I <b>in</b> Integer <b>range</b> Table'<b>Range</b> <b>loop</b>
        <b>if</b> Table(I) = Value <b>then</b>
           Found := True;
           Index := I;
            <b>exit</b>;
        <b>end</b> <b>if</b>;
     <b>end</b> <b>loop</b>;
  <b>end</b> Linear_Search;

Let's do it again: Examine, Simplify All, POGS ...
This time, columns "False" and "TO DO" are empty, meaning that all proofs were successful.

VCs <b>for</b> procedure_linear_search :
<em>----------------------------------------------------------------------------</em>
     |       |                     |  <em>-----Proved In-----  |       |       |</em>
#    | From  | To                  | vcg | siv | plg | prv | False | TO <b>DO</b> |
<em>----------------------------------------------------------------------------</em>
1    | start | rtc check @ 11      |     | YES |     |     |       |       | 
2    | start | rtc check @ 13      |     | YES |     |     |       |       | 
3    | start |    assert @ 13      |     | YES |     |     |       |       | 
4    | 13    |    assert @ 13      |     | YES |     |     |       |       | 
5    | 13    |    assert @ 13      |     | YES |     |     |       |       | 
6    | 13    | rtc check @ 14      |     | YES |     |     |       |       | 
7    | 13    | rtc check @ 16      |     | YES |     |     |       |       | 
8    | 13    |    assert @ finish  | YES |     |     |     |       |       | 
9    | 13    |    assert @ finish  | YES |     |     |     |       |       | 
<em>----------------------------------------------------------------------------</em>

Thus, we have proved that procedure Linear_Search is free from run-time errors.

In the next Gem, after the summer break, we will prove that procedure Linear_Search actually respects a given contract.


About the Author

Yannick Moy’s work focuses on software source code analysis, mostly to detect bugs or verify safety/security properties. Yannick previously worked for PolySpace (now The MathWorks) where he started the project C++ Verifier. He then joined INRIA Research Labs/Orange Labs in France to carry out a PhD on automatic modular static safety checking for C programs. Yannick joined AdaCore in 2009, after a short internship at Microsoft Research.

Yannick holds an engineering degree from the Ecole Polytechnique, an MSc from Stanford University and a PhD from Université Paris-Sud. He is a Siebel Scholar.


Last Updated: 10/26/2017
Posted on: 6/29/2009