@@ -180,11 +180,7 @@ ptmalloc_unlock_all2 (void)
if(__malloc_initialized < 1)
return;
-#if defined MALLOC_HOOKS
atfork_recursive_thread = 0;
- dlmalloc_hook = save_malloc_hook;
- dlfree_hook = save_free_hook;
-#endif
for(ar_ptr = &main_arena;;) {
mutex_init(&ar_ptr->mutex);
ar_ptr = ar_ptr->next;
@@ -310,12 +306,7 @@ static void ptmalloc_init(void)
s = getenv("MALLOC_CHECK_");
if (s && s[0]) {
mALLOPt(M_CHECK_ACTION, (int)(s[0] - '0'));
- if (check_action != 0)
- dlmalloc_check_init();
}
- void (*hook) (void) = force_reg(dlmalloc_initialize_hook);
- if (hook != NULL)
- (*hook) ();
__malloc_initialized = 1;
}
@@ -102,22 +102,6 @@ extern void *dlget_state __DLMALLOC_P ((void));
malloc_get_state(). */
extern int dlset_state __DLMALLOC_P ((void *__ptr));
-/* Called once when malloc is initialized; redefining this variable in
- the application provides the preferred way to set up the hook
- pointers. */
-extern void (*dlmalloc_initialize_hook) __DLMALLOC_PMT ((void));
-/* Hooks for debugging and user-defined versions. */
-extern void (*dlfree_hook) __DLMALLOC_PMT ((void *__ptr,
- const void *));
-extern void *(*dlmalloc_hook) __DLMALLOC_PMT ((size_t __size,
- const void *));
-extern void *(*dlrealloc_hook) __DLMALLOC_PMT ((void *__ptr, size_t __size,
- const void *));
-extern void *(*dlmemalign_hook) __DLMALLOC_PMT ((size_t __alignment,
- size_t __size,
- const void *));
-extern void (*dlafter_morecore_hook) __DLMALLOC_PMT ((void));
-
/* Activate a standard set of debugging hooks. */
extern void dlmalloc_check_init __DLMALLOC_P ((void));
@@ -25,31 +25,6 @@
/* Hooks for debugging versions. The initial hooks just call the
initialization routine, then do the normal work. */
-static Void_t*
-malloc_hook_ini(size_t sz, const __malloc_ptr_t caller)
-{
- dlmalloc_hook = NULL;
- ptmalloc_init();
- return public_mALLOc(sz);
-}
-
-static Void_t*
-realloc_hook_ini(Void_t* ptr, size_t sz, const __malloc_ptr_t caller)
-{
- dlmalloc_hook = NULL;
- dlrealloc_hook = NULL;
- ptmalloc_init();
- return public_rEALLOc(ptr, sz);
-}
-
-static Void_t*
-memalign_hook_ini(size_t alignment, size_t sz, const __malloc_ptr_t caller)
-{
- dlmemalign_hook = NULL;
- ptmalloc_init();
- return public_mEMALIGn(alignment, sz);
-}
-
/* Whether we are using malloc checking. */
static int using_malloc_checking;
@@ -68,21 +43,6 @@ static int using_malloc_checking;
further malloc checking is safe. */
static int disallow_malloc_check;
-/* Activate a standard set of debugging hooks. */
-void
-dlmalloc_check_init()
-{
- if (disallow_malloc_check) {
- disallow_malloc_check = 0;
- return;
- }
- using_malloc_checking = 1;
- dlmalloc_hook = malloc_check;
- dlfree_hook = free_check;
- dlrealloc_hook = realloc_check;
- dlmemalign_hook = memalign_check;
-}
-
/* A simple, standard set of debugging hooks. Overhead is `only' one
byte per chunk; still this will catch most cases of double frees or
overruns. The goal here is to avoid obscure crashes due to invalid
@@ -90,277 +50,9 @@ dlmalloc_check_init()
#define MAGICBYTE(p) ( ( ((size_t)p >> 3) ^ ((size_t)p >> 11)) & 0xFF )
-/* Instrument a chunk with overrun detector byte(s) and convert it
- into a user pointer with requested size sz. */
-
-static Void_t*
-internal_function
-mem2mem_check(Void_t *ptr, size_t sz)
-{
- mchunkptr p;
- unsigned char* m_ptr = (unsigned char*)BOUNDED_N(ptr, sz);
- size_t i;
-
- if (!ptr)
- return ptr;
- p = mem2chunk(ptr);
- for(i = chunksize(p) - (chunk_is_mmapped(p) ? 2*SIZE_SZ+1 : SIZE_SZ+1);
- i > sz;
- i -= 0xFF) {
- if(i-sz < 0x100) {
- m_ptr[i] = (unsigned char)(i-sz);
- break;
- }
- m_ptr[i] = 0xFF;
- }
- m_ptr[sz] = MAGICBYTE(p);
- return (Void_t*)m_ptr;
-}
-
-/* Convert a pointer to be free()d or realloc()ed to a valid chunk
- pointer. If the provided pointer is not valid, return NULL. */
-
-static mchunkptr
-internal_function
-mem2chunk_check(Void_t* mem, unsigned char **magic_p)
-{
- mchunkptr p;
- INTERNAL_SIZE_T sz, c;
- unsigned char magic;
-
- if(!aligned_OK(mem)) return NULL;
- p = mem2chunk(mem);
- if (!chunk_is_mmapped(p)) {
- /* Must be a chunk in conventional heap memory. */
- int contig = contiguous(&main_arena);
- sz = chunksize(p);
- if((contig &&
- ((char*)p<mp_.sbrk_base ||
- ((char*)p + sz)>=(mp_.sbrk_base+main_arena.system_mem) )) ||
- sz<MINSIZE || sz&MALLOC_ALIGN_MASK || !inuse(p) ||
- ( !prev_inuse(p) && (p->prev_size&MALLOC_ALIGN_MASK ||
- (contig && (char*)prev_chunk(p)<mp_.sbrk_base) ||
- next_chunk(prev_chunk(p))!=p) ))
- return NULL;
- magic = MAGICBYTE(p);
- for(sz += SIZE_SZ-1; (c = ((unsigned char*)p)[sz]) != magic; sz -= c) {
- if(c<=0 || sz<(c+2*SIZE_SZ)) return NULL;
- }
- } else {
- unsigned long offset, page_mask = malloc_getpagesize-1;
-
- /* mmap()ed chunks have MALLOC_ALIGNMENT or higher power-of-two
- alignment relative to the beginning of a page. Check this
- first. */
- offset = (unsigned long)mem & page_mask;
- if((offset!=MALLOC_ALIGNMENT && offset!=0 && offset!=0x10 &&
- offset!=0x20 && offset!=0x40 && offset!=0x80 && offset!=0x100 &&
- offset!=0x200 && offset!=0x400 && offset!=0x800 && offset!=0x1000 &&
- offset<0x2000) ||
- !chunk_is_mmapped(p) || (p->size & PREV_INUSE) ||
- ( (((unsigned long)p - p->prev_size) & page_mask) != 0 ) ||
- ( (sz = chunksize(p)), ((p->prev_size + sz) & page_mask) != 0 ) )
- return NULL;
- magic = MAGICBYTE(p);
- for(sz -= 1; (c = ((unsigned char*)p)[sz]) != magic; sz -= c) {
- if(c<=0 || sz<(c+2*SIZE_SZ)) return NULL;
- }
- }
- ((unsigned char*)p)[sz] ^= 0xFF;
- if (magic_p)
- *magic_p = (unsigned char *)p + sz;
- return p;
-}
-
/* Check for corruption of the top chunk, and try to recover if
necessary. */
-static int
-internal_function
-top_check(void)
-{
- mchunkptr t = top(&main_arena);
- char* brk, * new_brk;
- INTERNAL_SIZE_T front_misalign, sbrk_size;
- unsigned long pagesz = malloc_getpagesize;
-
- if (t == initial_top(&main_arena) ||
- (!chunk_is_mmapped(t) &&
- chunksize(t)>=MINSIZE &&
- prev_inuse(t) &&
- (!contiguous(&main_arena) ||
- (char*)t + chunksize(t) == mp_.sbrk_base + main_arena.system_mem)))
- return 0;
-
- malloc_printerr (check_action, "malloc: top chunk is corrupt", t);
-
- /* Try to set up a new top chunk. */
- brk = MORECORE(0);
- front_misalign = (unsigned long)chunk2mem(brk) & MALLOC_ALIGN_MASK;
- if (front_misalign > 0)
- front_misalign = MALLOC_ALIGNMENT - front_misalign;
- sbrk_size = front_misalign + mp_.top_pad + MINSIZE;
- sbrk_size += pagesz - ((unsigned long)(brk + sbrk_size) & (pagesz - 1));
- new_brk = (char*)(MORECORE (sbrk_size));
- if (new_brk == (char*)(MORECORE_FAILURE))
- {
- MALLOC_FAILURE_ACTION;
- return -1;
- }
- /* Call the `morecore' hook if necessary. */
- void (*hook) (void) = force_reg (dlafter_morecore_hook);
- if (hook)
- (*hook) ();
- main_arena.system_mem = (new_brk - mp_.sbrk_base) + sbrk_size;
-
- top(&main_arena) = (mchunkptr)(brk + front_misalign);
- set_head(top(&main_arena), (sbrk_size - front_misalign) | PREV_INUSE);
-
- return 0;
-}
-
-static Void_t*
-malloc_check(size_t sz, const Void_t *caller)
-{
- Void_t *victim;
-
- if (sz+1 == 0) {
- MALLOC_FAILURE_ACTION;
- return NULL;
- }
-
- (void)mutex_lock(&main_arena.mutex);
- victim = (top_check() >= 0) ? _int_malloc(&main_arena, sz+1) : NULL;
- (void)mutex_unlock(&main_arena.mutex);
- return mem2mem_check(victim, sz);
-}
-
-static void
-free_check(Void_t* mem, const Void_t *caller)
-{
- mchunkptr p;
-
- if(!mem) return;
- (void)mutex_lock(&main_arena.mutex);
- p = mem2chunk_check(mem, NULL);
- if(!p) {
- (void)mutex_unlock(&main_arena.mutex);
-
- malloc_printerr(check_action, "free(): invalid pointer", mem);
- return;
- }
- if (chunk_is_mmapped(p)) {
- (void)mutex_unlock(&main_arena.mutex);
- munmap_chunk(p);
- return;
- }
-#if 0 /* Erase freed memory. */
- memset(mem, 0, chunksize(p) - (SIZE_SZ+1));
-#endif
- _int_free(&main_arena, p);
- (void)mutex_unlock(&main_arena.mutex);
-}
-
-static Void_t*
-realloc_check(Void_t* oldmem, size_t bytes, const Void_t *caller)
-{
- INTERNAL_SIZE_T nb;
- Void_t* newmem = 0;
- unsigned char *magic_p;
-
- if (bytes+1 == 0) {
- MALLOC_FAILURE_ACTION;
- return NULL;
- }
- if (oldmem == 0) return malloc_check(bytes, NULL);
- if (bytes == 0) {
- free_check (oldmem, NULL);
- return NULL;
- }
- (void)mutex_lock(&main_arena.mutex);
- const mchunkptr oldp = mem2chunk_check(oldmem, &magic_p);
- (void)mutex_unlock(&main_arena.mutex);
- if(!oldp) {
- malloc_printerr(check_action, "realloc(): invalid pointer", oldmem);
- return malloc_check(bytes, NULL);
- }
- const INTERNAL_SIZE_T oldsize = chunksize(oldp);
-
- checked_request2size(bytes+1, nb);
- (void)mutex_lock(&main_arena.mutex);
-
- if (chunk_is_mmapped(oldp)) {
-#if HAVE_MREMAP
- mchunkptr newp = mremap_chunk(oldp, nb);
- if(newp)
- newmem = chunk2mem(newp);
- else
-#endif
- {
- /* Note the extra SIZE_SZ overhead. */
- if(oldsize - SIZE_SZ >= nb)
- newmem = oldmem; /* do nothing */
- else {
- /* Must alloc, copy, free. */
- if (top_check() >= 0)
- newmem = _int_malloc(&main_arena, bytes+1);
- if (newmem) {
- MALLOC_COPY(BOUNDED_N(newmem, bytes+1), oldmem, oldsize - 2*SIZE_SZ);
- munmap_chunk(oldp);
- }
- }
- }
- } else {
- if (top_check() >= 0) {
- INTERNAL_SIZE_T nb;
- checked_request2size(bytes + 1, nb);
- newmem = _int_realloc(&main_arena, oldp, oldsize, nb);
- }
-#if 0 /* Erase freed memory. */
- if(newmem)
- newp = mem2chunk(newmem);
- nb = chunksize(newp);
- if(oldp<newp || oldp>=chunk_at_offset(newp, nb)) {
- memset((char*)oldmem + 2*sizeof(mbinptr), 0,
- oldsize - (2*sizeof(mbinptr)+2*SIZE_SZ+1));
- } else if(nb > oldsize+SIZE_SZ) {
- memset((char*)BOUNDED_N(chunk2mem(newp), bytes) + oldsize,
- 0, nb - (oldsize+SIZE_SZ));
- }
-#endif
- }
-
- /* mem2chunk_check changed the magic byte in the old chunk.
- If newmem is NULL, then the old chunk will still be used though,
- so we need to invert that change here. */
- if (newmem == NULL) *magic_p ^= 0xFF;
-
- (void)mutex_unlock(&main_arena.mutex);
-
- return mem2mem_check(newmem, bytes);
-}
-
-static Void_t*
-memalign_check(size_t alignment, size_t bytes, const Void_t *caller)
-{
- INTERNAL_SIZE_T nb __attribute__((unused));
- Void_t* mem;
-
- if (alignment <= MALLOC_ALIGNMENT) return malloc_check(bytes, NULL);
- if (alignment < MINSIZE) alignment = MINSIZE;
-
- if (bytes+1 == 0) {
- MALLOC_FAILURE_ACTION;
- return NULL;
- }
- checked_request2size(bytes+1, nb);
- (void)mutex_lock(&main_arena.mutex);
- mem = (top_check() >= 0) ? _int_memalign(&main_arena, alignment, bytes+1) :
- NULL;
- (void)mutex_unlock(&main_arena.mutex);
- return mem2mem_check(mem, bytes);
-}
-
/* Get/set state: malloc_get_state() records the current state of all
malloc variables (_except_ for the actual heap contents and `hook'
function pointers) in a system dependent, opaque data structure.
@@ -411,6 +103,7 @@ public_gET_STATe(void)
int i;
mbinptr b;
+ abort();
ms = (struct malloc_save_state*)public_mALLOc(sizeof(*ms));
if (!ms)
return 0;
@@ -461,6 +154,7 @@ public_sET_STATe(Void_t* msptr)
size_t i;
mbinptr b;
+ abort();
disallow_malloc_check = 1;
ptmalloc_init();
if(ms->magic != MALLOC_STATE_MAGIC) return -1;
@@ -534,20 +228,6 @@ public_sET_STATe(Void_t* msptr)
mp_.mmapped_mem = ms->mmapped_mem;
mp_.max_mmapped_mem = ms->max_mmapped_mem;
/* add version-dependent code here */
- if (ms->version >= 1) {
- /* Check whether it is safe to enable malloc checking, or whether
- it is necessary to disable it. */
- if (ms->using_malloc_checking && !using_malloc_checking &&
- !disallow_malloc_check)
- dlmalloc_check_init ();
- else if (!ms->using_malloc_checking && using_malloc_checking) {
- dlmalloc_hook = NULL;
- dlfree_hook = NULL;
- dlrealloc_hook = NULL;
- dlmemalign_hook = NULL;
- using_malloc_checking = 0;
- }
- }
if (ms->version >= 4) {
}
check_malloc_state(&main_arena);
@@ -1379,21 +1379,11 @@ static int mALLOPt(int, int);
static struct mallinfo2 mALLINFo(struct malloc_state *);
static void malloc_printerr(int action, const char *str, void *ptr);
-static Void_t* internal_function mem2mem_check(Void_t *p, size_t sz);
-static int internal_function top_check(void);
static void internal_function munmap_chunk(mchunkptr p);
#if HAVE_MREMAP
static mchunkptr internal_function mremap_chunk(mchunkptr p, size_t new_size);
#endif
-static Void_t* malloc_check(size_t sz, const Void_t *caller);
-static void free_check(Void_t* mem, const Void_t *caller);
-static Void_t* realloc_check(Void_t* oldmem, size_t bytes,
- const Void_t *caller);
-static Void_t* memalign_check(size_t alignment, size_t bytes,
- const Void_t *caller);
-
-
/* ------------- Optional versions of memcopy ---------------- */
@@ -2206,36 +2196,6 @@ static Void_t** iALLOc(struct malloc_state *, size_t, size_t*, int, Void_t**);
static void tcache_destroy(void *_cache);
-/* -------------- Early definitions for debugging hooks ---------------- */
-
-/* Define and initialize the hook variables. These weak definitions must
- appear before any use of the variables in a function (arena.c uses one). */
-#ifndef weak_variable
-#define weak_variable /**/
-#endif
-
-/* Forward declarations. */
-static Void_t* malloc_hook_ini __MALLOC_P ((size_t sz,
- const __malloc_ptr_t caller));
-static Void_t* realloc_hook_ini __MALLOC_P ((Void_t* ptr, size_t sz,
- const __malloc_ptr_t caller));
-static Void_t* memalign_hook_ini __MALLOC_P ((size_t alignment, size_t sz,
- const __malloc_ptr_t caller));
-
-void weak_variable (*dlmalloc_initialize_hook) (void) = NULL;
-void weak_variable (*dlfree_hook) (__malloc_ptr_t __ptr,
- const __malloc_ptr_t) = NULL;
-__malloc_ptr_t weak_variable (*dlmalloc_hook)
- (size_t __size, const __malloc_ptr_t) = malloc_hook_ini;
-__malloc_ptr_t weak_variable (*dlrealloc_hook)
- (__malloc_ptr_t __ptr, size_t __size, const __malloc_ptr_t)
- = realloc_hook_ini;
-__malloc_ptr_t weak_variable (*dlmemalign_hook)
- (size_t __alignment, size_t __size, const __malloc_ptr_t)
- = memalign_hook_ini;
-void weak_variable (*dlafter_morecore_hook) (void) = NULL;
-
-
/* ---------------- Error behavior ------------------------------------ */
#ifndef DEFAULT_CHECK_ACTION
@@ -2855,10 +2815,6 @@ static Void_t* sYSMALLOc(INTERNAL_SIZE_T nb, struct malloc_state * av)
brk = (char*)(MORECORE(size));
if (brk != (char*)(MORECORE_FAILURE)) {
- /* Call the `morecore' hook if necessary. */
- void (*hook) (void) = force_reg (dlafter_morecore_hook);
- if (hook != NULL)
- (*hook) ();
} else {
/*
If have mmap, try using it as a backup when MORECORE fails or
@@ -2992,11 +2948,6 @@ static Void_t* sYSMALLOc(INTERNAL_SIZE_T nb, struct malloc_state * av)
if (snd_brk == (char*)(MORECORE_FAILURE)) {
correction = 0;
snd_brk = (char*)(MORECORE(0));
- } else {
- /* Call the `morecore' hook if necessary. */
- void (*hook) (void) = force_reg (dlafter_morecore_hook);
- if (hook != NULL)
- (*hook) ();
}
}
@@ -3136,10 +3087,6 @@ static int sYSTRIm(size_t pad, struct malloc_state * av)
*/
MORECORE(-extra);
- /* Call the `morecore' hook if necessary. */
- void (*hook) (void) = force_reg (dlafter_morecore_hook);
- if (hook != NULL)
- (*hook) ();
new_brk = (char*)(MORECORE(0));
if (new_brk != (char*)MORECORE_FAILURE) {
@@ -3318,11 +3265,6 @@ public_rEALLOc(Void_t* oldmem, size_t bytes)
Void_t* newp; /* chunk to return */
- __malloc_ptr_t (*hook) (__malloc_ptr_t, size_t, __const __malloc_ptr_t) =
- force_reg (dlrealloc_hook);
- if (hook != NULL)
- return (*hook)(oldmem, bytes, RETURN_ADDRESS (0));
-
#if REALLOC_ZERO_BYTES_FREES
if (bytes == 0 && oldmem != NULL) { public_fREe(oldmem); return 0; }
#endif
@@ -3401,10 +3343,6 @@ Void_t *public_mEMALIGn(size_t alignment, size_t bytes)
struct malloc_state *ar_ptr;
Void_t *p;
- __malloc_ptr_t(*hook) __MALLOC_PMT((size_t, size_t, __const __malloc_ptr_t)) = force_reg(dlmemalign_hook);
- if (hook != NULL)
- return (*hook) (alignment, bytes, RETURN_ADDRESS(0));
-
/* If need less alignment than we give anyway, just relay to malloc */
if (alignment <= MALLOC_ALIGNMENT)
return public_mALLOc(bytes);
@@ -3436,10 +3374,6 @@ Void_t *public_vALLOc(size_t bytes)
size_t pagesz = mp_.pagesize;
- __malloc_ptr_t(*hook) __MALLOC_PMT((size_t, size_t, __const __malloc_ptr_t)) = force_reg(dlmemalign_hook);
- if (hook != NULL)
- return (*hook) (pagesz, bytes, RETURN_ADDRESS(0));
-
ar_ptr = arena_get(bytes + pagesz + MINSIZE);
if (!ar_ptr)
return 0;
@@ -3467,10 +3401,6 @@ Void_t *public_pVALLOc(size_t bytes)
size_t page_mask = mp_.pagesize - 1;
size_t rounded_bytes = (bytes + page_mask) & ~(page_mask);
- __malloc_ptr_t(*hook) __MALLOC_PMT((size_t, size_t, __const __malloc_ptr_t)) = force_reg(dlmemalign_hook);
- if (hook != NULL)
- return (*hook) (pagesz, rounded_bytes, RETURN_ADDRESS(0));
-
ar_ptr = arena_get(bytes + 2 * pagesz + MINSIZE);
p = _int_pvalloc(ar_ptr, bytes);
if (!p) {
@@ -5193,7 +5123,6 @@ int mALLOPt(int param_number, int value)
break;
case M_CHECK_ACTION:
- check_action = value;
break;
case M_PERTURB:
@@ -5393,15 +5322,7 @@ dlposix_memalign (void **memptr, size_t alignment, size_t size)
|| alignment == 0)
return EINVAL;
- /* Call the hook here, so that caller is posix_memalign's caller
- and not posix_memalign itself. */
- __malloc_ptr_t (*hook) __MALLOC_PMT ((size_t, size_t,
- __const __malloc_ptr_t)) =
- force_reg (dlmemalign_hook);
- if (hook != NULL)
- mem = (*hook)(alignment, size, RETURN_ADDRESS (0));
- else
- mem = public_mEMALIGn (alignment, size);
+ mem = public_mEMALIGn (alignment, size);
if (mem != NULL) {
*memptr = mem;