Debian Codegraph @

crt/aws-c-common/verification/cbmc/stubs/memset_using_uint64.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 /**
7 * FUNCTION: memset
8 *
9 * Override the version of memset used by CBMC.
10 * This takes advantage of the fact that 64bit operations require fewer array updates,
11 * which can make this version faster than the naive unrolling when used in CBMC.
12 * Benchmark your particular proof to know for sure.
13 */
14
15 #include <stddef.h>
16 #include <stdint.h>
17
18 void *memset_using_uint64_impl(void *dst, int c, size_t n) {
19 __CPROVER_precondition(__CPROVER_w_ok(dst, n), "memset destination region writeable");
20
21 size_t num_uint64s = n >> 3;
22 size_t rem = n & 0x7;
23
24 uint8_t *d = (uint8_t *)dst;
25
26 // Use fallthrough to unroll the remainder loop
27 switch (rem) {
28 case 7:
29 d[6] = c;
30 case 6:
31 d[5] = c;
32 case 5:
33 d[4] = c;
34 case 4:
35 d[3] = c;
36 case 3:
37 d[2] = c;
38 case 2:
39 d[1] = c;
40 case 1:
41 d[0] = c;
42 }
43
44 d += rem;
45
46 uint64_t compounded = 0;
47 if (num_uint64s > 0 && c != 0) {
48 uint8_t *chars = (uint8_t *)&compounded;
49 chars[0] = c;
50 chars[1] = c;
51 chars[2] = c;
52 chars[3] = c;
53 chars[4] = c;
54 chars[5] = c;
55 chars[6] = c;
56 chars[7] = c;
57 }
58
59 for (size_t i = 0; i < num_uint64s; ++i) {
60 ((uint64_t *)d)[i] = compounded;
61 }
62
63 return dst;
64 }
65
66 void *memset(void *s, int c, size_t n) {
67 return memset_using_uint64_impl(s, c, n);
68 }
69
70 void *__builtin___memset_chk(void *s, int c, size_t n, size_t os) {
71 (void)os;
72 return memset_using_uint64_impl(s, c, n);
73 }