blob: 2fce4c1b153417558670becfd1ae54a426e5a279 [file] [log] [blame]
Andrew Scull2b5fbad2019-04-05 13:55:56 +01001/*
2 * Copyright 2019 The Hafnium Authors.
3 *
Andrew Walbrane959ec12020-06-17 15:01:09 +01004 * 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 Scull2b5fbad2019-04-05 13:55:56 +01007 */
8
9#include "hf/std.h"
10
David Brazdil74e9c3b2019-08-28 11:09:08 +010011#include "hf/check.h"
Andrew Scull2b5fbad2019-04-05 13:55:56 +010012
13/* Declare unsafe functions locally so they are not available globally. */
14void *memset(void *s, int c, size_t n);
Andrew Sculla1aa2ba2019-04-05 11:49:02 +010015void *memcpy(void *dst, const void *src, size_t n);
Andrew Scull8fbd7ee2019-04-05 14:36:34 +010016void *memmove(void *dst, const void *src, size_t n);
Andrew Scull2b5fbad2019-04-05 13:55:56 +010017
David Brazdil2246abe2019-08-23 12:21:06 +010018/*
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 Scull2b5fbad2019-04-05 13:55:56 +010036void memset_s(void *dest, rsize_t destsz, int ch, rsize_t count)
37{
Daniel Boulby8adf7482021-09-22 15:12:44 +010038 if (dest == NULL || destsz > RSIZE_MAX) {
39 panic("memset_s failed as either dest == NULL "
40 "or destsz > RSIZE_MAX.\n");
41 }
Andrew Scull2b5fbad2019-04-05 13:55:56 +010042
Andrew Walbrane52006c2019-10-22 18:01:28 +010043 /*
44 * Clang analyzer doesn't like us calling unsafe memory functions, so
45 * make it ignore this call.
46 */
Daniel Boulby8adf7482021-09-22 15:12:44 +010047 // NOLINTNEXTLINE
48 memset(dest, ch, (count <= destsz ? count : destsz));
Andrew Scull2b5fbad2019-04-05 13:55:56 +010049}
Andrew Sculla1aa2ba2019-04-05 11:49:02 +010050
51void memcpy_s(void *dest, rsize_t destsz, const void *src, rsize_t count)
52{
53 uintptr_t d = (uintptr_t)dest;
54 uintptr_t s = (uintptr_t)src;
55
David Brazdil2246abe2019-08-23 12:21:06 +010056 CHECK_OR_ZERO_FILL(dest != NULL, dest, destsz);
57 CHECK_OR_ZERO_FILL(src != NULL, dest, destsz);
Andrew Sculla1aa2ba2019-04-05 11:49:02 +010058
David Brazdil2246abe2019-08-23 12:21:06 +010059 /* Check count <= destsz <= RSIZE_MAX. */
60 CHECK_OR_ZERO_FILL(destsz <= RSIZE_MAX, dest, destsz);
61 CHECK_OR_ZERO_FILL(count <= destsz, dest, destsz);
Andrew Sculla1aa2ba2019-04-05 11:49:02 +010062
David Brazdil2246abe2019-08-23 12:21:06 +010063 /*
64 * Buffer overlap test.
65 * case a) `d < s` implies `s >= d+count`
66 * case b) `d > s` implies `d >= s+count`
67 */
68 CHECK_OR_ZERO_FILL(d != s, dest, destsz);
69 CHECK_OR_ZERO_FILL(d < s || d >= (s + count), dest, destsz);
70 CHECK_OR_ZERO_FILL(d > s || s >= (d + count), dest, destsz);
Andrew Sculla1aa2ba2019-04-05 11:49:02 +010071
Daniel Boulby8adf7482021-09-22 15:12:44 +010072 /*
73 * Clang analyzer doesn't like us calling unsafe memory functions, so
74 * make it ignore this call.
75 */
76 // NOLINTNEXTLINE
Andrew Sculla1aa2ba2019-04-05 11:49:02 +010077 memcpy(dest, src, count);
Andrew Sculla1aa2ba2019-04-05 11:49:02 +010078}
Andrew Scull8fbd7ee2019-04-05 14:36:34 +010079
80void memmove_s(void *dest, rsize_t destsz, const void *src, rsize_t count)
81{
David Brazdil2246abe2019-08-23 12:21:06 +010082 CHECK_OR_ZERO_FILL(dest != NULL, dest, destsz);
83 CHECK_OR_ZERO_FILL(src != NULL, dest, destsz);
Andrew Scull8fbd7ee2019-04-05 14:36:34 +010084
David Brazdil2246abe2019-08-23 12:21:06 +010085 /* Check count <= destsz <= RSIZE_MAX. */
86 CHECK_OR_ZERO_FILL(destsz <= RSIZE_MAX, dest, destsz);
87 CHECK_OR_ZERO_FILL(count <= destsz, dest, destsz);
Andrew Scull8fbd7ee2019-04-05 14:36:34 +010088
Daniel Boulby8adf7482021-09-22 15:12:44 +010089 /*
90 * Clang analyzer doesn't like us calling unsafe memory functions, so
91 * make it ignore this call.
92 */
93 // NOLINTNEXTLINE
Andrew Scull8fbd7ee2019-04-05 14:36:34 +010094 memmove(dest, src, count);
Andrew Scull8fbd7ee2019-04-05 14:36:34 +010095}
Andrew Scull55baca62019-04-05 14:56:20 +010096
David Brazdil2246abe2019-08-23 12:21:06 +010097/**
David Brazdil74e9c3b2019-08-28 11:09:08 +010098 * Finds the first occurrence of character `ch` in the first `count` bytes of
99 * memory pointed to by `ptr`.
100 *
101 * Returns NULL if `ch` is not found.
102 * Panics if `ptr` is NULL (undefined behaviour).
103 */
104void *memchr(const void *ptr, int ch, size_t count)
105{
106 size_t i;
107 const unsigned char *p = (const unsigned char *)ptr;
108
109 CHECK(ptr != NULL);
110
111 /* Iterate over at most `strsz` characters of `str`. */
112 for (i = 0; i < count; ++i) {
113 if (p[i] == (unsigned char)ch) {
114 return (void *)(&p[i]);
115 }
116 }
117
118 return NULL;
119}
120
121/**
David Brazdil2246abe2019-08-23 12:21:06 +0100122 * Returns the length of the null-terminated byte string `str`, examining at
123 * most `strsz` bytes.
124 *
125 * If `str` is a NULL pointer, it returns zero.
126 * If a NULL character is not found, it returns `strsz`.
127 */
Andrew Scull55baca62019-04-05 14:56:20 +0100128size_t strnlen_s(const char *str, size_t strsz)
129{
Andrew Scull55baca62019-04-05 14:56:20 +0100130 if (str == NULL) {
131 return 0;
132 }
133
David Brazdil2246abe2019-08-23 12:21:06 +0100134 for (size_t i = 0; i < strsz; ++i) {
135 if (str[i] == '\0') {
136 return i;
137 }
Andrew Scull55baca62019-04-05 14:56:20 +0100138 }
139
David Brazdil2246abe2019-08-23 12:21:06 +0100140 /* NULL character not found. */
141 return strsz;
Andrew Scull55baca62019-04-05 14:56:20 +0100142}