[JNPR] Proposal to add weak symbols for malloc, realloc, and free to libc

Konstantin Belousov kostikbel at gmail.com
Fri Jan 18 05:29:48 UTC 2013


On Thu, Jan 17, 2013 at 11:28:50PM -0500, Steve Kiernan wrote:
> On Fri, 18 Jan 2013 06:09:33 +0200
> Konstantin Belousov <kostikbel at gmail.com> wrote:
> 
> > On Thu, Jan 17, 2013 at 06:46:54PM -0500, Steve Kiernan wrote:
> > > On Thu, 17 Jan 2013 15:42:19 -0800
> > > Jason Evans <jasone at canonware.com> wrote:
> > > 
> > > > On Jan 17, 2013, at 1:13 PM, Steve Kiernan wrote:
> > > > > When libc was changed to use jemalloc, the weak symbols for malloc, realloc, and free ended up being removed.
> > > > > This makes it a bit difficult for an application to replace (or augment) the malloc implementation.
> > > > > 
> > > > > This proposal is to add back the weak symbols similar to how they existed in libc prior to jemalloc introduction.
> > > > > 
> > > > > See the following patch for the changes:
> > > > > http://people.freebsd.org/~marcel/Juniper/weak-malloc.diff
> > > > > 
> > > > > I'm not sure if the the symbols are in the proper place in the Symbol.map file and would welcome comments.
> > > > 
> > > > What about calloc(), posix_memalign(), and malloc_usable_size()?  Similarly, I think the *allocm() functions in -current may need the same treatment.
> > > 
> > > I think you are correct and those would probably be necessary, as well, yes.
> > > 
> > > It looked like previously, calloc was not made weak because it was implemented in terms of malloc, but since that is not the case in jemalloc, it will need to be addressed.
> > > 
> > > I'll update the patch.
> > 
> > New symbols should be added to the current version, which is FBSD_1.3
> > for the 10.0.
> 
> Okay, great. Thank you for the information.
> 
> > That said, what are the difficulties you experiencing with the malloc
> > interposing ? According to the normal ELF symbol lookup rules, the
> > definitions from any object which is loaded before libc overrides the
> > libc symbols.
> 
> The problem is when one want to augment the calls. For example, say
> you want to do some leak detection or keep track of statistics that
> the malloc library does not. One would need to still call the original
> call after replacing the malloc/realloc/free/etc. with their own.
> Without having the __malloc/__realloc/__free/etc. that _used_ to be
> in libc, one cannot do so.

Well, the standard technique is to do
 void *libc_handle = dlopen("libc.so");
 void *(*libc_malloc)(size_t) = dlsym(libc_handle, "malloc");
if you know exactly that you want malloc from libc.

If you want just any previous interposer for the malloc, then the right
technique is to do dlsym(RTLD_NEXT, "malloc") from your malloc wrapper.

Still I think that your patch, with additions noted by Jason, is useful.
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 834 bytes
Desc: not available
URL: <http://lists.freebsd.org/pipermail/freebsd-arch/attachments/20130118/0a76a1aa/attachment.sig>


More information about the freebsd-arch mailing list