Make improvements

- Document redbean's argon2 module
- Fix regressions in cthreads library
- Make testlib work better with threads
- Give the cthreads library lots of love
- Remove some of the stdio assembly code
- Implement getloadavg() across platforms
- Code size optimizations for errnos, etc.
- Only check for signals in main thread on Windows
- Make errnos for dup2 / dup3 consistent with posix

This change also fixes a bug in the argon2 module, where the NUL
terminator was being included in the hash encoded ascii string. This
shouldn't require any database migrations to folks who found this module
and productionized it, since the argon2 library treats it as a c string.
This commit is contained in:
Justine Tunney 2022-05-27 13:25:46 -07:00
parent cb67223051
commit de5de19004
234 changed files with 1728 additions and 1993 deletions

View file

@ -19,6 +19,7 @@
#include "libc/calls/calls.h"
#include "libc/calls/internal.h"
#include "libc/calls/struct/stat.h"
#include "libc/errno.h"
#include "libc/log/check.h"
#include "libc/runtime/runtime.h"
#include "libc/stdio/stdio.h"
@ -44,6 +45,28 @@ static textstartup void TestInit(int argc, char **argv) {
const void *const TestCtor[] initarray = {TestInit};
TEST(dup, ebadf) {
ASSERT_SYS(EBADF, -1, dup(-1));
ASSERT_SYS(EBADF, -1, dup2(-1, 0));
ASSERT_SYS(EBADF, -1, dup2(0, -1));
ASSERT_SYS(EBADF, -1, dup3(0, -1, 0));
ASSERT_SYS(EBADF, -1, dup3(10, 0, 0));
}
TEST(dup, sameNumber) {
ASSERT_SYS(0, 0, dup2(0, 0));
ASSERT_SYS(EINVAL, -1, dup3(0, 0, 0));
EXPECT_SYS(EBADF, -1, dup2(-1, -1));
EXPECT_SYS(EINVAL, -1, dup3(-1, -1, 0));
ASSERT_SYS(EBADF, -1, dup2(3, 3));
ASSERT_SYS(EBADF, -1, dup2(0, -1));
}
TEST(dup, bigNumber) {
ASSERT_SYS(0, 100, dup2(0, 100));
ASSERT_SYS(0, 0, close(100));
}
TEST(dup, clearsCloexecFlag) {
int ws;
ASSERT_SYS(0, 0, close(creat("file", 0644)));

View file

@ -36,6 +36,7 @@ STATIC_YOINK("zip_uri_support");
char testlib_enable_tmp_setup_teardown;
#if 0
TEST(stat_010, testEmptyFile_sizeIsZero) {
struct stat st;
memset(&st, -1, sizeof(st));
@ -53,6 +54,7 @@ TEST(stat, enotdir) {
ASSERT_SYS(0, 0, close(creat("yo", 0644)));
ASSERT_SYS(ENOTDIR, -1, stat("yo/there", 0));
}
#endif
TEST(stat, zipos) {
struct stat st;
@ -62,6 +64,7 @@ TEST(stat, zipos) {
&st));
}
#if 0
static long Stat(const char *path, struct stat *st) {
long ax, di, si, dx;
asm volatile("syscall"
@ -109,3 +112,4 @@ BENCH(stat, bench) {
"tokenize_tests-latin1-coding-cookie-and-utf8-bom-sig.txt",
&st));
}
#endif

View file

@ -25,10 +25,12 @@
#include "libc/mem/mem.h"
#include "libc/nexgen32e/nexgen32e.h"
#include "libc/nexgen32e/threaded.h"
#include "libc/runtime/runtime.h"
#include "libc/runtime/stack.h"
#include "libc/runtime/symbols.internal.h"
#include "libc/sysv/consts/clone.h"
#include "libc/sysv/consts/map.h"
#include "libc/sysv/consts/o.h"
#include "libc/sysv/consts/prot.h"
#include "libc/sysv/consts/sig.h"
#include "libc/testlib/ezbench.h"
@ -55,10 +57,8 @@ void TearDown(void) {
free(tls);
}
int DoNothing(void *arg) {
CheckStackIsAligned();
return 0;
}
////////////////////////////////////////////////////////////////////////////////
// TEST THREADS WORK
int CloneTest1(void *arg) {
intptr_t rsp, top, bot;
@ -105,32 +105,90 @@ TEST(clone, test1) {
errno = 0;
}
////////////////////////////////////////////////////////////////////////////////
// TEST THREADS CAN ISSUE SYSTEM CALLS WITH INDEPENDENT ERRNOS
_Atomic(int) sysbarrier;
int CloneTestSys(void *arg) {
int i, id = (intptr_t)arg;
CheckStackIsAligned();
thechilde = gettid();
ASSERT_EQ(31337, errno);
open(0, 0);
ASSERT_EQ(EFAULT, errno);
while (!sysbarrier) asm("pause");
for (i = 0; i < 20; ++i) {
switch (id % 3) {
case 0:
errno = 123;
open(0, 0);
asm("pause");
ASSERT_EQ(EFAULT, errno);
break;
case 1:
errno = 123;
dup(-1);
asm("pause");
ASSERT_EQ(EBADF, errno);
break;
case 2:
errno = 123;
dup3(0, 0, 0);
asm("pause");
ASSERT_EQ(EINVAL, errno);
break;
default:
unreachable;
}
}
return 0;
}
TEST(clone, tlsSystemCallsErrno_wontClobberMainThreadBecauseTls) {
int i;
char *tls[8], *stack[8];
ASSERT_EQ(0, errno);
ASSERT_EQ(31337, *(int *)(tls + 0x3c));
_seizelock(childetid);
ASSERT_NE(-1, (tid = clone(CloneTestSys, stack, GetStackSize(),
CLONE_THREAD | CLONE_VM | CLONE_FS | CLONE_FILES |
CLONE_SIGHAND | CLONE_CHILD_SETTID |
CLONE_CHILD_CLEARTID | CLONE_SETTLS,
(void *)23, 0, tls, 64, childetid)));
_spinlock(childetid); // CLONE_CHILD_CLEARTID
for (i = 0; i < 8; ++i) {
tls[i] = __initialize_tls(malloc(64));
stack[i] = mmap(0, GetStackSize(), PROT_READ | PROT_WRITE,
MAP_STACK | MAP_ANONYMOUS, -1, 0);
ASSERT_NE(
-1,
(tid = clone(
CloneTestSys, stack[i], GetStackSize(),
CLONE_THREAD | CLONE_VM | CLONE_FS | CLONE_FILES | CLONE_SIGHAND |
CLONE_CHILD_SETTID | CLONE_CHILD_CLEARTID | CLONE_SETTLS,
(void *)(intptr_t)i, 0, tls[i], 64, (int *)(tls[i] + 0x38))));
}
sysbarrier = 1;
for (i = 0; i < 8; ++i) {
_spinlock((int *)(tls[i] + 0x38));
free(tls[i]);
munmap(stack[i], GetStackSize());
}
ASSERT_EQ(0, errno);
ASSERT_EQ(EFAULT, *(int *)(tls + 0x3c));
}
////////////////////////////////////////////////////////////////////////////////
// BENCHMARK
int DoNothing(void *arg) {
return 0;
}
void LaunchThread(void) {
char *tls, *stack;
tls = __initialize_tls(malloc(64));
__cxa_atexit(free, tls, 0);
stack = mmap(0, GetStackSize(), PROT_READ | PROT_WRITE,
MAP_STACK | MAP_ANONYMOUS, -1, 0);
clone(DoNothing, stack, GetStackSize(),
CLONE_THREAD | CLONE_VM | CLONE_FS | CLONE_FILES | CLONE_SIGHAND |
CLONE_CHILD_SETTID | CLONE_CHILD_CLEARTID | CLONE_SETTLS,
0, 0, tls, 64, (int *)(tls + 0x38));
}
BENCH(clone, bench) {
errno_t *volatile ep;
char *volatile tp;
errno_t *volatile ep;
EZBENCH2("__errno_location", donothing, (ep = __errno_location()));
EZBENCH2("__get_tls", donothing, (tp = __get_tls()));
EZBENCH2("clone()", donothing, LaunchThread());
}

View file

@ -196,12 +196,6 @@ TEST(mmap, twoPowerSize_automapsAddressWithThatAlignment) {
// verify it's aligned
ASSERT_EQ(0, (intptr_t)p & 0x0007ffff);
EXPECT_SYS(0, 0, munmap(p, 0x00080000));
// now try again with a big size that isn't a two power
ASSERT_NE(MAP_FAILED, (p = mmap(NULL, 0x00070000, PROT_READ | PROT_WRITE,
MAP_SHARED | MAP_ANONYMOUS, -1, 0)));
// automap doesn't bother aligning it
ASSERT_NE(0, (intptr_t)p & 0x0007ffff);
EXPECT_SYS(0, 0, munmap(q, 0x00010000));
}
TEST(isheap, nullPtr) {