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.
Bug: 27208635
(cherry picked from commit 7a28a3cf1f8df36e30724e8b4021cddde0596118)
Change-Id: I84bc67be09b8b767e1cf2f14141e2ae8dc4fb462
diff --git a/libc/malloc_debug/TrackData.h b/libc/malloc_debug/TrackData.h
index 45e6892..dcf0ede 100644
--- a/libc/malloc_debug/TrackData.h
+++ b/libc/malloc_debug/TrackData.h
@@ -58,6 +58,10 @@
void DisplayLeaks(DebugData& debug);
+ void PrepareFork() { pthread_mutex_lock(&mutex_); }
+ void PostForkParent() { pthread_mutex_unlock(&mutex_); }
+ void PostForkChild() { pthread_mutex_init(&mutex_, NULL); }
+
private:
pthread_mutex_t mutex_ = PTHREAD_MUTEX_INITIALIZER;
std::unordered_set<Header*> headers_;