* [musl] Question about the exit()
@ 2024-08-30 13:34 JinCheng Li
2024-08-30 15:07 ` Markus Wichmann
2024-08-30 15:08 ` Rich Felker
0 siblings, 2 replies; 3+ messages in thread
From: JinCheng Li @ 2024-08-30 13:34 UTC (permalink / raw)
To: musl
[-- Attachment #1: Type: text/plain, Size: 1221 bytes --]
Hi
I have one question in exit().
Why is the __libc_exit_fini executed after __funcs_on_exit? If some finalized functions in .fini_array access global variables which is registered by __cxa_atexit and will be release in __funcs_on_exit, we may run into some crash during __libc_exit_fini executaion.
_Noreturn void exit(int code)
{
__funcs_on_exit();
__libc_exit_fini();
__stdio_exit();
_Exit(code);
}
In bionic, I found the fini_array functions may be registered at the last before we execute main function and called firstly in exit(before global variables release) . Its order looks like completely opposite to musl.
__noreturn void __libc_init(void* raw_args,
void (*onexit)(void) __unused,
int (*slingshot)(int, char**, char**),
structors_array_t const * const structors) {
......
if (structors->fini_array) {
__cxa_atexit(__libc_fini,structors->fini_array,nullptr);
}
......
exit(slingshot(args.argc - __libc_shared_globals()->initial_linker_arg_count,
args.argv + __libc_shared_globals()->initial_linker_arg_count,
args.envp));
}
Best
Li
[-- Attachment #2: Type: text/html, Size: 8363 bytes --]
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: [musl] Question about the exit()
2024-08-30 13:34 [musl] Question about the exit() JinCheng Li
@ 2024-08-30 15:07 ` Markus Wichmann
2024-08-30 15:08 ` Rich Felker
1 sibling, 0 replies; 3+ messages in thread
From: Markus Wichmann @ 2024-08-30 15:07 UTC (permalink / raw)
To: musl; +Cc: JinCheng Li
Am Fri, Aug 30, 2024 at 01:34:58PM +0000 schrieb JinCheng Li:
> Hi
>
> I have one question in exit().
> Why is the __libc_exit_fini executed after __funcs_on_exit? If some
> finalized functions in .fini_array access global variables which is
> registered by __cxa_atexit and will be release in __funcs_on_exit, we
> may run into some crash during __libc_exit_fini executaion.
>
POSIX does not require an ordering between atexit handlers and
destructors. Since the order is unspecified, an application requiring a
specific order is broken and must be fixed. Notice that atexit() is the
better mechanism here, since with it you can even select the order in
which the destructors run (they run in reverse order of registration).
With normal destructors, the order is again unspecified.
> In bionic, I found the fini_array functions may be registered at the
> last before we execute main function and called firstly in exit(before
> global variables release) . Its order looks like completely opposite
> to musl.
>
You probably missed the part where __cxa_finalize() calls all handlers
in reverse order. The first handler registered is the last handler
executed. Thus, in bionic as well, the application's atexit handlers run
first, followed by the destructors.
Also, according to enh, android rarely has applications exit. They
typically run until killed. So it is also likely that that code is not
well tested.
Ciao,
Markus
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: [musl] Question about the exit()
2024-08-30 13:34 [musl] Question about the exit() JinCheng Li
2024-08-30 15:07 ` Markus Wichmann
@ 2024-08-30 15:08 ` Rich Felker
1 sibling, 0 replies; 3+ messages in thread
From: Rich Felker @ 2024-08-30 15:08 UTC (permalink / raw)
To: JinCheng Li; +Cc: musl
On Fri, Aug 30, 2024 at 01:34:58PM +0000, JinCheng Li wrote:
> Hi
>
> I have one question in exit().
> Why is the __libc_exit_fini executed after __funcs_on_exit? If some
Because that is the contract.
> finalized functions in .fini_array access global variables which is
> registered by __cxa_atexit and will be release in __funcs_on_exit,
> we may run into some crash during __libc_exit_fini executaion.
Then that's a bug in the atexit handler. It should not be freeing
anything that could later be needed by a destructor or by any thread
that may still be running concurrent with exit. In general, freeing
anything from an atexit handler is a bug. See Rust issue 126600 where
this is discussed in detail:
https://github.com/rust-lang/rust/issues/126600
> In bionic, I found the fini_array functions may be registered at the
> last before we execute main function and called firstly in
> exit(before global variables release) . Its order looks like
> completely opposite to musl.
I don't follow. What do you mean by "registered last"? After what? And
where is this "global variables release" coming from?
Rich
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2024-08-30 15:09 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-08-30 13:34 [musl] Question about the exit() JinCheng Li
2024-08-30 15:07 ` Markus Wichmann
2024-08-30 15:08 ` Rich Felker
Code repositories for project(s) associated with this public inbox
https://git.vuxu.org/mirror/musl/
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 NNTP newsgroup(s).