Debian Codegraph @

crt/s2n/tests/cbmc/proofs/s2n_stuffer_write/s2n_stuffer_write_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_write_harness()
24 {
25 /* Non-deterministic inputs. */
26 struct s2n_stuffer *stuffer = cbmc_allocate_s2n_stuffer();
27 __CPROVER_assume(s2n_result_is_ok(s2n_stuffer_validate(stuffer)));
28 struct s2n_blob *blob = cbmc_allocate_s2n_blob();
29 __CPROVER_assume(s2n_result_is_ok(s2n_blob_validate(blob)));
30 uint32_t idx;
31
32 nondet_s2n_mem_init();
33
34 /* Save previous state from stuffer. */
35 struct s2n_stuffer old_stuffer = *stuffer;
36
37 /* Store a byte from the stuffer that wont be overwritten to compare if the write succeeds. */
38 __CPROVER_assume(idx < stuffer->blob.size);
39 if (__CPROVER_overflow_plus(old_stuffer.write_cursor, blob->size)) {
40 __CPROVER_assume(idx < old_stuffer.write_cursor);
41 } else {
42 __CPROVER_assume(idx < old_stuffer.write_cursor || idx >= old_stuffer.write_cursor + blob->size);
43 }
44 uint8_t untouched_byte = stuffer->blob.data[ idx ];
45
46 /* Store a byte from the blob to compare. */
47 struct s2n_blob old_blob = *blob;
48 struct store_byte_from_buffer old_byte_from_blob;
49 save_byte_from_blob(blob, &old_byte_from_blob);
50
51 /* Operation under verification. */
52 if (s2n_stuffer_write(stuffer, blob) == S2N_SUCCESS) {
53 assert(stuffer->write_cursor == old_stuffer.write_cursor + blob->size);
54 assert(stuffer->blob.data[ idx ] == untouched_byte);
55 assert(stuffer->high_water_mark == S2N_MAX(old_stuffer.write_cursor + blob->size, old_stuffer.high_water_mark));
56 assert(s2n_result_is_ok(s2n_stuffer_validate(stuffer)));
57 } else {
58 assert(stuffer->write_cursor == old_stuffer.write_cursor);
59 assert(stuffer->high_water_mark == old_stuffer.high_water_mark);
60 }
61 assert(stuffer->read_cursor == old_stuffer.read_cursor);
62 assert_blob_equivalence(blob, &old_blob, &old_byte_from_blob);
63 }