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.
This commit is contained in:
Augustin Cavalier
2025-02-24 14:12:28 -05:00
parent 1ee5848178
commit ce7c098db2
7 changed files with 60 additions and 91 deletions
+1
View File
@@ -299,6 +299,7 @@ for platform in [ MultiBootSubDirSetup ] {
$(TARGET_KERNEL_ARCH_DIR) ] ; $(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 string ] ;
SEARCH_SOURCE += [ FDirName $(SUBDIR) $(DOTDOT) libroot posix stdlib ] ; SEARCH_SOURCE += [ FDirName $(SUBDIR) $(DOTDOT) libroot posix stdlib ] ;
SEARCH_SOURCE += [ FDirName $(SUBDIR) $(DOTDOT) libroot posix locale ] ; SEARCH_SOURCE += [ FDirName $(SUBDIR) $(DOTDOT) libroot posix locale ] ;
+15 -11
View File
@@ -45,20 +45,13 @@ SEARCH_SOURCE += [ FDirName $(posixSources) string ] ;
SEARCH_SOURCE += [ FDirName $(posixSources) sys ] ; SEARCH_SOURCE += [ FDirName $(posixSources) sys ] ;
SEARCH_SOURCE += [ FDirName $(posixSources) time ] ; SEARCH_SOURCE += [ FDirName $(posixSources) time ] ;
SEARCH_SOURCE += [ FDirName $(posixSources) unistd ] ; SEARCH_SOURCE += [ FDirName $(posixSources) unistd ] ;
SEARCH_SOURCE += [ FDirName $(gnuSources) ] ; SEARCH_SOURCE += [ FDirName $(posixSources) musl string ] ;
#SEARCH_SOURCE += [ FDirName $(gnuSources) ] ;
local muslSources = local muslSources =
ffs.c ffs.c
rand.c rand.c
rand_r.c rand_r.c
memmem.c
stpcpy.c
strchrnul.c
strcspn.c
strpbrk.c
strspn.c
strstr.c
; ;
SourceHdrs $(muslSources) : SourceHdrs $(muslSources) :
@@ -76,12 +69,14 @@ KernelMergeObject kernel_lib_posix.o :
fcntl.cpp fcntl.cpp
poll.cpp poll.cpp
utime.c utime.c
# locale # locale
ctype_loc.cpp ctype_loc.cpp
ctype_l.cpp ctype_l.cpp
ctype.cpp ctype.cpp
localeconv.cpp localeconv.cpp
LocaleData.cpp LocaleData.cpp
# stdlib # stdlib
abs.c abs.c
atoi.c atoi.c
@@ -94,6 +89,7 @@ KernelMergeObject kernel_lib_posix.o :
strtoll.c strtoll.c
strtoul.c strtoul.c
strtoull.c strtoull.c
# sys # sys
chmod.c chmod.c
stat.c stat.c
@@ -101,13 +97,14 @@ KernelMergeObject kernel_lib_posix.o :
select.cpp select.cpp
gettimeofday.c gettimeofday.c
uio.c uio.c
# time # time
time.c time.c
# unistd # unistd
access.c access.c
chown.c chown.c
close.c close.c
#conf.c
directory.c directory.c
dup.c dup.c
ioctl.c ioctl.c
@@ -119,16 +116,21 @@ KernelMergeObject kernel_lib_posix.o :
truncate.c truncate.c
usergroup.cpp usergroup.cpp
write.c write.c
# string # string
memchr.c memchr.c
memcmp.c memcmp.c
memmem.c
memmove.c memmove.c
stpcpy.c
strcasecmp.c strcasecmp.c
strcasestr.c strcasestr.c
strcat.c strcat.c
strchr.c strchr.c
strchrnul.c
strcmp.c strcmp.c
strcpy.c strcpy.c
strcspn.c
strdup.cpp strdup.cpp
strerror.c strerror.c
strlcat.c strlcat.c
@@ -139,7 +141,10 @@ KernelMergeObject kernel_lib_posix.o :
strncpy.cpp strncpy.cpp
strndup.cpp strndup.cpp
strnlen.cpp strnlen.cpp
strpbrk.c
strrchr.c strrchr.c
strspn.c
strstr.c
strtok.c strtok.c
strupr.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 misc ] ;
SEARCH on [ FGristFiles $(muslSources) ] += [ FDirName $(posixSources) musl prng ] ; SEARCH on [ FGristFiles $(muslSources) ] += [ FDirName $(posixSources) musl prng ] ;
SEARCH on [ FGristFiles $(muslSources) ] += [ FDirName $(posixSources) musl string ] ;
# misc # misc
@@ -11,6 +11,7 @@ for architectureObject in [ MultiArchSubDirSetup ] {
MergeObject <$(architecture)>posix_musl_string.o : MergeObject <$(architecture)>posix_musl_string.o :
memccpy.c memccpy.c
memmem.c memmem.c
memmove.c
memrchr.c memrchr.c
stpcpy.c stpcpy.c
stpncpy.c stpncpy.c
@@ -0,0 +1,42 @@
#include <string.h>
#include <stdint.h>
#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<s) {
#if defined(__GNUC__) && __GNUC__ >= 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;
}
-1
View File
@@ -22,7 +22,6 @@ for architectureObject in [ MultiArchSubDirSetup ] {
bzero.c bzero.c
memchr.c memchr.c
memcmp.c memcmp.c
memmove.c
strcasecmp.c strcasecmp.c
strcasestr.c strcasestr.c
strcat.c strcat.c
-78
View File
@@ -1,78 +0,0 @@
/*
* Copyright 2001, Travis Geiselbrecht. All rights reserved.
* Distributed under the terms of the NewOS License.
*/
#include <sys/types.h>
#include <string.h>
#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
+1 -1
View File
@@ -52,6 +52,7 @@ for architectureObject in [ MultiArchSubDirSetup ] {
<src!system!libroot!posix!locale!$(architecture)>ctype_loc.o <src!system!libroot!posix!locale!$(architecture)>ctype_loc.o
<src!system!libroot!posix!locale!$(architecture)>LocaleData.o <src!system!libroot!posix!locale!$(architecture)>LocaleData.o
<src!system!libroot!posix!musl!string!$(architecture)>memmove.o
<src!system!libroot!posix!musl!string!$(architecture)>strchrnul.o <src!system!libroot!posix!musl!string!$(architecture)>strchrnul.o
<src!system!libroot!posix!musl!string!$(architecture)>strcspn.o <src!system!libroot!posix!musl!string!$(architecture)>strcspn.o
<src!system!libroot!posix!musl!string!$(architecture)>strpbrk.o <src!system!libroot!posix!musl!string!$(architecture)>strpbrk.o
@@ -60,7 +61,6 @@ for architectureObject in [ MultiArchSubDirSetup ] {
<src!system!libroot!posix!string!$(architecture)>memchr.o <src!system!libroot!posix!string!$(architecture)>memchr.o
<src!system!libroot!posix!string!$(architecture)>memcmp.o <src!system!libroot!posix!string!$(architecture)>memcmp.o
<src!system!libroot!posix!string!$(architecture)>memmove.o
<src!system!libroot!posix!string!$(architecture)>strcasecmp.o <src!system!libroot!posix!string!$(architecture)>strcasecmp.o
<src!system!libroot!posix!string!$(architecture)>strcat.o <src!system!libroot!posix!string!$(architecture)>strcat.o
<src!system!libroot!posix!string!$(architecture)>strchr.o <src!system!libroot!posix!string!$(architecture)>strchr.o