1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
|
#include <stdlib.h>
// Type of functions that manipulate arrays of a given size
typedef void (*arr_fun_t)(char *arr, size_t size);
// A contract for the arr_fun_t type
void arr_fun_contract(char *arr, size_t size)
// clang-format off
__CPROVER_requires(size > 0 && __CPROVER_is_fresh(arr, size))
__CPROVER_assigns(arr[0]) __CPROVER_ensures(arr[0] == 0)
// clang-format on
;
// Uses a function pointer
int foo(char *arr, size_t size, arr_fun_t arr_fun)
// clang-format off
__CPROVER_requires(arr ==> __CPROVER_is_fresh(arr, size))
__CPROVER_requires(
arr_fun ==> __CPROVER_obeys_contract(arr_fun, arr_fun_contract))
__CPROVER_assigns(arr && size > 0 && arr_fun: arr[0])
__CPROVER_ensures(__CPROVER_return_value ==> (arr[0] == 0))
// clang-format on
{
if(arr && size > 0 && arr_fun)
{
CALL:
arr_fun(arr, size);
return 1;
}
return 0;
}
// returns function pointer satisfying arr_fun_contract
arr_fun_t get_arr_fun()
// clang-format off
__CPROVER_ensures(
__CPROVER_obeys_contract(__CPROVER_return_value, arr_fun_contract))
// clang-format on
;
// allocates an array and uses get_arr_fun and foo to initialise it
char *bar(size_t size)
// clang-format off
__CPROVER_ensures(
__CPROVER_return_value ==> size > 0 &&
__CPROVER_is_fresh(__CPROVER_return_value, size) &&
__CPROVER_return_value[0] == 0)
// clang-format on
{
if(size > 0)
{
char *arr;
arr = malloc(size);
if(arr && foo(arr, size, get_arr_fun()))
return arr;
return NULL;
}
return NULL;
}
void main()
{
size_t size;
char *arr = bar(size);
}
|