Fix XNU / FreeBSD / OpenBSD / RHEL5 / NT bugs

For the first time ever, all tests in this codebase now pass, when
run automatically on macos, freebsd, openbsd, rhel5, rhel7, alpine
and windows via the network using the runit and runitd build tools

- Fix vfork exec path etc.
- Add XNU opendir() support
- Add OpenBSD opendir() support
- Add Linux history to syscalls.sh
- Use copy_file_range on FreeBSD 13+
- Fix system calls with 7+ arguments
- Fix Windows with greater than 16 FDs
- Fix RUNIT.COM and RUNITD.COM flakiness
- Fix OpenBSD munmap() when files are mapped
- Fix long double so it's actually long on Windows
- Fix OpenBSD truncate() and ftruncate() thunk typo
- Let Windows fcntl() be used on socket files descriptors
- Fix Windows fstat() which had an accidental printf statement
- Fix RHEL5 CLOCK_MONOTONIC by not aliasing to CLOCK_MONOTONIC_RAW

This is wonderful. I never could have dreamed it would be possible
to get it working so well on so many platforms with tiny binaries.

Fixes #31
Fixes #25
Fixes #14
This commit is contained in:
Justine Tunney 2021-01-25 13:08:05 -08:00
parent c20dad3534
commit 45b72485ad
1032 changed files with 6083 additions and 2348 deletions

View file

@ -26,11 +26,13 @@
*/
char *chomp(char *line) {
size_t i;
for (i = strlen(line); i--;) {
if (line[i] == '\r' || line[i] == '\n') {
line[i] = '\0';
} else {
break;
if (line) {
for (i = strlen(line); i--;) {
if (line[i] == '\r' || line[i] == '\n') {
line[i] = '\0';
} else {
break;
}
}
}
return line;

84
libc/str/memmem.c Normal file
View file

@ -0,0 +1,84 @@
/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│
vi: set net ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi
Copyright 2020 Justine Alexandra Roberts Tunney
Permission to use, copy, modify, and/or distribute this software for
any purpose with or without fee is hereby granted, provided that the
above copyright notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE
AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE.
*/
#include "libc/macros.h"
#include "libc/mem/alloca.h"
#include "libc/str/str.h"
static void KnuthMorrisPrattInit(ssize_t m, ssize_t *T, const char *W) {
ssize_t i = 2;
ssize_t j = 0;
T[0] = -1;
T[1] = 0;
while (i < m) {
if (W[i - 1] == W[j]) {
T[i++] = j++ + 1;
} else if (j > 0) {
j = T[j];
} else {
T[i++] = 0;
}
}
T[m] = 0;
}
static size_t KnuthMorrisPratt(long m, const long *T, const char *W, long n,
const char *S) {
long i = 0, j = 0;
while (i + j < n) {
if (W[i] == S[i + j]) {
i++;
if (i == m) break;
} else {
j = j + i - T[i];
if (i > 0) i = T[i];
}
}
return j;
}
/**
* Searches for fixed-length substring in memory region.
*
* @param haystack is the region of memory to be searched
* @param haystacklen is its character count
* @param needle contains the memory for which we're searching
* @param needlelen is its character count
* @return pointer to first result or NULL if not found
*/
void *memmem(const void *haystackp, size_t haystacklen, const void *needlep,
size_t needlelen) {
long *T;
size_t i;
const char *haystack, *needle, *h;
needle = needlep;
haystack = haystackp;
if (needlelen > haystacklen) return NULL;
if (!needlelen) return haystack;
h = memchr(haystack, *needle, haystacklen);
if (!h || needlelen == 1) return h;
haystacklen -= h - haystack;
if (needlelen < haystacklen && memcmp(h, needle, needlelen) == 0) {
return h;
} else {
T = alloca((needlelen + 1) * sizeof(long));
KnuthMorrisPrattInit(needlelen, T, needle);
i = KnuthMorrisPratt(needlelen, T, needle, haystacklen, h);
return i < haystacklen ? h + i : NULL;
}
}

View file

@ -175,6 +175,8 @@ compatfn wchar_t *wmemcpy(wchar_t *, const wchar_t *, size_t) memcpyesque;
compatfn wchar_t *wmempcpy(wchar_t *, const wchar_t *, size_t) memcpyesque;
compatfn wchar_t *wmemmove(wchar_t *, const wchar_t *, size_t) memcpyesque;
int timingsafe_memcmp(const void *, const void *, size_t);
void *memmem(const void *, size_t, const void *, size_t)
paramsnonnull() nothrow nocallback nosideeffect;
char *tinystrstr(const char *, const char *) strlenesque;
char16_t *tinystrstr16(const char16_t *, const char16_t *) strlenesque;

View file

@ -42,6 +42,10 @@ $(LIBC_STR_A).pkg: \
$(LIBC_STR_A_OBJS) \
$(foreach x,$(LIBC_STR_A_DIRECTDEPS),$($(x)_A).pkg)
o/$(MODE)/libc/str/memmem.o: \
OVERRIDE_CPPFLAGS += \
-DSTACK_FRAME_UNLIMITED
LIBC_STR_LIBS = $(foreach x,$(LIBC_STR_ARTIFACTS),$($(x)))
LIBC_STR_SRCS = $(foreach x,$(LIBC_STR_ARTIFACTS),$($(x)_SRCS))
LIBC_STR_HDRS = $(foreach x,$(LIBC_STR_ARTIFACTS),$($(x)_HDRS))

View file

@ -26,4 +26,6 @@
* @return 𝑑
* @asyncsignalsafe
*/
char *strcat(char *d, const char *s) { return strcpy(d + strlen(d), s); }
char *strcat(char *d, const char *s) {
return strcpy(d + strlen(d), s);
}

View file

@ -19,7 +19,6 @@
#include "libc/intrin/pcmpeqb.h"
#include "libc/intrin/pmovmskb.h"
#include "libc/limits.h"
#include "libc/nexgen32e/bsf.h"
#include "libc/str/str.h"
/**

View file

@ -22,7 +22,6 @@
* Prepares static search buffer.
*
* 1. If SRC is too long, it's truncated and *not* NUL-terminated.
*
* 2. If SRC is too short, the remainder is zero-filled.
*
* Please note this function isn't designed to prevent untrustworthy

View file

@ -16,9 +16,6 @@
TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE.
*/
#include "libc/alg/alg.h"
#include "libc/nexgen32e/x86feature.h"
#include "libc/str/internal.h"
#include "libc/str/str.h"
/**
@ -31,29 +28,5 @@
* @see memmem()
*/
char *strstr(const char *haystack, const char *needle) {
size_t i;
if (needle[0]) {
if (needle[1]) {
for (;;) {
#if 0 /* todo: fix me */
if (!((uintptr_t)haystack & 15) && X86_HAVE(SSE4_2) &&
(((uintptr_t)needle + strlen(needle)) & 0xfff) <= 0xff0) {
return strstr$sse42(haystack, needle);
}
#endif
for (i = 0;;) {
if (!needle[i]) return haystack;
if (!haystack[i]) break;
if (needle[i] != haystack[i]) break;
++i;
}
if (!*haystack++) break;
}
return NULL;
} else {
return strchr(haystack, needle[0]);
}
} else {
return haystack;
}
return memmem(haystack, strlen(haystack), needle, strlen(needle));
}

View file

@ -16,22 +16,18 @@
TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE.
*/
#include "libc/alg/alg.h"
#include "libc/dce.h"
#include "libc/str/internal.h"
#include "libc/str/str.h"
#undef memmem
#undef strlen
#undef strstr
#undef strchr
#define char char16_t
#define memmem memmem16
#define strlen strlen16
#define strstr strstr16
#define strchr strchr16
#define strstr$sse42 strstr16$sse42
#define tinystrstr tinystrstr16
#include "libc/str/strstr.c"
/**
* Searches for substring.
*
* @param haystack is the search area, as a NUL-terminated string
* @param needle is the desired substring, also NUL-terminated
* @return pointer to first substring within haystack, or NULL
* @asyncsignalsafe
* @see memmem()
*/
char16_t *strstr16(const char16_t *haystack, const char16_t *needle) {
return memmem(haystack, strlen16(haystack) * sizeof(char16_t), needle,
strlen16(needle) * sizeof(char16_t));
}

33
libc/str/wcslen.c Normal file
View file

@ -0,0 +1,33 @@
/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│
vi: set net ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi
Copyright 2020 Justine Alexandra Roberts Tunney
Permission to use, copy, modify, and/or distribute this software for
any purpose with or without fee is hereby granted, provided that the
above copyright notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE
AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE.
*/
#include "libc/str/str.h"
/**
* Searches for substring.
*
* @param haystack is the search area, as a NUL-terminated string
* @param needle is the desired substring, also NUL-terminated
* @return pointer to first substring within haystack, or NULL
* @asyncsignalsafe
* @see memmem()
*/
wchar_t *wcsstr(const wchar_t *haystack, const wchar_t *needle) {
return memmem(haystack, wcslen(haystack) * sizeof(wchar_t), needle,
wcslen(needle) * sizeof(wchar_t));
}