Debian Codegraph @

crt/s2n/tests/cbmc/proofs/s2n_stuffer_printf/s2n_stuffer_printf_harness.c

raw symbols SCIP
1 /*
2 * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License").
5 * You may not use this file except in compliance with the License.
6 * A copy of the License is located at
7 *
8 * http://aws.amazon.com/apache2.0
9 *
10 * or in the "license" file accompanying this file. This file is distributed
11 * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
12 * express or implied. See the License for the specific language governing
13 * permissions and limitations under the License.
14 */
15
16 #include <assert.h>
17 #include <cbmc_proof/cbmc_utils.h>
18 #include <cbmc_proof/make_common_datastructures.h>
19
20 #include "api/s2n.h"
21 #include "stuffer/s2n_stuffer.h"
22 #include "utils/s2n_mem.h"
23
24 int nondet_int(void);
25
26 /* The MacOS.sdk defines a macro called "vsnprintf" which breaks the
27 * function definition below, so make sure it's undef'd here.
28 */
29 #undef vsnprintf
30
31 int vsnprintf(char *str, size_t size, const char *fmt, va_list ap)
32 {
33 if (size > 0)
34 __CPROVER_havoc_slice(str, size);
35 (void) *fmt;
36 if (__CPROVER_OBJECT_SIZE(ap) > 0)
37 (void) *(char **) ap;
38 return nondet_int();
39 }
40
41 void s2n_stuffer_printf_harness()
42 {
43 nondet_s2n_mem_init();
44
45 struct s2n_stuffer *stuffer = cbmc_allocate_s2n_stuffer();
46 __CPROVER_assume(s2n_result_is_ok(s2n_stuffer_validate(stuffer)));
47
48 size_t format_len;
49 char *format = ensure_c_str_is_allocated(format_len);
50
51 /* CBMC defines va_list as void** */
52 size_t va_list_size;
53 __CPROVER_assume(va_list_size % sizeof(void *) == 0);
54 void **va_list_mem = malloc(va_list_size);
55 __CPROVER_assume(va_list_mem != NULL);
56
57 /* Store the stuffer to compare after the write */
58 struct s2n_stuffer old_stuffer = *stuffer;
59 struct store_byte_from_buffer old_byte;
60 save_byte_from_array(old_stuffer.blob.data, old_stuffer.write_cursor, &old_byte);
61
62 int result = s2n_stuffer_vprintf(stuffer, format, va_list_mem);
63 assert(s2n_result_is_ok(s2n_stuffer_validate(stuffer)));
64
65 /* The basic stuffer fields should NOT be updated */
66 assert(old_stuffer.growable == stuffer->growable);
67 assert(old_stuffer.tainted == stuffer->tainted);
68 assert(old_stuffer.alloced == stuffer->alloced);
69
70 /* Any previously written data should NOT be updated */
71 if (old_stuffer.write_cursor > 0) {
72 assert_byte_from_buffer_matches(stuffer->blob.data, &old_byte);
73 }
74
75 /* The read cursor should NOT be updated */
76 assert(old_stuffer.read_cursor == stuffer->read_cursor);
77
78 /* The write cursor should only be updated on success */
79 if (result == S2N_SUCCESS) {
80 assert(old_stuffer.write_cursor <= stuffer->write_cursor);
81 } else {
82 assert(old_stuffer.write_cursor == stuffer->write_cursor);
83 }
84 }