A mention of the malloc API helpers

There are plenty of malloc API helper routines. These can be useful when debugging a difficult scenario; it's a good idea to be aware of what's available.

On an Ubuntu Linux system, we check with man for a match to the keyword malloc:

$ man -k malloc
__after_morecore_hook (3) - malloc debugging variables
__free_hook (3) - malloc debugging variables
__malloc_hook (3) - malloc debugging variables
__malloc_initialize_hook (3) - malloc debugging variables
__memalign_hook (3) - malloc debugging variables
__realloc_hook (3) - malloc debugging variables
malloc (3) - allocate and free dynamic memory
malloc_get_state (3) - record and restore state of malloc implementation
malloc_hook (3) - malloc debugging variables
malloc_info (3) - export malloc state to a stream
malloc_set_state (3) - record and restore state of malloc implementation
malloc_stats (3) - print memory allocation statistics
malloc_trim (3) - release free memory from the top of the heap
malloc_usable_size (3) - obtain size of block of memory allocated from heap
mtrace (1) - interpret the malloc trace log
mtrace (3) - malloc tracing
muntrace (3) - malloc tracing
$

Quite a few of these malloc APIs (reminder: the number three within parentheses,(3), implies it's a library routine) deal with the concept of malloc hooks. The essential idea: one can replace the library malloc(3), realloc(3), memalign(3) and free(3) APIs with one's own hook function, which will be invoked when the application calls the API.

However, we will not be delving further into this area; why not? Recent versions of glibc document the fact that these hook functions are:

  • Not MT-Safe (covered in Chapter 16,  Multithreading with Pthreads Part III)
  • Deprecated from glibc ver. 2.24 onward

Finally, it might be obvious, but we would prefer to call this out explicitly: one must realize that using these tools serves a purpose only in testing environments; they are not meant to be used in production! Some studies have revealed security vulnerabilities that can be exploited when running ASan in production; see the Further reading section on the GitHub repository.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset