mirror of
https://github.com/jart/cosmopolitan.git
synced 2025-07-24 11:30:29 +00:00
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:
parent
cb67223051
commit
de5de19004
234 changed files with 1728 additions and 1993 deletions
|
@ -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)));
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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());
|
||||
}
|
||||
|
|
|
@ -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) {
|
||||
|
|
|
@ -16,12 +16,39 @@
|
|||
│ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │
|
||||
│ PERFORMANCE OF THIS SOFTWARE. │
|
||||
╚─────────────────────────────────────────────────────────────────────────────*/
|
||||
#include "libc/dce.h"
|
||||
#include "libc/testlib/ezbench.h"
|
||||
#include "libc/testlib/testlib.h"
|
||||
#include "net/https/https.h"
|
||||
#include "third_party/argon2/argon2.h"
|
||||
|
||||
TEST(argon2, test) {
|
||||
TEST(argon2, testArgon2ReadmeExample) {
|
||||
uint8_t hash[24];
|
||||
uint8_t salt[8] = {'s', 'o', 'm', 'e', 's', 'a', 'l', 't'};
|
||||
uint8_t pass[8] = {'p', 'a', 's', 's', 'w', 'o', 'r', 'd'};
|
||||
uint32_t parallelism = 4;
|
||||
uint32_t memorycost = 1 << 16;
|
||||
uint32_t iterations = 2;
|
||||
argon2i_hash_raw(iterations, memorycost, parallelism, pass, sizeof(pass),
|
||||
salt, sizeof(salt), hash, sizeof(hash));
|
||||
ASSERT_BINEQ("45d7ac72e76f242b20b77b9bf9bf9d5915894e669a24e6c6", hash);
|
||||
}
|
||||
|
||||
TEST(argon2, testArgon2ReadmeExampleEncoded) {
|
||||
char hash[128];
|
||||
uint8_t salt[8] = {'s', 'o', 'm', 'e', 's', 'a', 'l', 't'};
|
||||
uint8_t pass[8] = {'p', 'a', 's', 's', 'w', 'o', 'r', 'd'};
|
||||
uint32_t parallelism = 4;
|
||||
uint32_t memorycost = 1 << 16;
|
||||
uint32_t iterations = 2;
|
||||
argon2i_hash_encoded(iterations, memorycost, parallelism, pass, sizeof(pass),
|
||||
salt, sizeof(salt), 24, hash, sizeof(hash));
|
||||
ASSERT_STREQ("$argon2i$v=19$m=65536,t=2,p=4$c29tZXNhbHQ$RdescudvJCsgt3ub+b+"
|
||||
"dWRWJTmaaJObG",
|
||||
hash);
|
||||
}
|
||||
|
||||
TEST(argon2i, testOnlineGeneratorExample) {
|
||||
/* consistent with https://argon2.online/ */
|
||||
uint8_t hash[32];
|
||||
uint8_t salt[8] = {'s', 'a', 'l', 't', 's', 'a', 'l', 't'};
|
||||
|
@ -35,17 +62,31 @@ TEST(argon2, test) {
|
|||
"b5d0818d1c0c05684e68aef02de8b4ed14bfe6c60800e0aa36a5c18f846a297b", hash);
|
||||
}
|
||||
|
||||
void specimen(void) {
|
||||
TEST(argon2id, testOnlineGeneratorExample) {
|
||||
/* consistent with https://argon2.online/ */
|
||||
uint8_t hash[32];
|
||||
uint8_t salt[8] = {'s', 'a', 'l', 't', 's', 'a', 'l', 't'};
|
||||
uint8_t pass[8] = {'p', 'a', 's', 's', 'w', 'o', 'r', 'd'};
|
||||
uint32_t parallelism = 1;
|
||||
uint32_t memorycost = 65536 / 4;
|
||||
uint32_t memorycost = 65536;
|
||||
uint32_t iterations = 2;
|
||||
argon2id_hash_raw(iterations, memorycost, parallelism, pass, sizeof(pass),
|
||||
salt, sizeof(salt), hash, sizeof(hash));
|
||||
ASSERT_BINEQ(
|
||||
"360da2d90fd9d6f52923f293d142131a13909b780698daf09e6756422ebd1045", hash);
|
||||
}
|
||||
|
||||
void BenchmarkArgon2(void) {
|
||||
uint8_t hash[24];
|
||||
uint8_t salt[8] = {'s', 'o', 'm', 'e', 's', 'a', 'l', 't'};
|
||||
uint8_t pass[8] = {'p', 'a', 's', 's', 'w', 'o', 'r', 'd'};
|
||||
uint32_t parallelism = 4;
|
||||
uint32_t memorycost = (1 << 16) / 8;
|
||||
uint32_t iterations = 2;
|
||||
argon2i_hash_raw(iterations, memorycost, parallelism, pass, sizeof(pass),
|
||||
salt, sizeof(salt), hash, sizeof(hash));
|
||||
}
|
||||
|
||||
BENCH(argon2, bench) {
|
||||
EZBENCH2("argon2", donothing, specimen());
|
||||
EZBENCH2("argon2", donothing, BenchmarkArgon2());
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue