Brad Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame^] | 1 | From 6ea962e0946da7564a774b08dd3eda28d64e9e56 Mon Sep 17 00:00:00 2001 |
Brad Bishop | 316dfdd | 2018-06-25 12:45:53 -0400 | [diff] [blame] | 2 | From: Khem Raj <raj.khem@gmail.com> |
| 3 | Date: Sat, 27 Jan 2018 10:08:04 -0800 |
Brad Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame^] | 4 | Subject: [PATCH] Acquire ld.so lock before switching to malloc_atfork |
Brad Bishop | 316dfdd | 2018-06-25 12:45:53 -0400 | [diff] [blame] | 5 | |
| 6 | The patch is from |
| 7 | https://sourceware.org/bugzilla/show_bug.cgi?id=4578 |
| 8 | |
| 9 | If a thread happens to hold dl_load_lock and have r_state set to RT_ADD or |
| 10 | RT_DELETE at the time another thread calls fork(), then the child exit code |
| 11 | from fork (in nptl/sysdeps/unix/sysv/linux/fork.c in our case) re-initializes |
| 12 | dl_load_lock but does not restore r_state to RT_CONSISTENT. If the child |
| 13 | subsequently requires ld.so functionality before calling exec(), then the |
| 14 | assertion will fire. |
| 15 | |
| 16 | The patch acquires dl_load_lock on entry to fork() and releases it on exit |
| 17 | from the parent path. The child path is initialized as currently done. |
| 18 | This is essentially pthreads_atfork, but forced to be first because the |
| 19 | acquisition of dl_load_lock must happen before malloc_atfork is active |
| 20 | to avoid a deadlock. |
| 21 | |
| 22 | The patch has not yet been integrated upstream. |
| 23 | |
| 24 | Upstream-Status: Pending [ Not Author See bugzilla] |
| 25 | |
| 26 | Signed-off-by: Raghunath Lolur <Raghunath.Lolur@kpit.com> |
| 27 | Signed-off-by: Yuanjie Huang <yuanjie.huang@windriver.com> |
| 28 | Signed-off-by: Zhixiong Chi <zhixiong.chi@windriver.com> |
| 29 | Signed-off-by: Khem Raj <raj.khem@gmail.com> |
| 30 | --- |
Brad Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame^] | 31 | sysdeps/nptl/fork.c | 7 +++++++ |
| 32 | 1 file changed, 7 insertions(+) |
Brad Bishop | 316dfdd | 2018-06-25 12:45:53 -0400 | [diff] [blame] | 33 | |
| 34 | diff --git a/sysdeps/nptl/fork.c b/sysdeps/nptl/fork.c |
Brad Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame^] | 35 | index 0f48933ff1..eef3f9669b 100644 |
Brad Bishop | 316dfdd | 2018-06-25 12:45:53 -0400 | [diff] [blame] | 36 | --- a/sysdeps/nptl/fork.c |
| 37 | +++ b/sysdeps/nptl/fork.c |
| 38 | @@ -25,6 +25,7 @@ |
| 39 | #include <tls.h> |
| 40 | #include <hp-timing.h> |
| 41 | #include <ldsodefs.h> |
| 42 | +#include <libc-lock.h> |
| 43 | #include <stdio-lock.h> |
| 44 | #include <atomic.h> |
| 45 | #include <nptl/pthreadP.h> |
Brad Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame^] | 46 | @@ -56,6 +57,9 @@ __libc_fork (void) |
Brad Bishop | 316dfdd | 2018-06-25 12:45:53 -0400 | [diff] [blame] | 47 | bool multiple_threads = THREAD_GETMEM (THREAD_SELF, header.multiple_threads); |
| 48 | |
Brad Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame^] | 49 | __run_fork_handlers (atfork_run_prepare); |
Brad Bishop | 316dfdd | 2018-06-25 12:45:53 -0400 | [diff] [blame] | 50 | + /* grab ld.so lock BEFORE switching to malloc_atfork */ |
| 51 | + __rtld_lock_lock_recursive (GL(dl_load_lock)); |
| 52 | + __rtld_lock_lock_recursive (GL(dl_load_write_lock)); |
Brad Bishop | 316dfdd | 2018-06-25 12:45:53 -0400 | [diff] [blame] | 53 | |
Brad Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame^] | 54 | /* If we are not running multiple threads, we do not have to |
| 55 | preserve lock state. If fork runs from a signal handler, only |
| 56 | @@ -150,6 +154,9 @@ __libc_fork (void) |
| 57 | |
| 58 | /* Run the handlers registered for the parent. */ |
| 59 | __run_fork_handlers (atfork_run_parent); |
Brad Bishop | 316dfdd | 2018-06-25 12:45:53 -0400 | [diff] [blame] | 60 | + /* unlock ld.so last, because we locked it first */ |
| 61 | + __rtld_lock_unlock_recursive (GL(dl_load_write_lock)); |
| 62 | + __rtld_lock_unlock_recursive (GL(dl_load_lock)); |
| 63 | } |
| 64 | |
| 65 | return pid; |