From 7a28a3cf1f8df36e30724e8b4021cddde0596118 Mon Sep 17 00:00:00 2001 From: Colin Cross Date: Sun, 7 Feb 2016 22:51:15 -0800 Subject: malloc_debug: reset TrackData mutex after fork Add a pthread_atfork handler to malloc_debug to lock the TrackData mutex during fork and reset it in the child. Ensures that the TrackData is consistent when forking from a multi-threaded process, and that the mutex is in a defined state in the child. Change-Id: I0dfed30045a28551217ceac227d33b2e18894932 --- libc/malloc_debug/malloc_debug.cpp | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) (limited to 'libc/malloc_debug/malloc_debug.cpp') diff --git a/libc/malloc_debug/malloc_debug.cpp b/libc/malloc_debug/malloc_debug.cpp index 4f8657969..85629001f 100644 --- a/libc/malloc_debug/malloc_debug.cpp +++ b/libc/malloc_debug/malloc_debug.cpp @@ -84,6 +84,28 @@ void* debug_valloc(size_t size); __END_DECLS // ------------------------------------------------------------------------ +static void InitAtfork() { + static pthread_once_t atfork_init = PTHREAD_ONCE_INIT; + pthread_once(&atfork_init, [](){ + pthread_atfork( + [](){ + if (g_debug != nullptr) { + g_debug->PrepareFork(); + } + }, + [](){ + if (g_debug != nullptr) { + g_debug->PostForkParent(); + } + }, + [](){ + if (g_debug != nullptr) { + g_debug->PostForkChild(); + } + } + ); + }); +} static void LogTagError(const Header* header, const void* pointer, const char* name) { ScopedDisableDebugCalls disable; @@ -155,6 +177,9 @@ bool debug_initialize(const MallocDispatch* malloc_dispatch, int* malloc_zygote_ if (malloc_zygote_child == nullptr) { return false; } + + InitAtfork(); + g_malloc_zygote_child = malloc_zygote_child; g_dispatch = malloc_dispatch; -- cgit v1.2.3