blob: a467384f43951b73b5bb134ba9644c4d11d4772d [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
19#include "hf/panic.h"
20
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/**
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 Scull55baca62019-04-05 14:56:20 +010098size_t strnlen_s(const char *str, size_t strsz)
99{
Andrew Scull55baca62019-04-05 14:56:20 +0100100 if (str == NULL) {
101 return 0;
102 }
103
David Brazdil2246abe2019-08-23 12:21:06 +0100104 for (size_t i = 0; i < strsz; ++i) {
105 if (str[i] == '\0') {
106 return i;
107 }
Andrew Scull55baca62019-04-05 14:56:20 +0100108 }
109
David Brazdil2246abe2019-08-23 12:21:06 +0100110 /* NULL character not found. */
111 return strsz;
Andrew Scull55baca62019-04-05 14:56:20 +0100112}