glibc/sysdeps
Arjun Shankar 52a103e237 Fix deadlock when pthread_atfork handler calls pthread_atfork or dlclose
In multi-threaded programs, registering via pthread_atfork,
de-registering implicitly via dlclose, or running pthread_atfork
handlers during fork was protected by an internal lock.  This meant
that a pthread_atfork handler attempting to register another handler or
dlclose a dynamically loaded library would lead to a deadlock.

This commit fixes the deadlock in the following way:

During the execution of handlers at fork time, the atfork lock is
released prior to the execution of each handler and taken again upon its
return.  Any handler registrations or de-registrations that occurred
during the execution of the handler are accounted for before proceeding
with further handler execution.

If a handler that hasn't been executed yet gets de-registered by another
handler during fork, it will not be executed.   If a handler gets
registered by another handler during fork, it will not be executed
during that particular fork.

The possibility that handlers may now be registered or deregistered
during handler execution means that identifying the next handler to be
run after a given handler may register/de-register others requires some
bookkeeping.  The fork_handler struct has an additional field, 'id',
which is assigned sequentially during registration.  Thus, handlers are
executed in ascending order of 'id' during 'prepare', and descending
order of 'id' during parent/child handler execution after the fork.

Two tests are included:

* tst-atfork3: Adhemerval Zanella <adhemerval.zanella@linaro.org>
  This test exercises calling dlclose from prepare, parent, and child
  handlers.

* tst-atfork4: This test exercises calling pthread_atfork and dlclose
  from the prepare handler.

[BZ #24595, BZ #27054]

Co-authored-by: Adhemerval Zanella <adhemerval.zanella@linaro.org>
Reviewed-by: Adhemerval Zanella  <adhemerval.zanella@linaro.org>
2022-05-25 11:27:31 +02:00
..
aarch64 Revert "[AArch64][BZ #17711] Fix extern protected data handling" 2022-05-23 13:37:05 -07:00
alpha rtld: Remove DL_ARGV_NOT_RELRO and make _dl_skip_args const 2022-05-17 10:14:03 +01:00
arc rtld: Remove DL_ARGV_NOT_RELRO and make _dl_skip_args const 2022-05-17 10:14:03 +01:00
arm Revert "[ARM][BZ #17711] Fix extern protected data handling" 2022-05-23 13:42:10 -07:00
csky rtld: Remove DL_ARGV_NOT_RELRO and make _dl_skip_args const 2022-05-17 10:14:03 +01:00
generic math: Add math-use-builtins-fabs (BZ#29027) 2022-05-23 17:49:18 -03:00
gnu
hppa
htl
hurd
i386 math: Add math-use-builtins-fabs (BZ#29027) 2022-05-23 17:49:18 -03:00
ia64 math: Add math-use-builtins-fabs (BZ#29027) 2022-05-23 17:49:18 -03:00
ieee754 math: Add math-use-builtins-fabs (BZ#29027) 2022-05-23 17:49:18 -03:00
m68k m68k: Use an autoconf template to produce `preconfigure' 2022-05-13 17:07:23 +01:00
mach linux: Add P_PIDFD 2022-05-17 10:34:36 -03:00
microblaze
mips MIPS: Use an autoconf template to produce `preconfigure' 2022-05-13 17:07:23 +01:00
nios2 rtld: Remove DL_ARGV_NOT_RELRO and make _dl_skip_args const 2022-05-17 10:14:03 +01:00
nptl nptl: Add backoff mechanism to spinlock loop 2022-05-09 14:38:40 -07:00
or1k
posix
powerpc math: Add math-use-builtins-fabs (BZ#29027) 2022-05-23 17:49:18 -03:00
pthread Fix deadlock when pthread_atfork handler calls pthread_atfork or dlclose 2022-05-25 11:27:31 +02:00
riscv RISC-V: Use an autoconf template to produce `preconfigure' 2022-05-13 17:07:23 +01:00
s390 S390: Enable static PIE 2022-05-18 14:31:26 +02:00
sh
sparc math: Add math-use-builtins-fabs (BZ#29027) 2022-05-23 17:49:18 -03:00
unix linux: Add CLONE_NEWTIME from Linux 5.6 to bits/sched.h 2022-05-23 17:49:18 -03:00
wordsize-32
wordsize-64
x86 elf: Optimize _dl_new_hash in dl-new-hash.h 2022-05-23 10:38:40 -05:00
x86_64 math: Add math-use-builtins-fabs (BZ#29027) 2022-05-23 17:49:18 -03:00