Init fork handler after __libc_init_malloc.

pthread_atfork may call malloc() during its once-init. This causes
problems with allocators (GWP-ASan) that require explicit initialisation
before calls to malloc().

Bug: 135634846
Test: atest bionic
Change-Id: I1810a00465db99d5aa34fa6f74dea5908a628d3a
This commit is contained in:
Mitch Phillips 2019-11-14 16:02:09 -08:00
parent 6dc945babd
commit 1d2aadc036
4 changed files with 12 additions and 3 deletions

View file

@ -101,13 +101,15 @@ void __libc_init_common() {
__libc_add_main_thread();
// Register atfork handlers to take and release the arc4random lock.
pthread_atfork(arc4random_fork_handler, _thread_arc4_unlock, _thread_arc4_unlock);
__system_properties_init(); // Requires 'environ'.
__libc_init_fdsan(); // Requires system properties (for debug.fdsan).
}
void __libc_init_fork_handler() {
// Register atfork handlers to take and release the arc4random lock.
pthread_atfork(arc4random_fork_handler, _thread_arc4_unlock, _thread_arc4_unlock);
}
__noreturn static void __early_abort(int line) {
// We can't write to stdout or stderr because we're aborting before we've checked that
// it's safe for us to use those file descriptors. We probably can't strace either, so

View file

@ -56,4 +56,8 @@ __LIBC_HIDDEN__ void __libc_init_common();
__LIBC_HIDDEN__ void __libc_init_AT_SECURE(char** envp);
// The fork handler must be initialised after __libc_init_malloc, as
// pthread_atfork may call malloc() during its once-init.
__LIBC_HIDDEN__ void __libc_init_fork_handler();
#endif

View file

@ -99,6 +99,8 @@ static void __libc_preinit_impl() {
// Hooks for various libraries to let them know that we're starting up.
__libc_globals.mutate(__libc_init_malloc);
__libc_init_fork_handler();
#if __has_feature(hwaddress_sanitizer)
// Notify the HWASan runtime library whenever a library is loaded or unloaded
// so that it can update its shadow memory.

View file

@ -181,6 +181,7 @@ __noreturn static void __real_libc_init(void *raw_args,
layout_static_tls(args);
__libc_init_main_thread_final();
__libc_init_common();
__libc_init_fork_handler();
call_ifunc_resolvers();
apply_gnu_relro();