mirror of
https://github.com/jart/cosmopolitan.git
synced 2025-05-23 05:42:29 +00:00
Make more threading improvements
- ASAN memory morgue is now lockless - Make C11 atomics header more portable - Rewrote pthread keys support to be lockless - Simplify Python's unicode table unpacking code - Make crash report write(2) closer to being atomic - Make it possible to strace/ftrace a single thread - ASAN now checks nul-terminated strings fast and properly - Windows fork() now restores TLS memory of calling thread
This commit is contained in:
parent
d7b88734cd
commit
e522aa3a07
189 changed files with 1363 additions and 1217 deletions
|
@ -24,7 +24,7 @@
|
|||
#include "libc/testlib/subprocess.h"
|
||||
#include "libc/testlib/testlib.h"
|
||||
|
||||
#define N 127
|
||||
#define N 16
|
||||
|
||||
char *GenBuf(char buf[8], int x) {
|
||||
int i;
|
||||
|
|
|
@ -19,18 +19,13 @@
|
|||
#include "dsp/core/core.h"
|
||||
#include "libc/calls/calls.h"
|
||||
#include "libc/calls/struct/rlimit.h"
|
||||
#include "libc/calls/struct/sigaction.h"
|
||||
#include "libc/dce.h"
|
||||
#include "libc/errno.h"
|
||||
#include "libc/fmt/fmt.h"
|
||||
#include "libc/intrin/directmap.internal.h"
|
||||
#include "libc/intrin/safemacros.internal.h"
|
||||
#include "libc/log/check.h"
|
||||
#include "libc/math.h"
|
||||
#include "libc/mem/mem.h"
|
||||
#include "libc/runtime/directmap.internal.h"
|
||||
#include "libc/runtime/runtime.h"
|
||||
#include "libc/stdio/rand.h"
|
||||
#include "libc/str/str.h"
|
||||
#include "libc/sysv/consts/map.h"
|
||||
#include "libc/sysv/consts/o.h"
|
||||
#include "libc/sysv/consts/prot.h"
|
||||
|
@ -47,13 +42,13 @@ static char tmpname[PATH_MAX];
|
|||
|
||||
void OnSigxcpu(int sig) {
|
||||
ASSERT_EQ(SIGXCPU, sig);
|
||||
_exit(0);
|
||||
_Exit(0);
|
||||
}
|
||||
|
||||
void OnSigxfsz(int sig) {
|
||||
unlink(tmpname);
|
||||
ASSERT_EQ(SIGXFSZ, sig);
|
||||
_exit(0);
|
||||
_Exit(0);
|
||||
}
|
||||
|
||||
TEST(setrlimit, testCpuLimit) {
|
||||
|
@ -66,7 +61,7 @@ TEST(setrlimit, testCpuLimit) {
|
|||
if (IsOpenbsd()) return; /* TODO(jart): fix flake */
|
||||
ASSERT_NE(-1, (wstatus = xspawn(0)));
|
||||
if (wstatus == -2) {
|
||||
CHECK_EQ(0, xsigaction(SIGXCPU, OnSigxcpu, 0, 0, 0));
|
||||
ASSERT_EQ(0, xsigaction(SIGXCPU, OnSigxcpu, 0, 0, 0));
|
||||
ASSERT_EQ(0, getrlimit(RLIMIT_CPU, &rlim));
|
||||
rlim.rlim_cur = 1; /* set soft limit to one second */
|
||||
ASSERT_EQ(0, setrlimit(RLIMIT_CPU, &rlim));
|
||||
|
@ -77,7 +72,7 @@ TEST(setrlimit, testCpuLimit) {
|
|||
matmul3(matrices[0], matrices[1], matrices[2]);
|
||||
matmul3(matrices[0], matrices[1], matrices[2]);
|
||||
} while ((nowl() - start) < 5);
|
||||
_exit(1);
|
||||
_Exit(1);
|
||||
}
|
||||
EXPECT_TRUE(WIFEXITED(wstatus));
|
||||
EXPECT_FALSE(WIFSIGNALED(wstatus));
|
||||
|
@ -93,7 +88,7 @@ TEST(setrlimit, testFileSizeLimit) {
|
|||
if (IsWindows()) return; /* of course it doesn't work on windows */
|
||||
ASSERT_NE(-1, (wstatus = xspawn(0)));
|
||||
if (wstatus == -2) {
|
||||
CHECK_EQ(0, xsigaction(SIGXFSZ, OnSigxfsz, 0, 0, 0));
|
||||
ASSERT_EQ(0, xsigaction(SIGXFSZ, OnSigxfsz, 0, 0, 0));
|
||||
ASSERT_EQ(0, getrlimit(RLIMIT_FSIZE, &rlim));
|
||||
rlim.rlim_cur = 1024 * 1024; /* set soft limit to one megabyte */
|
||||
ASSERT_EQ(0, setrlimit(RLIMIT_FSIZE, &rlim));
|
||||
|
@ -107,7 +102,7 @@ TEST(setrlimit, testFileSizeLimit) {
|
|||
}
|
||||
close(fd);
|
||||
unlink(tmpname);
|
||||
_exit(1);
|
||||
_Exit(1);
|
||||
}
|
||||
EXPECT_TRUE(WIFEXITED(wstatus));
|
||||
EXPECT_FALSE(WIFSIGNALED(wstatus));
|
||||
|
@ -141,11 +136,11 @@ TEST(setrlimit, testMemoryLimit) {
|
|||
ASSERT_TRUE(gotsome);
|
||||
}
|
||||
ASSERT_EQ(ENOMEM, errno);
|
||||
_exit(0);
|
||||
_Exit(0);
|
||||
}
|
||||
rngset(p, PAGESIZE, _rand64, -1);
|
||||
}
|
||||
_exit(1);
|
||||
_Exit(1);
|
||||
}
|
||||
EXPECT_TRUE(WIFEXITED(wstatus));
|
||||
EXPECT_FALSE(WIFSIGNALED(wstatus));
|
||||
|
@ -169,11 +164,11 @@ TEST(setrlimit, testVirtualMemoryLimit) {
|
|||
.addr;
|
||||
if (p == MAP_FAILED) {
|
||||
ASSERT_EQ(ENOMEM, errno);
|
||||
_exit(0);
|
||||
_Exit(0);
|
||||
}
|
||||
rngset(p, PAGESIZE, _rand64, -1);
|
||||
}
|
||||
_exit(1);
|
||||
_Exit(1);
|
||||
}
|
||||
EXPECT_TRUE(WIFEXITED(wstatus));
|
||||
EXPECT_FALSE(WIFSIGNALED(wstatus));
|
||||
|
@ -199,11 +194,11 @@ TEST(setrlimit, testDataMemoryLimit) {
|
|||
.addr;
|
||||
if (p == MAP_FAILED) {
|
||||
ASSERT_EQ(ENOMEM, errno);
|
||||
_exit(0);
|
||||
_Exit(0);
|
||||
}
|
||||
rngset(p, PAGESIZE, _rand64, -1);
|
||||
}
|
||||
_exit(1);
|
||||
_Exit(1);
|
||||
}
|
||||
EXPECT_TRUE(WIFEXITED(wstatus));
|
||||
EXPECT_FALSE(WIFSIGNALED(wstatus));
|
||||
|
@ -225,7 +220,7 @@ wontreturn void OnVfork(void *ctx) {
|
|||
rlim = ctx;
|
||||
rlim->rlim_cur -= 1;
|
||||
ASSERT_EQ(0, getrlimit(RLIMIT_CPU, rlim));
|
||||
_exit(0);
|
||||
_Exit(0);
|
||||
}
|
||||
|
||||
TEST(setrlimit, isVforkSafe) {
|
||||
|
|
|
@ -19,6 +19,7 @@
|
|||
#include "libc/dce.h"
|
||||
#include "libc/fmt/fmt.h"
|
||||
#include "libc/intrin/asan.internal.h"
|
||||
#include "libc/intrin/kprintf.h"
|
||||
#include "libc/log/libfatal.internal.h"
|
||||
#include "libc/log/log.h"
|
||||
#include "libc/mem/gc.internal.h"
|
||||
|
@ -29,9 +30,16 @@
|
|||
#include "libc/testlib/ezbench.h"
|
||||
#include "libc/testlib/testlib.h"
|
||||
|
||||
noasan signed char ReadShadow(signed char *s) {
|
||||
return *s;
|
||||
}
|
||||
|
||||
void SetUp(void) {
|
||||
if (!IsAsan()) exit(0);
|
||||
}
|
||||
|
||||
TEST(asan, test) {
|
||||
char *p;
|
||||
if (!IsAsan()) return;
|
||||
p = gc(malloc(3));
|
||||
EXPECT_TRUE(__asan_is_valid(0, 0));
|
||||
EXPECT_TRUE(__asan_is_valid(p, 3));
|
||||
|
@ -53,7 +61,6 @@ TEST(asan, test) {
|
|||
|
||||
TEST(asan, test2) {
|
||||
char *p;
|
||||
if (!IsAsan()) return;
|
||||
p = gc(memalign(16, 64));
|
||||
// asan design precludes this kind of poisoning
|
||||
__asan_poison(p + 1, 1, kAsanProtected);
|
||||
|
@ -72,7 +79,6 @@ TEST(asan, test2) {
|
|||
}
|
||||
|
||||
TEST(asan, testEmptySize_isAlwaysValid) {
|
||||
if (!IsAsan()) return;
|
||||
EXPECT_TRUE(__asan_is_valid(0, 0));
|
||||
EXPECT_TRUE(__asan_is_valid((void *)(intptr_t)-2, 0));
|
||||
EXPECT_TRUE(__asan_is_valid((void *)(intptr_t)9999, 0));
|
||||
|
@ -80,7 +86,6 @@ TEST(asan, testEmptySize_isAlwaysValid) {
|
|||
|
||||
TEST(asan, testBigSize_worksFine) {
|
||||
char *p;
|
||||
if (!IsAsan()) return;
|
||||
p = malloc(64 * 1024);
|
||||
EXPECT_TRUE(__asan_is_valid(p, 64 * 1024));
|
||||
EXPECT_FALSE(__asan_is_valid(p - 1, 64 * 1024));
|
||||
|
@ -90,7 +95,6 @@ TEST(asan, testBigSize_worksFine) {
|
|||
}
|
||||
|
||||
TEST(asan, testUnmappedShadowMemory_doesntSegfault) {
|
||||
if (!IsAsan()) return;
|
||||
EXPECT_FALSE(__asan_is_valid(0, 1));
|
||||
EXPECT_FALSE(__asan_is_valid((void *)(intptr_t)-1, 1));
|
||||
EXPECT_FALSE(__asan_is_valid((void *)(intptr_t)-2, 1));
|
||||
|
@ -101,14 +105,59 @@ TEST(asan, testUnmappedShadowMemory_doesntSegfault) {
|
|||
EXPECT_FALSE(__asan_is_valid((void *)(intptr_t)9999, 7));
|
||||
}
|
||||
|
||||
TEST(asan, str) {
|
||||
char *p;
|
||||
int i, j, n, N = 64;
|
||||
struct AsanFault f;
|
||||
ASSERT_EQ(kAsanNullPage, __asan_check_str(0).kind);
|
||||
p = malloc(N);
|
||||
for (i = 0; i < N / 2; ++i) {
|
||||
for (n = 1; n < N / 2; ++n) {
|
||||
__asan_poison(p, i, kAsanProtected);
|
||||
__asan_unpoison(p + i, n);
|
||||
__asan_poison(p + i + n, N - i - n, kAsanProtected);
|
||||
for (j = 0; j < n; ++j) {
|
||||
p[i + j + 0] = j < n - 1;
|
||||
}
|
||||
// test valid string
|
||||
f = __asan_check_str(p + i);
|
||||
ASSERT_EQ(0, f.kind);
|
||||
ASSERT_EQ(0, f.shadow);
|
||||
// test pointer underrun
|
||||
f = __asan_check_str(p + i - 1);
|
||||
if (!i) {
|
||||
ASSERT_EQ(kAsanHeapUnderrun, f.kind);
|
||||
} else if (!((intptr_t)(p + i) & 7)) {
|
||||
ASSERT_EQ(kAsanProtected, f.kind);
|
||||
}
|
||||
// test missing nul terminator
|
||||
p[i + n - 1] = 1;
|
||||
f = __asan_check_str(p + i);
|
||||
if (i + n == N) {
|
||||
ASSERT_EQ(kAsanHeapOverrun, f.kind);
|
||||
} else if (i + n + 8 <= N) {
|
||||
ASSERT_EQ(kAsanProtected, f.kind);
|
||||
} else {
|
||||
ASSERT_EQ(kAsanHeapOverrun, f.kind);
|
||||
}
|
||||
}
|
||||
}
|
||||
__asan_unpoison(p, N);
|
||||
free(p);
|
||||
}
|
||||
|
||||
BENCH(asan, bench) {
|
||||
char *p;
|
||||
size_t n, m;
|
||||
if (!IsAsan()) return;
|
||||
m = 4 * 1024 * 1024;
|
||||
p = gc(malloc(m));
|
||||
EZBENCH_N("__asan_check", 0, EXPROPRIATE(__asan_check(p, 0).kind));
|
||||
for (n = 2; n <= m; n *= 2) {
|
||||
EZBENCH_N("__asan_check", n, EXPROPRIATE(__asan_check(p, n).kind));
|
||||
}
|
||||
memset(p, 1, m);
|
||||
for (n = m; n >= 2; n /= 2) {
|
||||
EZBENCH_N("__asan_check_str", n,
|
||||
(p[n - 1] = 0, EXPROPRIATE(__asan_check_str(p).kind)));
|
||||
}
|
||||
}
|
||||
|
|
|
@ -30,7 +30,9 @@
|
|||
#include "libc/sysv/consts/prot.h"
|
||||
#include "libc/sysv/consts/sig.h"
|
||||
#include "libc/testlib/ezbench.h"
|
||||
#include "libc/testlib/subprocess.h"
|
||||
#include "libc/testlib/testlib.h"
|
||||
#include "libc/thread/tls.h"
|
||||
|
||||
TEST(fork, testPipes) {
|
||||
int a, b;
|
||||
|
@ -131,6 +133,14 @@ TEST(fork, childToChild) {
|
|||
sigprocmask(SIG_SETMASK, &oldmask, 0);
|
||||
}
|
||||
|
||||
TEST(fork, preservesTlsMemory) {
|
||||
int pid;
|
||||
__get_tls()->tib_errno = 31337;
|
||||
SPAWN(fork);
|
||||
ASSERT_EQ(31337, __get_tls()->tib_errno);
|
||||
EXITS(0);
|
||||
}
|
||||
|
||||
void ForkInSerial(void) {
|
||||
int pid, ws;
|
||||
ASSERT_NE(-1, (pid = fork()));
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue