malloc debug: reset FreeTrackData mutex after fork

Lock the FreeTrackData mutex during fork and reset it in the child.
Ensures that the FreeTrackData is consistent when forking from a
multi-threaded process, and that the mutex is in a defined state
in the child.

Test: 89hours MTBF test
Change-Id: I5e5892832a733ea85727ec65abc7094d95a725ef
diff --git a/libc/malloc_debug/DebugData.cpp b/libc/malloc_debug/DebugData.cpp
index d6ca998..76f8fbb 100644
--- a/libc/malloc_debug/DebugData.cpp
+++ b/libc/malloc_debug/DebugData.cpp
@@ -94,16 +94,25 @@
   if (track != nullptr) {
     track->PrepareFork();
   }
+  if (free_track != nullptr) {
+    free_track->PrepareFork();
+  }
 }
 
 void DebugData::PostForkParent() {
   if (track != nullptr) {
     track->PostForkParent();
   }
+  if (free_track != nullptr) {
+    free_track->PostForkParent();
+  }
 }
 
 void DebugData::PostForkChild() {
   if (track != nullptr) {
     track->PostForkChild();
   }
+  if (free_track != nullptr) {
+    free_track->PostForkChild();
+  }
 }
diff --git a/libc/malloc_debug/FreeTrackData.h b/libc/malloc_debug/FreeTrackData.h
index 1758ef5..0e8c177 100644
--- a/libc/malloc_debug/FreeTrackData.h
+++ b/libc/malloc_debug/FreeTrackData.h
@@ -57,6 +57,12 @@
 
   void LogBacktrace(const Header* header);
 
+  void PrepareFork() { pthread_mutex_lock(&mutex_); }
+
+  void PostForkParent() { pthread_mutex_unlock(&mutex_); }
+
+  void PostForkChild() { pthread_mutex_init(&mutex_, NULL); }
+
  private:
   void LogFreeError(const Header* header, const uint8_t* pointer);
   void VerifyAndFree(const Header* header);