Debian Codegraph @

crt/aws-c-common/verification/cbmc/stubs/aws_byte_cursor_read_be16_override.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/byte_buf.h>
7 #include <proof_helpers/nondet.h>
8
9 /**
10 * The signature for the value generator, if it is used.
11 */
12 #ifdef AWS_BYTE_CURSOR_READ_BE16_GENERATOR
13 uint16_t AWS_BYTE_CURSOR_READ_BE16_GENERATOR(const struct aws_byte_cursor *cursor);
14 #endif
15
16 /**
17 * This function is used in deserializing values from a byte cursor.
18 * Sometimes, for CBMC proof, it is expected that certain values in byte stream are constrained.
19 * For example, in the aws_cryptosdk_enc_ctx_deserilize() proof, the first value we read is the number of elements,
20 * which we need to be constrained in order to ensure that CBMC can fully unwind all loops. All other values can be left
21 * nondet. In this case, define -DAWS_BYTE_CURSOR_READ_BE16_GENERATOR=generator_name, and the correct generator will be
22 * called. If there is no structure that must be followed, AWS_BYTE_CURSOR_READ_BE16_GENERATOR can be left undefined,
23 * and the var will be set to a nondet value.
24 */
25 bool aws_byte_cursor_read_be16(struct aws_byte_cursor *cursor, uint16_t *var) {
26 AWS_PRECONDITION(aws_byte_cursor_is_valid(cursor));
27 AWS_PRECONDITION(AWS_OBJECT_PTR_IS_WRITABLE(var));
28
29 #ifdef AWS_BYTE_CURSOR_READ_BE16_GENERATOR
30 *var = AWS_BYTE_CURSOR_READ_BE16_GENERATOR(cursor);
31 #else
32 *var = nondet_uint16_t();
33 #endif
34
35 const size_t len = sizeof(uint16_t);
36 /* If there are not 2 bytes left, or if we nondet fail */
37 if (cursor->len > (SIZE_MAX >> 1) || len > (SIZE_MAX >> 1) || len > cursor->len || nondet_bool()) {
38 AWS_POSTCONDITION(aws_byte_cursor_is_valid(cursor));
39 return false;
40 }
41
42 /* Otherwise, succeed */
43 cursor->ptr += len;
44 cursor->len -= len;
45 AWS_POSTCONDITION(aws_byte_cursor_is_valid(cursor));
46 return true;
47 }