mirror of
https://github.com/jart/cosmopolitan.git
synced 2025-05-28 08:12:28 +00:00
Refactor some code
- Write tests for cthreads - Fix bugs in pe2.com tool - Fix ASAN issue with GetDosEnviron() - Consolidate the cthread header files - Some code size optimizations for MODE= - Attempted to squash a tls linker warning - Attempted to get futexes working on FreeBSD
This commit is contained in:
parent
909e54510d
commit
425ff5dff0
61 changed files with 529 additions and 382 deletions
|
@ -36,7 +36,6 @@ 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));
|
||||
|
@ -54,7 +53,6 @@ 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;
|
||||
|
@ -64,7 +62,6 @@ TEST(stat, zipos) {
|
|||
&st));
|
||||
}
|
||||
|
||||
#if 0
|
||||
static long Stat(const char *path, struct stat *st) {
|
||||
long ax, di, si, dx;
|
||||
asm volatile("syscall"
|
||||
|
@ -112,4 +109,3 @@ BENCH(stat, bench) {
|
|||
"tokenize_tests-latin1-coding-cookie-and-utf8-bom-sig.txt",
|
||||
&st));
|
||||
}
|
||||
#endif
|
||||
|
|
|
@ -33,5 +33,6 @@ TEST(tls, test) {
|
|||
__install_tls(tib);
|
||||
EXPECT_EQ(31337, errno);
|
||||
EXPECT_EQ(tib, __get_tls());
|
||||
EXPECT_EQ(tib, __get_tls_inline());
|
||||
EXPECT_EQ(tib + 0x3c, (char *)__errno_location());
|
||||
}
|
||||
|
|
|
@ -16,6 +16,7 @@
|
|||
│ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │
|
||||
│ PERFORMANCE OF THIS SOFTWARE. │
|
||||
╚─────────────────────────────────────────────────────────────────────────────*/
|
||||
#include "libc/bits/atomic.h"
|
||||
#include "libc/calls/calls.h"
|
||||
#include "libc/calls/sigbits.h"
|
||||
#include "libc/calls/struct/sigaction.h"
|
||||
|
@ -24,6 +25,7 @@
|
|||
#include "libc/errno.h"
|
||||
#include "libc/intrin/kprintf.h"
|
||||
#include "libc/intrin/spinlock.h"
|
||||
#include "libc/limits.h"
|
||||
#include "libc/macros.internal.h"
|
||||
#include "libc/nexgen32e/threaded.h"
|
||||
#include "libc/rand/rand.h"
|
||||
|
@ -35,29 +37,28 @@
|
|||
#include "libc/sysv/consts/sa.h"
|
||||
#include "libc/sysv/consts/sig.h"
|
||||
#include "libc/testlib/testlib.h"
|
||||
#include "libc/thread/thread.h"
|
||||
#include "libc/time/time.h"
|
||||
|
||||
#define THREADS 8
|
||||
#define ENTRIES 1024
|
||||
|
||||
_Atomic(bool) ready;
|
||||
uint32_t ready;
|
||||
volatile uint64_t A[THREADS * ENTRIES];
|
||||
|
||||
void OnChld(int sig) {
|
||||
// do nothing
|
||||
}
|
||||
|
||||
dontinline void Pause(void) {
|
||||
// when ftrace is enabled
|
||||
}
|
||||
|
||||
dontinline void Generate(int i) {
|
||||
A[i] = rand64();
|
||||
}
|
||||
|
||||
int Thrasher(void *arg) {
|
||||
int i, id = (intptr_t)arg;
|
||||
while (!ready) Pause();
|
||||
while (!atomic_load(&ready)) {
|
||||
cthread_memory_wait32(&ready, 0, 0);
|
||||
}
|
||||
for (i = 0; i < ENTRIES; ++i) {
|
||||
Generate(id * ENTRIES + i);
|
||||
}
|
||||
|
@ -103,9 +104,13 @@ TEST(rand64, testThreadSafety_doesntProduceIdenticalValues) {
|
|||
CLONE_SETTLS | CLONE_CHILD_SETTID | CLONE_CHILD_CLEARTID,
|
||||
(void *)(intptr_t)i, 0, tls[i], 64, (int *)(tls[i] + 0x38)));
|
||||
}
|
||||
ready = true;
|
||||
atomic_store(&ready, 1);
|
||||
cthread_memory_wake32(&ready, INT_MAX);
|
||||
for (i = 0; i < THREADS; ++i) {
|
||||
_spinlock((int *)(tls[i] + 0x38));
|
||||
while ((j = atomic_load((uint32_t *)(tls[i] + 0x38)))) {
|
||||
// FUTEX_WAIT_PRIVATE makes it hang
|
||||
cthread_memory_wait32((uint32_t *)(tls[i] + 0x38), j, 0);
|
||||
}
|
||||
EXPECT_SYS(0, 0, munmap(stacks[i], GetStackSize()));
|
||||
free(tls[i]);
|
||||
}
|
||||
|
|
|
@ -57,6 +57,21 @@ void TearDown(void) {
|
|||
free(tls);
|
||||
}
|
||||
|
||||
int DoNothing(void *arg) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
// TEST ERROR NUMBERS
|
||||
|
||||
TEST(clone, testNullFunc_raisesEinval) {
|
||||
EXPECT_SYS(EINVAL, -1,
|
||||
clone(0, stack, GetStackSize(),
|
||||
CLONE_THREAD | CLONE_VM | CLONE_FS | CLONE_FILES |
|
||||
CLONE_SIGHAND | CLONE_SETTLS,
|
||||
0, 0, tls, 64, 0));
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
// TEST THREADS WORK
|
||||
|
||||
|
@ -169,10 +184,6 @@ TEST(clone, tlsSystemCallsErrno_wontClobberMainThreadBecauseTls) {
|
|||
////////////////////////////////////////////////////////////////////////////////
|
||||
// BENCHMARK
|
||||
|
||||
int DoNothing(void *arg) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
void LaunchThread(void) {
|
||||
char *tls, *stack;
|
||||
tls = __initialize_tls(malloc(64));
|
||||
|
@ -189,6 +200,7 @@ BENCH(clone, bench) {
|
|||
char *volatile tp;
|
||||
errno_t *volatile ep;
|
||||
EZBENCH2("__errno_location", donothing, (ep = __errno_location()));
|
||||
EZBENCH2("__get_tls_inline", donothing, (tp = __get_tls_inline()));
|
||||
EZBENCH2("__get_tls", donothing, (tp = __get_tls()));
|
||||
EZBENCH2("clone()", donothing, LaunchThread());
|
||||
}
|
||||
|
|
100
test/libc/thread/create_test.c
Normal file
100
test/libc/thread/create_test.c
Normal file
|
@ -0,0 +1,100 @@
|
|||
/*-*- 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 2022 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/calls/calls.h"
|
||||
#include "libc/dce.h"
|
||||
#include "libc/errno.h"
|
||||
#include "libc/runtime/internal.h"
|
||||
#include "libc/runtime/runtime.h"
|
||||
#include "libc/testlib/testlib.h"
|
||||
#include "libc/thread/thread.h"
|
||||
#include "libc/time/time.h"
|
||||
|
||||
static _Thread_local int tdata = 31337;
|
||||
static _Thread_local int tbss;
|
||||
|
||||
static void *ReturnArg(void *arg) {
|
||||
return arg;
|
||||
}
|
||||
|
||||
TEST(cthread_create, testJoinDeadlock) {
|
||||
ASSERT_SYS(0, EDEADLK, cthread_join(cthread_self(), 0));
|
||||
}
|
||||
|
||||
TEST(cthread_create, testCreateReturnJoin) {
|
||||
if (IsOpenbsd()) return; // TODO(jart): we've getting flakes
|
||||
void *exitcode;
|
||||
cthread_t thread;
|
||||
ASSERT_EQ(0, cthread_create(&thread, 0, ReturnArg, ReturnArg));
|
||||
ASSERT_EQ(0, cthread_join(thread, &exitcode));
|
||||
ASSERT_EQ(ReturnArg, exitcode);
|
||||
}
|
||||
|
||||
static void *ExitArg(void *arg) {
|
||||
cthread_exit(arg);
|
||||
}
|
||||
|
||||
TEST(cthread_create, testCreateExitJoin) {
|
||||
if (IsOpenbsd()) return; // TODO(jart): we've getting flakes
|
||||
void *exitcode;
|
||||
cthread_t thread;
|
||||
ASSERT_EQ(0, cthread_create(&thread, 0, ExitArg, (void *)-31337));
|
||||
ASSERT_EQ(0, cthread_join(thread, &exitcode));
|
||||
ASSERT_EQ((void *)-31337, exitcode);
|
||||
}
|
||||
|
||||
TEST(gcctls, size) {
|
||||
if (IsXnu()) return; // TODO(jart): codemorph
|
||||
if (IsWindows()) return; // TODO(jart): codemorph
|
||||
if (IsOpenbsd()) return; // TODO(jart): we've getting flakes
|
||||
// schlep in .zip section too
|
||||
// make sure executable isn't too huge
|
||||
size_t size;
|
||||
int64_t x = 0;
|
||||
gmtime(&x);
|
||||
ASSERT_LT((uintptr_t)_tls_size, 8192);
|
||||
size = GetFileSize(GetProgramExecutableName());
|
||||
if (IsTiny()) {
|
||||
ASSERT_LT(size, 200 * 1024);
|
||||
} else if (IsModeDbg()) {
|
||||
ASSERT_LT(size, 4 * 1024 * 1024);
|
||||
} else {
|
||||
ASSERT_LT(size, 500 * 1024);
|
||||
}
|
||||
}
|
||||
|
||||
static void *TlsWorker(void *arg) {
|
||||
ASSERT_EQ(31337, tdata);
|
||||
ASSERT_EQ(0, tbss);
|
||||
return 0;
|
||||
}
|
||||
|
||||
TEST(gcctls, worksAndIsNonInheritable) {
|
||||
if (IsXnu()) return; // TODO(jart): codemorph
|
||||
if (IsWindows()) return; // TODO(jart): codemorph
|
||||
if (IsOpenbsd()) return; // TODO(jart): we've getting flakes
|
||||
void *exitcode;
|
||||
cthread_t thread;
|
||||
ASSERT_EQ(tdata, 31337);
|
||||
ASSERT_EQ(tbss, 0);
|
||||
tdata = 1337;
|
||||
tbss = 1337;
|
||||
ASSERT_EQ(0, cthread_create(&thread, 0, TlsWorker, (void *)-31337));
|
||||
ASSERT_EQ(0, cthread_join(thread, &exitcode));
|
||||
ASSERT_EQ(NULL, exitcode);
|
||||
}
|
|
@ -33,6 +33,7 @@ TEST_LIBC_THREAD_DIRECTDEPS = \
|
|||
LIBC_STUBS \
|
||||
LIBC_SYSV \
|
||||
LIBC_THREAD \
|
||||
LIBC_TIME \
|
||||
LIBC_TESTLIB
|
||||
|
||||
TEST_LIBC_THREAD_DEPS := \
|
||||
|
@ -51,20 +52,6 @@ o/$(MODE)/test/libc/thread/%.com.dbg: \
|
|||
$(APE_NO_MODIFY_SELF)
|
||||
@$(APELINK)
|
||||
|
||||
$(TEST_LIBC_THREAD_OBJS): \
|
||||
DEFAULT_CCFLAGS += \
|
||||
-fno-builtin
|
||||
|
||||
o/$(MODE)/test/libc/thread/getenv_test.com.runs: \
|
||||
o/$(MODE)/test/libc/thread/getenv_test.com
|
||||
@HELLO=THERE build/runit $@ $<
|
||||
|
||||
o/$(MODE)/test/libc/thread/fun_test.o \
|
||||
o/$(MODE)/test/libc/thread/itsatrap_test.o: \
|
||||
OVERRIDE_CFLAGS += \
|
||||
-fno-sanitize=all \
|
||||
-ftrapv
|
||||
|
||||
.PHONY: o/$(MODE)/test/libc/thread
|
||||
o/$(MODE)/test/libc/thread: \
|
||||
$(TEST_LIBC_THREAD_BINS) \
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue