Hooks: hook the pure-deallocation functions.

This commit is contained in:
David Goldblatt 2018-04-19 15:02:53 -07:00 committed by David Goldblatt
parent 226327cf66
commit c154f5881b
2 changed files with 55 additions and 1 deletions

View File

@ -2441,6 +2441,8 @@ je_free(void *ptr) {
} else { } else {
tcache = NULL; tcache = NULL;
} }
uintptr_t args_raw[3] = {(uintptr_t)ptr};
hook_invoke_dalloc(hook_dalloc_free, ptr, args_raw);
ifree(tsd, ptr, tcache, true); ifree(tsd, ptr, tcache, true);
} }
check_entry_exit_locking(tsd_tsdn(tsd)); check_entry_exit_locking(tsd_tsdn(tsd));
@ -3012,6 +3014,8 @@ je_dallocx(void *ptr, int flags) {
tsd_assert_fast(tsd); tsd_assert_fast(tsd);
ifree(tsd, ptr, tcache, false); ifree(tsd, ptr, tcache, false);
} else { } else {
uintptr_t args_raw[3] = {(uintptr_t)ptr, flags};
hook_invoke_dalloc(hook_dalloc_dallocx, ptr, args_raw);
ifree(tsd, ptr, tcache, true); ifree(tsd, ptr, tcache, true);
} }
check_entry_exit_locking(tsd_tsdn(tsd)); check_entry_exit_locking(tsd_tsdn(tsd));
@ -3074,6 +3078,8 @@ je_sdallocx(void *ptr, size_t size, int flags) {
tsd_assert_fast(tsd); tsd_assert_fast(tsd);
isfree(tsd, ptr, usize, tcache, false); isfree(tsd, ptr, usize, tcache, false);
} else { } else {
uintptr_t args_raw[3] = {(uintptr_t)ptr, size, flags};
hook_invoke_dalloc(hook_dalloc_sdallocx, ptr, args_raw);
isfree(tsd, ptr, usize, tcache, true); isfree(tsd, ptr, usize, tcache, true);
} }
check_entry_exit_locking(tsd_tsdn(tsd)); check_entry_exit_locking(tsd_tsdn(tsd));

View File

@ -289,6 +289,53 @@ TEST_BEGIN(test_hooks_alloc_simple) {
} }
TEST_END TEST_END
TEST_BEGIN(test_hooks_dalloc_simple) {
/* "Simple" in the sense that we're not in a realloc variant. */
hooks_t hooks = {NULL, &test_dalloc_hook, NULL};
void *handle = hook_install(TSDN_NULL, &hooks, (void *)123);
assert_ptr_ne(handle, NULL, "Hook installation failed");
void *volatile ptr;
/* free() */
reset();
ptr = malloc(1);
free(ptr);
assert_d_eq(call_count, 1, "Hook not called");
assert_ptr_eq(arg_extra, (void *)123, "Wrong extra");
assert_d_eq(arg_type, (int)hook_dalloc_free, "Wrong hook type");
assert_ptr_eq(ptr, arg_address, "Wrong pointer freed");
assert_u64_eq((uintptr_t)ptr, arg_args_raw[0], "Wrong raw arg");
/* dallocx() */
reset();
ptr = malloc(1);
dallocx(ptr, MALLOCX_TCACHE_NONE);
assert_d_eq(call_count, 1, "Hook not called");
assert_ptr_eq(arg_extra, (void *)123, "Wrong extra");
assert_d_eq(arg_type, (int)hook_dalloc_dallocx, "Wrong hook type");
assert_ptr_eq(ptr, arg_address, "Wrong pointer freed");
assert_u64_eq((uintptr_t)ptr, arg_args_raw[0], "Wrong raw arg");
assert_u64_eq((uintptr_t)MALLOCX_TCACHE_NONE, arg_args_raw[1],
"Wrong raw arg");
/* sdallocx() */
reset();
ptr = malloc(1);
sdallocx(ptr, 1, MALLOCX_TCACHE_NONE);
assert_d_eq(call_count, 1, "Hook not called");
assert_ptr_eq(arg_extra, (void *)123, "Wrong extra");
assert_d_eq(arg_type, (int)hook_dalloc_sdallocx, "Wrong hook type");
assert_ptr_eq(ptr, arg_address, "Wrong pointer freed");
assert_u64_eq((uintptr_t)ptr, arg_args_raw[0], "Wrong raw arg");
assert_u64_eq((uintptr_t)1, arg_args_raw[1], "Wrong raw arg");
assert_u64_eq((uintptr_t)MALLOCX_TCACHE_NONE, arg_args_raw[2],
"Wrong raw arg");
hook_remove(TSDN_NULL, handle);
}
TEST_END
int int
main(void) { main(void) {
/* We assert on call counts. */ /* We assert on call counts. */
@ -296,5 +343,6 @@ main(void) {
test_hooks_basic, test_hooks_basic,
test_hooks_null, test_hooks_null,
test_hooks_remove, test_hooks_remove,
test_hooks_alloc_simple); test_hooks_alloc_simple,
test_hooks_dalloc_simple);
} }