Skip to content

Commit b5d22b5

Browse files
committed
instruments loops per given assigns clauses
1 parent 1b34d7c commit b5d22b5

File tree

12 files changed

+268
-77
lines changed

12 files changed

+268
-77
lines changed

regression/contracts/invar_assigns_alias_analysis/test.desc

Lines changed: 0 additions & 14 deletions
This file was deleted.

regression/contracts/invar_assigns_empty/main.c

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,8 @@
22

33
int main()
44
{
5-
int r;
6-
__CPROVER_assume(r >= 0);
7-
while(r > 0)
8-
__CPROVER_assigns() __CPROVER_loop_invariant(r >= 0)
5+
while(1 == 1)
6+
__CPROVER_assigns() __CPROVER_loop_invariant(1 == 1)
97
{
10-
r--;
118
}
12-
assert(r == 0);
13-
14-
return 0;
159
}

regression/contracts/invar_assigns_empty/test.desc

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ main.c
55
^SIGNAL=0$
66
^\[main.1\] .* Check loop invariant before entry: SUCCESS$
77
^\[main.2\] .* Check that loop invariant is preserved: SUCCESS$
8-
^\[main.assertion.1\] .* assertion r == 0: SUCCESS$
98
^VERIFICATION SUCCESSFUL$
109
--
1110
--

regression/contracts/invar_assigns_opt/test.desc

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,16 @@ main.c
33
--apply-loop-contracts
44
^EXIT=0$
55
^SIGNAL=0$
6-
^\[main.1\] .* Check loop invariant before entry: SUCCESS$
7-
^\[main.2\] .* Check that loop invariant is preserved: SUCCESS$
8-
^\[main.3\] .* Check decreases clause on loop iteration: SUCCESS$
9-
^\[main.assertion.1\] .* assertion r1 == 0: SUCCESS$
10-
^\[main.4\] .* Check loop invariant before entry: SUCCESS$
11-
^\[main.5\] .* Check that loop invariant is preserved: SUCCESS$
12-
^\[main.6\] .* Check decreases clause on loop iteration: SUCCESS$
13-
^\[main.assertion.2\] .* assertion r2 == 0: SUCCESS$
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.\d+\] .* Check that s2 is assignable: SUCCESS$
12+
^\[main.\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$
1416
^VERIFICATION SUCCESSFUL$
1517
--
1618
--
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#include <assert.h>
2+
#include <stdlib.h>
3+
4+
#define SIZE 8
5+
6+
struct blob
7+
{
8+
char *data;
9+
};
10+
11+
void main()
12+
{
13+
struct blob *b = malloc(sizeof(struct blob));
14+
b->data = malloc(SIZE);
15+
16+
b->data[5] = 0;
17+
for(unsigned i = 0; i < SIZE; i++)
18+
// clang-format off
19+
__CPROVER_assigns(i, __CPROVER_POINTER_OBJECT(b->data))
20+
__CPROVER_loop_invariant(i <= SIZE)
21+
// clang-format on
22+
{
23+
b->data[i] = 1;
24+
}
25+
26+
assert(b->data[5] == 0);
27+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
CORE
2+
main.c
3+
--apply-loop-contracts
4+
^EXIT=10$
5+
^SIGNAL=0$
6+
^\[main.\d+\] .* Check loop invariant before entry: SUCCESS$
7+
^\[main.\d+\] .* Check that i is assignable: SUCCESS$
8+
^\[main.\d+\] .* Check that b->data\[(.*)i\] is assignable: SUCCESS$
9+
^\[main.\d+\] .* Check that loop invariant is preserved: SUCCESS$
10+
^\[main.assertion.\d+\] .* assertion b->data\[5\] == 0: FAILURE$
11+
^VERIFICATION FAILED$
12+
--
13+
--
14+
This test (taken from #6021) shows the need for assigns clauses on loops.
15+
The alias analysis in this case returns `unknown`,
16+
and so we must manually annotate an assigns clause on the loop.

regression/contracts/invar_assigns_alias_analysis/main.c renamed to regression/contracts/loop_assigns-02/main.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ void main()
1616
b->data[5] = 0;
1717
for(unsigned i = 0; i < SIZE; i++)
1818
// clang-format off
19-
__CPROVER_assigns(b->data)
19+
__CPROVER_assigns(i, b->data[i])
2020
__CPROVER_loop_invariant(i <= SIZE)
2121
// clang-format on
2222
{
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
CORE
2+
main.c
3+
--apply-loop-contracts
4+
^EXIT=10$
5+
^SIGNAL=0$
6+
^\[main.\d+\] .* Check loop invariant before entry: SUCCESS$
7+
^\[main.\d+\] .* Check that i is assignable: SUCCESS$
8+
^\[main.\d+\] .* Check that b->data\[(.*)i\] is assignable: FAILURE$
9+
^\[main.\d+\] .* Check that loop invariant is preserved: SUCCESS$
10+
^\[main.assertion.\d+\] .* assertion b->data\[5\] == 0: FAILURE$
11+
^VERIFICATION FAILED$
12+
--
13+
--
14+
This test (taken from #6021) shows the need for assigns clauses on loops.
15+
The alias analysis in this case returns `unknown`,
16+
and so we must manually annotate an assigns clause on the loop.
17+
18+
Note that the annotated assigns clause in this case is an underapproximation,
19+
per the current semantics of the assigns clause -- it must model ALL memory
20+
being assigned to by the loop, not just a single symbolic iteration.
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#include <assert.h>
2+
#include <stdlib.h>
3+
4+
#define SIZE 8
5+
6+
struct blob
7+
{
8+
char *data;
9+
};
10+
11+
void main()
12+
{
13+
struct blob *b = malloc(sizeof(struct blob));
14+
b->data = malloc(SIZE);
15+
16+
b->data[5] = 0;
17+
for(unsigned i = 0; i < SIZE; i++)
18+
// clang-format off
19+
__CPROVER_assigns(__CPROVER_POINTER_OBJECT(b->data))
20+
__CPROVER_loop_invariant(i <= SIZE)
21+
// clang-format on
22+
{
23+
b->data[i] = 1;
24+
}
25+
26+
assert(b->data[5] == 0);
27+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
CORE
2+
main.c
3+
--apply-loop-contracts
4+
^EXIT=10$
5+
^SIGNAL=0$
6+
^\[main.\d+\] .* Check loop invariant before entry: SUCCESS$
7+
^\[main.\d+\] .* Check that i is assignable: FAILURE$
8+
^\[main.\d+\] .* Check that b->data\[(.*)i\] is assignable: SUCCESS$
9+
^\[main.\d+\] .* Check that loop invariant is preserved: SUCCESS$
10+
^\[main.assertion.\d+\] .* assertion b->data\[5\] == 0: SUCCESS$
11+
^VERIFICATION FAILED$
12+
--
13+
--
14+
This test (taken from #6021) shows the need for assigns clauses on loops.
15+
The alias analysis in this case returns `unknown`,
16+
and so we must manually annotate an assigns clause on the loop.
17+
18+
Note that the annotated assigns clause in this case is an underapproximation,
19+
because `i` is also assigned in the loop and should be marked as assignable.

0 commit comments

Comments
 (0)