public inbox for gcc@gcc.gnu.org
 help / color / mirror / Atom feed
From: Florian Weimer <fweimer@redhat.com>
To: Michael Matz <matz@suse.de>
Cc: binutils@sourceware.org,  gcc@gcc.gnu.org,  libc-alpha@sourceware.org
Subject: Re: Counting static __cxa_atexit calls
Date: Wed, 24 Aug 2022 14:06:49 +0200	[thread overview]
Message-ID: <87k06x7sme.fsf@oldenburg.str.redhat.com> (raw)
In-Reply-To: <alpine.LSU.2.20.2208231321300.15960@wotan.suse.de>

* Michael Matz:

> Hello,
>
> On Tue, 23 Aug 2022, Florian Weimer via Gcc wrote:
>
>> We currently have a latent bug in glibc where C++ constructor calls can
>> fail if they have static or thread storage duration and a non-trivial
>> destructor.  The reason is that __cxa_atexit (and
>> __cxa_thread_atexit_impl) may have to allocate memory.  We can avoid
>> that if we know how many such static calls exist in an object (for C++,
>> the compiler will never emit these calls repeatedly in a loop).  Then we
>> can allocate the resources beforehand, either during process and thread
>> start, or when dlopen is called and new objects are loaded.
>
> Isn't this merely moving the failure point from exception-at-ctor to 
> dlopen-fails?

Yes, and that is a soft error that can be handled (likewise for
pthread_create).

> If an individual __cxa_atexit can't allocate memory anymore for its
> list structure, why should pre-allocation (which is still dynamic,
> based on the number of actual atexit calls) have any more luck?

We can report the error properly, and not just terminate the process.

The existing ABI functions are mostly noexcept.  For C++ constructors of
global objects, there cannot even be a handler because they are invoked
by an ELF constructor, and throwing through an ELF constructor is
undefined.

>> What would be the most ELF-flavored way to implement this?  After the
>> final link, I expect that the count (or counts, we need a separate
>> counter for thread-local storage) would show up under a new dynamic tag
>> in the dynamic segment.  This is actually a very good fit because older
>> loaders will just ignore it.  But the question remains what GCC should
>> emit into assembler & object files, so that the link editor can compute
>> the total count from that.
>
> Probably a note section, which the link editor could either transform into 
> a dynamic tag or leave as note(s) in the PT_NOTE segment.  The latter 
> wouldn't require any specific tooling support in the link editor.  But the 
> consumer would have to iterate through all the notes to add the 
> individual counts together.  Might be acceptable, though.

I think we need some level of link editor support to avoid drastically
over-counting multiple static calls that get merged into one
implementation as the result of vague linkage.  Not sure how to express
that at the ELF level?

Thanks,
Florian


  reply	other threads:[~2022-08-24 12:07 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-08-23 11:58 Florian Weimer
2022-08-23 12:28 ` Nick Clifton
2022-08-23 13:40 ` Michael Matz
2022-08-24 12:06   ` Florian Weimer [this message]
2022-08-24 12:53     ` Michael Matz
2022-08-24 14:31       ` Florian Weimer
2022-08-24 15:25         ` Michael Matz

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=87k06x7sme.fsf@oldenburg.str.redhat.com \
    --to=fweimer@redhat.com \
    --cc=binutils@sourceware.org \
    --cc=gcc@gcc.gnu.org \
    --cc=libc-alpha@sourceware.org \
    --cc=matz@suse.de \
    /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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).