2022-02-07 16:23:20 +00:00
|
|
|
/* SPDX-License-Identifier: LGPL-2.1 OR MIT */
|
|
|
|
/*
|
|
|
|
* string function definitions for NOLIBC
|
|
|
|
* Copyright (C) 2017-2021 Willy Tarreau <w@1wt.eu>
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef _NOLIBC_STRING_H
|
|
|
|
#define _NOLIBC_STRING_H
|
|
|
|
|
|
|
|
#include "std.h"
|
|
|
|
|
2022-03-29 10:17:37 +00:00
|
|
|
static void *malloc(size_t len);
|
|
|
|
|
2022-02-07 16:23:20 +00:00
|
|
|
/*
|
|
|
|
* As much as possible, please keep functions alphabetically sorted.
|
|
|
|
*/
|
|
|
|
|
|
|
|
static __attribute__((unused))
|
|
|
|
int memcmp(const void *s1, const void *s2, size_t n)
|
|
|
|
{
|
|
|
|
size_t ofs = 0;
|
tools/nolibc/string: Fix memcmp() implementation
The C standard says that memcmp() must treat the buffers as consisting
of "unsigned chars". If char happens to be unsigned, the casts are ok,
but then obviously the c1 variable can never contain a negative
value. And when char is signed, the casts are wrong, and there's still
a problem with using an 8-bit quantity to hold the difference, because
that can range from -255 to +255.
For example, assuming char is signed, comparing two 1-byte buffers,
one containing 0x00 and another 0x80, the current implementation would
return -128 for both memcmp(a, b, 1) and memcmp(b, a, 1), whereas one
of those should of course return something positive.
Signed-off-by: Rasmus Villemoes <linux@rasmusvillemoes.dk>
Fixes: 66b6f755ad45 ("rcutorture: Import a copy of nolibc")
Cc: stable@vger.kernel.org # v5.0+
Signed-off-by: Willy Tarreau <w@1wt.eu>
Signed-off-by: Paul E. McKenney <paulmck@kernel.org>
2022-10-21 06:01:53 +00:00
|
|
|
int c1 = 0;
|
2022-02-07 16:23:20 +00:00
|
|
|
|
tools/nolibc/string: Fix memcmp() implementation
The C standard says that memcmp() must treat the buffers as consisting
of "unsigned chars". If char happens to be unsigned, the casts are ok,
but then obviously the c1 variable can never contain a negative
value. And when char is signed, the casts are wrong, and there's still
a problem with using an 8-bit quantity to hold the difference, because
that can range from -255 to +255.
For example, assuming char is signed, comparing two 1-byte buffers,
one containing 0x00 and another 0x80, the current implementation would
return -128 for both memcmp(a, b, 1) and memcmp(b, a, 1), whereas one
of those should of course return something positive.
Signed-off-by: Rasmus Villemoes <linux@rasmusvillemoes.dk>
Fixes: 66b6f755ad45 ("rcutorture: Import a copy of nolibc")
Cc: stable@vger.kernel.org # v5.0+
Signed-off-by: Willy Tarreau <w@1wt.eu>
Signed-off-by: Paul E. McKenney <paulmck@kernel.org>
2022-10-21 06:01:53 +00:00
|
|
|
while (ofs < n && !(c1 = ((unsigned char *)s1)[ofs] - ((unsigned char *)s2)[ofs])) {
|
2022-02-07 16:23:20 +00:00
|
|
|
ofs++;
|
|
|
|
}
|
|
|
|
return c1;
|
|
|
|
}
|
|
|
|
|
2022-02-07 16:23:40 +00:00
|
|
|
static __attribute__((unused))
|
|
|
|
void *_nolibc_memcpy_up(void *dst, const void *src, size_t len)
|
|
|
|
{
|
|
|
|
size_t pos = 0;
|
|
|
|
|
|
|
|
while (pos < len) {
|
|
|
|
((char *)dst)[pos] = ((const char *)src)[pos];
|
|
|
|
pos++;
|
|
|
|
}
|
|
|
|
return dst;
|
|
|
|
}
|
|
|
|
|
|
|
|
static __attribute__((unused))
|
|
|
|
void *_nolibc_memcpy_down(void *dst, const void *src, size_t len)
|
|
|
|
{
|
|
|
|
while (len) {
|
|
|
|
len--;
|
|
|
|
((char *)dst)[len] = ((const char *)src)[len];
|
|
|
|
}
|
|
|
|
return dst;
|
|
|
|
}
|
|
|
|
|
2022-02-07 16:23:47 +00:00
|
|
|
/* might be ignored by the compiler without -ffreestanding, then found as
|
|
|
|
* missing.
|
|
|
|
*/
|
|
|
|
__attribute__((weak,unused,section(".text.nolibc_memmove")))
|
2022-02-07 16:23:20 +00:00
|
|
|
void *memmove(void *dst, const void *src, size_t len)
|
|
|
|
{
|
2022-02-07 16:23:41 +00:00
|
|
|
size_t dir, pos;
|
2022-02-07 16:23:20 +00:00
|
|
|
|
2022-02-07 16:23:41 +00:00
|
|
|
pos = len;
|
|
|
|
dir = -1;
|
|
|
|
|
|
|
|
if (dst < src) {
|
|
|
|
pos = -1;
|
|
|
|
dir = 1;
|
2022-02-07 16:23:20 +00:00
|
|
|
}
|
2022-02-07 16:23:41 +00:00
|
|
|
|
|
|
|
while (len) {
|
|
|
|
pos += dir;
|
|
|
|
((char *)dst)[pos] = ((const char *)src)[pos];
|
|
|
|
len--;
|
|
|
|
}
|
|
|
|
return dst;
|
2022-02-07 16:23:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* must be exported, as it's used by libgcc on ARM */
|
2022-02-07 16:23:44 +00:00
|
|
|
__attribute__((weak,unused,section(".text.nolibc_memcpy")))
|
2022-02-07 16:23:20 +00:00
|
|
|
void *memcpy(void *dst, const void *src, size_t len)
|
|
|
|
{
|
2022-02-07 16:23:40 +00:00
|
|
|
return _nolibc_memcpy_up(dst, src, len);
|
2022-02-07 16:23:20 +00:00
|
|
|
}
|
|
|
|
|
2022-02-07 16:23:47 +00:00
|
|
|
/* might be ignored by the compiler without -ffreestanding, then found as
|
|
|
|
* missing.
|
|
|
|
*/
|
|
|
|
__attribute__((weak,unused,section(".text.nolibc_memset")))
|
2022-02-07 16:23:20 +00:00
|
|
|
void *memset(void *dst, int b, size_t len)
|
|
|
|
{
|
|
|
|
char *p = dst;
|
|
|
|
|
|
|
|
while (len--)
|
|
|
|
*(p++) = b;
|
|
|
|
return dst;
|
|
|
|
}
|
|
|
|
|
|
|
|
static __attribute__((unused))
|
|
|
|
char *strchr(const char *s, int c)
|
|
|
|
{
|
|
|
|
while (*s) {
|
|
|
|
if (*s == (char)c)
|
|
|
|
return (char *)s;
|
|
|
|
s++;
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2022-03-21 17:33:10 +00:00
|
|
|
static __attribute__((unused))
|
|
|
|
int strcmp(const char *a, const char *b)
|
|
|
|
{
|
|
|
|
unsigned int c;
|
|
|
|
int diff;
|
|
|
|
|
|
|
|
while (!(diff = (unsigned char)*a++ - (c = (unsigned char)*b++)) && c)
|
|
|
|
;
|
|
|
|
return diff;
|
|
|
|
}
|
|
|
|
|
2022-02-07 16:23:20 +00:00
|
|
|
static __attribute__((unused))
|
|
|
|
char *strcpy(char *dst, const char *src)
|
|
|
|
{
|
|
|
|
char *ret = dst;
|
|
|
|
|
|
|
|
while ((*dst++ = *src++));
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2022-03-23 07:18:06 +00:00
|
|
|
/* this function is only used with arguments that are not constants or when
|
tools/nolibc: Fix missing strlen() definition and infinite loop with gcc-12
When built at -Os, gcc-12 recognizes an strlen() pattern in nolibc_strlen()
and replaces it with a jump to strlen(), which is not defined as a symbol
and breaks compilation. Worse, when the function is called strlen(), the
function is simply replaced with a jump to itself, hence becomes an
infinite loop.
One way to avoid this is to always set -ffreestanding, but the calling
code doesn't know this and there's no way (either via attributes or
pragmas) to globally enable it from include files, effectively leaving
a painful situation for the caller.
Alexey suggested to place an empty asm() statement inside the loop to
stop gcc from recognizing a well-known pattern, which happens to work
pretty fine. At least it allows us to make sure our local definition
is not replaced with a self jump.
The function only needs to be renamed back to strlen() so that the symbol
exists, which implies that nolibc_strlen() which is used on variable
strings has to be declared as a macro that points back to it before the
strlen() macro is redifined.
It was verified to produce valid code with gcc 3.4 to 12.1 at different
optimization levels, and both with constant and variable strings.
In case this problem surfaces again in the future, an alternate approach
consisting in adding an optimize("no-tree-loop-distribute-patterns")
function attribute for gcc>=12 worked as well but is less pretty.
Reported-by: kernel test robot <yujie.liu@intel.com>
Link: https://lore.kernel.org/r/202210081618.754a77db-yujie.liu@intel.com
Fixes: 66b6f755ad45 ("rcutorture: Import a copy of nolibc")
Fixes: 96980b833a21 ("tools/nolibc/string: do not use __builtin_strlen() at -O0")
Cc: "Paul E. McKenney" <paulmck@kernel.org>
Cc: Alexey Dobriyan <adobriyan@gmail.com>
Signed-off-by: Willy Tarreau <w@1wt.eu>
Signed-off-by: Paul E. McKenney <paulmck@kernel.org>
2022-10-09 18:29:36 +00:00
|
|
|
* it's not known because optimizations are disabled. Note that gcc 12
|
|
|
|
* recognizes an strlen() pattern and replaces it with a jump to strlen(),
|
|
|
|
* thus itself, hence the asm() statement below that's meant to disable this
|
|
|
|
* confusing practice.
|
2022-03-23 07:18:06 +00:00
|
|
|
*/
|
2022-02-07 16:23:20 +00:00
|
|
|
static __attribute__((unused))
|
tools/nolibc: Fix missing strlen() definition and infinite loop with gcc-12
When built at -Os, gcc-12 recognizes an strlen() pattern in nolibc_strlen()
and replaces it with a jump to strlen(), which is not defined as a symbol
and breaks compilation. Worse, when the function is called strlen(), the
function is simply replaced with a jump to itself, hence becomes an
infinite loop.
One way to avoid this is to always set -ffreestanding, but the calling
code doesn't know this and there's no way (either via attributes or
pragmas) to globally enable it from include files, effectively leaving
a painful situation for the caller.
Alexey suggested to place an empty asm() statement inside the loop to
stop gcc from recognizing a well-known pattern, which happens to work
pretty fine. At least it allows us to make sure our local definition
is not replaced with a self jump.
The function only needs to be renamed back to strlen() so that the symbol
exists, which implies that nolibc_strlen() which is used on variable
strings has to be declared as a macro that points back to it before the
strlen() macro is redifined.
It was verified to produce valid code with gcc 3.4 to 12.1 at different
optimization levels, and both with constant and variable strings.
In case this problem surfaces again in the future, an alternate approach
consisting in adding an optimize("no-tree-loop-distribute-patterns")
function attribute for gcc>=12 worked as well but is less pretty.
Reported-by: kernel test robot <yujie.liu@intel.com>
Link: https://lore.kernel.org/r/202210081618.754a77db-yujie.liu@intel.com
Fixes: 66b6f755ad45 ("rcutorture: Import a copy of nolibc")
Fixes: 96980b833a21 ("tools/nolibc/string: do not use __builtin_strlen() at -O0")
Cc: "Paul E. McKenney" <paulmck@kernel.org>
Cc: Alexey Dobriyan <adobriyan@gmail.com>
Signed-off-by: Willy Tarreau <w@1wt.eu>
Signed-off-by: Paul E. McKenney <paulmck@kernel.org>
2022-10-09 18:29:36 +00:00
|
|
|
size_t strlen(const char *str)
|
2022-02-07 16:23:20 +00:00
|
|
|
{
|
|
|
|
size_t len;
|
|
|
|
|
tools/nolibc: Fix missing strlen() definition and infinite loop with gcc-12
When built at -Os, gcc-12 recognizes an strlen() pattern in nolibc_strlen()
and replaces it with a jump to strlen(), which is not defined as a symbol
and breaks compilation. Worse, when the function is called strlen(), the
function is simply replaced with a jump to itself, hence becomes an
infinite loop.
One way to avoid this is to always set -ffreestanding, but the calling
code doesn't know this and there's no way (either via attributes or
pragmas) to globally enable it from include files, effectively leaving
a painful situation for the caller.
Alexey suggested to place an empty asm() statement inside the loop to
stop gcc from recognizing a well-known pattern, which happens to work
pretty fine. At least it allows us to make sure our local definition
is not replaced with a self jump.
The function only needs to be renamed back to strlen() so that the symbol
exists, which implies that nolibc_strlen() which is used on variable
strings has to be declared as a macro that points back to it before the
strlen() macro is redifined.
It was verified to produce valid code with gcc 3.4 to 12.1 at different
optimization levels, and both with constant and variable strings.
In case this problem surfaces again in the future, an alternate approach
consisting in adding an optimize("no-tree-loop-distribute-patterns")
function attribute for gcc>=12 worked as well but is less pretty.
Reported-by: kernel test robot <yujie.liu@intel.com>
Link: https://lore.kernel.org/r/202210081618.754a77db-yujie.liu@intel.com
Fixes: 66b6f755ad45 ("rcutorture: Import a copy of nolibc")
Fixes: 96980b833a21 ("tools/nolibc/string: do not use __builtin_strlen() at -O0")
Cc: "Paul E. McKenney" <paulmck@kernel.org>
Cc: Alexey Dobriyan <adobriyan@gmail.com>
Signed-off-by: Willy Tarreau <w@1wt.eu>
Signed-off-by: Paul E. McKenney <paulmck@kernel.org>
2022-10-09 18:29:36 +00:00
|
|
|
for (len = 0; str[len]; len++)
|
|
|
|
asm("");
|
2022-02-07 16:23:20 +00:00
|
|
|
return len;
|
|
|
|
}
|
|
|
|
|
2022-03-23 07:18:06 +00:00
|
|
|
/* do not trust __builtin_constant_p() at -O0, as clang will emit a test and
|
|
|
|
* the two branches, then will rely on an external definition of strlen().
|
|
|
|
*/
|
|
|
|
#if defined(__OPTIMIZE__)
|
tools/nolibc: Fix missing strlen() definition and infinite loop with gcc-12
When built at -Os, gcc-12 recognizes an strlen() pattern in nolibc_strlen()
and replaces it with a jump to strlen(), which is not defined as a symbol
and breaks compilation. Worse, when the function is called strlen(), the
function is simply replaced with a jump to itself, hence becomes an
infinite loop.
One way to avoid this is to always set -ffreestanding, but the calling
code doesn't know this and there's no way (either via attributes or
pragmas) to globally enable it from include files, effectively leaving
a painful situation for the caller.
Alexey suggested to place an empty asm() statement inside the loop to
stop gcc from recognizing a well-known pattern, which happens to work
pretty fine. At least it allows us to make sure our local definition
is not replaced with a self jump.
The function only needs to be renamed back to strlen() so that the symbol
exists, which implies that nolibc_strlen() which is used on variable
strings has to be declared as a macro that points back to it before the
strlen() macro is redifined.
It was verified to produce valid code with gcc 3.4 to 12.1 at different
optimization levels, and both with constant and variable strings.
In case this problem surfaces again in the future, an alternate approach
consisting in adding an optimize("no-tree-loop-distribute-patterns")
function attribute for gcc>=12 worked as well but is less pretty.
Reported-by: kernel test robot <yujie.liu@intel.com>
Link: https://lore.kernel.org/r/202210081618.754a77db-yujie.liu@intel.com
Fixes: 66b6f755ad45 ("rcutorture: Import a copy of nolibc")
Fixes: 96980b833a21 ("tools/nolibc/string: do not use __builtin_strlen() at -O0")
Cc: "Paul E. McKenney" <paulmck@kernel.org>
Cc: Alexey Dobriyan <adobriyan@gmail.com>
Signed-off-by: Willy Tarreau <w@1wt.eu>
Signed-off-by: Paul E. McKenney <paulmck@kernel.org>
2022-10-09 18:29:36 +00:00
|
|
|
#define nolibc_strlen(x) strlen(x)
|
2022-02-07 16:23:20 +00:00
|
|
|
#define strlen(str) ({ \
|
|
|
|
__builtin_constant_p((str)) ? \
|
|
|
|
__builtin_strlen((str)) : \
|
|
|
|
nolibc_strlen((str)); \
|
|
|
|
})
|
2022-03-23 07:18:06 +00:00
|
|
|
#endif
|
2022-02-07 16:23:20 +00:00
|
|
|
|
2022-03-29 10:17:36 +00:00
|
|
|
static __attribute__((unused))
|
|
|
|
size_t strnlen(const char *str, size_t maxlen)
|
|
|
|
{
|
|
|
|
size_t len;
|
|
|
|
|
|
|
|
for (len = 0; (len < maxlen) && str[len]; len++);
|
|
|
|
return len;
|
|
|
|
}
|
|
|
|
|
2022-03-29 10:17:37 +00:00
|
|
|
static __attribute__((unused))
|
|
|
|
char *strdup(const char *str)
|
|
|
|
{
|
|
|
|
size_t len;
|
|
|
|
char *ret;
|
|
|
|
|
|
|
|
len = strlen(str);
|
|
|
|
ret = malloc(len + 1);
|
|
|
|
if (__builtin_expect(ret != NULL, 1))
|
|
|
|
memcpy(ret, str, len + 1);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static __attribute__((unused))
|
|
|
|
char *strndup(const char *str, size_t maxlen)
|
|
|
|
{
|
|
|
|
size_t len;
|
|
|
|
char *ret;
|
|
|
|
|
|
|
|
len = strnlen(str, maxlen);
|
|
|
|
ret = malloc(len + 1);
|
|
|
|
if (__builtin_expect(ret != NULL, 1)) {
|
|
|
|
memcpy(ret, str, len);
|
|
|
|
ret[len] = '\0';
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2022-02-07 16:23:43 +00:00
|
|
|
static __attribute__((unused))
|
|
|
|
size_t strlcat(char *dst, const char *src, size_t size)
|
|
|
|
{
|
|
|
|
size_t len;
|
|
|
|
char c;
|
|
|
|
|
|
|
|
for (len = 0; dst[len]; len++)
|
|
|
|
;
|
|
|
|
|
|
|
|
for (;;) {
|
|
|
|
c = *src;
|
|
|
|
if (len < size)
|
|
|
|
dst[len] = c;
|
|
|
|
if (!c)
|
|
|
|
break;
|
|
|
|
len++;
|
|
|
|
src++;
|
|
|
|
}
|
|
|
|
|
|
|
|
return len;
|
|
|
|
}
|
|
|
|
|
2022-02-07 16:23:42 +00:00
|
|
|
static __attribute__((unused))
|
|
|
|
size_t strlcpy(char *dst, const char *src, size_t size)
|
|
|
|
{
|
|
|
|
size_t len;
|
|
|
|
char c;
|
|
|
|
|
|
|
|
for (len = 0;;) {
|
|
|
|
c = src[len];
|
|
|
|
if (len < size)
|
|
|
|
dst[len] = c;
|
|
|
|
if (!c)
|
|
|
|
break;
|
|
|
|
len++;
|
|
|
|
}
|
|
|
|
return len;
|
|
|
|
}
|
|
|
|
|
2022-02-07 16:23:43 +00:00
|
|
|
static __attribute__((unused))
|
|
|
|
char *strncat(char *dst, const char *src, size_t size)
|
|
|
|
{
|
|
|
|
char *orig = dst;
|
|
|
|
|
|
|
|
while (*dst)
|
|
|
|
dst++;
|
|
|
|
|
|
|
|
while (size && (*dst = *src)) {
|
|
|
|
src++;
|
|
|
|
dst++;
|
|
|
|
size--;
|
|
|
|
}
|
|
|
|
|
|
|
|
*dst = 0;
|
|
|
|
return orig;
|
|
|
|
}
|
|
|
|
|
2022-03-21 17:33:10 +00:00
|
|
|
static __attribute__((unused))
|
|
|
|
int strncmp(const char *a, const char *b, size_t size)
|
|
|
|
{
|
|
|
|
unsigned int c;
|
|
|
|
int diff = 0;
|
|
|
|
|
|
|
|
while (size-- &&
|
|
|
|
!(diff = (unsigned char)*a++ - (c = (unsigned char)*b++)) && c)
|
|
|
|
;
|
|
|
|
|
|
|
|
return diff;
|
|
|
|
}
|
2022-02-07 16:23:43 +00:00
|
|
|
|
2022-02-07 16:23:42 +00:00
|
|
|
static __attribute__((unused))
|
|
|
|
char *strncpy(char *dst, const char *src, size_t size)
|
|
|
|
{
|
|
|
|
size_t len;
|
|
|
|
|
|
|
|
for (len = 0; len < size; len++)
|
|
|
|
if ((dst[len] = *src))
|
|
|
|
src++;
|
|
|
|
return dst;
|
|
|
|
}
|
|
|
|
|
2022-02-07 16:23:20 +00:00
|
|
|
static __attribute__((unused))
|
|
|
|
char *strrchr(const char *s, int c)
|
|
|
|
{
|
|
|
|
const char *ret = NULL;
|
|
|
|
|
|
|
|
while (*s) {
|
|
|
|
if (*s == (char)c)
|
|
|
|
ret = s;
|
|
|
|
s++;
|
|
|
|
}
|
|
|
|
return (char *)ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif /* _NOLIBC_STRING_H */
|