Andrew Scull | 2b5fbad | 2019-04-05 13:55:56 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2019 The Hafnium Authors. |
| 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 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #include "hf/std.h" |
| 18 | |
| 19 | #include "hf/panic.h" |
| 20 | |
| 21 | /* Declare unsafe functions locally so they are not available globally. */ |
| 22 | void *memset(void *s, int c, size_t n); |
Andrew Scull | a1aa2ba | 2019-04-05 11:49:02 +0100 | [diff] [blame] | 23 | void *memcpy(void *dst, const void *src, size_t n); |
Andrew Scull | 8fbd7ee | 2019-04-05 14:36:34 +0100 | [diff] [blame] | 24 | void *memmove(void *dst, const void *src, size_t n); |
Andrew Scull | 2b5fbad | 2019-04-05 13:55:56 +0100 | [diff] [blame] | 25 | |
David Brazdil | 2246abe | 2019-08-23 12:21:06 +0100 | [diff] [blame] | 26 | /* |
| 27 | * As per the C11 specification, mem*_s() operations fill the destination buffer |
| 28 | * if runtime constraint validation fails, assuming that `dest` and `destsz` |
| 29 | * are both valid. |
| 30 | */ |
| 31 | #define CHECK_OR_FILL(cond, dest, destsz, ch) \ |
| 32 | do { \ |
| 33 | if (!(cond)) { \ |
| 34 | if ((dest) != NULL && (destsz) <= RSIZE_MAX) { \ |
| 35 | memset_s((dest), (destsz), (ch), (destsz)); \ |
| 36 | } \ |
| 37 | panic("%s failed: " #cond, __func__); \ |
| 38 | } \ |
| 39 | } while (0) |
| 40 | |
| 41 | #define CHECK_OR_ZERO_FILL(cond, dest, destsz) \ |
| 42 | CHECK_OR_FILL(cond, dest, destsz, '\0') |
| 43 | |
Andrew Scull | 2b5fbad | 2019-04-05 13:55:56 +0100 | [diff] [blame] | 44 | void memset_s(void *dest, rsize_t destsz, int ch, rsize_t count) |
| 45 | { |
David Brazdil | 2246abe | 2019-08-23 12:21:06 +0100 | [diff] [blame] | 46 | CHECK_OR_FILL(dest != NULL, dest, destsz, ch); |
Andrew Scull | 2b5fbad | 2019-04-05 13:55:56 +0100 | [diff] [blame] | 47 | |
David Brazdil | 2246abe | 2019-08-23 12:21:06 +0100 | [diff] [blame] | 48 | /* Check count <= destsz <= RSIZE_MAX. */ |
| 49 | CHECK_OR_FILL(destsz <= RSIZE_MAX, dest, destsz, ch); |
| 50 | CHECK_OR_FILL(count <= destsz, dest, destsz, ch); |
Andrew Scull | 2b5fbad | 2019-04-05 13:55:56 +0100 | [diff] [blame] | 51 | |
| 52 | memset(dest, ch, count); |
Andrew Scull | 2b5fbad | 2019-04-05 13:55:56 +0100 | [diff] [blame] | 53 | } |
Andrew Scull | a1aa2ba | 2019-04-05 11:49:02 +0100 | [diff] [blame] | 54 | |
| 55 | void memcpy_s(void *dest, rsize_t destsz, const void *src, rsize_t count) |
| 56 | { |
| 57 | uintptr_t d = (uintptr_t)dest; |
| 58 | uintptr_t s = (uintptr_t)src; |
| 59 | |
David Brazdil | 2246abe | 2019-08-23 12:21:06 +0100 | [diff] [blame] | 60 | CHECK_OR_ZERO_FILL(dest != NULL, dest, destsz); |
| 61 | CHECK_OR_ZERO_FILL(src != NULL, dest, destsz); |
Andrew Scull | a1aa2ba | 2019-04-05 11:49:02 +0100 | [diff] [blame] | 62 | |
David Brazdil | 2246abe | 2019-08-23 12:21:06 +0100 | [diff] [blame] | 63 | /* Check count <= destsz <= RSIZE_MAX. */ |
| 64 | CHECK_OR_ZERO_FILL(destsz <= RSIZE_MAX, dest, destsz); |
| 65 | CHECK_OR_ZERO_FILL(count <= destsz, dest, destsz); |
Andrew Scull | a1aa2ba | 2019-04-05 11:49:02 +0100 | [diff] [blame] | 66 | |
David Brazdil | 2246abe | 2019-08-23 12:21:06 +0100 | [diff] [blame] | 67 | /* |
| 68 | * Buffer overlap test. |
| 69 | * case a) `d < s` implies `s >= d+count` |
| 70 | * case b) `d > s` implies `d >= s+count` |
| 71 | */ |
| 72 | CHECK_OR_ZERO_FILL(d != s, dest, destsz); |
| 73 | CHECK_OR_ZERO_FILL(d < s || d >= (s + count), dest, destsz); |
| 74 | CHECK_OR_ZERO_FILL(d > s || s >= (d + count), dest, destsz); |
Andrew Scull | a1aa2ba | 2019-04-05 11:49:02 +0100 | [diff] [blame] | 75 | |
| 76 | memcpy(dest, src, count); |
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 | |
| 88 | memmove(dest, src, count); |
Andrew Scull | 8fbd7ee | 2019-04-05 14:36:34 +0100 | [diff] [blame] | 89 | } |
Andrew Scull | 55baca6 | 2019-04-05 14:56:20 +0100 | [diff] [blame] | 90 | |
David Brazdil | 2246abe | 2019-08-23 12:21:06 +0100 | [diff] [blame] | 91 | /** |
| 92 | * Returns the length of the null-terminated byte string `str`, examining at |
| 93 | * most `strsz` bytes. |
| 94 | * |
| 95 | * If `str` is a NULL pointer, it returns zero. |
| 96 | * If a NULL character is not found, it returns `strsz`. |
| 97 | */ |
Andrew Scull | 55baca6 | 2019-04-05 14:56:20 +0100 | [diff] [blame] | 98 | size_t strnlen_s(const char *str, size_t strsz) |
| 99 | { |
Andrew Scull | 55baca6 | 2019-04-05 14:56:20 +0100 | [diff] [blame] | 100 | if (str == NULL) { |
| 101 | return 0; |
| 102 | } |
| 103 | |
David Brazdil | 2246abe | 2019-08-23 12:21:06 +0100 | [diff] [blame] | 104 | for (size_t i = 0; i < strsz; ++i) { |
| 105 | if (str[i] == '\0') { |
| 106 | return i; |
| 107 | } |
Andrew Scull | 55baca6 | 2019-04-05 14:56:20 +0100 | [diff] [blame] | 108 | } |
| 109 | |
David Brazdil | 2246abe | 2019-08-23 12:21:06 +0100 | [diff] [blame] | 110 | /* NULL character not found. */ |
| 111 | return strsz; |
Andrew Scull | 55baca6 | 2019-04-05 14:56:20 +0100 | [diff] [blame] | 112 | } |