Skip to content

Commit cb5ce98

Browse files
committed
Adds regression test to cover code contracts support
Code contracts now supports assigns clause with structs and array ranges. It also supports contracts specified across multiple source files. So, we added regression tests to cover these capabilites. Signed-off-by: Felipe R. Monteiro <[email protected]>
1 parent 7129fdf commit cb5ce98

File tree

54 files changed

+788
-20
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

54 files changed

+788
-20
lines changed
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
/* clang-format off */
2+
void f1(int a[], int len) __CPROVER_assigns(a[2 .. 5])
3+
/* clang-format on */
4+
{
5+
int b[10];
6+
a = b;
7+
int *indr = a + 2;
8+
*indr = 5;
9+
}
10+
11+
int main()
12+
{
13+
int arr[10];
14+
f1(arr, 10);
15+
16+
return 0;
17+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
CORE
2+
main.c
3+
--enforce-all-contracts
4+
^EXIT=0$
5+
^SIGNAL=0$
6+
^VERIFICATION SUCCESSFUL$
7+
--
8+
--
9+
Checks their assigns clause behavior when it reasons (indirectly)
10+
over a freshly-allocated variable.
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
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+
/* clang-format off */
12+
void f1(int a[], int len) __CPROVER_assigns(idx, a[2 .. 5])
13+
/* clang-format on */
14+
{
15+
a[nextIdx()] = 5;
16+
}
17+
18+
int main()
19+
{
20+
int arr[10];
21+
f1(arr, 10);
22+
23+
assert(idx == 5);
24+
25+
return 0;
26+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
CORE
2+
main.c
3+
--enforce-all-contracts
4+
^EXIT=0$
5+
^SIGNAL=0$
6+
^VERIFICATION SUCCESSFUL$
7+
--
8+
--
9+
Checks whether the instrumentation process does not duplicate function calls
10+
used as part of array-index operations, i.e., if a function call is used in
11+
the computation of the index of an array assignment, then instrumenting that
12+
statement with an assigns clause will not result in multiple function calls.
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
/* clang-format off */
2+
void f1(int a[], int len) __CPROVER_assigns(a[2 .. 5])
3+
/* clang-format on */
4+
{
5+
int *indr;
6+
indr = a + 3;
7+
*indr = 5;
8+
}
9+
10+
int main()
11+
{
12+
int arr[10];
13+
f1(arr, 10);
14+
15+
return 0;
16+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
CORE
2+
main.c
3+
--enforce-all-contracts
4+
^EXIT=0$
5+
^SIGNAL=0$
6+
^VERIFICATION SUCCESSFUL$
7+
--
8+
--
9+
Checks whether verification succeeds when an array is assigned indirectly
10+
through a pointer.
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
void assigns_single(int a[], int len) __CPROVER_assigns(a[8])
2+
{
3+
a[8] = 20;
4+
}
5+
6+
/* clang-format off */
7+
void assigns_upper_bound(int a[], int len) __CPROVER_assigns(a[2 .. 5])
8+
/* clang-format on */
9+
{
10+
a[5] = 10;
11+
}
12+
13+
/* clang-format off */
14+
void assigns_lower_bound(int a[], int len) __CPROVER_assigns(a[2 .. 5])
15+
/* clang-format oon */
16+
{
17+
a[2] = 10;
18+
}
19+
20+
int main()
21+
{
22+
int arr[10];
23+
assigns_upper_bound(arr, 10);
24+
assigns_lower_bound(arr, 10);
25+
assigns_single(arr, 10);
26+
27+
return 0;
28+
}
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=0$
5+
^SIGNAL=0$
6+
^VERIFICATION SUCCESSFUL$
7+
--
8+
--
9+
Checks whether verification succeeds when an array is assigned at both upper
10+
and lower bounds of its assignable array range. Single-value array range
11+
assignment is checked as well.
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
/* clang-format off */
2+
void assign_out_under(int a[], int len) __CPROVER_assigns(a[2 .. 5])
3+
/* clang-format on */
4+
{
5+
a[1] = 5;
6+
}
7+
8+
int main()
9+
{
10+
int arr[10];
11+
assign_out_under(arr, 10);
12+
13+
return 0;
14+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
CORE
2+
main.c
3+
--enforce-all-contracts
4+
^EXIT=10$
5+
^SIGNAL=0$
6+
^VERIFICATION FAILED$
7+
--
8+
--
9+
Checks whether verification fails when an array is assigned at an index
10+
below its lower bound.
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
void assign_out_under(int a[], int len) __CPROVER_assigns(a[8])
2+
{
3+
a[7] = 5;
4+
}
5+
6+
int main()
7+
{
8+
int arr[10];
9+
assign_out_under(arr, 10);
10+
11+
return 0;
12+
}
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+
Checks whether verification fails when an array is assigned at an index below
10+
its allowed index and specification contains only a single cell
11+
instead of a range.
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
/* clang-format off */
2+
void assign_out_over(int a[], int len) __CPROVER_assigns(a[2 .. 5])
3+
/* clang-format on */
4+
{
5+
a[6] = 5;
6+
}
7+
8+
int main()
9+
{
10+
int arr[10];
11+
assign_out_over(arr, 10);
12+
13+
return 0;
14+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
CORE
2+
main.c
3+
--enforce-all-contracts
4+
^EXIT=10$
5+
^SIGNAL=0$
6+
^VERIFICATION FAILED$
7+
--
8+
--
9+
Checks whether verification fails when an array is assigned at an index
10+
above its upper bound.
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
void assign_out_under(int a[], int len) __CPROVER_assigns(a[8])
2+
{
3+
a[9] = 5;
4+
}
5+
6+
int main()
7+
{
8+
int arr[10];
9+
assign_out_under(arr, 10);
10+
11+
return 0;
12+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
CORE
2+
main.c
3+
--enforce-all-contracts
4+
^EXIT=10$
5+
^SIGNAL=0$
6+
^VERIFICATION FAILED$
7+
--
8+
--
9+
Checks whether verification fails when an array is assigned at an index
10+
above its allowed index.
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
/* clang-format off */
2+
void assigns_in_range(int a[], int last_idx) __CPROVER_assigns(a[2 .. last_idx])
3+
/* clang-format on */
4+
{
5+
a[last_idx] = 6;
6+
}
7+
8+
int main()
9+
{
10+
int arr[10];
11+
assigns_in_range(arr, 9);
12+
13+
return 0;
14+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
CORE
2+
main.c
3+
--enforce-all-contracts
4+
^EXIT=0$
5+
^SIGNAL=0$
6+
^VERIFICATION SUCCESSFUL$
7+
--
8+
--
9+
Checks whether verification succeeds when an array is assigned at the upper
10+
bounds of its assignable array range, when the range has a non-constant bound.
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
/* clang-format off */
2+
void assigns_in_range(int a[], int last_idx) __CPROVER_assigns(a[2 .. last_idx])
3+
/* clang-format on */
4+
{
5+
last_idx++;
6+
a[last_idx] = 6;
7+
}
8+
9+
int main()
10+
{
11+
int arr[10];
12+
assigns_in_range(arr, 9);
13+
14+
return 0;
15+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
CORE
2+
main.c
3+
--enforce-all-contracts
4+
^EXIT=10$
5+
^SIGNAL=0$
6+
^VERIFICATION FAILED$
7+
--
8+
--
9+
Checks whether verification fails when an array is assigned past the upper
10+
bound of its assignable array range, using the symbol of the upper bound,
11+
after it has been incremented. This is intended to show that the bounds
12+
are determined at the time the function is called.
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
void assigns_single(int a[], int len) __CPROVER_assigns(a[7])
2+
{
3+
}
4+
5+
/* clang-format off */
6+
void assigns_range(int a[], int len) __CPROVER_assigns(a[2 .. 5])
7+
/* clang-format on */
8+
{
9+
}
10+
11+
/* clang-format off */
12+
void assigns_big_range(int a[], int len) __CPROVER_assigns(a[2 .. 7])
13+
/* clang-format on */
14+
{
15+
assigns_single(a, len);
16+
assigns_range(a, len);
17+
}
18+
19+
int main()
20+
{
21+
int arr[10];
22+
assigns_big_range(arr, 10);
23+
24+
return 0;
25+
}
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=0$
5+
^SIGNAL=0$
6+
^VERIFICATION SUCCESSFUL$
7+
--
8+
--
9+
Checks whether verification succeeds when an array is assigned through
10+
calls to functions with array assigns clauses which are compatible with
11+
that of the caller.
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/* clang-format off */
2+
void assign_25(int a[], int len) __CPROVER_assigns(a[2 .. 5])
3+
/* clang-format on */
4+
{
5+
}
6+
7+
/* clang-format off */
8+
void assign_37(int a[], int len) __CPROVER_assigns(a[3 .. 7])
9+
/* clang-format on */
10+
{
11+
assign_25(a, len);
12+
}
13+
14+
int main()
15+
{
16+
int arr[10];
17+
assign_37(arr, 10);
18+
19+
return 0;
20+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
CORE
2+
main.c
3+
--enforce-all-contracts
4+
^EXIT=10$
5+
^SIGNAL=0$
6+
^VERIFICATION FAILED$
7+
--
8+
--
9+
Checks whether verification fails when a function with an array assigns target
10+
calls a function which may assign below the allowable range.
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
void assigns_ptr(int *x) __CPROVER_assigns(*x)
2+
{
3+
*x = 200;
4+
}
5+
6+
/* clang-format off */
7+
void assigns_range(int a[], int len) __CPROVER_assigns(a[2 .. 5])
8+
/* clang-format on */
9+
{
10+
int *ptr = &(a[3]);
11+
assigns_ptr(ptr);
12+
}
13+
14+
int main()
15+
{
16+
int arr[10];
17+
assigns_range(arr, 10);
18+
19+
return 0;
20+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
CORE
2+
main.c
3+
--enforce-all-contracts
4+
^EXIT=0$
5+
^SIGNAL=0$
6+
^VERIFICATION SUCCESSFUL$
7+
--
8+
--
9+
Checks whether verification succeeds when an array is assigned via a
10+
function call which assigns a pointer to one of its elements.

0 commit comments

Comments
 (0)