Skip to content

Commit 3363d8c

Browse files
committed
Adds test to code contracts regression
New test checks whether CBMC properly verify function contracts with functions. At this point, we still don't check for purity, i.e., if functions in contracts are only predicates. Signed-off-by: Felipe R. Monteiro <[email protected]>
1 parent 9626a00 commit 3363d8c

File tree

4 files changed

+73
-0
lines changed

4 files changed

+73
-0
lines changed
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#include "utility.h"
2+
#include <stdbool.h>
3+
#include <stdlib.h>
4+
5+
int foo(int *x) __CPROVER_requires(s2n_result_is_ok(validity3(x)))
6+
__CPROVER_assigns(*x) __CPROVER_ensures(
7+
__CPROVER_return_value == *x + 5 && s2n_result_is_ok(validity3(x)))
8+
{
9+
*x = *x + 0;
10+
return *x + 5;
11+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#include "header.h"
2+
3+
int main()
4+
{
5+
int *n = malloc(sizeof(*n));
6+
*n = foo(n);
7+
8+
return 0;
9+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
CORE
2+
main.c
3+
--enforce-all-contracts
4+
^EXIT=0$
5+
^SIGNAL=0$
6+
^VERIFICATION SUCCESSFUL$
7+
--
8+
--
9+
This test checks whether verification succeeds when requires and ensures
10+
contain functions.
11+
12+
Note: We still don't check for function purity, i.e.,
13+
functions in contracts must only work as predicates.
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
#include <stdbool.h>
2+
#include <stdlib.h>
3+
4+
/* Function return code */
5+
#define S2N_SUCCESS 0
6+
#define S2N_FAILURE -1
7+
8+
/* A value which indicates the outcome of a function */
9+
typedef struct
10+
{
11+
int __error_signal;
12+
} s2n_result;
13+
14+
#define S2N_RESULT s2n_result
15+
#define S2N_RESULT_OK ((s2n_result){S2N_SUCCESS})
16+
#define S2N_RESULT_ERROR ((s2n_result){S2N_FAILURE})
17+
18+
bool s2n_result_is_ok(s2n_result result)
19+
{
20+
return result.__error_signal == S2N_SUCCESS;
21+
}
22+
23+
bool validity1(int *x)
24+
{
25+
return (x > 0);
26+
}
27+
28+
bool validity2(int *x)
29+
{
30+
return (x == 0);
31+
}
32+
33+
S2N_RESULT validity3(int *x)
34+
{
35+
if(x == NULL)
36+
return S2N_RESULT_ERROR;
37+
if(!validity1(x))
38+
return S2N_RESULT_ERROR;
39+
return S2N_RESULT_OK;
40+
}

0 commit comments

Comments
 (0)