summaryrefslogtreecommitdiff
path: root/test
diff options
context:
space:
mode:
authorDavid Goldblatt <davidgoldblatt@fb.com>2018-05-15 14:15:43 -0700
committerDavid Goldblatt <davidtgoldblatt@gmail.com>2018-05-18 11:43:03 -0700
commita7f749c9af0d5ca51b5b5eaf35c2c2913d8a77e1 (patch)
treeed1f92900318575e0649e38e6104c29d4fc77df0 /test
parent0379235f47585ac8f583ba85aab9d294abfa44b5 (diff)
Hooks: Protect against reentrancy.
Previously, we made the user deal with this themselves, but that's not good enough; if hooks may allocate, we should test the allocation pathways down hooks. If we're doing that, we might as well actually implement the protection for the user.
Diffstat (limited to 'test')
-rw-r--r--test/unit/hook.c42
1 files changed, 42 insertions, 0 deletions
diff --git a/test/unit/hook.c b/test/unit/hook.c
index 3f85ff10..72fcc433 100644
--- a/test/unit/hook.c
+++ b/test/unit/hook.c
@@ -26,6 +26,45 @@ reset_args() {
}
static void
+alloc_free_size(size_t sz) {
+ void *ptr = mallocx(1, 0);
+ free(ptr);
+ ptr = mallocx(1, 0);
+ free(ptr);
+ ptr = mallocx(1, MALLOCX_TCACHE_NONE);
+ dallocx(ptr, MALLOCX_TCACHE_NONE);
+}
+
+/*
+ * We want to support a degree of user reentrancy. This tests a variety of
+ * allocation scenarios.
+ */
+static void
+be_reentrant() {
+ /* Let's make sure the tcache is non-empty if enabled. */
+ alloc_free_size(1);
+ alloc_free_size(1024);
+ alloc_free_size(64 * 1024);
+ alloc_free_size(256 * 1024);
+ alloc_free_size(1024 * 1024);
+
+ /* Some reallocation. */
+ void *ptr = mallocx(129, 0);
+ ptr = rallocx(ptr, 130, 0);
+ free(ptr);
+
+ ptr = mallocx(2 * 1024 * 1024, 0);
+ free(ptr);
+ ptr = mallocx(1 * 1024 * 1024, 0);
+ ptr = rallocx(ptr, 2 * 1024 * 1024, 0);
+ free(ptr);
+
+ ptr = mallocx(1, 0);
+ ptr = rallocx(ptr, 1000, 0);
+ free(ptr);
+}
+
+static void
set_args_raw(uintptr_t *args_raw, int nargs) {
memcpy(arg_args_raw, args_raw, sizeof(uintptr_t) * nargs);
}
@@ -52,6 +91,7 @@ test_alloc_hook(void *extra, hook_alloc_t type, void *result,
arg_result = result;
arg_result_raw = result_raw;
set_args_raw(args_raw, 3);
+ be_reentrant();
}
static void
@@ -62,6 +102,7 @@ test_dalloc_hook(void *extra, hook_dalloc_t type, void *address,
arg_type = (int)type;
arg_address = address;
set_args_raw(args_raw, 3);
+ be_reentrant();
}
static void
@@ -76,6 +117,7 @@ test_expand_hook(void *extra, hook_expand_t type, void *address,
arg_new_usize = new_usize;
arg_result_raw = result_raw;
set_args_raw(args_raw, 4);
+ be_reentrant();
}
TEST_BEGIN(test_hooks_basic) {