Debian Codegraph @

crt/s2n/tests/cbmc/proofs/s2n_stuffer_free/s2n_stuffer_free_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
23 void s2n_stuffer_free_harness()
24 {
25 /* Non-deterministic inputs. */
26 struct s2n_stuffer *stuffer = cbmc_allocate_s2n_stuffer();
27
28 /* Assumptions. */
29 nondet_s2n_mem_init();
30 __CPROVER_assume(s2n_result_is_ok(s2n_stuffer_validate(stuffer)));
31 const bool old_alloced = stuffer ? stuffer->alloced : false;
32 struct s2n_blob old_blob;
33 old_blob.data = stuffer ? stuffer->blob.data : NULL;
34 old_blob.growable = stuffer ? stuffer->blob.growable : NULL;
35
36 /* Operation under verification. */
37 int result = s2n_stuffer_free(stuffer);
38 if (result == S2N_SUCCESS && stuffer != NULL) {
39 assert_all_zeroes((const uint8_t *)stuffer, sizeof(*stuffer));
40 } else if (result == S2N_FAILURE && s2n_errno == S2N_ERR_FREE_STATIC_BLOB) {
41 assert(!old_blob.growable);
42 }
43
44 /* Cleanup after expected error cases, for memory leak check. */
45 if ((result == S2N_FAILURE && s2n_errno == S2N_ERR_NOT_INITIALIZED) || !old_blob.growable || !old_alloced) {
46 /**
47 * 1. `s2n_free` failed _before_ calling `free`, or
48 * 2. `stuffer` had a static blow (i.e. with `blob.growable` was `0`), or
49 * 3. `stuffer` did not own its blob (i.e. `alloced` was `0`).
50 * Note that `stuffer` is zero-ed out (without `free`-ing the blob),
51 * so we can't use `stuffer->alloced` and `stuffer->blob.data` here.
52 */
53 free(old_blob.data);
54 }
55 /* 3. free our heap-allocated `stuffer` since `s2n_stuffer_free` only `free`s the contents. */
56 free(stuffer);
57 }