Refactor test extent hook code to be reusable.

Move test extent hook code from the extent integration test into a
header, and normalize the out-of-band controls and introspection.
Also refactor the base unit test to use the header.
This commit is contained in:
Jason Evans 2017-01-06 11:22:08 -08:00
parent dc2125cf95
commit 3f291d59ad
3 changed files with 366 additions and 348 deletions

View File

@ -0,0 +1,264 @@
/*
* Boilerplate code used for testing extent hooks via interception and
* passthrough.
*/
static void *extent_alloc_hook(extent_hooks_t *extent_hooks, void *new_addr,
size_t size, size_t alignment, bool *zero, bool *commit,
unsigned arena_ind);
static bool extent_dalloc_hook(extent_hooks_t *extent_hooks, void *addr,
size_t size, bool committed, unsigned arena_ind);
static bool extent_commit_hook(extent_hooks_t *extent_hooks, void *addr,
size_t size, size_t offset, size_t length, unsigned arena_ind);
static bool extent_decommit_hook(extent_hooks_t *extent_hooks, void *addr,
size_t size, size_t offset, size_t length, unsigned arena_ind);
static bool extent_purge_lazy_hook(extent_hooks_t *extent_hooks, void *addr,
size_t size, size_t offset, size_t length, unsigned arena_ind);
static bool extent_purge_forced_hook(extent_hooks_t *extent_hooks,
void *addr, size_t size, size_t offset, size_t length, unsigned arena_ind);
static bool extent_split_hook(extent_hooks_t *extent_hooks, void *addr,
size_t size, size_t size_a, size_t size_b, bool committed,
unsigned arena_ind);
static bool extent_merge_hook(extent_hooks_t *extent_hooks, void *addr_a,
size_t size_a, void *addr_b, size_t size_b, bool committed,
unsigned arena_ind);
static extent_hooks_t *default_hooks;
static extent_hooks_t hooks = {
extent_alloc_hook,
extent_dalloc_hook,
extent_commit_hook,
extent_decommit_hook,
extent_purge_lazy_hook,
extent_purge_forced_hook,
extent_split_hook,
extent_merge_hook
};
/* Control whether hook functions pass calls through to default hooks. */
static bool try_alloc = true;
static bool try_dalloc = true;
static bool try_commit = true;
static bool try_decommit = true;
static bool try_purge_lazy = true;
static bool try_purge_forced = true;
static bool try_split = true;
static bool try_merge = true;
/* Set to false prior to operations, then introspect after operations. */
static bool called_alloc;
static bool called_dalloc;
static bool called_commit;
static bool called_decommit;
static bool called_purge_lazy;
static bool called_purge_forced;
static bool called_split;
static bool called_merge;
/* Set to false prior to operations, then introspect after operations. */
static bool did_alloc;
static bool did_dalloc;
static bool did_commit;
static bool did_decommit;
static bool did_purge_lazy;
static bool did_purge_forced;
static bool did_split;
static bool did_merge;
#if 0
# define TRACE_HOOK(fmt, ...) malloc_printf(fmt, __VA_ARGS__)
#else
# define TRACE_HOOK(fmt, ...)
#endif
static void *
extent_alloc_hook(extent_hooks_t *extent_hooks, void *new_addr, size_t size,
size_t alignment, bool *zero, bool *commit, unsigned arena_ind)
{
void *ret;
TRACE_HOOK("%s(extent_hooks=%p, new_addr=%p, size=%zu, alignment=%zu, "
"*zero=%s, *commit=%s, arena_ind=%u)\n", __func__, extent_hooks,
new_addr, size, alignment, *zero ? "true" : "false", *commit ?
"true" : "false", arena_ind);
assert_ptr_eq(extent_hooks, &hooks,
"extent_hooks should be same as pointer used to set hooks");
assert_ptr_eq(extent_hooks->alloc, extent_alloc_hook,
"Wrong hook function");
called_alloc = true;
if (!try_alloc)
return (NULL);
ret = default_hooks->alloc(default_hooks, new_addr, size, alignment,
zero, commit, 0);
did_alloc = (ret != NULL);
return (ret);
}
static bool
extent_dalloc_hook(extent_hooks_t *extent_hooks, void *addr, size_t size,
bool committed, unsigned arena_ind)
{
bool err;
TRACE_HOOK("%s(extent_hooks=%p, addr=%p, size=%zu, committed=%s, "
"arena_ind=%u)\n", __func__, extent_hooks, addr, size, committed ?
"true" : "false", arena_ind);
assert_ptr_eq(extent_hooks, &hooks,
"extent_hooks should be same as pointer used to set hooks");
assert_ptr_eq(extent_hooks->dalloc, extent_dalloc_hook,
"Wrong hook function");
called_dalloc = true;
if (!try_dalloc)
return (true);
err = default_hooks->dalloc(default_hooks, addr, size, committed, 0);
did_dalloc = !err;
return (err);
}
static bool
extent_commit_hook(extent_hooks_t *extent_hooks, void *addr, size_t size,
size_t offset, size_t length, unsigned arena_ind)
{
bool err;
TRACE_HOOK("%s(extent_hooks=%p, addr=%p, size=%zu, offset=%zu, "
"length=%zu, arena_ind=%u)\n", __func__, extent_hooks, addr, size,
offset, length, arena_ind);
assert_ptr_eq(extent_hooks, &hooks,
"extent_hooks should be same as pointer used to set hooks");
assert_ptr_eq(extent_hooks->commit, extent_commit_hook,
"Wrong hook function");
called_commit = true;
if (!try_commit)
return (true);
err = default_hooks->commit(default_hooks, addr, size, offset, length,
0);
did_commit = !err;
return (err);
}
static bool
extent_decommit_hook(extent_hooks_t *extent_hooks, void *addr, size_t size,
size_t offset, size_t length, unsigned arena_ind)
{
bool err;
TRACE_HOOK("%s(extent_hooks=%p, addr=%p, size=%zu, offset=%zu, "
"length=%zu, arena_ind=%u)\n", __func__, extent_hooks, addr, size,
offset, length, arena_ind);
assert_ptr_eq(extent_hooks, &hooks,
"extent_hooks should be same as pointer used to set hooks");
assert_ptr_eq(extent_hooks->decommit, extent_decommit_hook,
"Wrong hook function");
called_decommit = true;
if (!try_decommit)
return (true);
err = default_hooks->decommit(default_hooks, addr, size, offset, length,
0);
did_decommit = !err;
return (err);
}
static bool
extent_purge_lazy_hook(extent_hooks_t *extent_hooks, void *addr, size_t size,
size_t offset, size_t length, unsigned arena_ind)
{
bool err;
TRACE_HOOK("%s(extent_hooks=%p, addr=%p, size=%zu, offset=%zu, "
"length=%zu arena_ind=%u)\n", __func__, extent_hooks, addr, size,
offset, length, arena_ind);
assert_ptr_eq(extent_hooks, &hooks,
"extent_hooks should be same as pointer used to set hooks");
assert_ptr_eq(extent_hooks->purge_lazy, extent_purge_lazy_hook,
"Wrong hook function");
called_purge_lazy = true;
if (!try_purge_lazy)
return (true);
err = default_hooks->purge_lazy == NULL ||
default_hooks->purge_lazy(default_hooks, addr, size, offset, length,
0);
did_purge_lazy = !err;
return (err);
}
static bool
extent_purge_forced_hook(extent_hooks_t *extent_hooks, void *addr, size_t size,
size_t offset, size_t length, unsigned arena_ind)
{
bool err;
TRACE_HOOK("%s(extent_hooks=%p, addr=%p, size=%zu, offset=%zu, "
"length=%zu arena_ind=%u)\n", __func__, extent_hooks, addr, size,
offset, length, arena_ind);
assert_ptr_eq(extent_hooks, &hooks,
"extent_hooks should be same as pointer used to set hooks");
assert_ptr_eq(extent_hooks->purge_forced, extent_purge_forced_hook,
"Wrong hook function");
called_purge_forced = true;
if (!try_purge_forced)
return (true);
err = default_hooks->purge_forced == NULL ||
default_hooks->purge_forced(default_hooks, addr, size, offset,
length, 0);
did_purge_forced = !err;
return (err);
}
static bool
extent_split_hook(extent_hooks_t *extent_hooks, void *addr, size_t size,
size_t size_a, size_t size_b, bool committed, unsigned arena_ind)
{
bool err;
TRACE_HOOK("%s(extent_hooks=%p, addr=%p, size=%zu, size_a=%zu, "
"size_b=%zu, committed=%s, arena_ind=%u)\n", __func__, extent_hooks,
addr, size, size_a, size_b, committed ? "true" : "false",
arena_ind);
assert_ptr_eq(extent_hooks, &hooks,
"extent_hooks should be same as pointer used to set hooks");
assert_ptr_eq(extent_hooks->split, extent_split_hook,
"Wrong hook function");
called_split = true;
if (!try_split)
return (true);
err = (default_hooks->split == NULL ||
default_hooks->split(default_hooks, addr, size, size_a, size_b,
committed, 0));
did_split = !err;
return (err);
}
static bool
extent_merge_hook(extent_hooks_t *extent_hooks, void *addr_a, size_t size_a,
void *addr_b, size_t size_b, bool committed, unsigned arena_ind)
{
bool err;
TRACE_HOOK("%s(extent_hooks=%p, addr_a=%p, size_a=%zu, addr_b=%p "
"size_b=%zu, committed=%s, arena_ind=%u)\n", __func__, extent_hooks,
addr_a, size_a, addr_b, size_b, committed ? "true" : "false",
arena_ind);
assert_ptr_eq(extent_hooks, &hooks,
"extent_hooks should be same as pointer used to set hooks");
assert_ptr_eq(extent_hooks->merge, extent_merge_hook,
"Wrong hook function");
called_merge = true;
if (!try_merge)
return (true);
err = (default_hooks->merge == NULL ||
default_hooks->merge(default_hooks, addr_a, size_a, addr_b, size_b,
committed, 0));
did_merge = !err;
return (err);
}
static void
extent_hooks_prep(void)
{
size_t sz;
sz = sizeof(default_hooks);
assert_d_eq(mallctl("arena.0.extent_hooks", (void *)&default_hooks, &sz,
NULL, 0), 0, "Unexpected mallctl() error");
}

View File

@ -4,204 +4,7 @@
const char *malloc_conf = "junk:false"; const char *malloc_conf = "junk:false";
#endif #endif
static void *extent_alloc(extent_hooks_t *extent_hooks, void *new_addr, #include "test/extent_hooks.h"
size_t size, size_t alignment, bool *zero, bool *commit,
unsigned arena_ind);
static bool extent_dalloc(extent_hooks_t *extent_hooks, void *addr,
size_t size, bool committed, unsigned arena_ind);
static bool extent_commit(extent_hooks_t *extent_hooks, void *addr,
size_t size, size_t offset, size_t length, unsigned arena_ind);
static bool extent_decommit(extent_hooks_t *extent_hooks, void *addr,
size_t size, size_t offset, size_t length, unsigned arena_ind);
static bool extent_purge_lazy(extent_hooks_t *extent_hooks, void *addr,
size_t size, size_t offset, size_t length, unsigned arena_ind);
static bool extent_purge_forced(extent_hooks_t *extent_hooks, void *addr,
size_t size, size_t offset, size_t length, unsigned arena_ind);
static bool extent_split(extent_hooks_t *extent_hooks, void *addr,
size_t size, size_t size_a, size_t size_b, bool committed,
unsigned arena_ind);
static bool extent_merge(extent_hooks_t *extent_hooks, void *addr_a,
size_t size_a, void *addr_b, size_t size_b, bool committed,
unsigned arena_ind);
static extent_hooks_t hooks = {
extent_alloc,
extent_dalloc,
extent_commit,
extent_decommit,
extent_purge_lazy,
extent_purge_forced,
extent_split,
extent_merge
};
static extent_hooks_t *new_hooks = &hooks;
static extent_hooks_t *orig_hooks;
static extent_hooks_t *old_hooks;
static bool do_dalloc = true;
static bool do_decommit;
static bool did_alloc;
static bool did_dalloc;
static bool did_commit;
static bool did_decommit;
static bool did_purge_lazy;
static bool did_purge_forced;
static bool tried_split;
static bool did_split;
static bool did_merge;
#if 0
# define TRACE_HOOK(fmt, ...) malloc_printf(fmt, __VA_ARGS__)
#else
# define TRACE_HOOK(fmt, ...)
#endif
static void *
extent_alloc(extent_hooks_t *extent_hooks, void *new_addr, size_t size,
size_t alignment, bool *zero, bool *commit, unsigned arena_ind)
{
TRACE_HOOK("%s(extent_hooks=%p, new_addr=%p, size=%zu, alignment=%zu, "
"*zero=%s, *commit=%s, arena_ind=%u)\n", __func__, extent_hooks,
new_addr, size, alignment, *zero ? "true" : "false", *commit ?
"true" : "false", arena_ind);
assert_ptr_eq(extent_hooks, new_hooks,
"extent_hooks should be same as pointer used to set hooks");
assert_ptr_eq(extent_hooks->alloc, extent_alloc, "Wrong hook function");
did_alloc = true;
return (old_hooks->alloc(old_hooks, new_addr, size, alignment, zero,
commit, 0));
}
static bool
extent_dalloc(extent_hooks_t *extent_hooks, void *addr, size_t size,
bool committed, unsigned arena_ind)
{
TRACE_HOOK("%s(extent_hooks=%p, addr=%p, size=%zu, committed=%s, "
"arena_ind=%u)\n", __func__, extent_hooks, addr, size, committed ?
"true" : "false", arena_ind);
assert_ptr_eq(extent_hooks, new_hooks,
"extent_hooks should be same as pointer used to set hooks");
assert_ptr_eq(extent_hooks->dalloc, extent_dalloc,
"Wrong hook function");
did_dalloc = true;
if (!do_dalloc)
return (true);
return (old_hooks->dalloc(old_hooks, addr, size, committed, 0));
}
static bool
extent_commit(extent_hooks_t *extent_hooks, void *addr, size_t size,
size_t offset, size_t length, unsigned arena_ind)
{
bool err;
TRACE_HOOK("%s(extent_hooks=%p, addr=%p, size=%zu, offset=%zu, "
"length=%zu, arena_ind=%u)\n", __func__, extent_hooks, addr, size,
offset, length, arena_ind);
assert_ptr_eq(extent_hooks, new_hooks,
"extent_hooks should be same as pointer used to set hooks");
assert_ptr_eq(extent_hooks->commit, extent_commit,
"Wrong hook function");
err = old_hooks->commit(old_hooks, addr, size, offset, length, 0);
did_commit = !err;
return (err);
}
static bool
extent_decommit(extent_hooks_t *extent_hooks, void *addr, size_t size,
size_t offset, size_t length, unsigned arena_ind)
{
bool err;
TRACE_HOOK("%s(extent_hooks=%p, addr=%p, size=%zu, offset=%zu, "
"length=%zu, arena_ind=%u)\n", __func__, extent_hooks, addr, size,
offset, length, arena_ind);
assert_ptr_eq(extent_hooks, new_hooks,
"extent_hooks should be same as pointer used to set hooks");
assert_ptr_eq(extent_hooks->decommit, extent_decommit,
"Wrong hook function");
if (!do_decommit)
return (true);
err = old_hooks->decommit(old_hooks, addr, size, offset, length, 0);
did_decommit = !err;
return (err);
}
static bool
extent_purge_lazy(extent_hooks_t *extent_hooks, void *addr, size_t size,
size_t offset, size_t length, unsigned arena_ind)
{
TRACE_HOOK("%s(extent_hooks=%p, addr=%p, size=%zu, offset=%zu, "
"length=%zu arena_ind=%u)\n", __func__, extent_hooks, addr, size,
offset, length, arena_ind);
assert_ptr_eq(extent_hooks, new_hooks,
"extent_hooks should be same as pointer used to set hooks");
assert_ptr_eq(extent_hooks->purge_lazy, extent_purge_lazy,
"Wrong hook function");
did_purge_lazy = true;
return (old_hooks->purge_lazy == NULL ||
old_hooks->purge_lazy(old_hooks, addr, size, offset, length, 0));
}
static bool
extent_purge_forced(extent_hooks_t *extent_hooks, void *addr, size_t size,
size_t offset, size_t length, unsigned arena_ind)
{
TRACE_HOOK("%s(extent_hooks=%p, addr=%p, size=%zu, offset=%zu, "
"length=%zu arena_ind=%u)\n", __func__, extent_hooks, addr, size,
offset, length, arena_ind);
assert_ptr_eq(extent_hooks, new_hooks,
"extent_hooks should be same as pointer used to set hooks");
assert_ptr_eq(extent_hooks->purge_forced, extent_purge_forced,
"Wrong hook function");
did_purge_forced = true;
return (old_hooks->purge_forced == NULL ||
old_hooks->purge_forced(old_hooks, addr, size, offset, length, 0));
}
static bool
extent_split(extent_hooks_t *extent_hooks, void *addr, size_t size,
size_t size_a, size_t size_b, bool committed, unsigned arena_ind)
{
bool err;
TRACE_HOOK("%s(extent_hooks=%p, addr=%p, size=%zu, size_a=%zu, "
"size_b=%zu, committed=%s, arena_ind=%u)\n", __func__, extent_hooks,
addr, size, size_a, size_b, committed ? "true" : "false",
arena_ind);
assert_ptr_eq(extent_hooks, new_hooks,
"extent_hooks should be same as pointer used to set hooks");
assert_ptr_eq(extent_hooks->split, extent_split, "Wrong hook function");
tried_split = true;
err = (old_hooks->split == NULL || old_hooks->split(old_hooks, addr,
size, size_a, size_b, committed, 0));
did_split = !err;
return (err);
}
static bool
extent_merge(extent_hooks_t *extent_hooks, void *addr_a, size_t size_a,
void *addr_b, size_t size_b, bool committed, unsigned arena_ind)
{
bool err;
TRACE_HOOK("%s(extent_hooks=%p, addr_a=%p, size_a=%zu, addr_b=%p "
"size_b=%zu, committed=%s, arena_ind=%u)\n", __func__, extent_hooks,
addr_a, size_a, addr_b, size_b, committed ? "true" : "false",
arena_ind);
assert_ptr_eq(extent_hooks, new_hooks,
"extent_hooks should be same as pointer used to set hooks");
assert_ptr_eq(extent_hooks->merge, extent_merge, "Wrong hook function");
err = (old_hooks->merge == NULL || old_hooks->merge(old_hooks, addr_a,
size_a, addr_b, size_b, committed, 0));
did_merge = !err;
return (err);
}
static void static void
test_extent_body(unsigned arena_ind) test_extent_body(unsigned arena_ind)
@ -229,37 +32,36 @@ test_extent_body(unsigned arena_ind)
assert_d_eq(mallctlnametomib("arena.0.purge", purge_mib, &purge_miblen), assert_d_eq(mallctlnametomib("arena.0.purge", purge_mib, &purge_miblen),
0, "Unexpected mallctlnametomib() failure"); 0, "Unexpected mallctlnametomib() failure");
purge_mib[1] = (size_t)arena_ind; purge_mib[1] = (size_t)arena_ind;
do_dalloc = false; try_dalloc = false;
do_decommit = false; try_decommit = false;
p = mallocx(large0 * 2, flags); p = mallocx(large0 * 2, flags);
assert_ptr_not_null(p, "Unexpected mallocx() error"); assert_ptr_not_null(p, "Unexpected mallocx() error");
did_dalloc = false; called_dalloc = false;
did_decommit = false; called_decommit = false;
did_purge_lazy = false; did_purge_lazy = false;
did_purge_forced = false; did_purge_forced = false;
tried_split = false; called_split = false;
did_split = false;
xallocx_success_a = (xallocx(p, large0, 0, flags) == large0); xallocx_success_a = (xallocx(p, large0, 0, flags) == large0);
assert_d_eq(mallctlbymib(purge_mib, purge_miblen, NULL, NULL, NULL, 0), assert_d_eq(mallctlbymib(purge_mib, purge_miblen, NULL, NULL, NULL, 0),
0, "Unexpected arena.%u.purge error", arena_ind); 0, "Unexpected arena.%u.purge error", arena_ind);
if (xallocx_success_a) { if (xallocx_success_a) {
assert_true(did_dalloc, "Expected dalloc"); assert_true(called_dalloc, "Expected dalloc call");
assert_false(did_decommit, "Unexpected decommit"); assert_true(called_decommit, "Expected decommit call");
assert_true(did_purge_lazy || did_purge_forced, assert_true(did_purge_lazy || did_purge_forced,
"Expected purge"); "Expected purge");
} }
assert_true(tried_split, "Expected split"); assert_true(called_split, "Expected split call");
dallocx(p, flags); dallocx(p, flags);
do_dalloc = true; try_dalloc = true;
/* Test decommit/commit and observe split/merge. */ /* Test decommit/commit and observe split/merge. */
do_dalloc = false; try_dalloc = false;
do_decommit = true; try_decommit = true;
p = mallocx(large0 * 2, flags); p = mallocx(large0 * 2, flags);
assert_ptr_not_null(p, "Unexpected mallocx() error"); assert_ptr_not_null(p, "Unexpected mallocx() error");
did_decommit = false; did_decommit = false;
did_commit = false; did_commit = false;
tried_split = false; called_split = false;
did_split = false; did_split = false;
did_merge = false; did_merge = false;
xallocx_success_b = (xallocx(p, large0, 0, flags) == large0); xallocx_success_b = (xallocx(p, large0, 0, flags) == large0);
@ -275,8 +77,8 @@ test_extent_body(unsigned arena_ind)
if (xallocx_success_b && xallocx_success_c) if (xallocx_success_b && xallocx_success_c)
assert_true(did_merge, "Expected merge"); assert_true(did_merge, "Expected merge");
dallocx(p, flags); dallocx(p, flags);
do_dalloc = true; try_dalloc = true;
do_decommit = false; try_decommit = false;
/* Make sure non-large allocation succeeds. */ /* Make sure non-large allocation succeeds. */
p = mallocx(42, flags); p = mallocx(42, flags);
@ -290,6 +92,9 @@ TEST_BEGIN(test_extent_manual_hook)
size_t old_size, new_size, sz; size_t old_size, new_size, sz;
size_t hooks_mib[3]; size_t hooks_mib[3];
size_t hooks_miblen; size_t hooks_miblen;
extent_hooks_t *new_hooks, *old_hooks;
extent_hooks_prep();
sz = sizeof(unsigned); sz = sizeof(unsigned);
assert_d_eq(mallctl("arenas.create", (void *)&arena_ind, &sz, NULL, 0), assert_d_eq(mallctl("arenas.create", (void *)&arena_ind, &sz, NULL, 0),
@ -301,24 +106,27 @@ TEST_BEGIN(test_extent_manual_hook)
&hooks_miblen), 0, "Unexpected mallctlnametomib() failure"); &hooks_miblen), 0, "Unexpected mallctlnametomib() failure");
hooks_mib[1] = (size_t)arena_ind; hooks_mib[1] = (size_t)arena_ind;
old_size = sizeof(extent_hooks_t *); old_size = sizeof(extent_hooks_t *);
new_hooks = &hooks;
new_size = sizeof(extent_hooks_t *); new_size = sizeof(extent_hooks_t *);
assert_d_eq(mallctlbymib(hooks_mib, hooks_miblen, (void *)&old_hooks, assert_d_eq(mallctlbymib(hooks_mib, hooks_miblen, (void *)&old_hooks,
&old_size, (void *)&new_hooks, new_size), 0, &old_size, (void *)&new_hooks, new_size), 0,
"Unexpected extent_hooks error"); "Unexpected extent_hooks error");
orig_hooks = old_hooks; assert_ptr_ne(old_hooks->alloc, extent_alloc_hook,
assert_ptr_ne(old_hooks->alloc, extent_alloc, "Unexpected alloc error"); "Unexpected extent_hooks error");
assert_ptr_ne(old_hooks->dalloc, extent_dalloc, assert_ptr_ne(old_hooks->dalloc, extent_dalloc_hook,
"Unexpected dalloc error"); "Unexpected extent_hooks error");
assert_ptr_ne(old_hooks->commit, extent_commit, assert_ptr_ne(old_hooks->commit, extent_commit_hook,
"Unexpected commit error"); "Unexpected extent_hooks error");
assert_ptr_ne(old_hooks->decommit, extent_decommit, assert_ptr_ne(old_hooks->decommit, extent_decommit_hook,
"Unexpected decommit error"); "Unexpected extent_hooks error");
assert_ptr_ne(old_hooks->purge_lazy, extent_purge_lazy, assert_ptr_ne(old_hooks->purge_lazy, extent_purge_lazy_hook,
"Unexpected purge_lazy error"); "Unexpected extent_hooks error");
assert_ptr_ne(old_hooks->purge_forced, extent_purge_forced, assert_ptr_ne(old_hooks->purge_forced, extent_purge_forced_hook,
"Unexpected purge_forced error"); "Unexpected extent_hooks error");
assert_ptr_ne(old_hooks->split, extent_split, "Unexpected split error"); assert_ptr_ne(old_hooks->split, extent_split_hook,
assert_ptr_ne(old_hooks->merge, extent_merge, "Unexpected merge error"); "Unexpected extent_hooks error");
assert_ptr_ne(old_hooks->merge, extent_merge_hook,
"Unexpected extent_hooks error");
test_extent_body(arena_ind); test_extent_body(arena_ind);
@ -327,23 +135,23 @@ TEST_BEGIN(test_extent_manual_hook)
(void *)&old_hooks, new_size), 0, "Unexpected extent_hooks error"); (void *)&old_hooks, new_size), 0, "Unexpected extent_hooks error");
assert_d_eq(mallctlbymib(hooks_mib, hooks_miblen, (void *)&old_hooks, assert_d_eq(mallctlbymib(hooks_mib, hooks_miblen, (void *)&old_hooks,
&old_size, NULL, 0), 0, "Unexpected extent_hooks error"); &old_size, NULL, 0), 0, "Unexpected extent_hooks error");
assert_ptr_eq(old_hooks, orig_hooks, "Unexpected hooks error"); assert_ptr_eq(old_hooks, default_hooks, "Unexpected extent_hooks error");
assert_ptr_eq(old_hooks->alloc, orig_hooks->alloc, assert_ptr_eq(old_hooks->alloc, default_hooks->alloc,
"Unexpected alloc error"); "Unexpected extent_hooks error");
assert_ptr_eq(old_hooks->dalloc, orig_hooks->dalloc, assert_ptr_eq(old_hooks->dalloc, default_hooks->dalloc,
"Unexpected dalloc error"); "Unexpected extent_hooks error");
assert_ptr_eq(old_hooks->commit, orig_hooks->commit, assert_ptr_eq(old_hooks->commit, default_hooks->commit,
"Unexpected commit error"); "Unexpected extent_hooks error");
assert_ptr_eq(old_hooks->decommit, orig_hooks->decommit, assert_ptr_eq(old_hooks->decommit, default_hooks->decommit,
"Unexpected decommit error"); "Unexpected extent_hooks error");
assert_ptr_eq(old_hooks->purge_lazy, orig_hooks->purge_lazy, assert_ptr_eq(old_hooks->purge_lazy, default_hooks->purge_lazy,
"Unexpected purge_lazy error"); "Unexpected extent_hooks error");
assert_ptr_eq(old_hooks->purge_forced, orig_hooks->purge_forced, assert_ptr_eq(old_hooks->purge_forced, default_hooks->purge_forced,
"Unexpected purge_forced error"); "Unexpected extent_hooks error");
assert_ptr_eq(old_hooks->split, orig_hooks->split, assert_ptr_eq(old_hooks->split, default_hooks->split,
"Unexpected split error"); "Unexpected extent_hooks error");
assert_ptr_eq(old_hooks->merge, orig_hooks->merge, assert_ptr_eq(old_hooks->merge, default_hooks->merge,
"Unexpected merge error"); "Unexpected extent_hooks error");
} }
TEST_END TEST_END
@ -351,8 +159,12 @@ TEST_BEGIN(test_extent_auto_hook)
{ {
unsigned arena_ind; unsigned arena_ind;
size_t new_size, sz; size_t new_size, sz;
extent_hooks_t *new_hooks;
extent_hooks_prep();
sz = sizeof(unsigned); sz = sizeof(unsigned);
new_hooks = &hooks;
new_size = sizeof(extent_hooks_t *); new_size = sizeof(extent_hooks_t *);
assert_d_eq(mallctl("arenas.create", (void *)&arena_ind, &sz, assert_d_eq(mallctl("arenas.create", (void *)&arena_ind, &sz,
(void *)&new_hooks, new_size), 0, "Unexpected mallctl() failure"); (void *)&new_hooks, new_size), 0, "Unexpected mallctl() failure");

View File

@ -1,27 +1,6 @@
#include "test/jemalloc_test.h" #include "test/jemalloc_test.h"
static void *extent_alloc_hook(extent_hooks_t *extent_hooks, void *new_addr, #include "test/extent_hooks.h"
size_t size, size_t alignment, bool *zero, bool *commit,
unsigned arena_ind);
static bool extent_dalloc_hook(extent_hooks_t *extent_hooks, void *addr,
size_t size, bool committed, unsigned arena_ind);
static bool extent_decommit_hook(extent_hooks_t *extent_hooks, void *addr,
size_t size, size_t offset, size_t length, unsigned arena_ind);
static bool extent_purge_lazy_hook(extent_hooks_t *extent_hooks, void *addr,
size_t size, size_t offset, size_t length, unsigned arena_ind);
static bool extent_purge_forced_hook(extent_hooks_t *extent_hooks,
void *addr, size_t size, size_t offset, size_t length, unsigned arena_ind);
static extent_hooks_t hooks_not_null = {
extent_alloc_hook,
extent_dalloc_hook,
NULL, /* commit */
extent_decommit_hook,
extent_purge_lazy_hook,
extent_purge_forced_hook,
NULL, /* split */
NULL /* merge */
};
static extent_hooks_t hooks_null = { static extent_hooks_t hooks_null = {
extent_alloc_hook, extent_alloc_hook,
@ -34,80 +13,16 @@ static extent_hooks_t hooks_null = {
NULL /* merge */ NULL /* merge */
}; };
static bool did_alloc; static extent_hooks_t hooks_not_null = {
static bool did_dalloc; extent_alloc_hook,
static bool did_decommit; extent_dalloc_hook,
static bool did_purge_lazy; NULL, /* commit */
static bool did_purge_forced; extent_decommit_hook,
extent_purge_lazy_hook,
#if 0 extent_purge_forced_hook,
# define TRACE_HOOK(fmt, ...) malloc_printf(fmt, __VA_ARGS__) NULL, /* split */
#else NULL /* merge */
# define TRACE_HOOK(fmt, ...) };
#endif
static void *
extent_alloc_hook(extent_hooks_t *extent_hooks, void *new_addr, size_t size,
size_t alignment, bool *zero, bool *commit, unsigned arena_ind)
{
TRACE_HOOK("%s(extent_hooks=%p, new_addr=%p, size=%zu, alignment=%zu, "
"*zero=%s, *commit=%s, arena_ind=%u)\n", __func__, extent_hooks,
new_addr, size, alignment, *zero ? "true" : "false", *commit ?
"true" : "false", arena_ind);
did_alloc = true;
return (extent_hooks_default.alloc(
(extent_hooks_t *)&extent_hooks_default, new_addr, size, alignment,
zero, commit, 0));
}
static bool
extent_dalloc_hook(extent_hooks_t *extent_hooks, void *addr, size_t size,
bool committed, unsigned arena_ind)
{
TRACE_HOOK("%s(extent_hooks=%p, addr=%p, size=%zu, committed=%s, "
"arena_ind=%u)\n", __func__, extent_hooks, addr, size, committed ?
"true" : "false", arena_ind);
did_dalloc = true;
return (true); /* Cause cascade. */
}
static bool
extent_decommit_hook(extent_hooks_t *extent_hooks, void *addr, size_t size,
size_t offset, size_t length, unsigned arena_ind)
{
TRACE_HOOK("%s(extent_hooks=%p, addr=%p, size=%zu, offset=%zu, "
"length=%zu, arena_ind=%u)\n", __func__, extent_hooks, addr, size,
offset, length, arena_ind);
did_decommit = true;
return (true); /* Cause cascade. */
}
static bool
extent_purge_lazy_hook(extent_hooks_t *extent_hooks, void *addr, size_t size,
size_t offset, size_t length, unsigned arena_ind)
{
TRACE_HOOK("%s(extent_hooks=%p, addr=%p, size=%zu, offset=%zu, "
"length=%zu arena_ind=%u)\n", __func__, extent_hooks, addr, size,
offset, length, arena_ind);
did_purge_lazy = true;
return (true); /* Cause cascade. */
}
static bool
extent_purge_forced_hook(extent_hooks_t *extent_hooks, void *addr, size_t size,
size_t offset, size_t length, unsigned arena_ind)
{
TRACE_HOOK("%s(extent_hooks=%p, addr=%p, size=%zu, offset=%zu, "
"length=%zu arena_ind=%u)\n", __func__, extent_hooks, addr, size,
offset, length, arena_ind);
did_purge_forced = true;
return (true); /* Cause cascade. */
}
TEST_BEGIN(test_base_hooks_default) TEST_BEGIN(test_base_hooks_default)
{ {
@ -135,12 +50,21 @@ TEST_END
TEST_BEGIN(test_base_hooks_null) TEST_BEGIN(test_base_hooks_null)
{ {
extent_hooks_t hooks_orig;
tsdn_t *tsdn; tsdn_t *tsdn;
base_t *base; base_t *base;
size_t allocated0, allocated1, resident, mapped; size_t allocated0, allocated1, resident, mapped;
extent_hooks_prep();
try_dalloc = false;
try_decommit = false;
try_purge_lazy = false;
try_purge_forced = false;
memcpy(&hooks_orig, &hooks, sizeof(extent_hooks_t));
memcpy(&hooks, &hooks_null, sizeof(extent_hooks_t));
tsdn = tsdn_fetch(); tsdn = tsdn_fetch();
base = base_new(tsdn, 0, (extent_hooks_t *)&hooks_null); base = base_new(tsdn, 0, &hooks);
assert_ptr_not_null(base, "Unexpected base_new() failure"); assert_ptr_not_null(base, "Unexpected base_new() failure");
base_stats_get(tsdn, base, &allocated0, &resident, &mapped); base_stats_get(tsdn, base, &allocated0, &resident, &mapped);
@ -155,20 +79,31 @@ TEST_BEGIN(test_base_hooks_null)
"At least 42 bytes were allocated by base_alloc()"); "At least 42 bytes were allocated by base_alloc()");
base_delete(base); base_delete(base);
memcpy(&hooks, &hooks_orig, sizeof(extent_hooks_t));
} }
TEST_END TEST_END
TEST_BEGIN(test_base_hooks_not_null) TEST_BEGIN(test_base_hooks_not_null)
{ {
extent_hooks_t hooks_orig;
tsdn_t *tsdn; tsdn_t *tsdn;
base_t *base; base_t *base;
void *p, *q, *r, *r_exp; void *p, *q, *r, *r_exp;
extent_hooks_prep();
try_dalloc = false;
try_decommit = false;
try_purge_lazy = false;
try_purge_forced = false;
memcpy(&hooks_orig, &hooks, sizeof(extent_hooks_t));
memcpy(&hooks, &hooks_not_null, sizeof(extent_hooks_t));
tsdn = tsdn_fetch(); tsdn = tsdn_fetch();
did_alloc = false; did_alloc = false;
base = base_new(tsdn, 0, (extent_hooks_t *)&hooks_not_null); base = base_new(tsdn, 0, &hooks);
assert_ptr_not_null(base, "Unexpected base_new() failure"); assert_ptr_not_null(base, "Unexpected base_new() failure");
assert_true(did_alloc, "Expected alloc hook call"); assert_true(did_alloc, "Expected alloc");
/* /*
* Check for tight packing at specified alignment under simple * Check for tight packing at specified alignment under simple
@ -254,12 +189,19 @@ TEST_BEGIN(test_base_hooks_not_null)
} }
} }
did_dalloc = did_decommit = did_purge_lazy = did_purge_forced = false; called_dalloc = called_decommit = called_purge_lazy =
called_purge_forced = false;
base_delete(base); base_delete(base);
assert_true(did_dalloc, "Expected dalloc hook call"); assert_true(called_dalloc, "Expected dalloc call");
assert_true(did_decommit, "Expected decommit hook call"); assert_true(called_decommit, "Expected decommit call");
assert_true(did_purge_lazy, "Expected purge_lazy hook call"); assert_true(called_purge_lazy, "Expected purge_lazy call");
assert_true(did_purge_forced, "Expected purge_forced hook call"); assert_true(called_purge_forced, "Expected purge_forced call");
try_dalloc = true;
try_decommit = true;
try_purge_lazy = true;
try_purge_forced = true;
memcpy(&hooks, &hooks_orig, sizeof(extent_hooks_t));
} }
TEST_END TEST_END