Skip to content

Commit 20408dd

Browse files
authored
Merge pull request #6049 from feliperodri/fix-contracts-asserts-and-assumes
Properly converts conditions into assertions and assumptions in function contracts
2 parents 8a117bc + 3363d8c commit 20408dd

File tree

19 files changed

+165
-38
lines changed

19 files changed

+165
-38
lines changed

regression/contracts/assigns_enforce_arrays_01/main.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ void f1(int a[], int len) __CPROVER_assigns(a)
44
a = b;
55
int *indr = a + 2;
66
*indr = 5;
7+
a[5] = 2;
78
}
89

910
int main()

regression/contracts/assigns_enforce_arrays_02/main.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ int nextIdx() __CPROVER_assigns(idx)
88
return idx;
99
}
1010

11-
void f1(int a[], int len)
11+
void f1(int a[], int len) __CPROVER_assigns(*a, idx)
1212
{
1313
a[nextIdx()] = 5;
1414
}

regression/contracts/assigns_enforce_arrays_02/test.desc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,5 @@ Checks whether the instrumentation process does not duplicate function calls
1010
used as part of array-index operations, i.e., if a function call is used in
1111
the computation of the index of an array assignment, then instrumenting that
1212
statement with an assigns clause will not result in multiple function calls.
13+
This test also ensures that assigns clauses correctly check for global
14+
variables modified by subcalls.

regression/contracts/assigns_enforce_arrays_03/test.desc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,5 @@ main.c
66
^VERIFICATION FAILED$
77
--
88
--
9-
Checks whether verification fails when an array is assigned at an index
10-
below its lower bound.
9+
Checks whether verification fails when an assigns clause contains pointer,
10+
but function only modifies its content.

regression/contracts/assigns_enforce_arrays_05/test.desc

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,6 @@ main.c
66
^VERIFICATION FAILED$
77
--
88
--
9-
Checks whether verification fails when an array is assigned via a
10-
function call which assigns a pointer to an element out of the
11-
allowable range.
9+
Checks whether verification fails when an assigns clause of a function
10+
indicates the pointer might change, but one of its internal function only
11+
modified its content.
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#include <assert.h>
2+
3+
int idx = 4;
4+
5+
int nextIdx() __CPROVER_assigns(idx)
6+
{
7+
idx++;
8+
return idx;
9+
}
10+
11+
void f1(int a[], int len) __CPROVER_assigns(*a)
12+
{
13+
a[nextIdx()] = 5;
14+
}
15+
16+
int main()
17+
{
18+
int arr[10];
19+
f1(arr, 10);
20+
21+
assert(idx == 5);
22+
23+
return 0;
24+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
CORE
2+
main.c
3+
--enforce-all-contracts
4+
^EXIT=10$
5+
^SIGNAL=0$
6+
^VERIFICATION FAILED$
7+
--
8+
--
9+
This test also ensures that assigns clauses correctly check for global
10+
variables modified by subcalls. In this case, since the assigns clause
11+
doesn't include the modified global variable, the verification should failed.
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#include "utility.h"
2+
#include <stdbool.h>
3+
#include <stdlib.h>
4+
5+
int foo(int *x) __CPROVER_requires(s2n_result_is_ok(validity3(x)))
6+
__CPROVER_assigns(*x) __CPROVER_ensures(
7+
__CPROVER_return_value == *x + 5 && s2n_result_is_ok(validity3(x)))
8+
{
9+
*x = *x + 0;
10+
return *x + 5;
11+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#include "header.h"
2+
3+
int main()
4+
{
5+
int *n = malloc(sizeof(*n));
6+
*n = foo(n);
7+
8+
return 0;
9+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
CORE
2+
main.c
3+
--enforce-all-contracts
4+
^EXIT=0$
5+
^SIGNAL=0$
6+
^VERIFICATION SUCCESSFUL$
7+
--
8+
--
9+
This test checks whether verification succeeds when requires and ensures
10+
contain functions.
11+
12+
Note: We still don't check for function purity, i.e.,
13+
functions in contracts must only work as predicates.

0 commit comments

Comments
 (0)