diff options
| author | Adhemerval Zanella <adhemerval.zanella@linaro.org> | 2024-07-25 15:41:44 -0300 |
|---|---|---|
| committer | Adhemerval Zanella <adhemerval.zanella@linaro.org> | 2024-07-30 08:54:23 -0300 |
| commit | f6ba993e0cda0ca5554fd47b00e6a87be5fdf05e (patch) | |
| tree | 037d48e49be508168157ed6377136814c2a208bc /stdlib/exit.c | |
| parent | 28f8cee64a3223636d15c78f69432503d8ef1c22 (diff) | |
| download | glibc-f6ba993e0cda0ca5554fd47b00e6a87be5fdf05e.tar.xz glibc-f6ba993e0cda0ca5554fd47b00e6a87be5fdf05e.zip | |
stdlib: Allow concurrent exit (BZ 31997)
Even if C/POSIX standard states that exit is not formally thread-unsafe,
calling it more than once is UB. The glibc already supports
it for the single-thread, and both elf/nodelete2.c and tst-rseq-disable.c
call exit from a DSO destructor (which is called by _dl_fini, registered
at program startup with __cxa_atexit).
However, there are still race issues when it is called more than once
concurrently by multiple threads. A recent Rust PR triggered this
issue [1], which resulted in an Austin Group ask for clarification [2].
Besides it, there is a discussion to make concurrent calling not UB [3],
wtih a defined semantic where any remaining callers block until the first
call to exit has finished (reentrant calls, leaving through longjmp, and
exceptions are still undefined).
For glibc, at least reentrant calls are required to be supported to avoid
changing the current behaviour. This requires locking using a recursive
lock, where any exit called by atexit() handlers resumes at the point of
the current handler (thus avoiding calling the current handle multiple
times).
Checked on x86_64-linux-gnu and aarch64-linux-gnu.
[1] https://github.com/rust-lang/rust/issues/126600
[2] https://austingroupbugs.net/view.php?id=1845
[3] https://www.openwall.com/lists/libc-coord/2024/07/24/4
Reviewed-by: Carlos O'Donell <carlos@redhat.com>
Diffstat (limited to 'stdlib/exit.c')
| -rw-r--r-- | stdlib/exit.c | 8 |
1 files changed, 8 insertions, 0 deletions
diff --git a/stdlib/exit.c b/stdlib/exit.c index 5166c78044..bbaf138806 100644 --- a/stdlib/exit.c +++ b/stdlib/exit.c @@ -132,9 +132,17 @@ __run_exit_handlers (int status, struct exit_function_list **listp, } +/* The lock handles concurrent exit(), even though the C/POSIX standard states + that calling exit() more than once is UB. The recursive lock allows + atexit() handlers or destructors to call exit() itself. In this case, the + handler list execution will resume at the point of the current handler. */ +__libc_lock_define_initialized_recursive (static, __exit_lock) + void exit (int status) { + /* The exit should never return, so there is no need to unlock it. */ + __libc_lock_lock_recursive (__exit_lock); __run_exit_handlers (status, &__exit_funcs, true, true); } libc_hidden_def (exit) |
