Blame jemalloc/include/jemalloc/internal/hook.h

Packit 345191
#ifndef JEMALLOC_INTERNAL_HOOK_H
Packit 345191
#define JEMALLOC_INTERNAL_HOOK_H
Packit 345191
Packit 345191
#include "jemalloc/internal/tsd.h"
Packit 345191
Packit 345191
/*
Packit 345191
 * This API is *extremely* experimental, and may get ripped out, changed in API-
Packit 345191
 * and ABI-incompatible ways, be insufficiently or incorrectly documented, etc.
Packit 345191
 *
Packit 345191
 * It allows hooking the stateful parts of the API to see changes as they
Packit 345191
 * happen.
Packit 345191
 *
Packit 345191
 * Allocation hooks are called after the allocation is done, free hooks are
Packit 345191
 * called before the free is done, and expand hooks are called after the
Packit 345191
 * allocation is expanded.
Packit 345191
 *
Packit 345191
 * For realloc and rallocx, if the expansion happens in place, the expansion
Packit 345191
 * hook is called.  If it is moved, then the alloc hook is called on the new
Packit 345191
 * location, and then the free hook is called on the old location (i.e. both
Packit 345191
 * hooks are invoked in between the alloc and the dalloc).
Packit 345191
 *
Packit 345191
 * If we return NULL from OOM, then usize might not be trustworthy.  Calling
Packit 345191
 * realloc(NULL, size) only calls the alloc hook, and calling realloc(ptr, 0)
Packit 345191
 * only calls the free hook.  (Calling realloc(NULL, 0) is treated as malloc(0),
Packit 345191
 * and only calls the alloc hook).
Packit 345191
 *
Packit 345191
 * Reentrancy:
Packit 345191
 *   Reentrancy is guarded against from within the hook implementation.  If you
Packit 345191
 *   call allocator functions from within a hook, the hooks will not be invoked
Packit 345191
 *   again.
Packit 345191
 * Threading:
Packit 345191
 *   The installation of a hook synchronizes with all its uses.  If you can
Packit 345191
 *   prove the installation of a hook happens-before a jemalloc entry point,
Packit 345191
 *   then the hook will get invoked (unless there's a racing removal).
Packit 345191
 *
Packit 345191
 *   Hook insertion appears to be atomic at a per-thread level (i.e. if a thread
Packit 345191
 *   allocates and has the alloc hook invoked, then a subsequent free on the
Packit 345191
 *   same thread will also have the free hook invoked).
Packit 345191
 *
Packit 345191
 *   The *removal* of a hook does *not* block until all threads are done with
Packit 345191
 *   the hook.  Hook authors have to be resilient to this, and need some
Packit 345191
 *   out-of-band mechanism for cleaning up any dynamically allocated memory
Packit 345191
 *   associated with their hook.
Packit 345191
 * Ordering:
Packit 345191
 *   Order of hook execution is unspecified, and may be different than insertion
Packit 345191
 *   order.
Packit 345191
 */
Packit 345191
Packit 345191
#define HOOK_MAX 4
Packit 345191
Packit 345191
enum hook_alloc_e {
Packit 345191
	hook_alloc_malloc,
Packit 345191
	hook_alloc_posix_memalign,
Packit 345191
	hook_alloc_aligned_alloc,
Packit 345191
	hook_alloc_calloc,
Packit 345191
	hook_alloc_memalign,
Packit 345191
	hook_alloc_valloc,
Packit 345191
	hook_alloc_mallocx,
Packit 345191
Packit 345191
	/* The reallocating functions have both alloc and dalloc variants */
Packit 345191
	hook_alloc_realloc,
Packit 345191
	hook_alloc_rallocx,
Packit 345191
};
Packit 345191
/*
Packit 345191
 * We put the enum typedef after the enum, since this file may get included by
Packit 345191
 * jemalloc_cpp.cpp, and C++ disallows enum forward declarations.
Packit 345191
 */
Packit 345191
typedef enum hook_alloc_e hook_alloc_t;
Packit 345191
Packit 345191
enum hook_dalloc_e {
Packit 345191
	hook_dalloc_free,
Packit 345191
	hook_dalloc_dallocx,
Packit 345191
	hook_dalloc_sdallocx,
Packit 345191
Packit 345191
	/*
Packit 345191
	 * The dalloc halves of reallocation (not called if in-place expansion
Packit 345191
	 * happens).
Packit 345191
	 */
Packit 345191
	hook_dalloc_realloc,
Packit 345191
	hook_dalloc_rallocx,
Packit 345191
};
Packit 345191
typedef enum hook_dalloc_e hook_dalloc_t;
Packit 345191
Packit 345191
Packit 345191
enum hook_expand_e {
Packit 345191
	hook_expand_realloc,
Packit 345191
	hook_expand_rallocx,
Packit 345191
	hook_expand_xallocx,
Packit 345191
};
Packit 345191
typedef enum hook_expand_e hook_expand_t;
Packit 345191
Packit 345191
typedef void (*hook_alloc)(
Packit 345191
    void *extra, hook_alloc_t type, void *result, uintptr_t result_raw,
Packit 345191
    uintptr_t args_raw[3]);
Packit 345191
Packit 345191
typedef void (*hook_dalloc)(
Packit 345191
    void *extra, hook_dalloc_t type, void *address, uintptr_t args_raw[3]);
Packit 345191
Packit 345191
typedef void (*hook_expand)(
Packit 345191
    void *extra, 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
Packit 345191
typedef struct hooks_s hooks_t;
Packit 345191
struct hooks_s {
Packit 345191
	hook_alloc alloc_hook;
Packit 345191
	hook_dalloc dalloc_hook;
Packit 345191
	hook_expand expand_hook;
Packit 345191
	void *extra;
Packit 345191
};
Packit 345191
Packit 345191
/*
Packit 345191
 * Begin implementation details; everything above this point might one day live
Packit 345191
 * in a public API.  Everything below this point never will.
Packit 345191
 */
Packit 345191
Packit 345191
/*
Packit 345191
 * The realloc pathways haven't gotten any refactoring love in a while, and it's
Packit 345191
 * fairly difficult to pass information from the entry point to the hooks.  We
Packit 345191
 * put the informaiton the hooks will need into a struct to encapsulate
Packit 345191
 * everything.
Packit 345191
 *
Packit 345191
 * Much of these pathways are force-inlined, so that the compiler can avoid
Packit 345191
 * materializing this struct until we hit an extern arena function.  For fairly
Packit 345191
 * goofy reasons, *many* of the realloc paths hit an extern arena function.
Packit 345191
 * These paths are cold enough that it doesn't matter; eventually, we should
Packit 345191
 * rewrite the realloc code to make the expand-in-place and the
Packit 345191
 * free-then-realloc paths more orthogonal, at which point we don't need to
Packit 345191
 * spread the hook logic all over the place.
Packit 345191
 */
Packit 345191
typedef struct hook_ralloc_args_s hook_ralloc_args_t;
Packit 345191
struct hook_ralloc_args_s {
Packit 345191
	/* I.e. as opposed to rallocx. */
Packit 345191
	bool is_realloc;
Packit 345191
	/*
Packit 345191
	 * The expand hook takes 4 arguments, even if only 3 are actually used;
Packit 345191
	 * we add an extra one in case the user decides to memcpy without
Packit 345191
	 * looking too closely at the hooked function.
Packit 345191
	 */
Packit 345191
	uintptr_t args[4];
Packit 345191
};
Packit 345191
Packit 345191
/*
Packit 345191
 * Returns an opaque handle to be used when removing the hook.  NULL means that
Packit 345191
 * we couldn't install the hook.
Packit 345191
 */
Packit 345191
bool hook_boot();
Packit 345191
Packit 345191
void *hook_install(tsdn_t *tsdn, hooks_t *hooks);
Packit 345191
/* Uninstalls the hook with the handle previously returned from hook_install. */
Packit 345191
void hook_remove(tsdn_t *tsdn, void *opaque);
Packit 345191
Packit 345191
/* Hooks */
Packit 345191
Packit 345191
void hook_invoke_alloc(hook_alloc_t type, void *result, uintptr_t result_raw,
Packit 345191
    uintptr_t args_raw[3]);
Packit 345191
Packit 345191
void hook_invoke_dalloc(hook_dalloc_t type, void *address,
Packit 345191
    uintptr_t args_raw[3]);
Packit 345191
Packit 345191
void 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
Packit 345191
#endif /* JEMALLOC_INTERNAL_HOOK_H */