From 167390f05564e0a4d3fcb4329377fd7743267560 Mon Sep 17 00:00:00 2001 From: Rich Felker Date: Wed, 11 Nov 2020 13:37:33 -0500 Subject: lift child restrictions after multi-threaded fork as the outcome of Austin Group tracker issue #62, future editions of POSIX have dropped the requirement that fork be AS-safe. this allows but does not require implementations to synchronize fork with internal locks and give forked children of multithreaded parents a partly or fully unrestricted execution environment where they can continue to use the standard library (per POSIX, they can only portably use AS-safe functions). up until recently, taking this allowance did not seem desirable. however, commit 8ed2bd8bfcb4ea6448afb55a941f4b5b2b0398c0 exposed the extent to which applications and libraries are depending on the ability to use malloc and other non-AS-safe interfaces in MT-forked children, by converting latent very-low-probability catastrophic state corruption into predictable deadlock. dealing with the fallout has been a huge burden for users/distros. while it looks like most of the non-portable usage in applications could be fixed given sufficient effort, at least some of it seems to occur in language runtimes which are exposing the ability to run unrestricted code in the child as part of the contract with the programmer. any attempt at fixing such contracts is not just a technical problem but a social one, and is probably not tractable. this patch extends the fork function to take locks for all libc singletons in the parent, and release or reset those locks in the child, so that when the underlying fork operation takes place, the state protected by these locks is consistent and ready for the child to use. locking is skipped in the case where the parent is single-threaded so as not to interfere with legacy AS-safety property of fork in single-threaded programs. lock order is mostly arbitrary, but the malloc locks (including bump allocator in case it's used) must be taken after the locks on any subsystems that might use malloc, and non-AS-safe locks cannot be taken while the thread list lock is held, imposing a requirement that it be taken last. --- src/locale/dcngettext.c | 5 ++++- src/locale/locale_map.c | 5 ++++- 2 files changed, 8 insertions(+), 2 deletions(-) (limited to 'src/locale') diff --git a/src/locale/dcngettext.c b/src/locale/dcngettext.c index 39a98e83..d1e6c6d1 100644 --- a/src/locale/dcngettext.c +++ b/src/locale/dcngettext.c @@ -10,6 +10,7 @@ #include "atomic.h" #include "pleval.h" #include "lock.h" +#include "fork_impl.h" #define malloc __libc_malloc #define calloc __libc_calloc @@ -39,9 +40,11 @@ static char *gettextdir(const char *domainname, size_t *dirlen) return 0; } +static volatile int lock[1]; +volatile int *const __gettext_lockptr = lock; + char *bindtextdomain(const char *domainname, const char *dirname) { - static volatile int lock[1]; struct binding *p, *q; if (!domainname) return 0; diff --git a/src/locale/locale_map.c b/src/locale/locale_map.c index 94f1b04e..fa51f2e3 100644 --- a/src/locale/locale_map.c +++ b/src/locale/locale_map.c @@ -5,6 +5,7 @@ #include "locale_impl.h" #include "libc.h" #include "lock.h" +#include "fork_impl.h" #define malloc __libc_malloc #define calloc undef @@ -27,9 +28,11 @@ static const char envvars[][12] = { "LC_MESSAGES", }; +static volatile int lock[1]; +volatile int *const __locale_lockptr = lock; + const struct __locale_map *__get_locale(int cat, const char *val) { - static volatile int lock[1]; static void *volatile loc_head; const struct __locale_map *p; struct __locale_map *new = 0; -- cgit 1.4.1