This assumes jemalloc3 is enabled. ================================================ .mozconfig ac_add_options --enable-replace-malloc ================================================ memory/replace/moz.build + DIRS += ['randchunks'] ================================================ memory/replace/randchunks/moz.build SharedLibrary('replace_randchunks') SOURCES += [ 'malloc.cpp', ] DEFINES['abort'] = 'moz_abort' DISABLE_STL_WRAPPING = True ================================================ memory/replace/randchunks/malloc.cpp #include "replace_malloc.h" #include #include #include #include //Requires https://bug1052573.bugzilla.mozilla.org/attachment.cgi?id=8573433 #define NUM_RAND_ARENAS 10 static const malloc_table_t *funcs = NULL; static unsigned int mallocs = 0, frees = 0, reallocs = 0, callocs = 0; static unsigned int arenas[NUM_RAND_ARENAS]; static const uintptr_t randseed = 5; extern "C" { void replace_init(const malloc_table_t *table) { funcs = table; for(int i=0; imalloc_create_partition(); } printf("In init!\n"); } void replace_jemalloc_stats(jemalloc_stats_t *stats) { void* fps[4]; backtrace(fps, 4); printf("%d mallocs, %d frees, %d reallocs, %d callocs\n4-point stacktrace:\n", mallocs, frees, reallocs, callocs); backtrace_symbols_fd(fps, 4, STDERR_FILENO); } void* replace_malloc(size_t size) { int arena_indx; uintptr_t fps[4]; //backtrace((void**)fps, 4); arena_indx = (randseed * fps[1] * fps[2] * fps[3]) % NUM_RAND_ARENAS; mallocs++; return funcs->malloc_from_partition(arenas[arena_indx], size); } void replace_free(void* ptr) { frees++; funcs->free(ptr); } void* replace_realloc(void* ptr, size_t size) { int arena_indx; uintptr_t fps[4]; //backtrace((void**)fps, 4); arena_indx = (randseed * fps[1] * fps[2] * fps[3]) % NUM_RAND_ARENAS; reallocs++; return funcs->realloc_from_partition(arenas[arena_indx], ptr, size); } void* replace_calloc(size_t nmemb, size_t size) { int arena_indx; uintptr_t fps[4]; //backtrace((void**)fps, 4); arena_indx = (randseed * fps[1] * fps[2] * fps[3]) % NUM_RAND_ARENAS; callocs++; return funcs->calloc_from_partition(arenas[arena_indx], nmemb, size); } } //extern C