[lttng-dev] [PATCH lttng-ust] Add trace support for calloc and realloc.

Alexander Monakov amonakov at ispras.ru
Thu Aug 1 13:08:27 EDT 2013


Okay, one more hack :)

Recurse *once* by calling dlsym inside your calloc, and terminate recursion by
obtaining memory from mmap.

dlsym calls your calloc
  your calloc calls dlsym
    dlsym calls your calloc, again, as it still needs to initialize
      your calloc obtains fresh memory from mmap, clears it and returns
    dlsym stores pointer to mmap'ed buffer, does its thing and returns
  your calloc now has a pointer to the real calloc, uses it, and returns
dlsym stores pointer to calloc'ed buffer, overwriting the pointer to mmap'ed
buffer, completing initialization for the second time and making the
subsequent free() safe.

You can even munmap the mmap'ed buffer in the outer calloc, avoiding leaks,
but you'd have to stash it in a thread-local storage.  You need a TLS flag to
detect recursion in calloc anyhow.

Instead of mmap you can use some other mechanism such as a static buffer, but
still you need to make it big enough, and thread-safe.

Alexander



More information about the lttng-dev mailing list