blob: 84806e2e9900c9f75963999bce4a109b74511c0b [file] [log] [blame]
Andrew Scull2b5fbad2019-04-05 13:55:56 +01001/*
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
David Brazdil74e9c3b2019-08-28 11:09:08 +010019#include "hf/check.h"
Andrew Scull2b5fbad2019-04-05 13:55:56 +010020
21/* Declare unsafe functions locally so they are not available globally. */
22void *memset(void *s, int c, size_t n);
Andrew Sculla1aa2ba2019-04-05 11:49:02 +010023void *memcpy(void *dst, const void *src, size_t n);
Andrew Scull8fbd7ee2019-04-05 14:36:34 +010024void *memmove(void *dst, const void *src, size_t n);
Andrew Scull2b5fbad2019-04-05 13:55:56 +010025
David Brazdil2246abe2019-08-23 12:21:06 +010026/*
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 Scull2b5fbad2019-04-05 13:55:56 +010044void memset_s(void *dest, rsize_t destsz, int ch, rsize_t count)
45{
David Brazdil2246abe2019-08-23 12:21:06 +010046 CHECK_OR_FILL(dest != NULL, dest, destsz, ch);
Andrew Scull2b5fbad2019-04-05 13:55:56 +010047
David Brazdil2246abe2019-08-23 12:21:06 +010048 /* 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 Scull2b5fbad2019-04-05 13:55:56 +010051
52 memset(dest, ch, count);
Andrew Scull2b5fbad2019-04-05 13:55:56 +010053}
Andrew Sculla1aa2ba2019-04-05 11:49:02 +010054
55void 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 Brazdil2246abe2019-08-23 12:21:06 +010060 CHECK_OR_ZERO_FILL(dest != NULL, dest, destsz);
61 CHECK_OR_ZERO_FILL(src != NULL, dest, destsz);
Andrew Sculla1aa2ba2019-04-05 11:49:02 +010062
David Brazdil2246abe2019-08-23 12:21:06 +010063 /* 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 Sculla1aa2ba2019-04-05 11:49:02 +010066
David Brazdil2246abe2019-08-23 12:21:06 +010067 /*
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 Sculla1aa2ba2019-04-05 11:49:02 +010075
76 memcpy(dest, src, count);
Andrew Sculla1aa2ba2019-04-05 11:49:02 +010077}
Andrew Scull8fbd7ee2019-04-05 14:36:34 +010078
79void memmove_s(void *dest, rsize_t destsz, const void *src, rsize_t count)
80{
David Brazdil2246abe2019-08-23 12:21:06 +010081 CHECK_OR_ZERO_FILL(dest != NULL, dest, destsz);
82 CHECK_OR_ZERO_FILL(src != NULL, dest, destsz);
Andrew Scull8fbd7ee2019-04-05 14:36:34 +010083
David Brazdil2246abe2019-08-23 12:21:06 +010084 /* 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 Scull8fbd7ee2019-04-05 14:36:34 +010087
88 memmove(dest, src, count);
Andrew Scull8fbd7ee2019-04-05 14:36:34 +010089}
Andrew Scull55baca62019-04-05 14:56:20 +010090
David Brazdil2246abe2019-08-23 12:21:06 +010091/**
David Brazdil74e9c3b2019-08-28 11:09:08 +010092 * Finds the first occurrence of character `ch` in the first `count` bytes of
93 * memory pointed to by `ptr`.
94 *
95 * Returns NULL if `ch` is not found.
96 * Panics if `ptr` is NULL (undefined behaviour).
97 */
98void *memchr(const void *ptr, int ch, size_t count)
99{
100 size_t i;
101 const unsigned char *p = (const unsigned char *)ptr;
102
103 CHECK(ptr != NULL);
104
105 /* Iterate over at most `strsz` characters of `str`. */
106 for (i = 0; i < count; ++i) {
107 if (p[i] == (unsigned char)ch) {
108 return (void *)(&p[i]);
109 }
110 }
111
112 return NULL;
113}
114
115/**
David Brazdil2246abe2019-08-23 12:21:06 +0100116 * Returns the length of the null-terminated byte string `str`, examining at
117 * most `strsz` bytes.
118 *
119 * If `str` is a NULL pointer, it returns zero.
120 * If a NULL character is not found, it returns `strsz`.
121 */
Andrew Scull55baca62019-04-05 14:56:20 +0100122size_t strnlen_s(const char *str, size_t strsz)
123{
Andrew Scull55baca62019-04-05 14:56:20 +0100124 if (str == NULL) {
125 return 0;
126 }
127
David Brazdil2246abe2019-08-23 12:21:06 +0100128 for (size_t i = 0; i < strsz; ++i) {
129 if (str[i] == '\0') {
130 return i;
131 }
Andrew Scull55baca62019-04-05 14:56:20 +0100132 }
133
David Brazdil2246abe2019-08-23 12:21:06 +0100134 /* NULL character not found. */
135 return strsz;
Andrew Scull55baca62019-04-05 14:56:20 +0100136}