Resolve bootstrapping issues when embedded in FreeBSD libc.

b2c0d6322d (Add witness, a simple online
locking validator.) caused a broad propagation of tsd throughout the
internal API, but tsd_fetch() was designed to fail prior to tsd
bootstrapping.  Fix this by splitting tsd_t into non-nullable tsd_t and
nullable tsdn_t, and modifying all internal APIs that do not critically
rely on tsd to take nullable pointers.  Furthermore, add the
tsd_booted_get() function so that tsdn_fetch() can probe whether tsd
bootstrapping is complete and return NULL if not.  All dangerous
conversions of nullable pointers are tsdn_tsd() calls that assert-fail
on invalid conversion.
This commit is contained in:
Jason Evans
2016-05-10 22:21:10 -07:00
parent 0c12dcabc5
commit c1e00ef2a6
34 changed files with 1709 additions and 1556 deletions

View File

@@ -86,7 +86,7 @@ TEST_BEGIN(test_arena_reset)
void **ptrs;
int flags;
size_t mib[3];
tsd_t *tsd;
tsdn_t *tsdn;
test_skip_if((config_valgrind && unlikely(in_valgrind)) || (config_fill
&& unlikely(opt_quarantine)));
@@ -124,11 +124,11 @@ TEST_BEGIN(test_arena_reset)
"Unexpected mallocx(%zu, %#x) failure", sz, flags);
}
tsd = tsd_fetch();
tsdn = tsdn_fetch();
/* Verify allocations. */
for (i = 0; i < nptrs; i++) {
assert_zu_gt(ivsalloc(tsd, ptrs[i], false), 0,
assert_zu_gt(ivsalloc(tsdn, ptrs[i], false), 0,
"Allocation should have queryable size");
}
@@ -142,7 +142,7 @@ TEST_BEGIN(test_arena_reset)
/* Verify allocations no longer exist. */
for (i = 0; i < nptrs; i++) {
assert_zu_eq(ivsalloc(tsd, ptrs[i], false), 0,
assert_zu_eq(ivsalloc(tsdn, ptrs[i], false), 0,
"Allocation should no longer exist");
}

View File

@@ -2,24 +2,24 @@
TEST_BEGIN(test_new_delete)
{
tsd_t *tsd;
tsdn_t *tsdn;
ckh_t ckh;
tsd = tsd_fetch();
tsdn = tsdn_fetch();
assert_false(ckh_new(tsd, &ckh, 2, ckh_string_hash, ckh_string_keycomp),
"Unexpected ckh_new() error");
ckh_delete(tsd, &ckh);
assert_false(ckh_new(tsdn, &ckh, 2, ckh_string_hash,
ckh_string_keycomp), "Unexpected ckh_new() error");
ckh_delete(tsdn, &ckh);
assert_false(ckh_new(tsd, &ckh, 3, ckh_pointer_hash,
assert_false(ckh_new(tsdn, &ckh, 3, ckh_pointer_hash,
ckh_pointer_keycomp), "Unexpected ckh_new() error");
ckh_delete(tsd, &ckh);
ckh_delete(tsdn, &ckh);
}
TEST_END
TEST_BEGIN(test_count_insert_search_remove)
{
tsd_t *tsd;
tsdn_t *tsdn;
ckh_t ckh;
const char *strs[] = {
"a string",
@@ -30,17 +30,17 @@ TEST_BEGIN(test_count_insert_search_remove)
const char *missing = "A string not in the hash table.";
size_t i;
tsd = tsd_fetch();
tsdn = tsdn_fetch();
assert_false(ckh_new(tsd, &ckh, 2, ckh_string_hash, ckh_string_keycomp),
"Unexpected ckh_new() error");
assert_false(ckh_new(tsdn, &ckh, 2, ckh_string_hash,
ckh_string_keycomp), "Unexpected ckh_new() error");
assert_zu_eq(ckh_count(&ckh), 0,
"ckh_count() should return %zu, but it returned %zu", ZU(0),
ckh_count(&ckh));
/* Insert. */
for (i = 0; i < sizeof(strs)/sizeof(const char *); i++) {
ckh_insert(tsd, &ckh, strs[i], strs[i]);
ckh_insert(tsdn, &ckh, strs[i], strs[i]);
assert_zu_eq(ckh_count(&ckh), i+1,
"ckh_count() should return %zu, but it returned %zu", i+1,
ckh_count(&ckh));
@@ -85,7 +85,7 @@ TEST_BEGIN(test_count_insert_search_remove)
vp = (i & 2) ? &v.p : NULL;
k.p = NULL;
v.p = NULL;
assert_false(ckh_remove(tsd, &ckh, strs[i], kp, vp),
assert_false(ckh_remove(tsdn, &ckh, strs[i], kp, vp),
"Unexpected ckh_remove() error");
ks = (i & 1) ? strs[i] : (const char *)NULL;
@@ -101,22 +101,22 @@ TEST_BEGIN(test_count_insert_search_remove)
ckh_count(&ckh));
}
ckh_delete(tsd, &ckh);
ckh_delete(tsdn, &ckh);
}
TEST_END
TEST_BEGIN(test_insert_iter_remove)
{
#define NITEMS ZU(1000)
tsd_t *tsd;
tsdn_t *tsdn;
ckh_t ckh;
void **p[NITEMS];
void *q, *r;
size_t i;
tsd = tsd_fetch();
tsdn = tsdn_fetch();
assert_false(ckh_new(tsd, &ckh, 2, ckh_pointer_hash,
assert_false(ckh_new(tsdn, &ckh, 2, ckh_pointer_hash,
ckh_pointer_keycomp), "Unexpected ckh_new() error");
for (i = 0; i < NITEMS; i++) {
@@ -128,7 +128,7 @@ TEST_BEGIN(test_insert_iter_remove)
size_t j;
for (j = i; j < NITEMS; j++) {
assert_false(ckh_insert(tsd, &ckh, p[j], p[j]),
assert_false(ckh_insert(tsdn, &ckh, p[j], p[j]),
"Unexpected ckh_insert() failure");
assert_false(ckh_search(&ckh, p[j], &q, &r),
"Unexpected ckh_search() failure");
@@ -143,13 +143,13 @@ TEST_BEGIN(test_insert_iter_remove)
for (j = i + 1; j < NITEMS; j++) {
assert_false(ckh_search(&ckh, p[j], NULL, NULL),
"Unexpected ckh_search() failure");
assert_false(ckh_remove(tsd, &ckh, p[j], &q, &r),
assert_false(ckh_remove(tsdn, &ckh, p[j], &q, &r),
"Unexpected ckh_remove() failure");
assert_ptr_eq(p[j], q, "Key pointer mismatch");
assert_ptr_eq(p[j], r, "Value pointer mismatch");
assert_true(ckh_search(&ckh, p[j], NULL, NULL),
"Unexpected ckh_search() success");
assert_true(ckh_remove(tsd, &ckh, p[j], &q, &r),
assert_true(ckh_remove(tsdn, &ckh, p[j], &q, &r),
"Unexpected ckh_remove() success");
}
@@ -184,13 +184,13 @@ TEST_BEGIN(test_insert_iter_remove)
for (i = 0; i < NITEMS; i++) {
assert_false(ckh_search(&ckh, p[i], NULL, NULL),
"Unexpected ckh_search() failure");
assert_false(ckh_remove(tsd, &ckh, p[i], &q, &r),
assert_false(ckh_remove(tsdn, &ckh, p[i], &q, &r),
"Unexpected ckh_remove() failure");
assert_ptr_eq(p[i], q, "Key pointer mismatch");
assert_ptr_eq(p[i], r, "Value pointer mismatch");
assert_true(ckh_search(&ckh, p[i], NULL, NULL),
"Unexpected ckh_search() success");
assert_true(ckh_remove(tsd, &ckh, p[i], &q, &r),
assert_true(ckh_remove(tsdn, &ckh, p[i], &q, &r),
"Unexpected ckh_remove() success");
dallocx(p[i], 0);
}
@@ -198,7 +198,7 @@ TEST_BEGIN(test_insert_iter_remove)
assert_zu_eq(ckh_count(&ckh), 0,
"ckh_count() should return %zu, but it returned %zu",
ZU(0), ckh_count(&ckh));
ckh_delete(tsd, &ckh);
ckh_delete(tsdn, &ckh);
#undef NITEMS
}
TEST_END

View File

@@ -53,10 +53,10 @@ arena_dalloc_junk_large_intercept(void *ptr, size_t usize)
}
static void
huge_dalloc_junk_intercept(tsd_t *tsd, void *ptr, size_t usize)
huge_dalloc_junk_intercept(tsdn_t *tsdn, void *ptr, size_t usize)
{
huge_dalloc_junk_orig(tsd, ptr, usize);
huge_dalloc_junk_orig(tsdn, ptr, usize);
/*
* The conditions under which junk filling actually occurs are nuanced
* enough that it doesn't make sense to duplicate the decision logic in

View File

@@ -94,7 +94,7 @@ TEST_END
bool prof_dump_header_intercepted = false;
prof_cnt_t cnt_all_copy = {0, 0, 0, 0};
static bool
prof_dump_header_intercept(tsd_t *tsd, bool propagate_err,
prof_dump_header_intercept(tsdn_t *tsdn, bool propagate_err,
const prof_cnt_t *cnt_all)
{

View File

@@ -60,76 +60,76 @@ witness_comp_reverse(const witness_t *a, const witness_t *b)
TEST_BEGIN(test_witness)
{
witness_t a, b;
tsd_t *tsd;
tsdn_t *tsdn;
test_skip_if(!config_debug);
tsd = tsd_fetch();
tsdn = tsdn_fetch();
witness_assert_lockless(tsd);
witness_assert_lockless(tsdn);
witness_init(&a, "a", 1, NULL);
witness_assert_not_owner(tsd, &a);
witness_lock(tsd, &a);
witness_assert_owner(tsd, &a);
witness_assert_not_owner(tsdn, &a);
witness_lock(tsdn, &a);
witness_assert_owner(tsdn, &a);
witness_init(&b, "b", 2, NULL);
witness_assert_not_owner(tsd, &b);
witness_lock(tsd, &b);
witness_assert_owner(tsd, &b);
witness_assert_not_owner(tsdn, &b);
witness_lock(tsdn, &b);
witness_assert_owner(tsdn, &b);
witness_unlock(tsd, &a);
witness_unlock(tsd, &b);
witness_unlock(tsdn, &a);
witness_unlock(tsdn, &b);
witness_assert_lockless(tsd);
witness_assert_lockless(tsdn);
}
TEST_END
TEST_BEGIN(test_witness_comp)
{
witness_t a, b, c, d;
tsd_t *tsd;
tsdn_t *tsdn;
test_skip_if(!config_debug);
tsd = tsd_fetch();
tsdn = tsdn_fetch();
witness_assert_lockless(tsd);
witness_assert_lockless(tsdn);
witness_init(&a, "a", 1, witness_comp);
witness_assert_not_owner(tsd, &a);
witness_lock(tsd, &a);
witness_assert_owner(tsd, &a);
witness_assert_not_owner(tsdn, &a);
witness_lock(tsdn, &a);
witness_assert_owner(tsdn, &a);
witness_init(&b, "b", 1, witness_comp);
witness_assert_not_owner(tsd, &b);
witness_lock(tsd, &b);
witness_assert_owner(tsd, &b);
witness_unlock(tsd, &b);
witness_assert_not_owner(tsdn, &b);
witness_lock(tsdn, &b);
witness_assert_owner(tsdn, &b);
witness_unlock(tsdn, &b);
witness_lock_error_orig = witness_lock_error;
witness_lock_error = witness_lock_error_intercept;
saw_lock_error = false;
witness_init(&c, "c", 1, witness_comp_reverse);
witness_assert_not_owner(tsd, &c);
witness_assert_not_owner(tsdn, &c);
assert_false(saw_lock_error, "Unexpected witness lock error");
witness_lock(tsd, &c);
witness_lock(tsdn, &c);
assert_true(saw_lock_error, "Expected witness lock error");
witness_unlock(tsd, &c);
witness_unlock(tsdn, &c);
saw_lock_error = false;
witness_init(&d, "d", 1, NULL);
witness_assert_not_owner(tsd, &d);
witness_assert_not_owner(tsdn, &d);
assert_false(saw_lock_error, "Unexpected witness lock error");
witness_lock(tsd, &d);
witness_lock(tsdn, &d);
assert_true(saw_lock_error, "Expected witness lock error");
witness_unlock(tsd, &d);
witness_unlock(tsdn, &d);
witness_unlock(tsd, &a);
witness_unlock(tsdn, &a);
witness_assert_lockless(tsd);
witness_assert_lockless(tsdn);
witness_lock_error = witness_lock_error_orig;
}
@@ -138,7 +138,7 @@ TEST_END
TEST_BEGIN(test_witness_reversal)
{
witness_t a, b;
tsd_t *tsd;
tsdn_t *tsdn;
test_skip_if(!config_debug);
@@ -146,22 +146,22 @@ TEST_BEGIN(test_witness_reversal)
witness_lock_error = witness_lock_error_intercept;
saw_lock_error = false;
tsd = tsd_fetch();
tsdn = tsdn_fetch();
witness_assert_lockless(tsd);
witness_assert_lockless(tsdn);
witness_init(&a, "a", 1, NULL);
witness_init(&b, "b", 2, NULL);
witness_lock(tsd, &b);
witness_lock(tsdn, &b);
assert_false(saw_lock_error, "Unexpected witness lock error");
witness_lock(tsd, &a);
witness_lock(tsdn, &a);
assert_true(saw_lock_error, "Expected witness lock error");
witness_unlock(tsd, &a);
witness_unlock(tsd, &b);
witness_unlock(tsdn, &a);
witness_unlock(tsdn, &b);
witness_assert_lockless(tsd);
witness_assert_lockless(tsdn);
witness_lock_error = witness_lock_error_orig;
}
@@ -170,7 +170,7 @@ TEST_END
TEST_BEGIN(test_witness_recursive)
{
witness_t a;
tsd_t *tsd;
tsdn_t *tsdn;
test_skip_if(!config_debug);
@@ -182,22 +182,22 @@ TEST_BEGIN(test_witness_recursive)
witness_lock_error = witness_lock_error_intercept;
saw_lock_error = false;
tsd = tsd_fetch();
tsdn = tsdn_fetch();
witness_assert_lockless(tsd);
witness_assert_lockless(tsdn);
witness_init(&a, "a", 1, NULL);
witness_lock(tsd, &a);
witness_lock(tsdn, &a);
assert_false(saw_lock_error, "Unexpected witness lock error");
assert_false(saw_not_owner_error, "Unexpected witness not owner error");
witness_lock(tsd, &a);
witness_lock(tsdn, &a);
assert_true(saw_lock_error, "Expected witness lock error");
assert_true(saw_not_owner_error, "Expected witness not owner error");
witness_unlock(tsd, &a);
witness_unlock(tsdn, &a);
witness_assert_lockless(tsd);
witness_assert_lockless(tsdn);
witness_owner_error = witness_owner_error_orig;
witness_lock_error = witness_lock_error_orig;
@@ -208,7 +208,7 @@ TEST_END
TEST_BEGIN(test_witness_unlock_not_owned)
{
witness_t a;
tsd_t *tsd;
tsdn_t *tsdn;
test_skip_if(!config_debug);
@@ -216,17 +216,17 @@ TEST_BEGIN(test_witness_unlock_not_owned)
witness_owner_error = witness_owner_error_intercept;
saw_owner_error = false;
tsd = tsd_fetch();
tsdn = tsdn_fetch();
witness_assert_lockless(tsd);
witness_assert_lockless(tsdn);
witness_init(&a, "a", 1, NULL);
assert_false(saw_owner_error, "Unexpected owner error");
witness_unlock(tsd, &a);
witness_unlock(tsdn, &a);
assert_true(saw_owner_error, "Expected owner error");
witness_assert_lockless(tsd);
witness_assert_lockless(tsdn);
witness_owner_error = witness_owner_error_orig;
}
@@ -235,7 +235,7 @@ TEST_END
TEST_BEGIN(test_witness_lockful)
{
witness_t a;
tsd_t *tsd;
tsdn_t *tsdn;
test_skip_if(!config_debug);
@@ -243,22 +243,22 @@ TEST_BEGIN(test_witness_lockful)
witness_lockless_error = witness_lockless_error_intercept;
saw_lockless_error = false;
tsd = tsd_fetch();
tsdn = tsdn_fetch();
witness_assert_lockless(tsd);
witness_assert_lockless(tsdn);
witness_init(&a, "a", 1, NULL);
assert_false(saw_lockless_error, "Unexpected lockless error");
witness_assert_lockless(tsd);
witness_assert_lockless(tsdn);
witness_lock(tsd, &a);
witness_assert_lockless(tsd);
witness_lock(tsdn, &a);
witness_assert_lockless(tsdn);
assert_true(saw_lockless_error, "Expected lockless error");
witness_unlock(tsd, &a);
witness_unlock(tsdn, &a);
witness_assert_lockless(tsd);
witness_assert_lockless(tsdn);
witness_lockless_error = witness_lockless_error_orig;
}