This is the mail archive of the libc-hacker@cygnus.com mailing list for the glibc project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

Re: Old malloc change -- explanation wanted


Wolfram Gloger <wmglo@dent.med.uni-muenchen.de> writes:

> 	* malloc/malloc.c (malloc_hook_ini): Don't overwrite realloc and
> 	memalign hook.
> 	(realloc_hook_ini): Don't overwrite memalign hook.
> 	(memalign_hook_ini): Don't overwrite malloc and memalign hooks.
> 	Reported by Philippe Troin <phil@fifi.org>.
> 
> Why was/is overwriting the hooks in question a problem ?

It can lead to problems if a program uses the hooks.  Something like this:

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
void *(*save_malloc) (size_t, void *);
void (*save_free) (void *, void *);
void *(*save_realloc) (size_t, void *, void *);

void
install (void)
{
  save_malloc = __malloc_hook;
  save_free = __free_hook;
  save_realloc = __realloc_hook;

  __malloc_hook = ma_malloc;
  ...
}

void *
my_malloc (size_t n, void *caller)
{
  /* do something */
  __malloc_hook = save_malloc;
  if (__malloc_hook != NULL)
    p = (*__malloc_hook) (n, caller);
  else
    p = malloc (n);
  __malloc_hook = my_malloc;
  /* maybe do something else */
  return p;
}
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

If the init hooks would overwrite all hooks the effect of the install
function would be partly undone since the registered functions do not
set and restore all hook values.

Ideally all registered functions would set and restore all hooks.  But
not all code is doing this and who knows, maybe we'll introduce
another hook in future.  So the init functions are very conservative
and change only those hooks which have to be changed.

No great solution but the best available.  Otherwise existing code
breaks.

-- 
---------------.      drepper at gnu.org  ,-.   1325 Chesapeake Terrace
Ulrich Drepper  \    ,-------------------'   \  Sunnyvale, CA 94089 USA
Cygnus Solutions `--' drepper at cygnus.com   `------------------------


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]