platform_bionic/libc/bionic/pthread-atfork.c

127 lines
4 KiB
C
Raw Normal View History

/*
* Copyright (C) 2008 The Android Open Source Project
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#include <stdlib.h>
#include <errno.h>
#include <pthread.h>
#include <sys/queue.h>
static pthread_mutex_t handler_mutex = PTHREAD_RECURSIVE_MUTEX_INITIALIZER;
struct atfork_t
{
CIRCLEQ_ENTRY(atfork_t) entries;
void (*prepare)(void);
void (*child)(void);
void (*parent)(void);
};
static CIRCLEQ_HEAD(atfork_head_t, atfork_t) atfork_head = \
CIRCLEQ_HEAD_INITIALIZER(atfork_head);
void __bionic_atfork_run_prepare()
{
struct atfork_t *cursor;
/* We will lock this here, and unlock it in the parent and child functions.
* This ensures that nobody can modify the handler array between the calls
* to the prepare and parent/child handlers.
*
* TODO: If a handler mucks with the list, it could cause problems. Right
* now it's ok because all they can do is add new items to the end
* of the list, but if/when we implement cleanup in dlclose() things
* will get more interesting...
*/
pthread_mutex_lock(&handler_mutex);
/* Call pthread_atfork() prepare handlers. Posix states that the prepare
* handlers should be called in the reverse order of the parent/child
* handlers, so we iterate backwards.
*/
for (cursor = atfork_head.cqh_last;
cursor != (void*)&atfork_head;
cursor = cursor->entries.cqe_prev) {
if (cursor->prepare != NULL) {
cursor->prepare();
}
}
}
void __bionic_atfork_run_child()
{
struct atfork_t *cursor;
Handle pthread-related changes (mutex/atfork) First commit: Revert "Revert "am be741d47: am 2f460fbe: am 73b5cad9: Merge "bionic: Fix wrong kernel_id in pthread descriptor after fork()""" This reverts commit 06823da2f0c8b4a4ce4c45113032f03df85c94b8. Second commit: bionic: fix atfork hanlder_mutex deadlock This cherry-picks commit 34e89c232dd5645fe3b5f9b40856d8e3e4cae57a After applying the kernel_id fix, the system refused to boot up and we got following crash log: I/DEBUG ( 113): pid: 618, tid: 618 >>> org.simalliance.openmobileapi.service:remote <<< I/DEBUG ( 113): signal 16 (SIGSTKFLT), code -6 (?), fault addr -------- I/DEBUG ( 113): eax fffffe00 ebx b77de994 ecx 00000080 edx 00724002 I/DEBUG ( 113): esi 00000000 edi 00004000 I/DEBUG ( 113): xcs 00000073 xds 0000007b xes 0000007b xfs 00000000 xss 0000007b I/DEBUG ( 113): eip b7761351 ebp bfdf3de8 esp bfdf3dc4 flags 00000202 I/DEBUG ( 113): #00 eip: 00015351 /system/lib/libc.so I/DEBUG ( 113): #01 eip: 0000d13c /system/lib/libc.so (pthread_mutex_lock) I/DEBUG ( 113): #02 eip: 00077b48 /system/lib/libc.so (__bionic_atfork_run_prepare) I/DEBUG ( 113): #03 eip: 00052cdb /system/lib/libc.so (fork) I/DEBUG ( 113): #04 eip: 0009ae91 /system/lib/libdvm.so (_Z18dvmOptimizeDexFileillPKcjjb) I/DEBUG ( 113): #05 eip: 000819d6 /system/lib/libdvm.so (_Z14dvmJarFileOpenPKcS0_PP7JarFileb) I/DEBUG ( 113): #06 eip: 000b175e /system/lib/libdvm.so (_ZL40Dalvik_dalvik_system_DexFile_openDexFilePKjP6JValue) I/DEBUG ( 113): #07 eip: 0011fb94 /system/lib/libdvm.so Root cause: The atfork uses the mutex handler_mutex to protect the atfork_head. The parent will call __bionic_atfork_run_prepare() to lock the handler_mutex, and need both the parent and child to unlock their own copy of handler_mutex after fork. At that time, the owner of hanlder_mutex is set as the parent. If we apply the kernel_id fix, then the child's kernel_id will be set as child's tid. The handler_mutex is a recursive lock, and pthread_mutex_unlock(&hander_mutex) will fail because the mutex owner is the parent, while the current tid (__get_thread()->kernel_id) is child, not matched with the mutex owner. At that time, the handler_mutex is left in lock state.If the child wants to fork other process after than, then it will try to lock handler_mutex, and then be deadlocked. Fix: Since the child has its own copy of vm space from the the parent, the child space's handler_mutex should be reset to the initialized state. Change-Id: I3907dd9a153418fb78862f2aa6d0302c375d9e27 Signed-off-by: Jack Ren <jack.ren@intel.com> Signed-off-by: Chenyang Du <chenyang.du@intel.com> Signed-off-by: Bruce Beare <bruce.j.beare@intel.com> Change-Id: Ic8072f366a877443a60fe215f3c00b3df5a259c8
2012-03-27 00:25:19 +02:00
pthread_mutexattr_t attr;
/* Call pthread_atfork() child handlers */
for (cursor = atfork_head.cqh_first;
cursor != (void*)&atfork_head;
cursor = cursor->entries.cqe_next) {
if (cursor->child != NULL) {
cursor->child();
}
}
Handle pthread-related changes (mutex/atfork) First commit: Revert "Revert "am be741d47: am 2f460fbe: am 73b5cad9: Merge "bionic: Fix wrong kernel_id in pthread descriptor after fork()""" This reverts commit 06823da2f0c8b4a4ce4c45113032f03df85c94b8. Second commit: bionic: fix atfork hanlder_mutex deadlock This cherry-picks commit 34e89c232dd5645fe3b5f9b40856d8e3e4cae57a After applying the kernel_id fix, the system refused to boot up and we got following crash log: I/DEBUG ( 113): pid: 618, tid: 618 >>> org.simalliance.openmobileapi.service:remote <<< I/DEBUG ( 113): signal 16 (SIGSTKFLT), code -6 (?), fault addr -------- I/DEBUG ( 113): eax fffffe00 ebx b77de994 ecx 00000080 edx 00724002 I/DEBUG ( 113): esi 00000000 edi 00004000 I/DEBUG ( 113): xcs 00000073 xds 0000007b xes 0000007b xfs 00000000 xss 0000007b I/DEBUG ( 113): eip b7761351 ebp bfdf3de8 esp bfdf3dc4 flags 00000202 I/DEBUG ( 113): #00 eip: 00015351 /system/lib/libc.so I/DEBUG ( 113): #01 eip: 0000d13c /system/lib/libc.so (pthread_mutex_lock) I/DEBUG ( 113): #02 eip: 00077b48 /system/lib/libc.so (__bionic_atfork_run_prepare) I/DEBUG ( 113): #03 eip: 00052cdb /system/lib/libc.so (fork) I/DEBUG ( 113): #04 eip: 0009ae91 /system/lib/libdvm.so (_Z18dvmOptimizeDexFileillPKcjjb) I/DEBUG ( 113): #05 eip: 000819d6 /system/lib/libdvm.so (_Z14dvmJarFileOpenPKcS0_PP7JarFileb) I/DEBUG ( 113): #06 eip: 000b175e /system/lib/libdvm.so (_ZL40Dalvik_dalvik_system_DexFile_openDexFilePKjP6JValue) I/DEBUG ( 113): #07 eip: 0011fb94 /system/lib/libdvm.so Root cause: The atfork uses the mutex handler_mutex to protect the atfork_head. The parent will call __bionic_atfork_run_prepare() to lock the handler_mutex, and need both the parent and child to unlock their own copy of handler_mutex after fork. At that time, the owner of hanlder_mutex is set as the parent. If we apply the kernel_id fix, then the child's kernel_id will be set as child's tid. The handler_mutex is a recursive lock, and pthread_mutex_unlock(&hander_mutex) will fail because the mutex owner is the parent, while the current tid (__get_thread()->kernel_id) is child, not matched with the mutex owner. At that time, the handler_mutex is left in lock state.If the child wants to fork other process after than, then it will try to lock handler_mutex, and then be deadlocked. Fix: Since the child has its own copy of vm space from the the parent, the child space's handler_mutex should be reset to the initialized state. Change-Id: I3907dd9a153418fb78862f2aa6d0302c375d9e27 Signed-off-by: Jack Ren <jack.ren@intel.com> Signed-off-by: Chenyang Du <chenyang.du@intel.com> Signed-off-by: Bruce Beare <bruce.j.beare@intel.com> Change-Id: Ic8072f366a877443a60fe215f3c00b3df5a259c8
2012-03-27 00:25:19 +02:00
pthread_mutexattr_init(&attr);
pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE);
pthread_mutex_init(&handler_mutex, &attr);
}
void __bionic_atfork_run_parent()
{
struct atfork_t *cursor;
/* Call pthread_atfork() parent handlers */
for (cursor = atfork_head.cqh_first;
cursor != (void*)&atfork_head;
cursor = cursor->entries.cqe_next) {
if (cursor->parent != NULL) {
cursor->parent();
}
}
pthread_mutex_unlock(&handler_mutex);
}
int pthread_atfork(void (*prepare)(void), void (*parent)(void), void(*child)(void))
{
struct atfork_t *entry = malloc(sizeof(struct atfork_t));
if (entry == NULL) {
return ENOMEM;
}
entry->prepare = prepare;
entry->parent = parent;
entry->child = child;
pthread_mutex_lock(&handler_mutex);
CIRCLEQ_INSERT_TAIL(&atfork_head, entry, entries);
pthread_mutex_unlock(&handler_mutex);
return 0;
}