From: Nat! <nat@mulle-kybernetik.com>
To: Florian Weimer <fw@deneb.enyo.de>
Cc: libc-help@sourceware.org
Subject: Re: Problem with atexit and _dl_fini
Date: Sun, 19 May 2019 19:37:00 -0000 [thread overview]
Message-ID: <0a7c2435-43f8-8dfb-83ab-22ceff7ca51c@mulle-kybernetik.com> (raw)
In-Reply-To: <87blzypg5j.fsf@mid.deneb.enyo.de>
On 19.05.19 18:23, Florian Weimer wrote:
> * Nat!:
>
>> So my problem is, that I observe that my atexit calls are not executed
>> in the correct order.
>>
>> i.e. atexit( a); atexit( b);Â should result in b(), a() being called in
>> that order. To quote the man page,
>>
>> "the registered functions are invoked in reverse order".
>>
>>
>> When I register with `atexit` I can see my functions being added
>> properly within `__internal_atexit` in the
>>
>> correct order. Finally after my functions, the elf-loader ? also adds
>> itself there. So it is being called first by
>>
>> `__run_exit_handlers`.
>>
>>
>> Then comes the part where it goes wrong. I registered my two function
>> with `__internal_atexit`, but for some reason
>>
>> `_dl_fini` is calling `__cxa_finalize` and that is calling the wrong
>> function first.
> When atexit is called from a DSO, glibc calls the registered function
> before the DSO is unloaded. This choice was made because after
> unloading, the function pointer becomes invalid.
>
> I haven't checked, but I suspect atexit still works this way even if
> it doesn't have to (because the DSO is never unloaded).
>
I understand, but the behavior is wrong :) The C standard (or the C++
standard for this matter)
http://www.cplusplus.com/reference/cstdlib/atexit/ states that
```
If more than one atexit function has been specified by different calls
to this function, they are all executed in reverse order as a stack
(i.e. the last function specified is the first to be executed at exit).
```
I think its been shown that glibc can violate this C standard, so for me
the argument would be over here already. That one should unwind in the
reverse order is, I assume, not a interesting discussion topic.
Currently atexit as a reliable mechanism is broken.
But I also don't think the way this is currently handled in glibc, can't
be of much use to anyone.
Case 1: a regular exe linked with shared libraries, nothing gets
unloaded at exit, so what's the point ?
Case 2: someone manually unloads a shared library, that contains atexit
code. The bug is either using `atexit` for a shared library that gets
unloaded, or unloading a shared library that contains atexit code. But
it's not really glibcs business IMO.
Case 3: some automatism unloads shared libraries. Then the automatism
should check if atexit code is affected and not unload, because the
shared library is still clearly needed. It's a bug in the automatism.
If one was hellbent on trying to support atexit for unloading shared
libraries, an atexit contained in a shared library should up the
reference count of the shared library during the atexit call and
decrement after the callback has executed.
Ciao
  Nat!
next prev parent reply other threads:[~2019-05-19 19:37 UTC|newest]
Thread overview: 21+ messages / expand[flat|nested] mbox.gz Atom feed top
2019-05-18 21:23 Nat!
2019-05-19 16:23 ` Florian Weimer
2019-05-19 19:37 ` Nat! [this message]
2019-05-21 20:43 ` Adhemerval Zanella
2019-05-22 10:22 ` Nat!
2019-05-22 15:01 ` Adhemerval Zanella
2019-05-22 15:29 ` Nat!
2019-05-22 19:35 ` Adhemerval Zanella
2019-05-29 21:16 ` Nat!
2019-06-09 20:59 ` Nat!
2019-06-10 11:48 ` Adhemerval Zanella
2019-06-10 13:08 ` Nat!
2019-06-10 20:27 ` Adhemerval Zanella
2019-06-11 18:39 ` Adhemerval Zanella
2019-06-11 20:20 ` Nat!
2019-06-11 22:40 ` Nat!
2019-06-12 3:41 ` Carlos O'Donell
2019-06-13 22:53 ` Nat!
2019-06-14 12:29 ` Manfred
2019-06-14 15:14 ` Adhemerval Zanella
2019-06-11 18:53 ` Nat!
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=0a7c2435-43f8-8dfb-83ab-22ceff7ca51c@mulle-kybernetik.com \
--to=nat@mulle-kybernetik.com \
--cc=fw@deneb.enyo.de \
--cc=libc-help@sourceware.org \
/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).