Andrew Scull | 2b5fbad | 2019-04-05 13:55:56 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2019 The Hafnium Authors. |
| 3 | * |
Andrew Walbran | e959ec1 | 2020-06-17 15:01:09 +0100 | [diff] [blame] | 4 | * Use of this source code is governed by a BSD-style |
| 5 | * license that can be found in the LICENSE file or at |
| 6 | * https://opensource.org/licenses/BSD-3-Clause. |
Andrew Scull | 2b5fbad | 2019-04-05 13:55:56 +0100 | [diff] [blame] | 7 | */ |
| 8 | |
| 9 | #include "hf/std.h" |
| 10 | |
David Brazdil | 74e9c3b | 2019-08-28 11:09:08 +0100 | [diff] [blame] | 11 | #include "hf/check.h" |
Andrew Scull | 2b5fbad | 2019-04-05 13:55:56 +0100 | [diff] [blame] | 12 | |
| 13 | /* Declare unsafe functions locally so they are not available globally. */ |
| 14 | void *memset(void *s, int c, size_t n); |
Andrew Scull | a1aa2ba | 2019-04-05 11:49:02 +0100 | [diff] [blame] | 15 | void *memcpy(void *dst, const void *src, size_t n); |
Andrew Scull | 8fbd7ee | 2019-04-05 14:36:34 +0100 | [diff] [blame] | 16 | void *memmove(void *dst, const void *src, size_t n); |
Andrew Scull | 2b5fbad | 2019-04-05 13:55:56 +0100 | [diff] [blame] | 17 | |
David Brazdil | 2246abe | 2019-08-23 12:21:06 +0100 | [diff] [blame] | 18 | /* |
| 19 | * As per the C11 specification, mem*_s() operations fill the destination buffer |
| 20 | * if runtime constraint validation fails, assuming that `dest` and `destsz` |
| 21 | * are both valid. |
| 22 | */ |
| 23 | #define CHECK_OR_FILL(cond, dest, destsz, ch) \ |
| 24 | do { \ |
| 25 | if (!(cond)) { \ |
| 26 | if ((dest) != NULL && (destsz) <= RSIZE_MAX) { \ |
| 27 | memset_s((dest), (destsz), (ch), (destsz)); \ |
| 28 | } \ |
| 29 | panic("%s failed: " #cond, __func__); \ |
| 30 | } \ |
| 31 | } while (0) |
| 32 | |
| 33 | #define CHECK_OR_ZERO_FILL(cond, dest, destsz) \ |
| 34 | CHECK_OR_FILL(cond, dest, destsz, '\0') |
| 35 | |
Andrew Scull | 2b5fbad | 2019-04-05 13:55:56 +0100 | [diff] [blame] | 36 | void memset_s(void *dest, rsize_t destsz, int ch, rsize_t count) |
| 37 | { |
David Brazdil | 2246abe | 2019-08-23 12:21:06 +0100 | [diff] [blame] | 38 | CHECK_OR_FILL(dest != NULL, dest, destsz, ch); |
Andrew Scull | 2b5fbad | 2019-04-05 13:55:56 +0100 | [diff] [blame] | 39 | |
David Brazdil | 2246abe | 2019-08-23 12:21:06 +0100 | [diff] [blame] | 40 | /* Check count <= destsz <= RSIZE_MAX. */ |
| 41 | CHECK_OR_FILL(destsz <= RSIZE_MAX, dest, destsz, ch); |
| 42 | CHECK_OR_FILL(count <= destsz, dest, destsz, ch); |
Andrew Scull | 2b5fbad | 2019-04-05 13:55:56 +0100 | [diff] [blame] | 43 | |
Andrew Walbran | e52006c | 2019-10-22 18:01:28 +0100 | [diff] [blame] | 44 | /* |
| 45 | * Clang analyzer doesn't like us calling unsafe memory functions, so |
| 46 | * make it ignore this call. |
| 47 | */ |
| 48 | #ifndef __clang_analyzer__ |
Andrew Scull | 2b5fbad | 2019-04-05 13:55:56 +0100 | [diff] [blame] | 49 | memset(dest, ch, count); |
Andrew Walbran | e52006c | 2019-10-22 18:01:28 +0100 | [diff] [blame] | 50 | #endif |
Andrew Scull | 2b5fbad | 2019-04-05 13:55:56 +0100 | [diff] [blame] | 51 | } |
Andrew Scull | a1aa2ba | 2019-04-05 11:49:02 +0100 | [diff] [blame] | 52 | |
| 53 | void memcpy_s(void *dest, rsize_t destsz, const void *src, rsize_t count) |
| 54 | { |
| 55 | uintptr_t d = (uintptr_t)dest; |
| 56 | uintptr_t s = (uintptr_t)src; |
| 57 | |
David Brazdil | 2246abe | 2019-08-23 12:21:06 +0100 | [diff] [blame] | 58 | CHECK_OR_ZERO_FILL(dest != NULL, dest, destsz); |
| 59 | CHECK_OR_ZERO_FILL(src != NULL, dest, destsz); |
Andrew Scull | a1aa2ba | 2019-04-05 11:49:02 +0100 | [diff] [blame] | 60 | |
David Brazdil | 2246abe | 2019-08-23 12:21:06 +0100 | [diff] [blame] | 61 | /* Check count <= destsz <= RSIZE_MAX. */ |
| 62 | CHECK_OR_ZERO_FILL(destsz <= RSIZE_MAX, dest, destsz); |
| 63 | CHECK_OR_ZERO_FILL(count <= destsz, dest, destsz); |
Andrew Scull | a1aa2ba | 2019-04-05 11:49:02 +0100 | [diff] [blame] | 64 | |
David Brazdil | 2246abe | 2019-08-23 12:21:06 +0100 | [diff] [blame] | 65 | /* |
| 66 | * Buffer overlap test. |
| 67 | * case a) `d < s` implies `s >= d+count` |
| 68 | * case b) `d > s` implies `d >= s+count` |
| 69 | */ |
| 70 | CHECK_OR_ZERO_FILL(d != s, dest, destsz); |
| 71 | CHECK_OR_ZERO_FILL(d < s || d >= (s + count), dest, destsz); |
| 72 | CHECK_OR_ZERO_FILL(d > s || s >= (d + count), dest, destsz); |
Andrew Scull | a1aa2ba | 2019-04-05 11:49:02 +0100 | [diff] [blame] | 73 | |
Andrew Walbran | e52006c | 2019-10-22 18:01:28 +0100 | [diff] [blame] | 74 | #ifndef __clang_analyzer__ |
Andrew Scull | a1aa2ba | 2019-04-05 11:49:02 +0100 | [diff] [blame] | 75 | memcpy(dest, src, count); |
Andrew Walbran | e52006c | 2019-10-22 18:01:28 +0100 | [diff] [blame] | 76 | #endif |
Andrew Scull | a1aa2ba | 2019-04-05 11:49:02 +0100 | [diff] [blame] | 77 | } |
Andrew Scull | 8fbd7ee | 2019-04-05 14:36:34 +0100 | [diff] [blame] | 78 | |
| 79 | void memmove_s(void *dest, rsize_t destsz, const void *src, rsize_t count) |
| 80 | { |
David Brazdil | 2246abe | 2019-08-23 12:21:06 +0100 | [diff] [blame] | 81 | CHECK_OR_ZERO_FILL(dest != NULL, dest, destsz); |
| 82 | CHECK_OR_ZERO_FILL(src != NULL, dest, destsz); |
Andrew Scull | 8fbd7ee | 2019-04-05 14:36:34 +0100 | [diff] [blame] | 83 | |
David Brazdil | 2246abe | 2019-08-23 12:21:06 +0100 | [diff] [blame] | 84 | /* Check count <= destsz <= RSIZE_MAX. */ |
| 85 | CHECK_OR_ZERO_FILL(destsz <= RSIZE_MAX, dest, destsz); |
| 86 | CHECK_OR_ZERO_FILL(count <= destsz, dest, destsz); |
Andrew Scull | 8fbd7ee | 2019-04-05 14:36:34 +0100 | [diff] [blame] | 87 | |
Andrew Walbran | e52006c | 2019-10-22 18:01:28 +0100 | [diff] [blame] | 88 | #ifndef __clang_analyzer__ |
Andrew Scull | 8fbd7ee | 2019-04-05 14:36:34 +0100 | [diff] [blame] | 89 | memmove(dest, src, count); |
Andrew Walbran | e52006c | 2019-10-22 18:01:28 +0100 | [diff] [blame] | 90 | #endif |
Andrew Scull | 8fbd7ee | 2019-04-05 14:36:34 +0100 | [diff] [blame] | 91 | } |
Andrew Scull | 55baca6 | 2019-04-05 14:56:20 +0100 | [diff] [blame] | 92 | |
David Brazdil | 2246abe | 2019-08-23 12:21:06 +0100 | [diff] [blame] | 93 | /** |
David Brazdil | 74e9c3b | 2019-08-28 11:09:08 +0100 | [diff] [blame] | 94 | * Finds the first occurrence of character `ch` in the first `count` bytes of |
| 95 | * memory pointed to by `ptr`. |
| 96 | * |
| 97 | * Returns NULL if `ch` is not found. |
| 98 | * Panics if `ptr` is NULL (undefined behaviour). |
| 99 | */ |
| 100 | void *memchr(const void *ptr, int ch, size_t count) |
| 101 | { |
| 102 | size_t i; |
| 103 | const unsigned char *p = (const unsigned char *)ptr; |
| 104 | |
| 105 | CHECK(ptr != NULL); |
| 106 | |
| 107 | /* Iterate over at most `strsz` characters of `str`. */ |
| 108 | for (i = 0; i < count; ++i) { |
| 109 | if (p[i] == (unsigned char)ch) { |
| 110 | return (void *)(&p[i]); |
| 111 | } |
| 112 | } |
| 113 | |
| 114 | return NULL; |
| 115 | } |
| 116 | |
| 117 | /** |
David Brazdil | 2246abe | 2019-08-23 12:21:06 +0100 | [diff] [blame] | 118 | * Returns the length of the null-terminated byte string `str`, examining at |
| 119 | * most `strsz` bytes. |
| 120 | * |
| 121 | * If `str` is a NULL pointer, it returns zero. |
| 122 | * If a NULL character is not found, it returns `strsz`. |
| 123 | */ |
Andrew Scull | 55baca6 | 2019-04-05 14:56:20 +0100 | [diff] [blame] | 124 | size_t strnlen_s(const char *str, size_t strsz) |
| 125 | { |
Andrew Scull | 55baca6 | 2019-04-05 14:56:20 +0100 | [diff] [blame] | 126 | if (str == NULL) { |
| 127 | return 0; |
| 128 | } |
| 129 | |
David Brazdil | 2246abe | 2019-08-23 12:21:06 +0100 | [diff] [blame] | 130 | for (size_t i = 0; i < strsz; ++i) { |
| 131 | if (str[i] == '\0') { |
| 132 | return i; |
| 133 | } |
Andrew Scull | 55baca6 | 2019-04-05 14:56:20 +0100 | [diff] [blame] | 134 | } |
| 135 | |
David Brazdil | 2246abe | 2019-08-23 12:21:06 +0100 | [diff] [blame] | 136 | /* NULL character not found. */ |
| 137 | return strsz; |
Andrew Scull | 55baca6 | 2019-04-05 14:56:20 +0100 | [diff] [blame] | 138 | } |