From 33ddb437e6bde48be352567465673636f1745188 Mon Sep 17 00:00:00 2001 From: Martin Quinson Date: Wed, 1 Feb 2012 15:25:05 +0100 Subject: [PATCH] kill the standard hooks of mmalloc: we will change the implementation directly, not declare hooks --- include/xbt/mmalloc.h | 6 -- src/xbt/mmalloc/attach.c | 3 - src/xbt/mmalloc/mfree.c | 6 +- src/xbt/mmalloc/mm.c | 1 - src/xbt/mmalloc/mmalloc.c | 4 - src/xbt/mmalloc/mmcheck.c | 183 ------------------------------------ src/xbt/mmalloc/mmprivate.h | 26 ----- src/xbt/mmalloc/mrealloc.c | 7 +- 8 files changed, 3 insertions(+), 233 deletions(-) delete mode 100644 src/xbt/mmalloc/mmcheck.c diff --git a/include/xbt/mmalloc.h b/include/xbt/mmalloc.h index 8ecb836bd0..b416105599 100644 --- a/include/xbt/mmalloc.h +++ b/include/xbt/mmalloc.h @@ -34,12 +34,6 @@ extern void *mmemalign(void *md, size_t alignment, size_t size); extern void *mvalloc(void *md, size_t size); -/* Activate a standard collection of debugging hooks. */ - -extern int mmcheck(void *md, void (*func) (void)); - -extern int mmcheckf(void *md, void (*func) (void), int force); - /* Pick up the current statistics. (see FIXME elsewhere) */ extern struct mstats mmstats(void *md); diff --git a/src/xbt/mmalloc/attach.c b/src/xbt/mmalloc/attach.c index 254182e635..5c6b3d286b 100644 --- a/src/xbt/mmalloc/attach.c +++ b/src/xbt/mmalloc/attach.c @@ -197,9 +197,6 @@ static struct mdesc *reuse(int fd) sem_init(&mdp->sem, 1, 1); mdp->refcount++; } - if (mdp->mfree_hook != NULL) { - mmcheckf((void *) mdp, (void (*)(void)) NULL, 1); - } } /* Add the new heap to the linked list of heaps attached by mmalloc */ diff --git a/src/xbt/mmalloc/mfree.c b/src/xbt/mmalloc/mfree.c index 53e83ce65b..d86b0fefc1 100644 --- a/src/xbt/mmalloc/mfree.c +++ b/src/xbt/mmalloc/mfree.c @@ -187,10 +187,6 @@ void mfree(void *md, void *ptr) break; } } - if (mdp->mfree_hook != NULL) { - mdp->mfree_hook(mdp, ptr); - } else { - __mmalloc_free(mdp, ptr); - } + __mmalloc_free(mdp, ptr); } } diff --git a/src/xbt/mmalloc/mm.c b/src/xbt/mmalloc/mm.c index c312a0adab..c5a79c4d5d 100644 --- a/src/xbt/mmalloc/mm.c +++ b/src/xbt/mmalloc/mm.c @@ -18,7 +18,6 @@ #include "mcalloc.c" #include "mfree.c" #include "mmalloc.c" -#include "mmcheck.c" #include "mmemalign.c" #include "mmstats.c" #include "mrealloc.c" diff --git a/src/xbt/mmalloc/mmalloc.c b/src/xbt/mmalloc/mmalloc.c index 0af8597cfd..fe893b0c25 100644 --- a/src/xbt/mmalloc/mmalloc.c +++ b/src/xbt/mmalloc/mmalloc.c @@ -118,10 +118,6 @@ void *mmalloc(void *md, size_t size) mdp = MD_TO_MDP(md); // printf("(%s) Mallocing %d bytes on %p (default: %p)...",xbt_thread_self_name(),size,mdp,__mmalloc_default_mdp);fflush(stdout); - if (mdp->mmalloc_hook != NULL) { - return mdp->mmalloc_hook(md, size); - } - if (!(mdp->flags & MMALLOC_INITIALIZED)) { if (!initialize(mdp)) { return (NULL); diff --git a/src/xbt/mmalloc/mmcheck.c b/src/xbt/mmalloc/mmcheck.c deleted file mode 100644 index 8e0dc549e3..0000000000 --- a/src/xbt/mmalloc/mmcheck.c +++ /dev/null @@ -1,183 +0,0 @@ -/* Standard debugging hooks for `mmalloc'. - Copyright 1990, 1991, 1992 Free Software Foundation - - Written May 1989 by Mike Haertel. - Heavily modified Mar 1992 by Fred Fish (fnf@cygnus.com) */ - -/* Copyright (c) 2010. The SimGrid Team. - * All rights reserved. */ - -/* This program is free software; you can redistribute it and/or modify it - * under the terms of the license (GNU LGPL) which comes with this package. */ - -#include "mmprivate.h" - -/* Default function to call when something awful happens. The application - can specify an alternate function to be called instead (and probably will - want to). */ - -extern void abort(void); - -/* Arbitrary magical numbers. */ - -#define MAGICWORD (unsigned int) 0xfedabeeb /* Active chunk */ -#define MAGICWORDFREE (unsigned int) 0xdeadbeef /* Inactive chunk */ -#define MAGICBYTE ((char) 0xd7) - -/* Each memory allocation is bounded by a header structure and a trailer - byte. I.E. - - - - The pointer returned to the user points to the first byte in the - user's allocation area. The magic word can be tested to detect - buffer underruns and the magic byte can be tested to detect overruns. */ - -struct hdr { - size_t size; /* Exact size requested by user. */ - unsigned long int magic; /* Magic number to check header integrity. */ -}; - -static void checkhdr(struct mdesc *mdp, const struct hdr *hdr); -static void mfree_check(void *md, void *ptr); -static void *mmalloc_check(void *md, size_t size); -static void *mrealloc_check(void *md, void *ptr, size_t size); - -/* Check the magicword and magicbyte, and if either is corrupted then - call the emergency abort function specified for the heap in use. */ - -static void checkhdr(struct mdesc *mdp, const struct hdr *hdr) -{ - if (hdr->magic != MAGICWORD || - ((char *) &hdr[1])[hdr->size] != MAGICBYTE) { - mdp->abortfunc(); - } -} - -static void mfree_check(void *md, void *ptr) -{ - struct hdr *hdr = ((struct hdr *) ptr) - 1; - struct mdesc *mdp; - - mdp = MD_TO_MDP(md); - checkhdr(mdp, hdr); - hdr->magic = MAGICWORDFREE; - mdp->mfree_hook = NULL; - mfree(md, (void *) hdr); - mdp->mfree_hook = mfree_check; -} - -static void *mmalloc_check(void *md, size_t size) -{ - struct hdr *hdr; - struct mdesc *mdp; - size_t nbytes; - - mdp = MD_TO_MDP(md); - mdp->mmalloc_hook = NULL; - nbytes = sizeof(struct hdr) + size + 1; - hdr = (struct hdr *) mmalloc(md, nbytes); - mdp->mmalloc_hook = mmalloc_check; - if (hdr != NULL) { - hdr->size = size; - hdr->magic = MAGICWORD; - hdr++; - *((char *) hdr + size) = MAGICBYTE; - } - return ((void *) hdr); -} - -static void *mrealloc_check(void *md, void *ptr, size_t size) -{ - struct hdr *hdr = ((struct hdr *) ptr) - 1; - struct mdesc *mdp; - size_t nbytes; - - mdp = MD_TO_MDP(md); - checkhdr(mdp, hdr); - mdp->mfree_hook = NULL; - mdp->mmalloc_hook = NULL; - mdp->mrealloc_hook = NULL; - nbytes = sizeof(struct hdr) + size + 1; - hdr = (struct hdr *) mrealloc(md, (void *) hdr, nbytes); - mdp->mfree_hook = mfree_check; - mdp->mmalloc_hook = mmalloc_check; - mdp->mrealloc_hook = mrealloc_check; - if (hdr != NULL) { - hdr->size = size; - hdr++; - *((char *) hdr + size) = MAGICBYTE; - } - return ((void *) hdr); -} - -/* Turn on default checking for mmalloc/mrealloc/mfree, for the heap specified - by MD. If FUNC is non-NULL, it is a pointer to the function to call - to abort whenever memory corruption is detected. By default, this is the - standard library function abort(). - - Note that we disallow installation of initial checking hooks if mmalloc - has been called at any time for this particular heap, since if any region - that is allocated prior to installation of the hooks is subsequently - reallocated or freed after installation of the hooks, it is guaranteed - to trigger a memory corruption error. We do this by checking the state - of the MMALLOC_INITIALIZED flag. If the FORCE argument is non-zero, this - checking is disabled and it is allowed to install the checking hooks at any - time. This is useful on systems where the C runtime makes one or more - malloc calls before the user code had a chance to call mmcheck or mmcheckf, - but never calls free with these values. Thus if we are certain that only - values obtained from mallocs after an mmcheck/mmcheckf will ever be passed - to free(), we can go ahead and force installation of the useful checking - hooks. - - However, we can call this function at any time after the initial call, - to update the function pointers to the checking routines and to the - user defined corruption handler routine, as long as these function pointers - have been previously extablished by the initial call. Note that we - do this automatically when remapping a previously used heap, to ensure - that the hooks get updated to the correct values, although the corruption - handler pointer gets set back to the default. The application can then - call mmcheck to use a different corruption handler if desired. - - Returns non-zero if checking is successfully enabled, zero otherwise. */ - -int mmcheckf(void *md, void (*func) (void), int force) -{ - struct mdesc *mdp; - int rtnval; - - mdp = MD_TO_MDP(md); - - /* We can safely set or update the abort function at any time, regardless - of whether or not we successfully do anything else. */ - - mdp->abortfunc = (func != NULL ? func : abort); - - /* If we haven't yet called mmalloc the first time for this heap, or if we - have hooks that were previously installed, then allow the hooks to be - initialized or updated. */ - - if (force || - !(mdp->flags & MMALLOC_INITIALIZED) || (mdp->mfree_hook != NULL)) { - mdp->mfree_hook = mfree_check; - mdp->mmalloc_hook = mmalloc_check; - mdp->mrealloc_hook = mrealloc_check; - mdp->flags |= MMALLOC_MMCHECK_USED; - rtnval = 1; - } else { - rtnval = 0; - } - - return (rtnval); -} - -/* This routine is for backwards compatibility only, in case there are - still callers to the original mmcheck function. */ - -int mmcheck(void *md, void (*func) (void)) -{ - int rtnval; - - rtnval = mmcheckf(md, func, 0); - return (rtnval); -} diff --git a/src/xbt/mmalloc/mmprivate.h b/src/xbt/mmalloc/mmprivate.h index a9018c5d98..d3f8442f1b 100644 --- a/src/xbt/mmalloc/mmprivate.h +++ b/src/xbt/mmalloc/mmprivate.h @@ -180,32 +180,6 @@ struct mdesc { needs to be maintained on a per-process basis. */ void *(*morecore) (struct mdesc * mdp, int size); - /* Pointer to the function that causes an abort when the memory checking - features are activated. By default this is set to abort(), but can - be set to another function by the application using mmalloc(). - - FIXME: For mapped regions shared by more than one process, this - needs to be maintained on a per-process basis. */ - void (*abortfunc) (void); - - /* Debugging hook for free. - - FIXME: For mapped regions shared by more than one process, this - needs to be maintained on a per-process basis. */ - void (*mfree_hook) (void *mdp, void *ptr); - - /* Debugging hook for `malloc'. - - FIXME: For mapped regions shared by more than one process, this - needs to be maintained on a per-process basis. */ - void *(*mmalloc_hook) (void *mdp, size_t size); - - /* Debugging hook for realloc. - - FIXME: For mapped regions shared by more than one process, this - needs to be maintained on a per-process basis. */ - void *(*mrealloc_hook) (void *mdp, void *ptr, size_t size); - /* Number of info entries. */ size_t heapsize; diff --git a/src/xbt/mmalloc/mrealloc.c b/src/xbt/mmalloc/mrealloc.c index 7e83e20876..9b0e907eaf 100644 --- a/src/xbt/mmalloc/mrealloc.c +++ b/src/xbt/mmalloc/mrealloc.c @@ -9,6 +9,7 @@ * under the terms of the license (GNU LGPL) which comes with this package. */ #include /* Prototypes for memcpy, memmove, memset, etc */ +#include /* abort */ #include "mmprivate.h" @@ -39,16 +40,12 @@ void *mrealloc(void *md, void *ptr, size_t size) if ((char *) ptr < (char *) mdp->heapbase || BLOCK(ptr) > mdp->heapsize) { printf - ("FIXME. Ouch, this pointer is not mine. I will malloc it instead of reallocing it. (please report this bug)\n"); + ("FIXME. Ouch, this pointer is not mine, refusing to proceed (another solution would be to malloc it instead of reallocing it, see source code)\n"); result = mmalloc(md, size); abort(); return result; } - if (mdp->mrealloc_hook != NULL) { - return mdp->mrealloc_hook(md, ptr, size); - } - block = BLOCK(ptr); type = mdp->heapinfo[block].busy.type; -- 2.20.1