Perform more low-level code cleanup

This commit is contained in:
Justine Tunney 2022-09-09 04:07:08 -07:00
parent c32e2d4486
commit 2d17ab016c
No known key found for this signature in database
GPG key ID: BE714B4575D6E328
42 changed files with 977 additions and 265 deletions

View file

@ -0,0 +1,130 @@
/*-*- 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/calls/ucontext.h"
#include "libc/dce.h"
#include "libc/intrin/kprintf.h"
#include "libc/runtime/gc.h"
#include "libc/runtime/runtime.h"
#include "libc/stdio/append.internal.h"
#include "libc/str/str.h"
#include "libc/sysv/consts/nr.h"
#include "libc/testlib/testlib.h"
#define Z 0x5555555555555555
#define FLAGS_cf 0
#define FLAGS_pf 2
#define FLAGS_sf 7
#define FLAGS_of 11
intptr_t diagnose_syscall(intptr_t nr, //
intptr_t arg1, //
intptr_t arg2, //
intptr_t arg3, //
intptr_t arg4, //
intptr_t arg5, //
intptr_t arg6, //
intptr_t arg7, //
ucontext_t *before, //
ucontext_t *after); //
#define GREG(FIELD) \
do { \
uint64_t f1 = x->uc_mcontext.FIELD; \
uint64_t f2 = y->uc_mcontext.FIELD; \
if (f1 != f2) { \
if (b) appendw(&b, ' '); \
appends(&b, #FIELD); \
kprintf("%3s %016lx → %016lx\n", #FIELD, f1, f2); \
} \
} while (0)
#define FLAG(FLAG) \
if ((x->uc_mcontext.eflags & (1ul << FLAGS_##FLAG)) ^ \
(y->uc_mcontext.eflags & (1ul << FLAGS_##FLAG))) { \
if (b) appendw(&b, ' '); \
appends(&b, #FLAG); \
}
char *DiffContexts(ucontext_t *x, ucontext_t *y) {
char *b = 0;
GREG(rax);
GREG(rdx);
GREG(rdi);
GREG(rsi);
GREG(rcx);
GREG(r8);
GREG(r9);
GREG(r10);
GREG(r11);
GREG(r12);
GREG(r13);
GREG(r14);
GREG(r15);
GREG(rbx);
GREG(rbp);
FLAG(cf);
FLAG(sf);
FLAG(of);
FLAG(pf);
return b;
}
void SetUp(void) {
if (IsWindows()) {
exit(0);
}
}
TEST(diagnose_syscall, getpid) {
ucontext_t x, y;
diagnose_syscall(__NR_getpid, Z, Z, Z, Z, Z, Z, Z, &x, &y);
if (IsFreebsd()) {
ASSERT_STREQ("rax rcx r8 r9 r10 r11", _gc(DiffContexts(&x, &y)));
} else if (IsNetbsd() || IsXnu()) {
// netbsd puts parent pid in edx
// xnu seems to just clobber it!
ASSERT_STREQ("rax rdx rcx r11", _gc(DiffContexts(&x, &y)));
} else {
ASSERT_STREQ("rax rcx r11", _gc(DiffContexts(&x, &y)));
}
}
TEST(diagnose_syscall, testWriteSuccess) {
ucontext_t x, y;
diagnose_syscall(__NR_write, 2, Z, 0, Z, Z, Z, Z, &x, &y);
if (IsFreebsd()) {
ASSERT_STREQ("rax rcx r8 r9 r10 r11", _gc(DiffContexts(&x, &y)));
} else {
ASSERT_STREQ("rax rcx r11", _gc(DiffContexts(&x, &y)));
}
}
TEST(diagnose_syscall, testWriteFailed) {
ucontext_t x, y;
diagnose_syscall(__NR_write, -1, Z, Z, Z, Z, Z, Z, &x, &y);
if (IsFreebsd()) {
ASSERT_STREQ("rax rcx r8 r9 r10 r11 cf", _gc(DiffContexts(&x, &y)));
} else if (IsBsd()) {
ASSERT_STREQ("rax rcx r11 cf", _gc(DiffContexts(&x, &y)));
} else {
ASSERT_STREQ("rax rcx r11", _gc(DiffContexts(&x, &y)));
}
}

View file

@ -19,6 +19,7 @@
#include "libc/calls/calls.h"
#include "libc/calls/ucontext.h"
#include "libc/runtime/runtime.h"
#include "libc/testlib/ezbench.h"
#include "libc/testlib/testlib.h"
int x;
@ -47,3 +48,17 @@ TEST(getcontext, test) {
ASSERT_TRUE(ok1);
ASSERT_TRUE(ok2);
}
void SetGetContext(void) {
static int a;
a = 0;
getcontext(&context);
if (!a) {
a = 1;
setcontext(&context);
}
}
BENCH(getcontext, bench) {
EZBENCH2("get/setcontext", donothing, SetGetContext());
}

View file

@ -25,6 +25,7 @@
#include "libc/testlib/ezbench.h"
#include "libc/testlib/testlib.h"
#include "libc/thread/posixthread.internal.h"
#include "libc/thread/spawn.h"
int THREADS = 16;
int ITERATIONS = 100;

View file

@ -16,10 +16,18 @@
TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE.
*/
#include "libc/intrin/segmentation.h"
#include "libc/calls/calls.h"
#include "libc/calls/struct/sigaction.h"
#include "libc/calls/syscall-sysv.internal.h"
#include "libc/dce.h"
#include "libc/errno.h"
#include "libc/intrin/fsgsbase.h"
#include "libc/intrin/kprintf.h"
#include "libc/intrin/segmentation.h"
#include "libc/nexgen32e/threaded.h"
#include "libc/nt/version.h"
#include "libc/sysv/consts/sa.h"
#include "libc/sysv/consts/sig.h"
#include "libc/testlib/testlib.h"
void SetUpOnce(void) {
@ -27,19 +35,35 @@ void SetUpOnce(void) {
ASSERT_SYS(0, 0, pledge("stdio rpath", 0));
}
void OnTrap(int sig, struct siginfo *si, void *vctx) {
struct ucontext *ctx = vctx;
}
void TriggerSignal(void) {
struct sigaction old;
struct sigaction sig = {.sa_sigaction = OnTrap, .sa_flags = SA_SIGINFO};
sched_yield();
sigaction(SIGTRAP, &sig, &old);
asm("int3");
sigaction(SIGTRAP, &old, 0);
sched_yield();
}
TEST(arch_prctl, fs) {
if (IsLinux() || IsOpenbsd()) {
if (IsLinux() || IsFreebsd() || IsNetbsd() || IsOpenbsd()) {
uint64_t n, x;
x = 0xdeadbeef;
arch_prctl(ARCH_SET_FS, &x);
ASSERT_NE(-1, arch_prctl(ARCH_GET_FS, (intptr_t)&n));
ASSERT_EQ((intptr_t)&x, n);
ASSERT_EQ(0xdeadbeef, fs((int64_t *)0));
TriggerSignal();
ASSERT_EQ(0xdeadbeef, fs((int64_t *)0));
}
}
TEST(arch_prctl, pointerRebasingFs) {
if (IsLinux() || IsOpenbsd()) {
if (IsLinux() || IsFreebsd() || IsOpenbsd() || IsNetbsd()) {
unsigned long s[] = {0x0706050403020100, 0x0f0e0d0c0b0a0908};
ASSERT_EQ(0x0706050403020100, s[0]);
ASSERT_EQ(0, arch_prctl(ARCH_SET_FS, 1));
@ -53,26 +77,52 @@ TEST(arch_prctl, pointerRebasingFs) {
}
TEST(arch_prctl, gs) {
if (IsLinux()) {
if (IsLinux() || IsFreebsd() || IsNetbsd() || IsXnu()) {
uint64_t n, x;
x = 0xdeadbeef;
arch_prctl(ARCH_SET_GS, &x);
ASSERT_NE(-1, arch_prctl(ARCH_GET_GS, (intptr_t)&n));
ASSERT_EQ((intptr_t)&x, n);
if (!IsXnu()) {
ASSERT_NE(-1, arch_prctl(ARCH_GET_GS, (intptr_t)&n));
ASSERT_EQ((intptr_t)&x, n);
}
ASSERT_EQ(0xdeadbeef, gs((int64_t *)0));
TriggerSignal();
ASSERT_EQ(0xdeadbeef, gs((int64_t *)0));
}
}
TEST(arch_prctl, pointerRebasing) {
if (IsLinux()) {
if (IsLinux() || IsFreebsd() || IsNetbsd() || IsXnu()) {
unsigned long s[] = {0x0706050403020100, 0x0f0e0d0c0b0a0908};
ASSERT_EQ(0x0706050403020100, s[0]);
ASSERT_EQ(0, arch_prctl(ARCH_SET_GS, 1));
ASSERT_EQ(0x0807060504030201, gs(&s[0]));
ASSERT_EQ(0, arch_prctl(ARCH_SET_GS, 2));
ASSERT_EQ(0x0908070605040302, gs(&s[0]));
intptr_t gs;
ASSERT_EQ(0, arch_prctl(ARCH_GET_GS, &gs));
ASSERT_EQ(2, gs);
if (!IsXnu()) {
intptr_t gs;
ASSERT_EQ(0, arch_prctl(ARCH_GET_GS, &gs));
ASSERT_EQ(2, gs);
}
}
}
TEST(fsgsbase, fs) {
if (!_have_fsgsbase()) return;
int64_t mem = 0xdeadbeef;
_wrfsbase(&mem);
ASSERT_EQ(&mem, _rdfsbase());
ASSERT_EQ(0xdeadbeef, fs((int64_t *)0));
TriggerSignal();
ASSERT_EQ(0xdeadbeef, fs((int64_t *)0));
}
TEST(fsgsbase, gs) {
if (!_have_fsgsbase()) return;
int64_t mem = 0xdeadbeef;
_wrgsbase(&mem);
ASSERT_EQ(&mem, _rdgsbase());
ASSERT_EQ(0xdeadbeef, gs((int64_t *)0));
TriggerSignal();
ASSERT_EQ(0xdeadbeef, gs((int64_t *)0));
}

View file

@ -18,14 +18,18 @@
*/
#include "libc/calls/calls.h"
#include "libc/dce.h"
#include "libc/intrin/kprintf.h"
#include "libc/intrin/pthread.h"
#include "libc/macros.internal.h"
#include "libc/mem/mem.h"
#include "libc/runtime/runtime.h"
#include "libc/runtime/stack.h"
#include "libc/sysv/consts/prot.h"
#include "libc/testlib/ezbench.h"
#include "libc/testlib/subprocess.h"
#include "libc/testlib/testlib.h"
#include "libc/thread/thread.h"
#if 0
static void *Increment(void *arg) {
ASSERT_EQ(gettid(), pthread_getthreadid_np());
return (void *)((uintptr_t)arg + 1);
@ -75,7 +79,7 @@ TEST(pthread_detach, testBigStack) {
pthread_t id;
pthread_attr_t attr;
ASSERT_EQ(0, pthread_attr_init(&attr));
ASSERT_EQ(0, pthread_attr_setstacksize(&attr, 2 * 1024 * 1024));
ASSERT_EQ(0, pthread_attr_setstacksize(&attr, 2 * 1000 * 1000));
ASSERT_EQ(0, pthread_create(&id, &attr, CheckStack, 0));
ASSERT_EQ(0, pthread_attr_destroy(&attr));
ASSERT_EQ(0, pthread_join(id, 0));
@ -101,7 +105,6 @@ TEST(pthread_detach, testCustomStack_withReallySmallSize) {
ASSERT_EQ(0, pthread_join(id, 0));
free(stk);
}
#endif
TEST(pthread_exit, mainThreadWorks) {
// _Exit1() can't set process exit code on XNU/NetBSD/OpenBSD.
@ -115,3 +118,31 @@ TEST(pthread_exit, mainThreadWorks) {
EXITS(0);
}
}
static void CreateJoin(void) {
pthread_t id;
ASSERT_EQ(0, pthread_create(&id, 0, Increment, 0));
ASSERT_EQ(0, pthread_join(id, 0));
}
// this is de facto the same as create+join
static void CreateDetach(void) {
pthread_t id;
ASSERT_EQ(0, pthread_create(&id, 0, Increment, 0));
ASSERT_EQ(0, pthread_detach(id));
}
// this is really fast
static void CreateDetached(void) {
pthread_attr_t attr;
ASSERT_EQ(0, pthread_attr_init(&attr));
ASSERT_EQ(0, pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED));
ASSERT_EQ(0, pthread_create(0, &attr, Increment, 0));
ASSERT_EQ(0, pthread_attr_destroy(&attr));
}
BENCH(pthread_create, bench) {
EZBENCH2("CreateJoin", donothing, CreateJoin());
EZBENCH2("CreateDetach", donothing, CreateDetach());
EZBENCH2("CreateDetached", donothing, CreateDetached());
}