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 sort5() sorts the input array of 5 elements using a sorting-network */
/*@
requires \valid(a+i) && \valid(a+j);
assigns a[i], a[j];
ensures a[i] <= a[j];
ensures (a[i] == \old(a[i]) && a[j] == \old(a[j])) ||
(a[i] == \old(a[j]) && a[j] == \old(a[i]));
*/
void SWAP(int a[], int i, int j)
{
if(a[i] > a[j])
{
int t;
t = a[i];
a[i] = a[j];
a[j] = t;
}
}
/*@
requires \valid(a + (0..4));
assigns a[0..4];
// the array elements at method return are sorted
ensures \forall integer i; 0 <= i <= 3 ==> a[i] <= a[i+1];
// the array elements at method return are a permutation of the initial elements
ensures \forall integer i; 0 <= i <= 4 ==>
(\exists integer j; 0 <= j <= 4 && a[i] == \old(a[j]));
ensures \forall integer i; 0 <= i <= 4 ==>
(\exists integer j; 0 <= j <= 4 && \old(a[i]) == a[j]);
*/
void sort5(int a[])
{
SWAP(a, 0, 1);
SWAP(a, 3, 4);
SWAP(a, 2, 4);
SWAP(a, 2, 3);
SWAP(a, 0, 3);
SWAP(a, 0, 2);
SWAP(a, 1, 4);
SWAP(a, 1, 3);
SWAP(a, 1, 2);
}
Copyright © 2022 Nitin Verma. All rights reserved.