|
| 1 | +# Rewriting Declarative Assign and Frees Specification Functions {#contracts-dev-spec-memory-predicates-rewriting} |
| 2 | + |
| 3 | +Back to top @ref contracts-dev-spec |
| 4 | + |
| 5 | +@tableofcontents |
| 6 | + |
| 7 | +The C extensions for contract specification provide three pointer-related memory |
| 8 | +predicates: |
| 9 | + |
| 10 | +```c |
| 11 | +// Holds iff ptr is pointing to an object distinct to all objects pointed to by |
| 12 | +// other __CPROVER_is_fresh occurrences in the contract's pre and post conditions |
| 13 | +__CPROVER_bool __CPROVER_is_fresh(void *ptr, size_t size); |
| 14 | + |
| 15 | +// Holds iff ptr is a valid pointer pointing between lb and ub pointers. |
| 16 | +// \pre lb and ub must be valid pointers pointing in the same object. |
| 17 | +__CPROVER_bool __CPROVER_in_range(void *ptr, void *lb, void *ub); |
| 18 | + |
| 19 | +// Holds iff the function pointer \p fptr points to a function satisfying |
| 20 | +// \p contract. |
| 21 | +__CPROVER_bool __CPROVER_obeys_contract(void (*fptr)(void), void (*contract)(void)); |
| 22 | +``` |
| 23 | +
|
| 24 | +Users are free to express pre and post conditions using these predicates, |
| 25 | +and we let users define their own functions in terms of these predicates. |
| 26 | +
|
| 27 | +
|
| 28 | +For instance, one could write a predicate defining linked lists of at most `len` |
| 29 | +elements as follows: |
| 30 | +
|
| 31 | +```c |
| 32 | +struct list_t { |
| 33 | + int value; |
| 34 | + struct list_t *next; |
| 35 | +}; |
| 36 | +
|
| 37 | +bool is_list(struct list_t *ptr, size_t len) |
| 38 | +{ |
| 39 | + if(ptr) |
| 40 | + return len > 0 && |
| 41 | + __CPROVER_is_fresh(ptr, sizeof(list_t)) && is_list(ptr->next, len-1)); |
| 42 | + else |
| 43 | + return 0; |
| 44 | +} |
| 45 | +``` |
| 46 | + |
| 47 | +One can also simply describe finite structures: |
| 48 | + |
| 49 | +```c |
| 50 | +struct buffer_t { |
| 51 | + size_t size; |
| 52 | + char *arr; |
| 53 | + char *cursor; |
| 54 | +}; |
| 55 | + |
| 56 | +struct two_buffers_t { |
| 57 | + struct buffer_t *left; |
| 58 | + struct buffer_t *right; |
| 59 | +}; |
| 60 | + |
| 61 | +bool is_buffer(struct buffer_t *ptr) |
| 62 | +{ |
| 63 | + return |
| 64 | + __CPROVER_is_fresh(ptr, sizeof(struct buffer_t)) && |
| 65 | + __CPROVER_is_fresh(ptr->arr, ptr->size) && |
| 66 | + __CPROVER_in_range(ptr->cursor, ptr->arr, ptr->arr + size-1); |
| 67 | +} |
| 68 | + |
| 69 | +bool is_two_buffers(struct two_buffers_t *ptr) |
| 70 | +{ |
| 71 | + return |
| 72 | + __CPROVER_is_fresh(ptr, sizeof(struct two_buffers_t)) && |
| 73 | + is_buffer(ptr->left) && |
| 74 | + is_buffer(ptr->right); |
| 75 | +} |
| 76 | +``` |
| 77 | +
|
| 78 | +By rewriting such user-defined predicate we achieve two things: |
| 79 | +1. in assumption contexts, evaluating the predicate allocates the data stucture |
| 80 | + specified by the predicate; |
| 81 | +1. in assertion contexts, evaluating the predicate checks that a given pointer |
| 82 | + satisfies the predicate definiton; |
| 83 | +
|
| 84 | +To be able to achieve point 1., the user-defined functions need to be |
| 85 | +transformed into functions that take their pointer arguments by reference |
| 86 | +instead of by value, so that enforcing the assumption described by the |
| 87 | +predicate can be done by updating the pointer in place using a side effect. |
| 88 | +
|
| 89 | +## Collecting user-defined memory predicates {#contracts-dev-spec-memory-predicate-collect} |
| 90 | +
|
| 91 | +We first run a pass that collects all user-defined functions that are defined |
| 92 | +in terms of one of the three memory predicates using this fixpoint algorithm: |
| 93 | +
|
| 94 | +``` |
| 95 | +functions_to_lift = |
| 96 | + {`__CPROVER_is_fresh`, `__CPROVER_in_range`, `__CPROVER_obeys_contract`}; |
| 97 | + |
| 98 | +updated = true; |
| 99 | +while(updated) |
| 100 | +{ |
| 101 | + updated = false; |
| 102 | + for(function : goto_model) |
| 103 | + { |
| 104 | + if(!contains(function_to_lift, function) && |
| 105 | + calls_one_of(function, functions_to_lift) &&) |
| 106 | + { |
| 107 | + rewrite(function); |
| 108 | + functions_to_lift.insert(function); |
| 109 | + updated = true; |
| 110 | + } |
| 111 | + } |
| 112 | +} |
| 113 | +``` |
| 114 | +
|
| 115 | +## Rewriting user-defined memory predicates {#contracts-dev-spec-memory-predicate-collect} |
| 116 | +
|
| 117 | +Rewriting a user-defined memory predicate consists in: |
| 118 | +- Adding an extra write set parameter to the predicate signature; |
| 119 | +- identifying the subset of its pointer-typed arguments which eventually gets |
| 120 | + passed to a built-in or user-defined memory predicate in a lifted parameter position; Let's call this subset `Lifted`; |
| 121 | +- in the signature of the function, lift the types of all parameters of `Lifted` into pointer-to-pointer types; |
| 122 | +- rewrite all occurences of a lifted parameter `p` into `*p`; |
| 123 | +- add an `address_of` operator to all arguments passed to a lifted parameter of a memory predicate; |
| 124 | +- pass the write set as ghost parameter to function calls; |
| 125 | +- rewrite calls to built-in predicates to call their library implementation; |
| 126 | +
|
| 127 | +
|
| 128 | +This yields the following result |
| 129 | +```c |
| 130 | +bool is_list( |
| 131 | + struct list_t **ptr, |
| 132 | + size_t len, |
| 133 | + __CPROVER_contracts_write_set_ptr_t __write_set) |
| 134 | +{ |
| 135 | + if(*ptr) |
| 136 | + { |
| 137 | + return (len > 0 && |
| 138 | + __CPROVER_contracts_is_fresh(ptr, sizeof(list_t), __write_set) && |
| 139 | + is_list(&((*ptr)->next), len-1, __write_set)); |
| 140 | + } |
| 141 | + else |
| 142 | + } |
| 143 | + return 0; |
| 144 | + } |
| 145 | +} |
| 146 | +``` |
| 147 | + |
| 148 | +```c |
| 149 | +bool is_buffer(struct buffer_t **ptr, |
| 150 | +__CPROVER_contracts_write_set_ptr_t __write_set) |
| 151 | +{ |
| 152 | + return |
| 153 | + __CPROVER_contracts_is_fresh( |
| 154 | + ptr, sizeof(struct buffer_t), __write_set) && |
| 155 | + __CPROVER_contracts_is_fresh( |
| 156 | + &((*ptr)->arr), (*ptr)->size, __write_set) && |
| 157 | + __CPROVER_contracts_in_range( |
| 158 | + &((*ptr)->cursor), (*ptr)->arr, (*ptr)->arr + size-1, __write_set); |
| 159 | +} |
| 160 | + |
| 161 | +bool is_two_buffers(struct two_buffers_t **ptr, |
| 162 | +__CPROVER_contracts_write_set_ptr_t __write_set) |
| 163 | +{ |
| 164 | + return |
| 165 | + __CPROVER_contracts_is_fresh( |
| 166 | + ptr, sizeof(struct two_buffers_t), __write_set) && |
| 167 | + is_buffer(&((*ptr)->left), __write_set) && |
| 168 | + is_buffer(&((*ptr)->right), __write_set); |
| 169 | +} |
| 170 | +``` |
| 171 | +
|
| 172 | +--- |
| 173 | + Prev | Next |
| 174 | +:-----|:------ |
| 175 | + @ref contracts-dev-spec-spec-rewriting | @ref contracts-dev-spec-dfcc |
0 commit comments