This is a program which also contains annotations to help prove its correctness using the Frama-C software.
The annotations specify various properties for a method like the pre-condition, post-condition, assertions, states
modified, loop-invariants and loop-variants. They are also used to define other artifacts to ease specification/proving like
predicates, logic functions, axioms and lemmas. These annotations are provided inside code-comments
of the form /*@ ... */
and //@ ...
.
Frama-C with its WP plugin works with external provers like Alt-Ergo and CVC4 to automatically prove these specified properties. The WP plugin internally works based on the Weakest Precondition calculus. You can read more about this plugin's usage and meaning of the annotations in this tutorial.
The programs provided here have been proved using these versions of the tools: Frama-C (contains WP plugin) 21.1, Alt-Ergo 2.3.1 and CVC4 1.6. The system is x86_64 running Debian 10 Linux.
In addition to proving the specified properties, we will also be checking for other issues like
overflows via another Frama-C plugin called
RTE (by using option -wp-rte
).
Command to prove this program:
frama-c -wp -wp-prover cvc4 -wp-rte filename.c
/* Method findmax() returns the index of the maximum element in array a[] of
n elements */
typedef unsigned int uint;
/*@
// is the element a[m] the maximum among indices 0 to e?
predicate is_max(int *a, integer e, integer m) =
\forall integer i; 0 <= i <= e ==> a[i] <= a[m];
*/
/*@
requires n > 0;
requires \valid_read(a + (0..n-1));
assigns \nothing;
ensures is_max(a, n-1, \result);
*/
uint findmax(int a[], uint n)
{
uint i, m;
i = 1;
m = 0;
/*@
loop assigns i, m;
loop invariant is_max(a, i-1, m);
loop invariant 0 <= m <= n-1;
loop variant n-i;
*/
while(i < n)
{
if(a[i] > a[m])
m = i;
i++;
}
return m;
}
Copyright © 2022 Nitin Verma. All rights reserved.