Debian Codegraph @

crt/aws-c-common/verification/cbmc/stubs/memcpy_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: memcpy
8 *
9 * This function overrides the original implementation of the memcpy function
10 * from string.h. It copies the values of n bytes from the *src to the *dst.
11 * It also checks if the size of the arrays pointed to by both the *dst and
12 * *src parameters are at least n bytes and if they overlap.
13 *
14 * This takes advantage of the fact that 64bit operations require fewer array updates,
15 * which can make this version faster than the naive unrolling when used in CBMC.
16 * Benchmark your particular proof to know for sure.
17 */
18
19 #include <stddef.h>
20 #include <stdint.h>
21
22 void *memcpy_using_uint64_impl(void *dst, const void *src, size_t n) {
23 __CPROVER_precondition(
24 __CPROVER_POINTER_OBJECT(dst) != __CPROVER_POINTER_OBJECT(src) ||
25 ((const char *)src >= (const char *)dst + n) || ((const char *)dst >= (const char *)src + n),
26 "memcpy src/dst overlap");
27 __CPROVER_precondition(__CPROVER_r_ok(src, n), "memcpy source region readable");
28 __CPROVER_precondition(__CPROVER_w_ok(dst, n), "memcpy destination region writeable");
29
30 size_t num_uint64s = n >> 3;
31 size_t rem = n & 0x7;
32
33 uint8_t *d = (uint8_t *)dst;
34 const uint8_t *s = (const uint8_t *)src;
35
36 // Use fallthrough to unroll the remainder loop
37 switch (rem) {
38 case 7:
39 d[6] = s[6];
40 case 6:
41 d[5] = s[5];
42 case 5:
43 d[4] = s[4];
44 case 4:
45 d[3] = s[3];
46 case 3:
47 d[2] = s[2];
48 case 2:
49 d[1] = s[1];
50 case 1:
51 d[0] = s[0];
52 }
53
54 d += rem;
55 s += rem;
56
57 for (size_t i = 0; i < num_uint64s; ++i) {
58 ((uint64_t *)d)[i] = ((const uint64_t *)s)[i];
59 }
60
61 return dst;
62 }
63
64 void *memcpy(void *dst, const void *src, size_t n) {
65 return memcpy_using_uint64_impl(dst, src, n);
66 }
67
68 void *__builtin___memcpy_chk(void *dst, const void *src, __CPROVER_size_t n, __CPROVER_size_t size) {
69 (void)size;
70 return memcpy_using_uint64_impl(dst, src, n);
71 }