Blame memkind-1.10.0/jemalloc/test/unit/hook.c

Packit 345191
#include "test/jemalloc_test.h"
Packit 345191
Packit 345191
#include "jemalloc/internal/hook.h"
Packit 345191
Packit 345191
static void *arg_extra;
Packit 345191
static int arg_type;
Packit 345191
static void *arg_result;
Packit 345191
static void *arg_address;
Packit 345191
static size_t arg_old_usize;
Packit 345191
static size_t arg_new_usize;
Packit 345191
static uintptr_t arg_result_raw;
Packit 345191
static uintptr_t arg_args_raw[4];
Packit 345191
Packit 345191
static int call_count = 0;
Packit 345191
Packit 345191
static void
Packit 345191
reset_args() {
Packit 345191
	arg_extra = NULL;
Packit 345191
	arg_type = 12345;
Packit 345191
	arg_result = NULL;
Packit 345191
	arg_address = NULL;
Packit 345191
	arg_old_usize = 0;
Packit 345191
	arg_new_usize = 0;
Packit 345191
	arg_result_raw = 0;
Packit 345191
	memset(arg_args_raw, 77, sizeof(arg_args_raw));
Packit 345191
}
Packit 345191
Packit 345191
static void
Packit 345191
alloc_free_size(size_t sz) {
Packit 345191
	void *ptr = mallocx(1, 0);
Packit 345191
	free(ptr);
Packit 345191
	ptr = mallocx(1, 0);
Packit 345191
	free(ptr);
Packit 345191
	ptr = mallocx(1, MALLOCX_TCACHE_NONE);
Packit 345191
	dallocx(ptr, MALLOCX_TCACHE_NONE);
Packit 345191
}
Packit 345191
Packit 345191
/*
Packit 345191
 * We want to support a degree of user reentrancy.  This tests a variety of
Packit 345191
 * allocation scenarios.
Packit 345191
 */
Packit 345191
static void
Packit 345191
be_reentrant() {
Packit 345191
	/* Let's make sure the tcache is non-empty if enabled. */
Packit 345191
	alloc_free_size(1);
Packit 345191
	alloc_free_size(1024);
Packit 345191
	alloc_free_size(64 * 1024);
Packit 345191
	alloc_free_size(256 * 1024);
Packit 345191
	alloc_free_size(1024 * 1024);
Packit 345191
Packit 345191
	/* Some reallocation. */
Packit 345191
	void *ptr = mallocx(129, 0);
Packit 345191
	ptr = rallocx(ptr, 130, 0);
Packit 345191
	free(ptr);
Packit 345191
Packit 345191
	ptr = mallocx(2 * 1024 * 1024, 0);
Packit 345191
	free(ptr);
Packit 345191
	ptr = mallocx(1 * 1024 * 1024, 0);
Packit 345191
	ptr = rallocx(ptr, 2 * 1024 * 1024, 0);
Packit 345191
	free(ptr);
Packit 345191
Packit 345191
	ptr = mallocx(1, 0);
Packit 345191
	ptr = rallocx(ptr, 1000, 0);
Packit 345191
	free(ptr);
Packit 345191
}
Packit 345191
Packit 345191
static void
Packit 345191
set_args_raw(uintptr_t *args_raw, int nargs) {
Packit 345191
	memcpy(arg_args_raw, args_raw, sizeof(uintptr_t) * nargs);
Packit 345191
}
Packit 345191
Packit 345191
static void
Packit 345191
assert_args_raw(uintptr_t *args_raw_expected, int nargs) {
Packit 345191
	int cmp = memcmp(args_raw_expected, arg_args_raw,
Packit 345191
	    sizeof(uintptr_t) * nargs);
Packit 345191
	assert_d_eq(cmp, 0, "Raw args mismatch");
Packit 345191
}
Packit 345191
Packit 345191
static void
Packit 345191
reset() {
Packit 345191
	call_count = 0;
Packit 345191
	reset_args();
Packit 345191
}
Packit 345191
Packit 345191
static void
Packit 345191
test_alloc_hook(void *extra, hook_alloc_t type, void *result,
Packit 345191
    uintptr_t result_raw, uintptr_t args_raw[3]) {
Packit 345191
	call_count++;
Packit 345191
	arg_extra = extra;
Packit 345191
	arg_type = (int)type;
Packit 345191
	arg_result = result;
Packit 345191
	arg_result_raw = result_raw;
Packit 345191
	set_args_raw(args_raw, 3);
Packit 345191
	be_reentrant();
Packit 345191
}
Packit 345191
Packit 345191
static void
Packit 345191
test_dalloc_hook(void *extra, hook_dalloc_t type, void *address,
Packit 345191
    uintptr_t args_raw[3]) {
Packit 345191
	call_count++;
Packit 345191
	arg_extra = extra;
Packit 345191
	arg_type = (int)type;
Packit 345191
	arg_address = address;
Packit 345191
	set_args_raw(args_raw, 3);
Packit 345191
	be_reentrant();
Packit 345191
}
Packit 345191
Packit 345191
static void
Packit 345191
test_expand_hook(void *extra, hook_expand_t type, void *address,
Packit 345191
    size_t old_usize, size_t new_usize, uintptr_t result_raw,
Packit 345191
    uintptr_t args_raw[4]) {
Packit 345191
	call_count++;
Packit 345191
	arg_extra = extra;
Packit 345191
	arg_type = (int)type;
Packit 345191
	arg_address = address;
Packit 345191
	arg_old_usize = old_usize;
Packit 345191
	arg_new_usize = new_usize;
Packit 345191
	arg_result_raw = result_raw;
Packit 345191
	set_args_raw(args_raw, 4);
Packit 345191
	be_reentrant();
Packit 345191
}
Packit 345191
Packit 345191
TEST_BEGIN(test_hooks_basic) {
Packit 345191
	/* Just verify that the record their arguments correctly. */
Packit 345191
	hooks_t hooks = {
Packit 345191
		&test_alloc_hook, &test_dalloc_hook, &test_expand_hook,
Packit 345191
		(void *)111};
Packit 345191
	void *handle = hook_install(TSDN_NULL, &hooks;;
Packit 345191
	uintptr_t args_raw[4] = {10, 20, 30, 40};
Packit 345191
Packit 345191
	/* Alloc */
Packit 345191
	reset_args();
Packit 345191
	hook_invoke_alloc(hook_alloc_posix_memalign, (void *)222, 333,
Packit 345191
	    args_raw);
Packit 345191
	assert_ptr_eq(arg_extra, (void *)111, "Passed wrong user pointer");
Packit 345191
	assert_d_eq((int)hook_alloc_posix_memalign, arg_type,
Packit 345191
	    "Passed wrong alloc type");
Packit 345191
	assert_ptr_eq((void *)222, arg_result, "Passed wrong result address");
Packit 345191
	assert_u64_eq(333, arg_result_raw, "Passed wrong result");
Packit 345191
	assert_args_raw(args_raw, 3);
Packit 345191
Packit 345191
	/* Dalloc */
Packit 345191
	reset_args();
Packit 345191
	hook_invoke_dalloc(hook_dalloc_sdallocx, (void *)222, args_raw);
Packit 345191
	assert_d_eq((int)hook_dalloc_sdallocx, arg_type,
Packit 345191
	    "Passed wrong dalloc type");
Packit 345191
	assert_ptr_eq((void *)111, arg_extra, "Passed wrong user pointer");
Packit 345191
	assert_ptr_eq((void *)222, arg_address, "Passed wrong address");
Packit 345191
	assert_args_raw(args_raw, 3);
Packit 345191
Packit 345191
	/* Expand */
Packit 345191
	reset_args();
Packit 345191
	hook_invoke_expand(hook_expand_xallocx, (void *)222, 333, 444, 555,
Packit 345191
	    args_raw);
Packit 345191
	assert_d_eq((int)hook_expand_xallocx, arg_type,
Packit 345191
	    "Passed wrong expand type");
Packit 345191
	assert_ptr_eq((void *)111, arg_extra, "Passed wrong user pointer");
Packit 345191
	assert_ptr_eq((void *)222, arg_address, "Passed wrong address");
Packit 345191
	assert_zu_eq(333, arg_old_usize, "Passed wrong old usize");
Packit 345191
	assert_zu_eq(444, arg_new_usize, "Passed wrong new usize");
Packit 345191
	assert_zu_eq(555, arg_result_raw, "Passed wrong result");
Packit 345191
	assert_args_raw(args_raw, 4);
Packit 345191
Packit 345191
	hook_remove(TSDN_NULL, handle);
Packit 345191
}
Packit 345191
TEST_END
Packit 345191
Packit 345191
TEST_BEGIN(test_hooks_null) {
Packit 345191
	/* Null hooks should be ignored, not crash. */
Packit 345191
	hooks_t hooks1 = {NULL, NULL, NULL, NULL};
Packit 345191
	hooks_t hooks2 = {&test_alloc_hook, NULL, NULL, NULL};
Packit 345191
	hooks_t hooks3 = {NULL, &test_dalloc_hook, NULL, NULL};
Packit 345191
	hooks_t hooks4 = {NULL, NULL, &test_expand_hook, NULL};
Packit 345191
Packit 345191
	void *handle1 = hook_install(TSDN_NULL, &hooks1);
Packit 345191
	void *handle2 = hook_install(TSDN_NULL, &hooks2);
Packit 345191
	void *handle3 = hook_install(TSDN_NULL, &hooks3);
Packit 345191
	void *handle4 = hook_install(TSDN_NULL, &hooks4);
Packit 345191
Packit 345191
	assert_ptr_ne(handle1, NULL, "Hook installation failed");
Packit 345191
	assert_ptr_ne(handle2, NULL, "Hook installation failed");
Packit 345191
	assert_ptr_ne(handle3, NULL, "Hook installation failed");
Packit 345191
	assert_ptr_ne(handle4, NULL, "Hook installation failed");
Packit 345191
Packit 345191
	uintptr_t args_raw[4] = {10, 20, 30, 40};
Packit 345191
Packit 345191
	call_count = 0;
Packit 345191
	hook_invoke_alloc(hook_alloc_malloc, NULL, 0, args_raw);
Packit 345191
	assert_d_eq(call_count, 1, "Called wrong number of times");
Packit 345191
Packit 345191
	call_count = 0;
Packit 345191
	hook_invoke_dalloc(hook_dalloc_free, NULL, args_raw);
Packit 345191
	assert_d_eq(call_count, 1, "Called wrong number of times");
Packit 345191
Packit 345191
	call_count = 0;
Packit 345191
	hook_invoke_expand(hook_expand_realloc, NULL, 0, 0, 0, args_raw);
Packit 345191
	assert_d_eq(call_count, 1, "Called wrong number of times");
Packit 345191
Packit 345191
	hook_remove(TSDN_NULL, handle1);
Packit 345191
	hook_remove(TSDN_NULL, handle2);
Packit 345191
	hook_remove(TSDN_NULL, handle3);
Packit 345191
	hook_remove(TSDN_NULL, handle4);
Packit 345191
}
Packit 345191
TEST_END
Packit 345191
Packit 345191
TEST_BEGIN(test_hooks_remove) {
Packit 345191
	hooks_t hooks = {&test_alloc_hook, NULL, NULL, NULL};
Packit 345191
	void *handle = hook_install(TSDN_NULL, &hooks;;
Packit 345191
	assert_ptr_ne(handle, NULL, "Hook installation failed");
Packit 345191
	call_count = 0;
Packit 345191
	uintptr_t args_raw[4] = {10, 20, 30, 40};
Packit 345191
	hook_invoke_alloc(hook_alloc_malloc, NULL, 0, args_raw);
Packit 345191
	assert_d_eq(call_count, 1, "Hook not invoked");
Packit 345191
Packit 345191
	call_count = 0;
Packit 345191
	hook_remove(TSDN_NULL, handle);
Packit 345191
	hook_invoke_alloc(hook_alloc_malloc, NULL, 0, NULL);
Packit 345191
	assert_d_eq(call_count, 0, "Hook invoked after removal");
Packit 345191
Packit 345191
}
Packit 345191
TEST_END
Packit 345191
Packit 345191
TEST_BEGIN(test_hooks_alloc_simple) {
Packit 345191
	/* "Simple" in the sense that we're not in a realloc variant. */
Packit 345191
	hooks_t hooks = {&test_alloc_hook, NULL, NULL, (void *)123};
Packit 345191
	void *handle = hook_install(TSDN_NULL, &hooks;;
Packit 345191
	assert_ptr_ne(handle, NULL, "Hook installation failed");
Packit 345191
Packit 345191
	/* Stop malloc from being optimized away. */
Packit 345191
	volatile int err;
Packit 345191
	void *volatile ptr;
Packit 345191
Packit 345191
	/* malloc */
Packit 345191
	reset();
Packit 345191
	ptr = malloc(1);
Packit 345191
	assert_d_eq(call_count, 1, "Hook not called");
Packit 345191
	assert_ptr_eq(arg_extra, (void *)123, "Wrong extra");
Packit 345191
	assert_d_eq(arg_type, (int)hook_alloc_malloc, "Wrong hook type");
Packit 345191
	assert_ptr_eq(ptr, arg_result, "Wrong result");
Packit 345191
	assert_u64_eq((uintptr_t)ptr, (uintptr_t)arg_result_raw,
Packit 345191
	    "Wrong raw result");
Packit 345191
	assert_u64_eq((uintptr_t)1, arg_args_raw[0], "Wrong argument");
Packit 345191
	free(ptr);
Packit 345191
Packit 345191
	/* posix_memalign */
Packit 345191
	reset();
Packit 345191
	err = posix_memalign((void **)&ptr, 1024, 1);
Packit 345191
	assert_d_eq(call_count, 1, "Hook not called");
Packit 345191
	assert_ptr_eq(arg_extra, (void *)123, "Wrong extra");
Packit 345191
	assert_d_eq(arg_type, (int)hook_alloc_posix_memalign,
Packit 345191
	    "Wrong hook type");
Packit 345191
	assert_ptr_eq(ptr, arg_result, "Wrong result");
Packit 345191
	assert_u64_eq((uintptr_t)err, (uintptr_t)arg_result_raw,
Packit 345191
	    "Wrong raw result");
Packit 345191
	assert_u64_eq((uintptr_t)&ptr, arg_args_raw[0], "Wrong argument");
Packit 345191
	assert_u64_eq((uintptr_t)1024, arg_args_raw[1], "Wrong argument");
Packit 345191
	assert_u64_eq((uintptr_t)1, arg_args_raw[2], "Wrong argument");
Packit 345191
	free(ptr);
Packit 345191
Packit 345191
	/* aligned_alloc */
Packit 345191
	reset();
Packit 345191
	ptr = aligned_alloc(1024, 1);
Packit 345191
	assert_d_eq(call_count, 1, "Hook not called");
Packit 345191
	assert_ptr_eq(arg_extra, (void *)123, "Wrong extra");
Packit 345191
	assert_d_eq(arg_type, (int)hook_alloc_aligned_alloc,
Packit 345191
	    "Wrong hook type");
Packit 345191
	assert_ptr_eq(ptr, arg_result, "Wrong result");
Packit 345191
	assert_u64_eq((uintptr_t)ptr, (uintptr_t)arg_result_raw,
Packit 345191
	    "Wrong raw result");
Packit 345191
	assert_u64_eq((uintptr_t)1024, arg_args_raw[0], "Wrong argument");
Packit 345191
	assert_u64_eq((uintptr_t)1, arg_args_raw[1], "Wrong argument");
Packit 345191
	free(ptr);
Packit 345191
Packit 345191
	/* calloc */
Packit 345191
	reset();
Packit 345191
	ptr = calloc(11, 13);
Packit 345191
	assert_d_eq(call_count, 1, "Hook not called");
Packit 345191
	assert_ptr_eq(arg_extra, (void *)123, "Wrong extra");
Packit 345191
	assert_d_eq(arg_type, (int)hook_alloc_calloc, "Wrong hook type");
Packit 345191
	assert_ptr_eq(ptr, arg_result, "Wrong result");
Packit 345191
	assert_u64_eq((uintptr_t)ptr, (uintptr_t)arg_result_raw,
Packit 345191
	    "Wrong raw result");
Packit 345191
	assert_u64_eq((uintptr_t)11, arg_args_raw[0], "Wrong argument");
Packit 345191
	assert_u64_eq((uintptr_t)13, arg_args_raw[1], "Wrong argument");
Packit 345191
	free(ptr);
Packit 345191
Packit 345191
	/* memalign */
Packit 345191
#ifdef JEMALLOC_OVERRIDE_MEMALIGN
Packit 345191
	reset();
Packit 345191
	ptr = memalign(1024, 1);
Packit 345191
	assert_d_eq(call_count, 1, "Hook not called");
Packit 345191
	assert_ptr_eq(arg_extra, (void *)123, "Wrong extra");
Packit 345191
	assert_d_eq(arg_type, (int)hook_alloc_memalign, "Wrong hook type");
Packit 345191
	assert_ptr_eq(ptr, arg_result, "Wrong result");
Packit 345191
	assert_u64_eq((uintptr_t)ptr, (uintptr_t)arg_result_raw,
Packit 345191
	    "Wrong raw result");
Packit 345191
	assert_u64_eq((uintptr_t)1024, arg_args_raw[0], "Wrong argument");
Packit 345191
	assert_u64_eq((uintptr_t)1, arg_args_raw[1], "Wrong argument");
Packit 345191
	free(ptr);
Packit 345191
#endif /* JEMALLOC_OVERRIDE_MEMALIGN */
Packit 345191
Packit 345191
	/* valloc */
Packit 345191
#ifdef JEMALLOC_OVERRIDE_VALLOC
Packit 345191
	reset();
Packit 345191
	ptr = valloc(1);
Packit 345191
	assert_d_eq(call_count, 1, "Hook not called");
Packit 345191
	assert_ptr_eq(arg_extra, (void *)123, "Wrong extra");
Packit 345191
	assert_d_eq(arg_type, (int)hook_alloc_valloc, "Wrong hook type");
Packit 345191
	assert_ptr_eq(ptr, arg_result, "Wrong result");
Packit 345191
	assert_u64_eq((uintptr_t)ptr, (uintptr_t)arg_result_raw,
Packit 345191
	    "Wrong raw result");
Packit 345191
	assert_u64_eq((uintptr_t)1, arg_args_raw[0], "Wrong argument");
Packit 345191
	free(ptr);
Packit 345191
#endif /* JEMALLOC_OVERRIDE_VALLOC */
Packit 345191
Packit 345191
	/* mallocx */
Packit 345191
	reset();
Packit 345191
	ptr = mallocx(1, MALLOCX_LG_ALIGN(10));
Packit 345191
	assert_d_eq(call_count, 1, "Hook not called");
Packit 345191
	assert_ptr_eq(arg_extra, (void *)123, "Wrong extra");
Packit 345191
	assert_d_eq(arg_type, (int)hook_alloc_mallocx, "Wrong hook type");
Packit 345191
	assert_ptr_eq(ptr, arg_result, "Wrong result");
Packit 345191
	assert_u64_eq((uintptr_t)ptr, (uintptr_t)arg_result_raw,
Packit 345191
	    "Wrong raw result");
Packit 345191
	assert_u64_eq((uintptr_t)1, arg_args_raw[0], "Wrong argument");
Packit 345191
	assert_u64_eq((uintptr_t)MALLOCX_LG_ALIGN(10), arg_args_raw[1],
Packit 345191
	    "Wrong flags");
Packit 345191
	free(ptr);
Packit 345191
Packit 345191
	hook_remove(TSDN_NULL, handle);
Packit 345191
}
Packit 345191
TEST_END
Packit 345191
Packit 345191
TEST_BEGIN(test_hooks_dalloc_simple) {
Packit 345191
	/* "Simple" in the sense that we're not in a realloc variant. */
Packit 345191
	hooks_t hooks = {NULL, &test_dalloc_hook, NULL, (void *)123};
Packit 345191
	void *handle = hook_install(TSDN_NULL, &hooks;;
Packit 345191
	assert_ptr_ne(handle, NULL, "Hook installation failed");
Packit 345191
Packit 345191
	void *volatile ptr;
Packit 345191
Packit 345191
	/* free() */
Packit 345191
	reset();
Packit 345191
	ptr = malloc(1);
Packit 345191
	free(ptr);
Packit 345191
	assert_d_eq(call_count, 1, "Hook not called");
Packit 345191
	assert_ptr_eq(arg_extra, (void *)123, "Wrong extra");
Packit 345191
	assert_d_eq(arg_type, (int)hook_dalloc_free, "Wrong hook type");
Packit 345191
	assert_ptr_eq(ptr, arg_address, "Wrong pointer freed");
Packit 345191
	assert_u64_eq((uintptr_t)ptr, arg_args_raw[0], "Wrong raw arg");
Packit 345191
Packit 345191
	/* dallocx() */
Packit 345191
	reset();
Packit 345191
	ptr = malloc(1);
Packit 345191
	dallocx(ptr, MALLOCX_TCACHE_NONE);
Packit 345191
	assert_d_eq(call_count, 1, "Hook not called");
Packit 345191
	assert_ptr_eq(arg_extra, (void *)123, "Wrong extra");
Packit 345191
	assert_d_eq(arg_type, (int)hook_dalloc_dallocx, "Wrong hook type");
Packit 345191
	assert_ptr_eq(ptr, arg_address, "Wrong pointer freed");
Packit 345191
	assert_u64_eq((uintptr_t)ptr, arg_args_raw[0], "Wrong raw arg");
Packit 345191
	assert_u64_eq((uintptr_t)MALLOCX_TCACHE_NONE, arg_args_raw[1],
Packit 345191
	    "Wrong raw arg");
Packit 345191
Packit 345191
	/* sdallocx() */
Packit 345191
	reset();
Packit 345191
	ptr = malloc(1);
Packit 345191
	sdallocx(ptr, 1, MALLOCX_TCACHE_NONE);
Packit 345191
	assert_d_eq(call_count, 1, "Hook not called");
Packit 345191
	assert_ptr_eq(arg_extra, (void *)123, "Wrong extra");
Packit 345191
	assert_d_eq(arg_type, (int)hook_dalloc_sdallocx, "Wrong hook type");
Packit 345191
	assert_ptr_eq(ptr, arg_address, "Wrong pointer freed");
Packit 345191
	assert_u64_eq((uintptr_t)ptr, arg_args_raw[0], "Wrong raw arg");
Packit 345191
	assert_u64_eq((uintptr_t)1, arg_args_raw[1], "Wrong raw arg");
Packit 345191
	assert_u64_eq((uintptr_t)MALLOCX_TCACHE_NONE, arg_args_raw[2],
Packit 345191
	    "Wrong raw arg");
Packit 345191
Packit 345191
	hook_remove(TSDN_NULL, handle);
Packit 345191
}
Packit 345191
TEST_END
Packit 345191
Packit 345191
TEST_BEGIN(test_hooks_expand_simple) {
Packit 345191
	/* "Simple" in the sense that we're not in a realloc variant. */
Packit 345191
	hooks_t hooks = {NULL, NULL, &test_expand_hook, (void *)123};
Packit 345191
	void *handle = hook_install(TSDN_NULL, &hooks;;
Packit 345191
	assert_ptr_ne(handle, NULL, "Hook installation failed");
Packit 345191
Packit 345191
	void *volatile ptr;
Packit 345191
Packit 345191
	/* xallocx() */
Packit 345191
	reset();
Packit 345191
	ptr = malloc(1);
Packit 345191
	size_t new_usize = xallocx(ptr, 100, 200, MALLOCX_TCACHE_NONE);
Packit 345191
	assert_d_eq(call_count, 1, "Hook not called");
Packit 345191
	assert_ptr_eq(arg_extra, (void *)123, "Wrong extra");
Packit 345191
	assert_d_eq(arg_type, (int)hook_expand_xallocx, "Wrong hook type");
Packit 345191
	assert_ptr_eq(ptr, arg_address, "Wrong pointer expanded");
Packit 345191
	assert_u64_eq(arg_old_usize, nallocx(1, 0), "Wrong old usize");
Packit 345191
	assert_u64_eq(arg_new_usize, sallocx(ptr, 0), "Wrong new usize");
Packit 345191
	assert_u64_eq(new_usize, arg_result_raw, "Wrong result");
Packit 345191
	assert_u64_eq((uintptr_t)ptr, arg_args_raw[0], "Wrong arg");
Packit 345191
	assert_u64_eq(100, arg_args_raw[1], "Wrong arg");
Packit 345191
	assert_u64_eq(200, arg_args_raw[2], "Wrong arg");
Packit 345191
	assert_u64_eq(MALLOCX_TCACHE_NONE, arg_args_raw[3], "Wrong arg");
Packit 345191
Packit 345191
	hook_remove(TSDN_NULL, handle);
Packit 345191
}
Packit 345191
TEST_END
Packit 345191
Packit 345191
TEST_BEGIN(test_hooks_realloc_as_malloc_or_free) {
Packit 345191
	hooks_t hooks = {&test_alloc_hook, &test_dalloc_hook,
Packit 345191
		&test_expand_hook, (void *)123};
Packit 345191
	void *handle = hook_install(TSDN_NULL, &hooks;;
Packit 345191
	assert_ptr_ne(handle, NULL, "Hook installation failed");
Packit 345191
Packit 345191
	void *volatile ptr;
Packit 345191
Packit 345191
	/* realloc(NULL, size) as malloc */
Packit 345191
	reset();
Packit 345191
	ptr = realloc(NULL, 1);
Packit 345191
	assert_d_eq(call_count, 1, "Hook not called");
Packit 345191
	assert_ptr_eq(arg_extra, (void *)123, "Wrong extra");
Packit 345191
	assert_d_eq(arg_type, (int)hook_alloc_realloc, "Wrong hook type");
Packit 345191
	assert_ptr_eq(ptr, arg_result, "Wrong result");
Packit 345191
	assert_u64_eq((uintptr_t)ptr, (uintptr_t)arg_result_raw,
Packit 345191
	    "Wrong raw result");
Packit 345191
	assert_u64_eq((uintptr_t)NULL, arg_args_raw[0], "Wrong argument");
Packit 345191
	assert_u64_eq((uintptr_t)1, arg_args_raw[1], "Wrong argument");
Packit 345191
	free(ptr);
Packit 345191
Packit 345191
	/* realloc(ptr, 0) as free */
Packit 345191
	ptr = malloc(1);
Packit 345191
	reset();
Packit 345191
	realloc(ptr, 0);
Packit 345191
	assert_d_eq(call_count, 1, "Hook not called");
Packit 345191
	assert_ptr_eq(arg_extra, (void *)123, "Wrong extra");
Packit 345191
	assert_d_eq(arg_type, (int)hook_dalloc_realloc, "Wrong hook type");
Packit 345191
	assert_ptr_eq(ptr, arg_address, "Wrong pointer freed");
Packit 345191
	assert_u64_eq((uintptr_t)ptr, arg_args_raw[0], "Wrong raw arg");
Packit 345191
	assert_u64_eq((uintptr_t)0, arg_args_raw[1], "Wrong raw arg");
Packit 345191
Packit 345191
	/* realloc(NULL, 0) as malloc(0) */
Packit 345191
	reset();
Packit 345191
	ptr = realloc(NULL, 0);
Packit 345191
	assert_d_eq(call_count, 1, "Hook not called");
Packit 345191
	assert_ptr_eq(arg_extra, (void *)123, "Wrong extra");
Packit 345191
	assert_d_eq(arg_type, (int)hook_alloc_realloc, "Wrong hook type");
Packit 345191
	assert_ptr_eq(ptr, arg_result, "Wrong result");
Packit 345191
	assert_u64_eq((uintptr_t)ptr, (uintptr_t)arg_result_raw,
Packit 345191
	    "Wrong raw result");
Packit 345191
	assert_u64_eq((uintptr_t)NULL, arg_args_raw[0], "Wrong argument");
Packit 345191
	assert_u64_eq((uintptr_t)0, arg_args_raw[1], "Wrong argument");
Packit 345191
	free(ptr);
Packit 345191
Packit 345191
	hook_remove(TSDN_NULL, handle);
Packit 345191
}
Packit 345191
TEST_END
Packit 345191
Packit 345191
static void
Packit 345191
do_realloc_test(void *(*ralloc)(void *, size_t, int), int flags,
Packit 345191
    int expand_type, int dalloc_type) {
Packit 345191
	hooks_t hooks = {&test_alloc_hook, &test_dalloc_hook,
Packit 345191
		&test_expand_hook, (void *)123};
Packit 345191
	void *handle = hook_install(TSDN_NULL, &hooks;;
Packit 345191
	assert_ptr_ne(handle, NULL, "Hook installation failed");
Packit 345191
Packit 345191
	void *volatile ptr;
Packit 345191
	void *volatile ptr2;
Packit 345191
Packit 345191
	/* Realloc in-place, small. */
Packit 345191
	ptr = malloc(129);
Packit 345191
	reset();
Packit 345191
	ptr2 = ralloc(ptr, 130, flags);
Packit 345191
	assert_ptr_eq(ptr, ptr2, "Small realloc moved");
Packit 345191
Packit 345191
	assert_d_eq(call_count, 1, "Hook not called");
Packit 345191
	assert_ptr_eq(arg_extra, (void *)123, "Wrong extra");
Packit 345191
	assert_d_eq(arg_type, expand_type, "Wrong hook type");
Packit 345191
	assert_ptr_eq(ptr, arg_address, "Wrong address");
Packit 345191
	assert_u64_eq((uintptr_t)ptr, (uintptr_t)arg_result_raw,
Packit 345191
	    "Wrong raw result");
Packit 345191
	assert_u64_eq((uintptr_t)ptr, arg_args_raw[0], "Wrong argument");
Packit 345191
	assert_u64_eq((uintptr_t)130, arg_args_raw[1], "Wrong argument");
Packit 345191
	free(ptr);
Packit 345191
Packit 345191
	/*
Packit 345191
	 * Realloc in-place, large.  Since we can't guarantee the large case
Packit 345191
	 * across all platforms, we stay resilient to moving results.
Packit 345191
	 */
Packit 345191
	ptr = malloc(2 * 1024 * 1024);
Packit 345191
	free(ptr);
Packit 345191
	ptr2 = malloc(1 * 1024 * 1024);
Packit 345191
	reset();
Packit 345191
	ptr = ralloc(ptr2, 2 * 1024 * 1024, flags);
Packit 345191
	/* ptr is the new address, ptr2 is the old address. */
Packit 345191
	if (ptr == ptr2) {
Packit 345191
		assert_d_eq(call_count, 1, "Hook not called");
Packit 345191
		assert_d_eq(arg_type, expand_type, "Wrong hook type");
Packit 345191
	} else {
Packit 345191
		assert_d_eq(call_count, 2, "Wrong hooks called");
Packit 345191
		assert_ptr_eq(ptr, arg_result, "Wrong address");
Packit 345191
		assert_d_eq(arg_type, dalloc_type, "Wrong hook type");
Packit 345191
	}
Packit 345191
	assert_ptr_eq(arg_extra, (void *)123, "Wrong extra");
Packit 345191
	assert_ptr_eq(ptr2, arg_address, "Wrong address");
Packit 345191
	assert_u64_eq((uintptr_t)ptr, (uintptr_t)arg_result_raw,
Packit 345191
	    "Wrong raw result");
Packit 345191
	assert_u64_eq((uintptr_t)ptr2, arg_args_raw[0], "Wrong argument");
Packit 345191
	assert_u64_eq((uintptr_t)2 * 1024 * 1024, arg_args_raw[1],
Packit 345191
	    "Wrong argument");
Packit 345191
	free(ptr);
Packit 345191
Packit 345191
	/* Realloc with move, small. */
Packit 345191
	ptr = malloc(8);
Packit 345191
	reset();
Packit 345191
	ptr2 = ralloc(ptr, 128, flags);
Packit 345191
	assert_ptr_ne(ptr, ptr2, "Small realloc didn't move");
Packit 345191
Packit 345191
	assert_d_eq(call_count, 2, "Hook not called");
Packit 345191
	assert_ptr_eq(arg_extra, (void *)123, "Wrong extra");
Packit 345191
	assert_d_eq(arg_type, dalloc_type, "Wrong hook type");
Packit 345191
	assert_ptr_eq(ptr, arg_address, "Wrong address");
Packit 345191
	assert_ptr_eq(ptr2, arg_result, "Wrong address");
Packit 345191
	assert_u64_eq((uintptr_t)ptr2, (uintptr_t)arg_result_raw,
Packit 345191
	    "Wrong raw result");
Packit 345191
	assert_u64_eq((uintptr_t)ptr, arg_args_raw[0], "Wrong argument");
Packit 345191
	assert_u64_eq((uintptr_t)128, arg_args_raw[1], "Wrong argument");
Packit 345191
	free(ptr2);
Packit 345191
Packit 345191
	/* Realloc with move, large. */
Packit 345191
	ptr = malloc(1);
Packit 345191
	reset();
Packit 345191
	ptr2 = ralloc(ptr, 2 * 1024 * 1024, flags);
Packit 345191
	assert_ptr_ne(ptr, ptr2, "Large realloc didn't move");
Packit 345191
Packit 345191
	assert_d_eq(call_count, 2, "Hook not called");
Packit 345191
	assert_ptr_eq(arg_extra, (void *)123, "Wrong extra");
Packit 345191
	assert_d_eq(arg_type, dalloc_type, "Wrong hook type");
Packit 345191
	assert_ptr_eq(ptr, arg_address, "Wrong address");
Packit 345191
	assert_ptr_eq(ptr2, arg_result, "Wrong address");
Packit 345191
	assert_u64_eq((uintptr_t)ptr2, (uintptr_t)arg_result_raw,
Packit 345191
	    "Wrong raw result");
Packit 345191
	assert_u64_eq((uintptr_t)ptr, arg_args_raw[0], "Wrong argument");
Packit 345191
	assert_u64_eq((uintptr_t)2 * 1024 * 1024, arg_args_raw[1],
Packit 345191
	    "Wrong argument");
Packit 345191
	free(ptr2);
Packit 345191
Packit 345191
	hook_remove(TSDN_NULL, handle);
Packit 345191
}
Packit 345191
Packit 345191
static void *
Packit 345191
realloc_wrapper(void *ptr, size_t size, UNUSED int flags) {
Packit 345191
	return realloc(ptr, size);
Packit 345191
}
Packit 345191
Packit 345191
TEST_BEGIN(test_hooks_realloc) {
Packit 345191
	do_realloc_test(&realloc_wrapper, 0, hook_expand_realloc,
Packit 345191
	    hook_dalloc_realloc);
Packit 345191
}
Packit 345191
TEST_END
Packit 345191
Packit 345191
TEST_BEGIN(test_hooks_rallocx) {
Packit 345191
	do_realloc_test(&rallocx, MALLOCX_TCACHE_NONE, hook_expand_rallocx,
Packit 345191
	    hook_dalloc_rallocx);
Packit 345191
}
Packit 345191
TEST_END
Packit 345191
Packit 345191
int
Packit 345191
main(void) {
Packit 345191
	/* We assert on call counts. */
Packit 345191
	return test_no_reentrancy(
Packit 345191
	    test_hooks_basic,
Packit 345191
	    test_hooks_null,
Packit 345191
	    test_hooks_remove,
Packit 345191
	    test_hooks_alloc_simple,
Packit 345191
	    test_hooks_dalloc_simple,
Packit 345191
	    test_hooks_expand_simple,
Packit 345191
	    test_hooks_realloc_as_malloc_or_free,
Packit 345191
	    test_hooks_realloc,
Packit 345191
	    test_hooks_rallocx);
Packit 345191
}