mirror of
https://github.com/jart/cosmopolitan.git
synced 2025-06-27 06:48:31 +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
|
@ -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)));
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue