Improve locks and signals

- Introduce fast spinlock API
- Double rand64() perf w/ spinlock
- Improve raise() on New Technology
- Support gettid() across platforms
- Implement SA_NODEFER on New Technology
- Move the lock intrinsics into LIBC_INTRIN
- Make SIGTRAP recoverable on New Technology
- Block SIGCHLD in wait4() on New Technology
- Add threading prototypes for XNU and FreeBSD
- Rewrite abort() fixing its minor bugs on XNU/NT
- Shave down a lot of the content in libc/bits/bits.h
- Let signal handlers modify CPU registers on New Technology
This commit is contained in:
Justine Tunney 2022-04-12 05:20:17 -07:00
parent f68f1789bd
commit 046c7ebd4a
110 changed files with 1514 additions and 876 deletions

View file

@ -23,6 +23,7 @@
#include "libc/calls/ucontext.h"
#include "libc/dce.h"
#include "libc/errno.h"
#include "libc/intrin/kprintf.h"
#include "libc/runtime/runtime.h"
#include "libc/sysv/consts/sa.h"
#include "libc/sysv/consts/sig.h"
@ -39,37 +40,10 @@ void OnSigInt(int sig) {
void SetUp(void) {
gotsigint = false;
/* TODO(jart): Windows needs huge signal overhaul */
if (IsWindows()) exit(0);
}
TEST(sigaction, test) {
int pid, status;
sigset_t block, ignore, oldmask;
struct sigaction saint = {.sa_handler = OnSigInt};
sigemptyset(&block);
sigaddset(&block, SIGINT);
EXPECT_NE(-1, sigprocmask(SIG_BLOCK, &block, &oldmask));
sigfillset(&ignore);
sigdelset(&ignore, SIGINT);
EXPECT_NE(-1, sigaction(SIGINT, &saint, &oldsa));
ASSERT_NE(-1, (pid = fork()));
if (!pid) {
EXPECT_NE(-1, kill(getppid(), SIGINT));
EXPECT_EQ(-1, sigsuspend(&ignore));
EXPECT_EQ(EINTR, errno);
EXPECT_TRUE(gotsigint);
_exit(0);
}
EXPECT_EQ(-1, sigsuspend(&ignore));
EXPECT_NE(-1, kill(pid, SIGINT));
EXPECT_NE(-1, waitpid(pid, &status, 0));
EXPECT_EQ(1, WIFEXITED(status));
EXPECT_EQ(0, WEXITSTATUS(status));
EXPECT_EQ(0, WTERMSIG(status));
EXPECT_NE(-1, sigprocmask(SIG_SETMASK, &oldmask, NULL));
EXPECT_NE(-1, sigaction(SIGINT, &oldsa, NULL));
}
////////////////////////////////////////////////////////////////////////////////
// test raise()
TEST(sigaction, raise) {
struct sigaction saint = {.sa_handler = OnSigInt};
@ -80,6 +54,58 @@ TEST(sigaction, raise) {
EXPECT_NE(-1, sigaction(SIGINT, &oldsa, NULL));
}
////////////////////////////////////////////////////////////////////////////////
// test kill()
TEST(sigaction, testPingPongParentChildWithSigint) {
int pid, status;
sigset_t blockint, oldmask;
struct sigaction oldint;
struct sigaction ignoreint = {.sa_handler = SIG_IGN};
struct sigaction catchint = {.sa_handler = OnSigInt};
if (IsWindows()) {
// this works if it's run by itself on the command prompt. but it
// doesn't currently work if it's launched as a subprocess of some
// kind of runner. todo(fixme!)
return;
}
EXPECT_NE(-1, sigemptyset(&blockint));
EXPECT_NE(-1, sigaddset(&blockint, SIGINT));
EXPECT_NE(-1, sigprocmask(SIG_BLOCK, &blockint, &oldmask));
EXPECT_NE(-1, sigaction(SIGINT, &catchint, &oldint));
ASSERT_NE(-1, (pid = fork()));
if (!pid) {
// ping
EXPECT_NE(-1, kill(getppid(), SIGINT));
EXPECT_FALSE(gotsigint);
// pong
EXPECT_NE(-1, sigaction(SIGINT, &catchint, 0));
EXPECT_EQ(-1, sigsuspend(0));
EXPECT_EQ(EINTR, errno);
EXPECT_TRUE(gotsigint);
_exit(0);
}
// pong
EXPECT_FALSE(gotsigint);
EXPECT_NE(-1, sigaction(SIGINT, &catchint, 0));
EXPECT_EQ(-1, sigsuspend(0));
EXPECT_TRUE(gotsigint);
// ping
EXPECT_NE(-1, sigaction(SIGINT, &ignoreint, 0));
EXPECT_NE(-1, kill(pid, SIGINT));
// cleanup
EXPECT_NE(-1, wait4(pid, &status, 0, 0));
EXPECT_EQ(1, WIFEXITED(status));
EXPECT_EQ(0, WEXITSTATUS(status));
EXPECT_EQ(0, WTERMSIG(status));
EXPECT_NE(-1, sigaction(SIGINT, &oldint, 0));
EXPECT_NE(-1, sigprocmask(SIG_BLOCK, &oldmask, 0));
}
////////////////////////////////////////////////////////////////////////////////
// test int3 crash
// we expect this to be recoverable by default
volatile int trapeax;
void OnTrap(int sig, struct siginfo *si, struct ucontext *ctx) {
@ -94,6 +120,10 @@ TEST(sigaction, debugBreak_handlerCanReadCpuState) {
EXPECT_NE(-1, sigaction(SIGTRAP, &oldsa, NULL));
}
////////////////////////////////////////////////////////////////////////////////
// test fpu crash (unrecoverable)
// test signal handler can modify cpu registers (now it's recoverable!)
void OnFpe(int sig, struct siginfo *si, struct ucontext *ctx) {
struct XedDecodedInst xedd;
xed_decoded_inst_zero_set_mode(&xedd, XED_MACHINE_MODE_LONG_64);

View file

@ -24,13 +24,12 @@
#include "libc/sysv/consts/sig.h"
#include "libc/testlib/testlib.h"
testonly void OnCtrlC(int sig) {
testonly void OnUsr1(int sig) {
_exit(0);
}
TEST(signal, test) {
if (IsWindows()) return; /* omg */
ASSERT_NE(SIG_ERR, signal(SIGINT, OnCtrlC));
ASSERT_NE(-1, raise(SIGINT));
ASSERT_NE(SIG_ERR, signal(SIGUSR1, OnUsr1));
ASSERT_NE(-1, raise(SIGUSR1));
__die();
}

View file

@ -23,11 +23,12 @@
#include "libc/calls/struct/sigset.h"
#include "libc/calls/ucontext.h"
#include "libc/dce.h"
#include "libc/intrin/kprintf.h"
#include "libc/sysv/consts/sa.h"
#include "libc/sysv/consts/sig.h"
#include "libc/testlib/testlib.h"
int n;
volatile int n;
void OnSig(int sig, siginfo_t *si, ucontext_t *ctx) {
++n;

View file

@ -46,10 +46,6 @@ void OnSigQueuing(int sig, siginfo_t *si, ucontext_t *ctx) {
}
TEST(sigsuspend, testSignalQueuingSelf) {
if (IsWindows()) {
// xxx: probably need a signal server to do this kind of signalling
return;
}
sigset_t neu, old, bits;
struct sigaction oldusr1, oldusr2;
struct sigaction sa = {.sa_sigaction = OnSigQueuing, .sa_flags = SA_SIGINFO};

View file

@ -231,7 +231,6 @@ TEST(ksnprintf, fuzzTheUnbreakable) {
}
TEST(kprintf, testFailure_wontClobberErrnoAndBypassesSystemCallSupport) {
if (IsWindows()) return; // TODO(jart): fixme
int n;
ASSERT_EQ(0, errno);
EXPECT_SYS(0, 3, dup(2));
@ -344,30 +343,27 @@ TEST(ksnprintf, badUtf16) {
BENCH(printf, bench) {
char b[128];
int snprintf_(char *, size_t, const char *, ...) asm("snprintf");
EZBENCH2("ksnprintf fmt", donothing,
ksnprintf(b, 128,
"hello world\nhello world\nhello world\nhello world\n"));
EZBENCH2("snprintf fmt", donothing,
snprintf_(b, 128,
"hello world\nhello world\nhello world\nhello world\n"));
EZBENCH2("ksnprintf str", donothing,
ksnprintf(b, 128, "%s\n", "hello world"));
EZBENCH2("ksnprintf fmt", donothing, ksnprintf(b, 128, "."));
EZBENCH2("kusnprintf fmt", donothing, kusnprintf(b, 128, "."));
EZBENCH2("snprintf fmt", donothing, snprintf_(b, 128, "."));
EZBENCH2("kusnprintf str", donothing,
kusnprintf(b, 128, "%s\n", "hello world"));
EZBENCH2("snprintf str", donothing,
snprintf_(b, 128, "%s\n", "hello world"));
EZBENCH2("ksnprintf utf8", donothing,
ksnprintf(b, 128, "%s\n", "天地玄黄宇宙洪荒天地玄黄宇宙洪荒"));
EZBENCH2("kusnprintf utf8", donothing,
kusnprintf(b, 128, "%s\n", "天地玄黄宇宙洪荒天地玄黄宇宙洪荒"));
EZBENCH2("snprintf utf8", donothing,
snprintf_(b, 128, "%s\n", "天地玄黄宇宙洪荒天地玄黄宇宙洪荒"));
EZBENCH2("ksnprintf chinese", donothing,
ksnprintf(b, 128, "%hs\n", u"天地玄黄宇宙洪荒"));
EZBENCH2("kusnprintf chinese", donothing,
kusnprintf(b, 128, "%hs\n", u"天地玄黄宇宙洪荒"));
EZBENCH2("snprintf chinese", donothing,
snprintf_(b, 128, "%hs\n", u"天地玄黄宇宙洪荒"));
EZBENCH2("ksnprintf astral", donothing,
ksnprintf(b, 128, "%hs\n", u"𐌰𐌱𐌲𐌳𐌴𐌵𐌶𐌷"));
EZBENCH2("kusnprintf astral", donothing,
kusnprintf(b, 128, "%hs\n", u"𐌰𐌱𐌲𐌳𐌴𐌵𐌶𐌷"));
EZBENCH2("snprintf astral", donothing,
snprintf_(b, 128, "%hs\n", u"𐌰𐌱𐌲𐌳𐌴𐌵𐌶𐌷"));
EZBENCH2("ksnprintf long", donothing, ksnprintf(b, 128, "%ld", LONG_MAX));
EZBENCH2("kusnprintf long", donothing, kusnprintf(b, 128, "%ld", LONG_MAX));
EZBENCH2("snprintf long", donothing, snprintf_(b, 128, "%ld", LONG_MAX));
EZBENCH2("ksnprintf thou", donothing, ksnprintf(b, 128, "%'ld", LONG_MAX));
EZBENCH2("kusnprintf thou", donothing, kusnprintf(b, 128, "%'ld", LONG_MAX));
EZBENCH2("snprintf thou", donothing, snprintf_(b, 128, "%'ld", LONG_MAX));
}

View file

@ -30,8 +30,8 @@
#include "libc/sysv/consts/sig.h"
#include "libc/testlib/testlib.h"
#define THREADS 1
#define ENTRIES 10
#define THREADS 4
#define ENTRIES 256
volatile bool ready;
volatile uint64_t A[THREADS * ENTRIES];
@ -73,7 +73,6 @@ TEST(rand64, testThreadSafety_doesntProduceIdenticalValues) {
struct sigaction sa = {.sa_handler = OnChld, .sa_flags = SA_RESTART};
EXPECT_NE(-1, sigaction(SIGCHLD, &sa, &oldsa));
bzero(A, sizeof(A));
rand64(); // for effect
sigemptyset(&ss);
sigaddset(&ss, SIGCHLD);
EXPECT_EQ(0, sigprocmask(SIG_BLOCK, &ss, &oldss));