Debian Codegraph @

crt/s2n/tests/cbmc/proofs/s2n_free/s2n_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 "utils/s2n_blob.h"
22
23 void s2n_free_harness()
24 {
25 /* Non-deterministic inputs. */
26 struct s2n_blob *blob = cbmc_allocate_s2n_blob();
27
28 /* Assumptions. */
29 nondet_s2n_mem_init();
30 __CPROVER_assume(s2n_result_is_ok(s2n_blob_validate(blob)));
31 const struct s2n_blob old_blob = *blob;
32
33 /* Operation under verification. */
34 int result = s2n_free(blob);
35 if (result == S2N_SUCCESS) {
36 /* Postconditions. */
37 assert(S2N_IMPLIES(old_blob.allocated, blob->data == NULL));
38 assert_all_zeroes((const uint8_t *)blob, sizeof(*blob));
39 if (old_blob.size != 0 && s2n_blob_is_growable(&old_blob)) {
40 assert(!S2N_MEM_IS_READABLE(blob->data, old_blob.size));
41 }
42 }
43
44 /* Cleanup after expected error cases, for memory leak check. */
45 bool failed_before_free = (s2n_errno == S2N_ERR_NOT_INITIALIZED) || (s2n_errno == S2N_ERR_FREE_STATIC_BLOB);
46 if ((result != S2N_SUCCESS && failed_before_free) || !s2n_blob_is_growable(blob)) {
47 /* 1. `s2n_free` failed _before_ calling `free`, either because:
48 (a) s2n was not initialized, or (b) the blob was a static blob.
49 2. `blob` is not growable, then `s2n_free` is not supposed to `free` even if successful.
50 */
51 free(blob->data);
52 }
53 /* 3. free our heap-allocated `blob` since `s2n_free` only `free`s the contents. */
54 free(blob);
55 }