Debian Codegraph @

crt/aws-c-common/verification/cbmc/stubs/aws_hash_iter_overrides.c

raw symbols SCIP
1 #include <aws/common/hash_table.h>
2 #include <aws/common/math.h>
3 #include <aws/common/private/hash_table_impl.h>
4 #include <aws/common/string.h>
5
6 #include <limits.h>
7 #include <proof_helpers/make_common_data_structures.h>
8 #include <proof_helpers/utils.h>
9 #include <stdio.h>
10 #include <stdlib.h>
11
12 /* If the consumer of the iterator doesn't use the .element in the iterator, we can just leave it undef
13 * This is sound, as it gives you a totally nondet value every time you call the iterator, and is the default behaviour
14 * of CBMC. But if it is used, we need a way for the harness to specify valid values for the element, for example if
15 * they are copying values out of the table. They can do this by defining
16 * -DHASH_ITER_ELEMENT_GENERATOR=the_generator_fn, where the_generator_fn has signature:
17 * the_generator_fn(struct aws_hash_iter *new_iter, const struct aws_hash_iter* old_iter).
18 *
19 * [new_iter] is a pointer to the iterator that will be returned from this function, and the generator function can
20 * modify it in any way it sees fit. In particular, it can update the [new_iter->element] field to be valid for the
21 * type of hash-table being proved. Two obvious generators are:
22 * (a) one that simply creates a new non-determinsitic value each time its called using malloc
23 * (b) one that uses a value stored in the underlying map, and copies it into the iterator.
24 *
25 * [old_iter] is a pointer to the old iterator, in the case of a "aws_hash_iter_next" call, and null in the case of
26 * "aws_hash_iter_begin".
27 */
28 #ifdef HASH_ITER_ELEMENT_GENERATOR
29 void HASH_ITER_ELEMENT_GENERATOR(struct aws_hash_iter *new_iter, const struct aws_hash_iter *old_iter);
30 #endif
31
32 /* Simple map for what the iterator does: it just chugs along, returns a nondet value, until its gone at most map->size
33 * times */
34 struct aws_hash_iter aws_hash_iter_begin(const struct aws_hash_table *map) {
35 /* Leave it as non-det as possible */
36 AWS_PRECONDITION(aws_hash_table_is_valid(map));
37
38 /* Build a nondet iterator, set the required fields, and return it */
39 struct aws_hash_iter rval;
40 rval.map = map;
41 rval.limit = map->p_impl->size;
42 __CPROVER_assume(rval.slot <= rval.limit);
43 rval.status = (rval.slot == rval.limit) ? AWS_HASH_ITER_STATUS_DONE : AWS_HASH_ITER_STATUS_READY_FOR_USE;
44 #ifdef HASH_ITER_ELEMENT_GENERATOR
45 HASH_ITER_ELEMENT_GENERATOR(&rval, NULL);
46 #endif
47 return rval;
48 }
49
50 bool aws_hash_iter_done(const struct aws_hash_iter *iter) {
51 AWS_PRECONDITION(
52 iter->status == AWS_HASH_ITER_STATUS_DONE || iter->status == AWS_HASH_ITER_STATUS_READY_FOR_USE,
53 "Input aws_hash_iter [iter] status should either be done or ready to use.");
54 bool rval = iter->slot == iter->limit;
55 assert(rval == (iter->status == AWS_HASH_ITER_STATUS_DONE));
56 return rval;
57 }
58
59 void aws_hash_iter_next(struct aws_hash_iter *iter) {
60 if (iter->slot == iter->limit) {
61 iter->status = AWS_HASH_ITER_STATUS_DONE;
62 return;
63 }
64
65 /* Build a nondet iterator, set the required fields, and copy it over */
66 struct aws_hash_iter rval;
67 rval.map = iter->map;
68 rval.limit = iter->limit;
69 size_t next_step;
70 __CPROVER_assume(next_step > 0 && next_step <= iter->limit - iter->slot);
71 rval.limit = iter->limit;
72 rval.slot = iter->slot + next_step;
73 rval.status = (rval.slot == rval.limit) ? AWS_HASH_ITER_STATUS_DONE : AWS_HASH_ITER_STATUS_READY_FOR_USE;
74 #ifdef HASH_ITER_ELEMENT_GENERATOR
75 HASH_ITER_ELEMENT_GENERATOR(&rval, iter);
76 #endif
77
78 *iter = rval;
79 }
80
81 /* delete always leaves the element unusable, so we'll just leave the element totally nondet */
82 void aws_hash_iter_delete(struct aws_hash_iter *iter, bool destroy_contents) {
83 /* Build a nondet iterator, set the required fields, and copy it over */
84 struct aws_hash_iter rval;
85 rval.map = iter->map;
86 rval.slot = iter->slot;
87 rval.limit = iter->limit - 1;
88 rval.status = AWS_HASH_ITER_STATUS_DELETE_CALLED;
89 rval.map->p_impl->entry_count = iter->map->p_impl->entry_count;
90 if (rval.map->p_impl->entry_count)
91 rval.map->p_impl->entry_count--;
92 *iter = rval;
93 }