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

@ -1,83 +0,0 @@
/*-*- 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 2020 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 "dsp/core/core.h"
#include "dsp/mpeg/mpeg.h"
#include "libc/log/check.h"
#include "libc/macros.internal.h"
#include "libc/rand/rand.h"
#include "libc/runtime/buffer.h"
#include "libc/testlib/ezbench.h"
#include "libc/testlib/testlib.h"
short pcm[8][8];
float binary32[8][8];
struct GuardedBuffer b1, b2;
TEST(float2short, test) {
binary32[0][0] = -0.5;
binary32[0][1] = 0.0;
binary32[0][2] = 0.5;
float2short(8, pcm, binary32);
EXPECT_EQ(-16384, pcm[0][0]);
EXPECT_EQ(0, pcm[0][1]);
EXPECT_EQ(16384, pcm[0][2]);
}
TEST(float2short, testOverflow) {
binary32[0][0] = -1.1;
binary32[0][1] = -1.0;
binary32[0][2] = 1.0;
binary32[0][3] = 1.1;
float2short(8, pcm, binary32);
EXPECT_EQ(-32768, pcm[0][0]);
EXPECT_EQ(-32768, pcm[0][1]);
EXPECT_EQ(32767, pcm[0][2]);
EXPECT_EQ(32767, pcm[0][3]);
}
void unclamped(size_t n, short pcm16[n][8], const float binary32[n][8]) {
size_t i, j;
for (i = 0; i < n; ++i) {
for (j = 0; j < 8; ++j) {
pcm16[i][j] = binary32[i][j] * 32768;
}
}
}
plm_samples_t samps;
void randomizeaudio(void) {
size_t i;
for (i = 0; i < ARRAYLEN(samps.interleaved); ++i) {
samps.interleaved[i] = randf();
}
}
void float2short_pure(void) {
float2short(ARRAYLEN(samps.interleaved) / 8, pcm, (void *)samps.interleaved);
}
void float2short_unclamped(void) {
unclamped(ARRAYLEN(samps.interleaved) / 8, pcm, (void *)samps.interleaved);
}
BENCH(float2short, audioframe) {
EZBENCH(randomizeaudio(), float2short_pure());
EZBENCH(randomizeaudio(), float2short_unclamped());
}

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