Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 742f4ce

Browse files
authoredNov 25, 2022
Merge pull request #7355 from remi-delmas-3000/obeys-contract-predicate
CONTRACTS: add __CPROVER_obeys_contract predicate
2 parents c988532 + 272ebf0 commit 742f4ce

File tree

48 files changed

+1266
-119
lines changed

Some content is hidden

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

48 files changed

+1266
-119
lines changed
 
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
#include <stdbool.h>
2+
#include <stdlib.h>
3+
4+
// type of functions that manipulate arrays
5+
typedef void (*arr_fun_t)(char *arr, size_t size);
6+
7+
// A contract for the arr_fun_t type
8+
// requires a fresh array and positive size
9+
// resets the first element to zero
10+
void arr_fun_contract(char *arr, size_t size)
11+
// clang-format off
12+
__CPROVER_requires(size > 0 && __CPROVER_is_fresh(arr, size))
13+
__CPROVER_assigns(arr[0])
14+
__CPROVER_ensures(arr[0] == 0)
15+
// clang-format on
16+
;
17+
18+
// Testing pre-conditions constructs
19+
// Takes a function pointer as input, uses it if its preconditions are met
20+
// to establish post-conditions
21+
int foo(char *arr, size_t size, arr_fun_t arr_fun)
22+
// clang-format off
23+
__CPROVER_requires(arr == NULL || __CPROVER_is_fresh(arr, size))
24+
__CPROVER_requires(__CPROVER_obeys_contract(arr_fun, arr_fun_contract))
25+
__CPROVER_assigns(arr && size > 0 : arr[0])
26+
__CPROVER_ensures((__CPROVER_return_value == 0) ==> (arr[0] == 0))
27+
// clang-format on
28+
{
29+
int retval = -1;
30+
if(arr && size > 0)
31+
{
32+
CALL:
33+
arr_fun(arr, size);
34+
retval = 0;
35+
}
36+
return retval;
37+
}
38+
39+
void main()
40+
{
41+
size_t size;
42+
char *arr;
43+
arr_fun_t fun;
44+
foo(arr, size, fun);
45+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
CORE
2+
main.c
3+
--restrict-function-pointer foo.CALL/arr_fun_contract --dfcc main --enforce-contract foo
4+
^EXIT=0$
5+
^SIGNAL=0$
6+
^VERIFICATION SUCCESSFUL$
7+
--
8+
--
9+
Checks that when a function taking a function pointer as input satisfying a given
10+
contract is checked against its contract, the function pointer can be called
11+
like a regular function pointer and establishes the post conditions specified
12+
by its contract.

0 commit comments

Comments
 (0)
Please sign in to comment.