Skip to content

Commit de218d2

Browse files
author
Remi Delmas
committed
CONTRACTS: Front-end extensions for __CPROVER_frees clauses
Add the following to the front-end: - add __CPROVER_frees as a new contract clause for functons and loops - add `__CPROVER_freeable_t` built-in type - add `__CPROVER_freeable` built-in function - allow calls to `__CPROVER_freeable_t` functions in frees clauses - add `__CPROVER_is_freeable` built-in predicate - add `__CPROVER_is_freed` built-in predicate The __CPROVER_frees clause and predicates are not yet supported in the back-end.
1 parent 6e1617a commit de218d2

File tree

40 files changed

+792
-15
lines changed

40 files changed

+792
-15
lines changed

doc/cprover-manual/contracts-frees.md

Lines changed: 149 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,149 @@
1+
[CPROVER Manual TOC](../../)
2+
3+
# Frees Clauses
4+
5+
A _frees clause_ lets the user specify the set of pointers that may be freed
6+
by a function or a loop.
7+
8+
The _frees clause_ may only refer to locations that exist in the calling context
9+
of the function or before entering the loop, i.e. that are part of the _frame_
10+
of the function call or loop execution.
11+
## Syntax
12+
13+
```c
14+
__CPROVER_frees(*targets*)
15+
```
16+
17+
Where targets have the following syntax:
18+
19+
```
20+
targets ::= conditional-target-group (';' conditional-target-group)* ';'?
21+
conditional-target-group ::= (condition ':')? target (',' target)*
22+
target ::= pointer-typed-expression
23+
| f(args)
24+
```
25+
Where `f` is a built-in or user-defined function returning the built-in type
26+
`__CPROVER_freeable_t`.
27+
28+
### Pointer-typed targets
29+
30+
Pointer-typed targets can be directly listed in the clause.
31+
32+
In a function contract:
33+
```c
34+
int foo(char *arr1, char *arr2, size_t size)
35+
__CPROVER_frees(
36+
// arr1 freeable only if the condition holds
37+
size > 0 && arr1: arr1;
38+
// arr2 always freeable
39+
arr2
40+
)
41+
{
42+
if(size > 0 && arr1)
43+
free(arr1);
44+
free(arr2);
45+
return 0;
46+
}
47+
```
48+
49+
In a loop contract:
50+
51+
```c
52+
int main()
53+
{
54+
size_t size = 10;
55+
char *arr = malloc(size);
56+
57+
for(size_t i = 0; i <= size; i++)
58+
// clang-format off
59+
__CPROVER_assigns(i, __CPROVER_POINTER_OBJECT(arr))
60+
__CPROVER_frees(arr)
61+
// clang-format on2
62+
{
63+
if(i < size)
64+
arr[i] = 0;
65+
else
66+
free(arr);
67+
}
68+
return 0;
69+
}
70+
```
71+
72+
## Using calls to __CPROVER_freable_t functions as targets
73+
74+
Users can define parametric sets of freeable pointers by writing functions that
75+
return the built-in type `__CPROVER_freeable_t`.
76+
77+
```c
78+
__CPROVER_freeable_t my_freeable_set(char **arr, size_t size)
79+
{
80+
if (arr && size > 3) {
81+
__CPROVER_freeable(arr[0]);
82+
__CPROVER_freeable(arr[1]);
83+
__CPROVER_freeable(arr[2]);
84+
}
85+
}
86+
```
87+
88+
In such functions, calls to the built-in function:
89+
90+
```c
91+
__CPROVER_freeable_t __CPROVER_freeable(void *ptr);
92+
```
93+
94+
add the given pointer to the freeable set described by the enclosing function.
95+
One can also call user-defined functions returning `__CPROVER_freeable_t`, which
96+
also add their targets to the set defined by the enclosing function.
97+
98+
Calls to `__CPROVER_freeable_t` functions are accepted as targets in
99+
`__CPROVER_frees` clauses:
100+
101+
```c
102+
void my_function(char **arr, size_t size)
103+
// adds the locations defined by my_freeable_set to the freeable locations for
104+
// my_function.
105+
__CPROVER_frees(my_freeable_set(arr, size))
106+
{
107+
...
108+
}
109+
```
110+
## Semantics
111+
112+
The set of pointers specified by the frees clause of the contract is interpreted
113+
at the function call-site for function contracts, and right before entering the
114+
loop for loop contracts.
115+
116+
#### For contract checking
117+
When checking a contract against a function or a loop, each pointer that the
118+
function or loop body attempts to free gets checked for membership in the set of
119+
pointers specified by the contract.
120+
121+
#### For replacement of function calls or loops by contracts
122+
When replacing a function call or a loop by a contract, each pointer of the
123+
_frees_ clause is non-deterministically freed after the function call
124+
or after the loop.
125+
126+
127+
# Frees clauses related predicates
128+
Two predicates meant to express pre and post conditions about freeable pointers
129+
are provided to the user.
130+
131+
## For preconditions
132+
133+
```c
134+
__CPROVER_bool __CPROVER_is_freeable(void *ptr);
135+
```
136+
can only be used in preconditions, in contract checking or replacement
137+
modes. It returns `true` if and only if the pointer satisfies the preconditions
138+
of the `free` function from `stdlib.h`, that is if and only if the pointer
139+
points to a valid dynamically allocated object and has offset zero.
140+
141+
## For postconditions
142+
143+
```c
144+
__CPROVER_bool __CPROVER_is_freed(void *ptr);
145+
```
146+
can only be used in post conditions and returns `true` if and only if the
147+
pointer was effectively freed during the execution of the function or the
148+
loop under analysis.
149+
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
#include <stdlib.h>
2+
3+
// A function defining a conditionally freeable target
4+
__CPROVER_freeable_t
5+
foo_frees(char *arr, const size_t size, const size_t new_size)
6+
{
7+
__CPROVER_freeable(arr);
8+
}
9+
10+
char *foo(char *arr, const size_t size, const size_t new_size)
11+
// clang-format off
12+
// error is_freed cannot be used in preconditions
13+
__CPROVER_requires(!__CPROVER_is_freed(arr))
14+
__CPROVER_requires(__CPROVER_is_freeable(arr))
15+
__CPROVER_assigns(__CPROVER_object_whole(arr))
16+
__CPROVER_frees(foo_frees(arr, size, new_size))
17+
__CPROVER_ensures(
18+
(arr && new_size > size) ==>
19+
__CPROVER_is_fresh(__CPROVER_return_value, new_size))
20+
__CPROVER_ensures(
21+
(arr && new_size > size) ==>
22+
__CPROVER_is_freed(__CPROVER_old(arr)))
23+
__CPROVER_ensures(
24+
!(arr && new_size > size) ==>
25+
__CPROVER_return_value == __CPROVER_old(arr))
26+
// clang-format on
27+
{
28+
if(arr && new_size > size)
29+
{
30+
free(arr);
31+
return malloc(new_size);
32+
}
33+
else
34+
{
35+
return arr;
36+
}
37+
}
38+
39+
int main()
40+
{
41+
size_t size;
42+
size_t new_size;
43+
char *arr = malloc(size);
44+
arr = foo(arr, size, new_size);
45+
return 0;
46+
}
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-contract foo
4+
^main.c.* error: __CPROVER_is_freed is not allowed in preconditions.$
5+
^CONVERSION ERROR$
6+
^EXIT=(1|64)$
7+
^SIGNAL=0$
8+
--
9+
--
10+
This test checks that the front end rejects __CPROVER_is_freed in preconditions.
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
#include <stdlib.h>
2+
3+
// A function defining a conditionally freeable target
4+
void foo_frees(char *arr, const size_t size, const size_t new_size)
5+
{
6+
__CPROVER_freeable(arr);
7+
}
8+
9+
char *foo(char *arr, const size_t size, const size_t new_size)
10+
// clang-format off
11+
__CPROVER_requires(__CPROVER_is_freeable(arr))
12+
__CPROVER_assigns(__CPROVER_object_whole(arr))
13+
__CPROVER_frees(foo_frees(arr, size, new_size))
14+
__CPROVER_ensures(
15+
(arr && new_size > size) ==>
16+
__CPROVER_is_fresh(__CPROVER_return_value, new_size))
17+
__CPROVER_ensures(
18+
(arr && new_size > size) ==>
19+
__CPROVER_is_freed(__CPROVER_old(arr)))
20+
__CPROVER_ensures(
21+
!(arr && new_size > size) ==>
22+
__CPROVER_return_value == __CPROVER_old(arr))
23+
// clang-format on
24+
{
25+
if(arr && new_size > size)
26+
{
27+
free(arr);
28+
return malloc(new_size);
29+
}
30+
else
31+
{
32+
return arr;
33+
}
34+
}
35+
36+
int main()
37+
{
38+
size_t size;
39+
size_t new_size;
40+
char *arr = malloc(size);
41+
arr = foo(arr, size, new_size);
42+
return 0;
43+
}
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-contract foo
4+
^main.c.* error: expecting __CPROVER_freeable_t return type for function foo_frees called in frees clause$
5+
^CONVERSION ERROR$
6+
^EXIT=(1|64)$
7+
^SIGNAL=0$
8+
--
9+
--
10+
This test checks that the front-end rejects non-__CPROVER_freeable_t-typed
11+
function calls in frees clauses.
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
#include <stdlib.h>
2+
3+
// A function defining a freeable target
4+
__CPROVER_freeable_t
5+
foo_frees(char *arr, const size_t size, const size_t new_size)
6+
{
7+
__CPROVER_freeable(arr);
8+
}
9+
10+
char *foo(char *arr, const size_t size, const size_t new_size)
11+
// clang-format off
12+
__CPROVER_requires(__CPROVER_is_freeable(arr))
13+
__CPROVER_assigns(__CPROVER_object_whole(arr))
14+
__CPROVER_frees(foo_frees(arr, size, new_size))
15+
__CPROVER_ensures(
16+
(arr && new_size > size) ==>
17+
__CPROVER_is_fresh(__CPROVER_return_value, new_size))
18+
__CPROVER_ensures(
19+
(arr && new_size > size) ==>
20+
__CPROVER_is_freed(__CPROVER_old(arr)))
21+
__CPROVER_ensures(
22+
!(arr && new_size > size) ==>
23+
__CPROVER_return_value == __CPROVER_old(arr))
24+
// clang-format on
25+
{
26+
if(arr && new_size > size)
27+
{
28+
free(arr);
29+
return malloc(new_size);
30+
}
31+
else
32+
{
33+
return arr;
34+
}
35+
}
36+
37+
int main()
38+
{
39+
size_t size;
40+
size_t new_size;
41+
char *arr = malloc(size);
42+
arr = foo(arr, size, new_size);
43+
return 0;
44+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
CORE
2+
main.c
3+
--enforce-contract foo
4+
^\[foo.postcondition.\d+\] line \d+ Check ensures clause: FAILURE$
5+
^VERIFICATION FAILED$
6+
^EXIT=10$
7+
^SIGNAL=0$
8+
--
9+
--
10+
This test checks that the front end parses and typchecks correct uses of:
11+
- __CPROVER_freeable_t function calls as frees clause targets
12+
- the predicate __CPROVER_freeable
13+
- the predicate __CPROVER_is_freeable
14+
- the predicate __CPROVER_is_freed
15+
16+
The post condition of the contract is expected to fail because the predicates
17+
have no interpretation in the back-end yet.
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#include <stdlib.h>
2+
int main()
3+
{
4+
size_t size = 10;
5+
char *arr = malloc(size);
6+
7+
for(size_t i = 0; i <= size; i++)
8+
// clang-format off
9+
__CPROVER_assigns(i, arr[2], __CPROVER_POINTER_OBJECT(arr))
10+
__CPROVER_frees(arr[2])
11+
// clang-format on
12+
{
13+
if(i == 2)
14+
arr[i] = 0;
15+
if(i == size)
16+
free(arr);
17+
}
18+
return 0;
19+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
CORE
2+
main.c
3+
--apply-loop-contracts
4+
^main.c.* error: frees clause target must be a pointer-typed expression or a call to a function returning __CPROVER_freeable_t$
5+
^CONVERSION ERROR$
6+
^EXIT=(1|64)$
7+
^SIGNAL=0$
8+
--
9+
--
10+
This test is expected trigger a typchecking error on a frees clause target.
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#include <stdlib.h>
2+
int main()
3+
{
4+
size_t size = 10;
5+
char *arr = malloc(size);
6+
7+
for(size_t i = 0; i <= size; i++)
8+
// clang-format off
9+
__CPROVER_assigns(i, arr[2], __CPROVER_POINTER_OBJECT(arr))
10+
__CPROVER_frees(arr)
11+
// clang-format on
12+
{
13+
if(i == 2)
14+
arr[i] = 0;
15+
if(i == size)
16+
free(arr);
17+
}
18+
return 0;
19+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
CORE
2+
main.c
3+
--apply-loop-contracts
4+
^VERIFICATION SUCCESSFUL$
5+
^EXIT=0$
6+
^SIGNAL=0$
7+
--
8+
--
9+
This test checks that frees clauses are parsed and typechecked for for loops.

0 commit comments

Comments
 (0)