From ce7c098db258a0c4ba51c2df41732f37ca6dfe32 Mon Sep 17 00:00:00 2001 From: Augustin Cavalier Date: Mon, 24 Feb 2025 14:12:28 -0500 Subject: [PATCH] libroot: Replace memmove() with musl's. Includes an optimization to use memcpy where possible, and protection against aliasing undefined behavior. Also clean up the Jamrules for the kernel's usage of musl methods. --- src/system/boot/Jamfile | 1 + src/system/kernel/lib/Jamfile | 26 ++++--- src/system/libroot/posix/musl/string/Jamfile | 1 + .../libroot/posix/musl/string/memmove.c | 42 ++++++++++ src/system/libroot/posix/string/Jamfile | 1 - src/system/libroot/posix/string/memmove.c | 78 ------------------- src/system/runtime_loader/Jamfile | 2 +- 7 files changed, 60 insertions(+), 91 deletions(-) create mode 100644 src/system/libroot/posix/musl/string/memmove.c delete mode 100644 src/system/libroot/posix/string/memmove.c diff --git a/src/system/boot/Jamfile b/src/system/boot/Jamfile index 1a0f09148e..50a57e6c47 100644 --- a/src/system/boot/Jamfile +++ b/src/system/boot/Jamfile @@ -299,6 +299,7 @@ for platform in [ MultiBootSubDirSetup ] { $(TARGET_KERNEL_ARCH_DIR) ] ; } + SEARCH_SOURCE += [ FDirName $(SUBDIR) $(DOTDOT) libroot posix musl string ] ; SEARCH_SOURCE += [ FDirName $(SUBDIR) $(DOTDOT) libroot posix string ] ; SEARCH_SOURCE += [ FDirName $(SUBDIR) $(DOTDOT) libroot posix stdlib ] ; SEARCH_SOURCE += [ FDirName $(SUBDIR) $(DOTDOT) libroot posix locale ] ; diff --git a/src/system/kernel/lib/Jamfile b/src/system/kernel/lib/Jamfile index 4a1ad58b38..339dc87eff 100644 --- a/src/system/kernel/lib/Jamfile +++ b/src/system/kernel/lib/Jamfile @@ -45,20 +45,13 @@ SEARCH_SOURCE += [ FDirName $(posixSources) string ] ; SEARCH_SOURCE += [ FDirName $(posixSources) sys ] ; SEARCH_SOURCE += [ FDirName $(posixSources) time ] ; SEARCH_SOURCE += [ FDirName $(posixSources) unistd ] ; -SEARCH_SOURCE += [ FDirName $(gnuSources) ] ; +SEARCH_SOURCE += [ FDirName $(posixSources) musl string ] ; +#SEARCH_SOURCE += [ FDirName $(gnuSources) ] ; local muslSources = ffs.c rand.c rand_r.c - - memmem.c - stpcpy.c - strchrnul.c - strcspn.c - strpbrk.c - strspn.c - strstr.c ; SourceHdrs $(muslSources) : @@ -76,12 +69,14 @@ KernelMergeObject kernel_lib_posix.o : fcntl.cpp poll.cpp utime.c + # locale ctype_loc.cpp ctype_l.cpp ctype.cpp localeconv.cpp LocaleData.cpp + # stdlib abs.c atoi.c @@ -94,6 +89,7 @@ KernelMergeObject kernel_lib_posix.o : strtoll.c strtoul.c strtoull.c + # sys chmod.c stat.c @@ -101,13 +97,14 @@ KernelMergeObject kernel_lib_posix.o : select.cpp gettimeofday.c uio.c + # time time.c + # unistd access.c chown.c close.c - #conf.c directory.c dup.c ioctl.c @@ -119,16 +116,21 @@ KernelMergeObject kernel_lib_posix.o : truncate.c usergroup.cpp write.c + # string memchr.c memcmp.c + memmem.c memmove.c + stpcpy.c strcasecmp.c strcasestr.c strcat.c strchr.c + strchrnul.c strcmp.c strcpy.c + strcspn.c strdup.cpp strerror.c strlcat.c @@ -139,7 +141,10 @@ KernelMergeObject kernel_lib_posix.o : strncpy.cpp strndup.cpp strnlen.cpp + strpbrk.c strrchr.c + strspn.c + strstr.c strtok.c strupr.c @@ -149,7 +154,6 @@ KernelMergeObject kernel_lib_posix.o : SEARCH on [ FGristFiles $(muslSources) ] += [ FDirName $(posixSources) musl misc ] ; SEARCH on [ FGristFiles $(muslSources) ] += [ FDirName $(posixSources) musl prng ] ; -SEARCH on [ FGristFiles $(muslSources) ] += [ FDirName $(posixSources) musl string ] ; # misc diff --git a/src/system/libroot/posix/musl/string/Jamfile b/src/system/libroot/posix/musl/string/Jamfile index 5dbec07d50..dcf84a9dac 100644 --- a/src/system/libroot/posix/musl/string/Jamfile +++ b/src/system/libroot/posix/musl/string/Jamfile @@ -11,6 +11,7 @@ for architectureObject in [ MultiArchSubDirSetup ] { MergeObject <$(architecture)>posix_musl_string.o : memccpy.c memmem.c + memmove.c memrchr.c stpcpy.c stpncpy.c diff --git a/src/system/libroot/posix/musl/string/memmove.c b/src/system/libroot/posix/musl/string/memmove.c new file mode 100644 index 0000000000..5e025fd9a2 --- /dev/null +++ b/src/system/libroot/posix/musl/string/memmove.c @@ -0,0 +1,42 @@ +#include +#include + +#if defined(__GNUC__) && __GNUC__ >= 4 +typedef __attribute__((__may_alias__)) size_t WT; +#define WS (sizeof(WT)) +#endif + +void *memmove(void *dest, const void *src, size_t n) +{ + char *d = dest; + const char *s = src; + + if (d==s) return d; + if ((uintptr_t)s-(uintptr_t)d-n <= -2*n) return memcpy(d, s, n); + + if (d= 4 + if ((uintptr_t)s % WS == (uintptr_t)d % WS) { + while ((uintptr_t)d % WS) { + if (!n--) return dest; + *d++ = *s++; + } + for (; n>=WS; n-=WS, d+=WS, s+=WS) *(WT *)d = *(WT *)s; + } +#endif + for (; n; n--) *d++ = *s++; + } else { +#if defined(__GNUC__) && __GNUC__ >= 4 + if ((uintptr_t)s % WS == (uintptr_t)d % WS) { + while ((uintptr_t)(d+n) % WS) { + if (!n--) return dest; + d[n] = s[n]; + } + while (n>=WS) n-=WS, *(WT *)(d+n) = *(WT *)(s+n); + } +#endif + while (n) n--, d[n] = s[n]; + } + + return dest; +} diff --git a/src/system/libroot/posix/string/Jamfile b/src/system/libroot/posix/string/Jamfile index 355e759d95..ce6af35138 100644 --- a/src/system/libroot/posix/string/Jamfile +++ b/src/system/libroot/posix/string/Jamfile @@ -22,7 +22,6 @@ for architectureObject in [ MultiArchSubDirSetup ] { bzero.c memchr.c memcmp.c - memmove.c strcasecmp.c strcasestr.c strcat.c diff --git a/src/system/libroot/posix/string/memmove.c b/src/system/libroot/posix/string/memmove.c deleted file mode 100644 index 0fe136336d..0000000000 --- a/src/system/libroot/posix/string/memmove.c +++ /dev/null @@ -1,78 +0,0 @@ -/* - * Copyright 2001, Travis Geiselbrecht. All rights reserved. - * Distributed under the terms of the NewOS License. - */ - - -#include -#include - -#if !_ASM_MEMCPY - -typedef int word; - -#define lsize sizeof(word) -#define lmask (lsize - 1) - - -void* -memmove(void* dest, void const* src, size_t count) -{ - char* d = (char*)dest; - const char* s = (const char*)src; - int len; - - if (count == 0 || dest == src) - return dest; - - if ((long)d < (long)s) { - if (((long)d | (long)s) & lmask) { - // src and/or dest do not align on word boundary - if ((((long)d ^ (long)s) & lmask) || (count < lsize)) - len = count; // copy the rest of the buffer with the byte mover - else - len = lsize - ((long)d & lmask); // move the ptrs up to a word boundary - - count -= len; - for (; len > 0; len--) - *d++ = *s++; - } - for (len = count / lsize; len > 0; len--) { - *(word*)d = *(word*)s; - d += lsize; - s += lsize; - } - for (len = count & lmask; len > 0; len--) - *d++ = *s++; - } else { - d += count; - s += count; - if (((long)d | (long)s) & lmask) { - // src and/or dest do not align on word boundary - if ((((long)d ^ (long)s) & lmask) || (count <= lsize)) - len = count; - else - len = ((long)d & lmask); - - count -= len; - for (; len > 0; len--) - *--d = *--s; - } - for (len = count / lsize; len > 0; len--) { - d -= lsize; - s -= lsize; - *(word*)d = *(word*)s; - } - for (len = count & lmask; len > 0; len--) - *--d = *--s; - } - - return dest; -} - -#if defined(__arm__) -void* __aeabi_memmove(void* dest, void const* src, size_t count) - __attribute__((__alias__("memmove"))); -#endif - -#endif diff --git a/src/system/runtime_loader/Jamfile b/src/system/runtime_loader/Jamfile index 8c3f2a68be..3e0ddf9f16 100644 --- a/src/system/runtime_loader/Jamfile +++ b/src/system/runtime_loader/Jamfile @@ -52,6 +52,7 @@ for architectureObject in [ MultiArchSubDirSetup ] { ctype_loc.o LocaleData.o + memmove.o strchrnul.o strcspn.o strpbrk.o @@ -60,7 +61,6 @@ for architectureObject in [ MultiArchSubDirSetup ] { memchr.o memcmp.o - memmove.o strcasecmp.o strcat.o strchr.o