blob: 9ec234bd51b06713e5adb600a11912f35af477d9 [file] [log] [blame]
Brad Bishop316dfdd2018-06-25 12:45:53 -04001From 94225ab4bcc1613531558a632270b5edce779bc9 Mon Sep 17 00:00:00 2001
2From: Khem Raj <raj.khem@gmail.com>
3Date: Sat, 27 Jan 2018 10:08:04 -0800
4Subject: [PATCH 27/27] Acquire ld.so lock before switching to malloc_atfork
5
6The patch is from
7 https://sourceware.org/bugzilla/show_bug.cgi?id=4578
8
9If a thread happens to hold dl_load_lock and have r_state set to RT_ADD or
10RT_DELETE at the time another thread calls fork(), then the child exit code
11from fork (in nptl/sysdeps/unix/sysv/linux/fork.c in our case) re-initializes
12dl_load_lock but does not restore r_state to RT_CONSISTENT. If the child
13subsequently requires ld.so functionality before calling exec(), then the
14assertion will fire.
15
16The patch acquires dl_load_lock on entry to fork() and releases it on exit
17from the parent path. The child path is initialized as currently done.
18This is essentially pthreads_atfork, but forced to be first because the
19acquisition of dl_load_lock must happen before malloc_atfork is active
20to avoid a deadlock.
21
22The patch has not yet been integrated upstream.
23
24Upstream-Status: Pending [ Not Author See bugzilla]
25
26Signed-off-by: Raghunath Lolur <Raghunath.Lolur@kpit.com>
27Signed-off-by: Yuanjie Huang <yuanjie.huang@windriver.com>
28Signed-off-by: Zhixiong Chi <zhixiong.chi@windriver.com>
29Signed-off-by: Khem Raj <raj.khem@gmail.com>
30---
31 sysdeps/nptl/fork.c | 9 +++++++++
32 1 file changed, 9 insertions(+)
33
34diff --git a/sysdeps/nptl/fork.c b/sysdeps/nptl/fork.c
35index f87506f398..225e7b51f8 100644
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>
46@@ -60,6 +61,10 @@ __libc_fork (void)
47 but our current fork implementation is not. */
48 bool multiple_threads = THREAD_GETMEM (THREAD_SELF, header.multiple_threads);
49
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));
53+
54 /* Run all the registered preparation handlers. In reverse order.
55 While doing this we build up a list of all the entries. */
56 struct fork_handler *runp;
57@@ -246,6 +251,10 @@ __libc_fork (void)
58
59 allp = allp->next;
60 }
61+
62+ /* unlock ld.so last, because we locked it first */
63+ __rtld_lock_unlock_recursive (GL(dl_load_write_lock));
64+ __rtld_lock_unlock_recursive (GL(dl_load_lock));
65 }
66
67 return pid;
68--
692.16.1
70