Skip to content

Commit 436679f

Browse files
committed
Migrate loop contracts regressions to dfcc
1 parent 8fa5ac8 commit 436679f

File tree

154 files changed

+3499
-23
lines changed

Some content is hidden

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

154 files changed

+3499
-23
lines changed
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#include <stdlib.h>
2+
3+
int main()
4+
{
5+
unsigned char *i = malloc(5);
6+
7+
while(i != i + 5)
8+
__CPROVER_loop_invariant(1 == 1)
9+
{
10+
const char lower = *i++;
11+
}
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+
--dfcc main --apply-loop-contracts
4+
^\[main.assigns.\d+\].*line 10 Check that i is assignable: SUCCESS$
5+
^EXIT=0$
6+
^SIGNAL=0$
7+
^VERIFICATION SUCCESSFUL$
8+
--
9+
--
10+
Checks that loop local variables do not cause explicit checks.
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
static void foo()
2+
{
3+
unsigned i;
4+
5+
for(i = 0; i < 16; i++)
6+
__CPROVER_loop_invariant(1 == 1)
7+
{
8+
int v = 1;
9+
}
10+
}
11+
12+
static void bar()
13+
{
14+
unsigned i;
15+
16+
for(i = 0; i < 16; i++)
17+
__CPROVER_loop_invariant(1 == 1)
18+
{
19+
int v = 1;
20+
}
21+
}
22+
23+
int main()
24+
{
25+
bar();
26+
foo();
27+
foo();
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+
--dfcc main --apply-loop-contracts
4+
^\[bar.assigns.\d+\].*Check that i is assignable: SUCCESS$
5+
^\[foo.assigns.\d+\].*Check that i is assignable: SUCCESS$
6+
^EXIT=0$
7+
^SIGNAL=0$
8+
^VERIFICATION SUCCESSFUL$
9+
--
10+
--
11+
Checks that loop local variables do not cause explicit checks
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#include <assert.h>
2+
#include <stdlib.h>
3+
4+
static int adder(const int *a, const int *b)
5+
{
6+
return (*a + *b);
7+
}
8+
9+
int main()
10+
{
11+
int x = 1024;
12+
13+
int (*local_adder)(const int *, const int *) = adder;
14+
15+
while(x > 0)
16+
__CPROVER_loop_invariant(1 == 1)
17+
{
18+
x += local_adder(&x, &x); // loop detection fails
19+
//x += adder(&x, &x); // works fine
20+
}
21+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
CORE
2+
main.c
3+
--dfcc main --apply-loop-contracts
4+
^EXIT=0$
5+
^SIGNAL=0$
6+
^VERIFICATION SUCCESSFUL$
7+
^\[main.\d+\] line \d+ Check loop invariant before entry: SUCCESS$
8+
^\[main.\d+\] line \d+ Check that loop invariant is preserved: SUCCESS$
9+
--
10+
--
11+
This is guarding against an issue described in https://github.com/diffblue/cbmc/issues/6168.
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#include <assert.h>
2+
3+
int main()
4+
{
5+
int i, n, x[10];
6+
__CPROVER_assume(x[0] == x[9]);
7+
while(i < n)
8+
__CPROVER_loop_invariant(x[0] == __CPROVER_loop_entry(x[0]))
9+
{
10+
x[0] = x[9] - 1;
11+
x[0]++;
12+
}
13+
assert(x[0] == x[9]);
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+
--dfcc main --apply-loop-contracts
4+
^EXIT=0$
5+
^SIGNAL=0$
6+
^VERIFICATION SUCCESSFUL$
7+
--
8+
^Tracking history of index expressions is not supported yet\.
9+
--
10+
This test checks that `ID_index` expressions are allowed within history variables.
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#include <assert.h>
2+
3+
int foo(int x)
4+
{
5+
return x;
6+
}
7+
8+
int main()
9+
{
10+
int i, n, x[10];
11+
__CPROVER_assume(x[0] == x[9]);
12+
while(i < n)
13+
__CPROVER_loop_invariant(x[0] == __CPROVER_loop_entry(foo(x[0])))
14+
{
15+
x[0] = x[9] - 1;
16+
x[0]++;
17+
}
18+
assert(x[0] == x[9]);
19+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
CORE
2+
main.c
3+
--dfcc --apply-loop-contracts
4+
^main.c.* error: Tracking history of side_effect expressions is not supported yet.$
5+
^CONVERSION ERROR$
6+
^EXIT=(1|64)$
7+
^SIGNAL=0$
8+
--
9+
--
10+
This test ensures that expressions with side effect, such as function calls,
11+
may not be used in history variables.
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#include <assert.h>
2+
3+
int main()
4+
{
5+
while(1 == 1)
6+
__CPROVER_assigns() __CPROVER_loop_invariant(1 == 1)
7+
{
8+
}
9+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
CORE
2+
main.c
3+
--dfcc main --apply-loop-contracts
4+
^EXIT=0$
5+
^SIGNAL=0$
6+
^\[main.\d+\] .* Check loop invariant before entry: SUCCESS$
7+
^\[main.\d+\] .* Check that loop invariant is preserved: SUCCESS$
8+
^VERIFICATION SUCCESSFUL$
9+
--
10+
--
11+
This test checks that empty assigns clause is supported
12+
in loop contracts.

regression/contracts-dfcc/invar_assigns_opt/test.desc

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
11
CORE
22
main.c
3-
--dfcc main
3+
--dfcc main --apply-loop-contracts
44
^EXIT=0$
55
^SIGNAL=0$
6-
^\[main.\d+\] .* Check loop invariant before entry: SUCCESS$
7-
^\[main.\d+\] .* Check that loop invariant is preserved: SUCCESS$
8-
^\[main.\d+\] .* Check decreases clause on loop iteration: SUCCESS$
9-
^\[main.assertion.\d+\] .* assertion r1 == 0: SUCCESS$
10-
^\[main.\d+\] .* Check loop invariant before entry: SUCCESS$
11-
^\[main.assigns.\d+\] .* Check that s2 is assignable: SUCCESS$
12-
^\[main.assigns.\d+\] .* Check that r2 is assignable: SUCCESS$
13-
^\[main.\d+\] .* Check that loop invariant is preserved: SUCCESS$
14-
^\[main.\d+\] .* Check decreases clause on loop iteration: SUCCESS$
15-
^\[main.assertion.\d+\] .* assertion r2 == 0: SUCCESS$
6+
^\[foo.\d+\] .* Check loop invariant before entry: SUCCESS$
7+
^\[foo.\d+\] .* Check that loop invariant is preserved: SUCCESS$
8+
^\[foo.\d+\] .* Check decreases clause on loop iteration: SUCCESS$
9+
^\[foo.assertion.\d+\] .* assertion r1 == 0: SUCCESS$
10+
^\[foo.\d+\] .* Check loop invariant before entry: SUCCESS$
11+
^\[foo.assigns.\d+\] .* Check that s2 is assignable: SUCCESS$
12+
^\[foo.assigns.\d+\] .* Check that r2 is assignable: SUCCESS$
13+
^\[foo.\d+\] .* Check that loop invariant is preserved: SUCCESS$
14+
^\[foo.\d+\] .* Check decreases clause on loop iteration: SUCCESS$
15+
^\[foo.assertion.\d+\] .* assertion r2 == 0: SUCCESS$
1616
^VERIFICATION SUCCESSFUL$
1717
--
1818
--
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
#include <assert.h>
2+
3+
int main()
4+
{
5+
int r;
6+
__CPROVER_assume(r >= 0);
7+
8+
while(r > 0)
9+
__CPROVER_loop_invariant(r >= 0)
10+
{
11+
--r;
12+
if(r <= 1)
13+
{
14+
break;
15+
}
16+
else
17+
{
18+
--r;
19+
}
20+
}
21+
22+
assert(r == 0);
23+
24+
return 0;
25+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
CORE
2+
main.c
3+
--dfcc main --apply-loop-contracts
4+
^EXIT=10$
5+
^SIGNAL=0$
6+
^\[main\.\d+\] .* Check loop invariant before entry: SUCCESS$
7+
^\[main\.\d+\] .* Check that loop invariant is preserved: SUCCESS$
8+
^\[main.assigns.\d+\] .* Check that r is assignable: SUCCESS$
9+
^\[main\.assertion\.\d+\] .* assertion r == 0: FAILURE$
10+
^VERIFICATION FAILED$
11+
--
12+
This test is expected to fail along the code path where r is an even integer
13+
before entering the loop.
14+
The program is simply incompatible with the desired property in this case --
15+
there is no loop invariant that can establish the required assertion.
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
#include <assert.h>
2+
3+
int main()
4+
{
5+
int r;
6+
__CPROVER_assume(r >= 0);
7+
8+
while(r > 0)
9+
// clang-format off
10+
__CPROVER_loop_invariant(r >= 0)
11+
__CPROVER_decreases(r)
12+
// clang-format on
13+
{
14+
--r;
15+
if(r <= 1)
16+
{
17+
break;
18+
}
19+
else
20+
{
21+
--r;
22+
}
23+
}
24+
25+
assert(r == 0 || r == 1);
26+
27+
return 0;
28+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
CORE
2+
main.c
3+
--dfcc main --apply-loop-contracts
4+
^EXIT=0$
5+
^SIGNAL=0$
6+
^\[main\.\d+\] .* Check loop invariant before entry: SUCCESS$
7+
^\[main\.\d+\] .* Check that loop invariant is preserved: SUCCESS$
8+
^\[main.assigns.\d+\] .* Check that r is assignable: SUCCESS$
9+
^\[main\.assertion\.\d+\] .* assertion r == 0 || r == 1: SUCCESS$
10+
^VERIFICATION SUCCESSFUL$
11+
--
12+
This test checks that conditionals and `break` are correctly handled.
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
#include <assert.h>
2+
3+
int main()
4+
{
5+
int r;
6+
__CPROVER_assume(r >= 0);
7+
8+
while(r > 0)
9+
// clang-format off
10+
__CPROVER_loop_invariant(r >= 0)
11+
__CPROVER_decreases(r)
12+
// clang-format on
13+
{
14+
--r;
15+
if(r < 5)
16+
{
17+
continue;
18+
}
19+
else
20+
{
21+
--r;
22+
}
23+
}
24+
25+
assert(r == 0);
26+
27+
return 0;
28+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
CORE
2+
main.c
3+
--dfcc main --apply-loop-contracts
4+
^EXIT=0$
5+
^SIGNAL=0$
6+
^\[main\.\d+\] .* Check loop invariant before entry: SUCCESS$
7+
^\[main\.\d+\] .* Check that loop invariant is preserved: SUCCESS$
8+
^\[main.assigns.\d+\] .* Check that r is assignable: SUCCESS$
9+
^\[main\.assertion\.\d+\] .* assertion r == 0: SUCCESS$
10+
^VERIFICATION SUCCESSFUL$
11+
--
12+
This test checks that conditionals and `continue` are correctly handled.

0 commit comments

Comments
 (0)