Blame jemalloc/src/hook.c

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