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 binary_search() performs a binary-search for element x in array a[]
of n elements. If a match is found, the corresponding index is returned;
otherwise -1 is returned. */
/*@
// Does array a[] contain x between its indices s and e?
predicate contains(int *a, integer s, integer e, int x) =
\exists integer i; s <= i <= e && a[i] == x;
*/
/*@
requires \valid_read(a + (0..n-1));
requires n > 0;
// To specify the sorted order, if we instead use the predicate
// (\forall int i; 0 <= i <= n-2 ==> a[i] <= a[i+1]), the provers
// like cvc4 and alt-ergo are not able to prove this method.
requires \forall int i, j; 0 <= i < j <= n-1 ==> a[i] <= a[j];
ensures contains(a, 0, n-1, x) ==> a[\result] == x;
ensures !contains(a, 0, n-1, x) ==> \result == -1;
*/
int binary_search(int a[], int n, int x)
{
int s, e;
/* e can become -1, so using signed int */
s = 0;
e = n-1;
/*@
loop assigns s, e;
loop invariant (s == e+1) || (s <= e && 0 <= s <= n-1 && 0 <= e <= n-1);
loop invariant !contains(a, 0, s-1, x) && !contains(a, e+1, n-1, x);
loop invariant contains(a, 0, n-1, x) ==> contains(a, s, e, x);
loop variant e-s+1;
*/
while(s <= e)
{
int m = s + (e-s)/2;
if(a[m] == x)
return m;
if(a[m] < x)
{
//@assert !contains(a, s, m, x);
s = m+1;
}
else
{
//@assert !contains(a, m, e, x);
e = m-1;
}
}
return -1;
}
Copyright © 2022 Nitin Verma. All rights reserved.