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);