All of lore.kernel.org
 help / color / mirror / Atom feed
From: "Jérémie Galarneau" <jeremie.galarneau@efficios.com>
To: Stefan Seefeld <stefan_seefeld@mentor.com>
Cc: lttng-dev@lists.lttng.org
Subject: Re: [PATCH lttng-ust] Add trace support for calloc and realloc.
Date: Tue, 30 Jul 2013 22:14:02 -0400	[thread overview]
Message-ID: <CA+jJMxtMH5Ajgoszg8pO8dq-KsrTg9NwS2YcYOz=FbLr0ijaEQ__38822.724906686$1375236902$gmane$org@mail.gmail.com> (raw)
In-Reply-To: <51F82C45.6020502@mentor.com>

On Tue, Jul 30, 2013 at 5:12 PM, Stefan Seefeld
<stefan_seefeld@mentor.com> wrote:
> Hi Jérémie,
>
> thanks for the quick review. I agree with the fragility of the calloc()
> hack. However, we aren't the only ones hitting this circular dependency
> (as google confirms).
> Some more investigation reveals that dlsym() is in fact prepared for
> calloc() to return NULL (in which case an internal static buffer is
> used. And yes, it appears to check for that case during cleanup.)
>
> Thus I have updated the patch, addressing all of the issues you raise,
> and replaced the "dummy_malloc" business by a simple dummy_calloc()
> returning NULL. This appears to be working fine.
>

I have no doubt it works fine with a simple program, but this comment
in glibc/dlfcn/dlerror.c:143 worries me:

/* ... call to calloc ... */
if (result == NULL) {
/* We are out of memory.  Since this is no really critical
   situation we carry on by using the global variable.
   This might lead to conflicts between the threads but
   they soon all will have memory problems. */

> Meanwhile, I'd like to understand your suggestion, so let me follow up
> on that, too:
>
> On 07/29/2013 07:19 PM, Jérémie Galarneau wrote:
>
>> I may be missing something, but since this structure is allocated only
>> once per thread, subsequent calls from the same thread to dlsym()
>> should not result in another call to calloc(). That should make it
>> possible to call malloc() in dummy_calloc() without causing the
>> recursion.
>
> You are right. But the problem is that, if I call malloc() in
> dummy_calloc(), I indirectly end up calling dlsym() from within dlsym()...
>

You're right. I was thinking __dlerror_run() would have initialized
its dl_action_result structure by the point dlsym() was called a
second time... but it's not the case, hence the recursion... Oops!

>> Perhaps we could also forego this mechanism completely and initialize
>> the symbols from a constructor since this library is meant to be
>> LD_PRELOAD-ed?
>
> I don't quite understand what you are suggesting. dlsym() still calls
> calloc(), so our wrapper still needs to check whether initialization has
> already been completed, and if not, use a fallback.
>
> The code may be reorganized, but that doesn't seem to make it any simpler.
>

Indeed, I was basing this on the same false assumption.

The only other solution I can propose is calling __libc_calloc, which
is exported by glibc, instead of using dlsym to find calloc(). That
somehow appears cleaner to me, but I'm not sure how to handle building
against other implementations of libc...

Anyway, Mathieu will undoubtedly have a strong opinion on this matter ;-)
You may want to wait until he steps-in before continuing to work on this patch.

Regards,
Jérémie

> Regards,
>         Stefan
>
> --
> Stefan Seefeld
> CodeSourcery / Mentor Graphics
> http://www.mentor.com/embedded-software/



-- 
Jérémie Galarneau
EfficiOS Inc.
http://www.efficios.com

  parent reply	other threads:[~2013-07-31  2:14 UTC|newest]

Thread overview: 22+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <51F3585E.20705@mentor.com>
2013-07-29 23:19 ` [PATCH lttng-ust] Add trace support for calloc and realloc Jérémie Galarneau
     [not found] ` <CA+jJMxtn-DsBjzunLNE3_7KS4RCqAFLJfPL7fVkH3z62r0jPxw@mail.gmail.com>
2013-07-30 21:12   ` Stefan Seefeld
     [not found]   ` <51F82C45.6020502@mentor.com>
2013-07-31  2:14     ` Jérémie Galarneau [this message]
     [not found]     ` <CA+jJMxtMH5Ajgoszg8pO8dq-KsrTg9NwS2YcYOz=FbLr0ijaEQ@mail.gmail.com>
2013-08-01 16:26       ` Alexander Monakov
     [not found]       ` <alpine.LNX.2.00.1308012013380.9574@monopod.intra.ispras.ru>
2013-08-01 16:32         ` Stefan Seefeld
     [not found]         ` <51FA8DAE.3070409@mentor.com>
2013-08-01 17:08           ` Alexander Monakov
     [not found]           ` <alpine.LNX.2.00.1308012045570.9574@monopod.intra.ispras.ru>
2013-08-01 17:19             ` Alexander Monakov
     [not found]             ` <alpine.LNX.2.00.1308012115300.9574@monopod.intra.ispras.ru>
2013-08-01 17:28               ` Stefan Seefeld
     [not found]               ` <51FA9AD8.4010502@mentor.com>
2013-08-01 17:57                 ` Alexander Monakov
     [not found]                 ` <CABtfrpDg_NOi+yP5GABmjBv6G9Zj+rvTwc1OdQ1s_9DM7uzeZg@mail.gmail.com>
2013-08-03  1:18                   ` Mathieu Desnoyers
     [not found]                   ` <20130803011841.GE9033@Krystal>
2013-08-05 14:57                     ` Stefan Seefeld
2013-08-05 15:29                     ` Stefan Seefeld
     [not found]                     ` <51FFBD46.5030105@mentor.com>
2013-08-07  1:42                       ` Mathieu Desnoyers
     [not found]                       ` <20130807014241.GH19407@Krystal>
2013-08-07 13:37                         ` Stefan Seefeld
     [not found]                         ` <52024D88.8090409@seefeld.name>
2013-08-07 13:57                           ` Mathieu Desnoyers
     [not found]                           ` <20130807135757.GA32387@Krystal>
2013-08-07 14:01                             ` Stefan Seefeld
     [not found]                     ` <51FFC4DB.7080905@mentor.com>
2013-08-07 14:47                       ` Stefan Seefeld
     [not found]                       ` <52025E03.7090201@mentor.com>
2013-08-07 15:50                         ` Mathieu Desnoyers
     [not found]                         ` <20130807155057.GB1035@Krystal>
2013-08-07 15:56                           ` Stefan Seefeld
2013-08-07 17:07                           ` Stefan Seefeld
     [not found]                           ` <52027EE6.60704@mentor.com>
2013-08-07 18:35                             ` Mathieu Desnoyers
2013-07-27  5:19 Stefan Seefeld

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to='CA+jJMxtMH5Ajgoszg8pO8dq-KsrTg9NwS2YcYOz=FbLr0ijaEQ__38822.724906686$1375236902$gmane$org@mail.gmail.com' \
    --to=jeremie.galarneau@efficios.com \
    --cc=lttng-dev@lists.lttng.org \
    --cc=stefan_seefeld@mentor.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.