Debian Codegraph @

crt/s2n/tests/cbmc/stubs/memmove_simple.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"). You may not use
5 * this file except in compliance with the License. A copy of the License is
6 * located at
7 *
8 * http://aws.amazon.com/apache2.0/
9 *
10 * or in the "license" file accompanying this file. This file is distributed on an
11 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
12 * implied. See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #undef memmove
17
18 #include <assert.h>
19 #include <cbmc_proof/nondet.h>
20 #include <stdint.h>
21 #include <stdlib.h>
22
23 /**
24 * CBMC can struggle to model memmove.
25 * If a proof needs real memmove behavior without paying its high cost,
26 * that proof can use this simple looping based solution.
27 */
28 void *memmove_impl(void *dest, const void *src, size_t n) {
29 __CPROVER_HIDE:;
30 if (n > 0) {
31 assert(dest);
32 assert(src);
33 }
34
35 uint8_t *dest_bytes = (uint8_t*) dest;
36 uint8_t *src_bytes = (uint8_t*) src;
37
38 /* src and dst can overlap, so we need to save a copy of src
39 * in case modifying dst modifies src */
40 uint8_t *src_copy = malloc(n);
41 if (src_copy == NULL) {
42 return NULL;
43 }
44 for (size_t i = 0; i < n; i++) {
45 src_copy[i] = src_bytes[i];
46 }
47
48 for (size_t i = 0; i < n; i++) {
49 dest_bytes[i] = src_copy[i];
50 }
51
52 free(src_copy);
53 return dest;
54 }
55
56 void *memmove(void *dest, const void *src, size_t n) {
57 __CPROVER_HIDE:;
58 return memmove_impl(dest, src, n);
59 }
60
61 void *__builtin___memmove_chk(void *dest, const void *src, size_t n, size_t size) {
62 __CPROVER_HIDE:;
63 (void)size;
64 return memmove_impl(dest, src, n);
65 }