Blame jemalloc/src/hook.c

Packit Service 724aca
#include "jemalloc/internal/jemalloc_preamble.h"
Packit Service 724aca
Packit Service 724aca
#include "jemalloc/internal/hook.h"
Packit Service 724aca
Packit Service 724aca
#include "jemalloc/internal/atomic.h"
Packit Service 724aca
#include "jemalloc/internal/mutex.h"
Packit Service 724aca
#include "jemalloc/internal/seq.h"
Packit Service 724aca
Packit Service 724aca
typedef struct hooks_internal_s hooks_internal_t;
Packit Service 724aca
struct hooks_internal_s {
Packit Service 724aca
	hooks_t hooks;
Packit Service 724aca
	bool in_use;
Packit Service 724aca
};
Packit Service 724aca
Packit Service 724aca
seq_define(hooks_internal_t, hooks)
Packit Service 724aca
Packit Service 724aca
static atomic_u_t nhooks = ATOMIC_INIT(0);
Packit Service 724aca
static seq_hooks_t hooks[HOOK_MAX];
Packit Service 724aca
static malloc_mutex_t hooks_mu;
Packit Service 724aca
Packit Service 724aca
bool
Packit Service 724aca
hook_boot() {
Packit Service 724aca
	return malloc_mutex_init(&hooks_mu, "hooks", WITNESS_RANK_HOOK,
Packit Service 724aca
	    malloc_mutex_rank_exclusive);
Packit Service 724aca
}
Packit Service 724aca
Packit Service 724aca
static void *
Packit Service 724aca
hook_install_locked(hooks_t *to_install) {
Packit Service 724aca
	hooks_internal_t hooks_internal;
Packit Service 724aca
	for (int i = 0; i < HOOK_MAX; i++) {
Packit Service 724aca
		bool success = seq_try_load_hooks(&hooks_internal, &hooks[i]);
Packit Service 724aca
		/* We hold mu; no concurrent access. */
Packit Service 724aca
		assert(success);
Packit Service 724aca
		if (!hooks_internal.in_use) {
Packit Service 724aca
			hooks_internal.hooks = *to_install;
Packit Service 724aca
			hooks_internal.in_use = true;
Packit Service 724aca
			seq_store_hooks(&hooks[i], &hooks_internal);
Packit Service 724aca
			atomic_store_u(&nhooks,
Packit Service 724aca
			    atomic_load_u(&nhooks, ATOMIC_RELAXED) + 1,
Packit Service 724aca
			    ATOMIC_RELAXED);
Packit Service 724aca
			return &hooks[i];
Packit Service 724aca
		}
Packit Service 724aca
	}
Packit Service 724aca
	return NULL;
Packit Service 724aca
}
Packit Service 724aca
Packit Service 724aca
void *
Packit Service 724aca
hook_install(tsdn_t *tsdn, hooks_t *to_install) {
Packit Service 724aca
	malloc_mutex_lock(tsdn, &hooks_mu);
Packit Service 724aca
	void *ret = hook_install_locked(to_install);
Packit Service 724aca
	if (ret != NULL) {
Packit Service 724aca
		tsd_global_slow_inc(tsdn);
Packit Service 724aca
	}
Packit Service 724aca
	malloc_mutex_unlock(tsdn, &hooks_mu);
Packit Service 724aca
	return ret;
Packit Service 724aca
}
Packit Service 724aca
Packit Service 724aca
static void
Packit Service 724aca
hook_remove_locked(seq_hooks_t *to_remove) {
Packit Service 724aca
	hooks_internal_t hooks_internal;
Packit Service 724aca
	bool success = seq_try_load_hooks(&hooks_internal, to_remove);
Packit Service 724aca
	/* We hold mu; no concurrent access. */
Packit Service 724aca
	assert(success);
Packit Service 724aca
	/* Should only remove hooks that were added. */
Packit Service 724aca
	assert(hooks_internal.in_use);
Packit Service 724aca
	hooks_internal.in_use = false;
Packit Service 724aca
	seq_store_hooks(to_remove, &hooks_internal);
Packit Service 724aca
	atomic_store_u(&nhooks, atomic_load_u(&nhooks, ATOMIC_RELAXED) - 1,
Packit Service 724aca
	    ATOMIC_RELAXED);
Packit Service 724aca
}
Packit Service 724aca
Packit Service 724aca
void
Packit Service 724aca
hook_remove(tsdn_t *tsdn, void *opaque) {
Packit Service 724aca
	if (config_debug) {
Packit Service 724aca
		char *hooks_begin = (char *)&hooks[0];
Packit Service 724aca
		char *hooks_end = (char *)&hooks[HOOK_MAX];
Packit Service 724aca
		char *hook = (char *)opaque;
Packit Service 724aca
		assert(hooks_begin <= hook && hook < hooks_end
Packit Service 724aca
		    && (hook - hooks_begin) % sizeof(seq_hooks_t) == 0);
Packit Service 724aca
	}
Packit Service 724aca
	malloc_mutex_lock(tsdn, &hooks_mu);
Packit Service 724aca
	hook_remove_locked((seq_hooks_t *)opaque);
Packit Service 724aca
	tsd_global_slow_dec(tsdn);
Packit Service 724aca
	malloc_mutex_unlock(tsdn, &hooks_mu);
Packit Service 724aca
}
Packit Service 724aca
Packit Service 724aca
#define FOR_EACH_HOOK_BEGIN(hooks_internal_ptr)				\
Packit Service 724aca
for (int for_each_hook_counter = 0;					\
Packit Service 724aca
    for_each_hook_counter < HOOK_MAX;					\
Packit Service 724aca
    for_each_hook_counter++) {						\
Packit Service 724aca
	bool for_each_hook_success = seq_try_load_hooks(		\
Packit Service 724aca
	    (hooks_internal_ptr), &hooks[for_each_hook_counter]);	\
Packit Service 724aca
	if (!for_each_hook_success) {					\
Packit Service 724aca
		continue;						\
Packit Service 724aca
	}								\
Packit Service 724aca
	if (!(hooks_internal_ptr)->in_use) {				\
Packit Service 724aca
		continue;						\
Packit Service 724aca
	}
Packit Service 724aca
#define FOR_EACH_HOOK_END						\
Packit Service 724aca
}
Packit Service 724aca
Packit Service 724aca
static bool *
Packit Service 724aca
hook_reentrantp() {
Packit Service 724aca
	/*
Packit Service 724aca
	 * We prevent user reentrancy within hooks.  This is basically just a
Packit Service 724aca
	 * thread-local bool that triggers an early-exit.
Packit Service 724aca
	 *
Packit Service 724aca
	 * We don't fold in_hook into reentrancy.  There are two reasons for
Packit Service 724aca
	 * this:
Packit Service 724aca
	 * - Right now, we turn on reentrancy during things like extent hook
Packit Service 724aca
	 *   execution.  Allocating during extent hooks is not officially
Packit Service 724aca
	 *   supported, but we don't want to break it for the time being.  These
Packit Service 724aca
	 *   sorts of allocations should probably still be hooked, though.
Packit Service 724aca
	 * - If a hook allocates, we may want it to be relatively fast (after
Packit Service 724aca
	 *   all, it executes on every allocator operation).  Turning on
Packit Service 724aca
	 *   reentrancy is a fairly heavyweight mode (disabling tcache,
Packit Service 724aca
	 *   redirecting to arena 0, etc.).  It's possible we may one day want
Packit Service 724aca
	 *   to turn on reentrant mode here, if it proves too difficult to keep
Packit Service 724aca
	 *   this working.  But that's fairly easy for us to see; OTOH, people
Packit Service 724aca
	 *   not using hooks because they're too slow is easy for us to miss.
Packit Service 724aca
	 *
Packit Service 724aca
	 * The tricky part is
Packit Service 724aca
	 * that this code might get invoked even if we don't have access to tsd.
Packit Service 724aca
	 * This function mimics getting a pointer to thread-local data, except
Packit Service 724aca
	 * that it might secretly return a pointer to some global data if we
Packit Service 724aca
	 * know that the caller will take the early-exit path.
Packit Service 724aca
	 * If we return a bool that indicates that we are reentrant, then the
Packit Service 724aca
	 * caller will go down the early exit path, leaving the global
Packit Service 724aca
	 * untouched.
Packit Service 724aca
	 */
Packit Service 724aca
	static bool in_hook_global = true;
Packit Service 724aca
	tsdn_t *tsdn = tsdn_fetch();
Packit Service 724aca
	tcache_t *tcache = tsdn_tcachep_get(tsdn);
Packit Service 724aca
	if (tcache != NULL) {
Packit Service 724aca
		return &tcache->in_hook;
Packit Service 724aca
	}
Packit Service 724aca
	return &in_hook_global;
Packit Service 724aca
}
Packit Service 724aca
Packit Service 724aca
#define HOOK_PROLOGUE							\
Packit Service 724aca
	if (likely(atomic_load_u(&nhooks, ATOMIC_RELAXED) == 0)) {	\
Packit Service 724aca
		return;							\
Packit Service 724aca
	}								\
Packit Service 724aca
	bool *in_hook = hook_reentrantp();				\
Packit Service 724aca
	if (*in_hook) {							\
Packit Service 724aca
		return;							\
Packit Service 724aca
	}								\
Packit Service 724aca
	*in_hook = true;
Packit Service 724aca
Packit Service 724aca
#define HOOK_EPILOGUE							\
Packit Service 724aca
	*in_hook = false;
Packit Service 724aca
Packit Service 724aca
void
Packit Service 724aca
hook_invoke_alloc(hook_alloc_t type, void *result, uintptr_t result_raw,
Packit Service 724aca
    uintptr_t args_raw[3]) {
Packit Service 724aca
	HOOK_PROLOGUE
Packit Service 724aca
Packit Service 724aca
	hooks_internal_t hook;
Packit Service 724aca
	FOR_EACH_HOOK_BEGIN(&hook)
Packit Service 724aca
		hook_alloc h = hook.hooks.alloc_hook;
Packit Service 724aca
		if (h != NULL) {
Packit Service 724aca
			h(hook.hooks.extra, type, result, result_raw, args_raw);
Packit Service 724aca
		}
Packit Service 724aca
	FOR_EACH_HOOK_END
Packit Service 724aca
Packit Service 724aca
	HOOK_EPILOGUE
Packit Service 724aca
}
Packit Service 724aca
Packit Service 724aca
void
Packit Service 724aca
hook_invoke_dalloc(hook_dalloc_t type, void *address, uintptr_t args_raw[3]) {
Packit Service 724aca
	HOOK_PROLOGUE
Packit Service 724aca
	hooks_internal_t hook;
Packit Service 724aca
	FOR_EACH_HOOK_BEGIN(&hook)
Packit Service 724aca
		hook_dalloc h = hook.hooks.dalloc_hook;
Packit Service 724aca
		if (h != NULL) {
Packit Service 724aca
			h(hook.hooks.extra, type, address, args_raw);
Packit Service 724aca
		}
Packit Service 724aca
	FOR_EACH_HOOK_END
Packit Service 724aca
	HOOK_EPILOGUE
Packit Service 724aca
}
Packit Service 724aca
Packit Service 724aca
void
Packit Service 724aca
hook_invoke_expand(hook_expand_t type, void *address, size_t old_usize,
Packit Service 724aca
    size_t new_usize, uintptr_t result_raw, uintptr_t args_raw[4]) {
Packit Service 724aca
	HOOK_PROLOGUE
Packit Service 724aca
	hooks_internal_t hook;
Packit Service 724aca
	FOR_EACH_HOOK_BEGIN(&hook)
Packit Service 724aca
		hook_expand h = hook.hooks.expand_hook;
Packit Service 724aca
		if (h != NULL) {
Packit Service 724aca
			h(hook.hooks.extra, type, address, old_usize, new_usize,
Packit Service 724aca
			    result_raw, args_raw);
Packit Service 724aca
		}
Packit Service 724aca
	FOR_EACH_HOOK_END
Packit Service 724aca
	HOOK_EPILOGUE
Packit Service 724aca
}