Debian Codegraph @

crt/aws-c-common/verification/cbmc/sources/utils.c

raw symbols SCIP
1 /**
2 * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3 * SPDX-License-Identifier: Apache-2.0.
4 */
5
6 #include <aws/common/private/hash_table_impl.h>
7 #include <proof_helpers/utils.h>
8
9 void assert_bytes_match(const uint8_t *const a, const uint8_t *const b, const size_t len) {
10 assert(len == 0 || !a == !b);
11 if (len > 0 && a != NULL && b != NULL) {
12 size_t i;
13 __CPROVER_assume(i < len && len < MAX_MALLOC); /* prevent spurious pointer overflows */
14 assert(a[i] == b[i]);
15 }
16 }
17
18 void assert_all_bytes_are(const uint8_t *const a, const uint8_t c, const size_t len) {
19 if (len > 0 && a != NULL) {
20 size_t i;
21 __CPROVER_assume(i < len);
22 assert(a[i] == c);
23 }
24 }
25
26 void assert_all_zeroes(const uint8_t *const a, const size_t len) {
27 assert_all_bytes_are(a, 0, len);
28 }
29
30 void assert_byte_from_buffer_matches(const uint8_t *const buffer, const struct store_byte_from_buffer *const b) {
31 if (buffer && b) {
32 assert(*(buffer + b->index) == b->byte);
33 }
34 }
35
36 void save_byte_from_array(const uint8_t *const array, const size_t size, struct store_byte_from_buffer *const storage) {
37 if (size > 0 && array && storage) {
38 storage->index = nondet_size_t();
39 __CPROVER_assume(storage->index < size);
40 storage->byte = array[storage->index];
41 }
42 }
43
44 void assert_array_list_equivalence(
45 const struct aws_array_list *const lhs,
46 const struct aws_array_list *const rhs,
47 const struct store_byte_from_buffer *const rhs_byte) {
48 /* In order to be equivalent, either both are NULL or both are non-NULL */
49 if (lhs == rhs) {
50 return;
51 } else {
52 assert(lhs && rhs); /* if only one is null, they differ */
53 }
54 assert(lhs->alloc == rhs->alloc);
55 assert(lhs->current_size == rhs->current_size);
56 assert(lhs->length == rhs->length);
57 assert(lhs->item_size == rhs->item_size);
58 if (lhs->current_size > 0) {
59 assert_byte_from_buffer_matches((uint8_t *)lhs->data, rhs_byte);
60 }
61 }
62
63 void assert_byte_buf_equivalence(
64 const struct aws_byte_buf *const lhs,
65 const struct aws_byte_buf *const rhs,
66 const struct store_byte_from_buffer *const rhs_byte) {
67 /* In order to be equivalent, either both are NULL or both are non-NULL */
68 if (lhs == rhs) {
69 return;
70 } else {
71 assert(lhs && rhs); /* if only one is null, they differ */
72 }
73 assert(lhs->len == rhs->len);
74 assert(lhs->capacity == rhs->capacity);
75 assert(lhs->allocator == rhs->allocator);
76 if (lhs->len > 0) {
77 assert_byte_from_buffer_matches(lhs->buffer, rhs_byte);
78 }
79 }
80
81 void assert_byte_cursor_equivalence(
82 const struct aws_byte_cursor *const lhs,
83 const struct aws_byte_cursor *const rhs,
84 const struct store_byte_from_buffer *const rhs_byte) {
85 assert(!lhs == !rhs);
86 if (lhs && rhs) {
87 assert(lhs->len == rhs->len);
88 if (lhs->len > 0) {
89 assert_byte_from_buffer_matches(lhs->ptr, rhs_byte);
90 }
91 }
92 }
93
94 void save_byte_from_hash_table(const struct aws_hash_table *map, struct store_byte_from_buffer *storage) {
95 struct hash_table_state *state = map->p_impl;
96 size_t size_in_bytes;
97 __CPROVER_assume(hash_table_state_required_bytes(state->size, &size_in_bytes) == AWS_OP_SUCCESS);
98 save_byte_from_array((uint8_t *)state, size_in_bytes, storage);
99 }
100
101 void check_hash_table_unchanged(const struct aws_hash_table *map, const struct store_byte_from_buffer *storage) {
102 struct hash_table_state *state = map->p_impl;
103 uint8_t *byte_array = (uint8_t *)state;
104 assert(byte_array[storage->index] == storage->byte);
105 }
106
107 int nondet_compare(const void *const a, const void *const b) {
108 assert(a != NULL);
109 assert(b != NULL);
110 return nondet_int();
111 }
112
113 int __CPROVER_uninterpreted_compare(const void *const a, const void *const b);
114 int uninterpreted_compare(const void *const a, const void *const b) {
115 assert(a != NULL);
116 assert(b != NULL);
117 int rval = __CPROVER_uninterpreted_compare(a, b);
118 /* Compare is reflexive */
119 __CPROVER_assume(IMPLIES(a == b, rval == 0));
120 /* Compare is anti-symmetric*/
121 __CPROVER_assume(__CPROVER_uninterpreted_compare(b, a) == -rval);
122 /* If two things are equal, their hashes are also equal */
123 if (rval == 0) {
124 __CPROVER_assume(__CPROVER_uninterpreted_hasher(a) == __CPROVER_uninterpreted_hasher(b));
125 }
126 return rval;
127 }
128
129 bool nondet_equals(const void *const a, const void *const b) {
130 assert(a != NULL);
131 assert(b != NULL);
132 return nondet_bool();
133 }
134
135 bool __CPROVER_uninterpreted_equals(const void *const a, const void *const b);
136 uint64_t __CPROVER_uninterpreted_hasher(const void *const a);
137 /**
138 * Add assumptions that equality is reflexive and symmetric. Don't bother with
139 * transitivity because it doesn't cause any spurious proof failures on hash-table
140 */
141 bool uninterpreted_equals(const void *const a, const void *const b) {
142 bool rval = __CPROVER_uninterpreted_equals(a, b);
143 /* Equals is reflexive */
144 __CPROVER_assume(IMPLIES(a == b, rval));
145 /* Equals is symmetric */
146 __CPROVER_assume(__CPROVER_uninterpreted_equals(b, a) == rval);
147 /* If two things are equal, their hashes are also equal */
148 if (rval) {
149 __CPROVER_assume(__CPROVER_uninterpreted_hasher(a) == __CPROVER_uninterpreted_hasher(b));
150 }
151 return rval;
152 }
153
154 bool uninterpreted_equals_assert_inputs_nonnull(const void *const a, const void *const b) {
155 assert(a != NULL);
156 assert(b != NULL);
157 return uninterpreted_equals(a, b);
158 }
159
160 uint64_t nondet_hasher(const void *a) {
161 assert(a != NULL);
162 return nondet_uint64_t();
163 }
164
165 /**
166 * Standard stub function to hash one item.
167 */
168 uint64_t uninterpreted_hasher(const void *a) {
169 assert(a != NULL);
170 return __CPROVER_uninterpreted_hasher(a);
171 }
172
173 bool __CPROVER_uninterpreted_predicate_uint8_t(uint8_t);
174 bool uninterpreted_predicate_fn(uint8_t value) {
175 return __CPROVER_uninterpreted_predicate_uint8_t(value);
176 }