Do not dlclose after failed reinit.
Update stale comment.
The reinitialization logic is tested in HeapprofdEndToEnd::ReInit in https://android.googlesource.com/platform/external/perfetto/+/master/src/profiling/memory/heapprofd_end_to_end_test.cc
Change-Id: Id496ee02e208d4f4cea7129b47ef327fb2bb67f2
diff --git a/libc/bionic/malloc_common.cpp b/libc/bionic/malloc_common.cpp
index fc65e15..50c2fec 100644
--- a/libc/bionic/malloc_common.cpp
+++ b/libc/bionic/malloc_common.cpp
@@ -604,9 +604,8 @@
return impl_handle;
}
-// A function pointer to heapprofds init function. Used to re-initialize
-// heapprofd. This will start a new profiling session and tear down the old
-// one in case it is still active.
+// The handle returned by dlopen when previously loading the heapprofd
+// hooks. nullptr if they had not been loaded before.
static _Atomic (void*) g_heapprofd_handle = nullptr;
static void install_hooks(libc_globals* globals, const char* options,
@@ -614,7 +613,8 @@
MallocDispatch dispatch_table;
void* impl_handle = atomic_load(&g_heapprofd_handle);
- if (impl_handle != nullptr) {
+ bool reusing_handle = impl_handle != nullptr;
+ if (reusing_handle) {
if (!InitSharedLibrary(impl_handle, shared_lib, prefix, &dispatch_table)) {
return;
}
@@ -627,7 +627,11 @@
init_func_t init_func = reinterpret_cast<init_func_t>(g_functions[FUNC_INITIALIZE]);
if (!init_func(&__libc_malloc_default_dispatch, &gMallocLeakZygoteChild, options)) {
error_log("%s: failed to enable malloc %s", getprogname(), prefix);
- dlclose(impl_handle);
+ if (!reusing_handle) {
+ // We should not close if we are re-using an old handle, as we cannot be
+ // sure other threads are not currently in the hooks.
+ dlclose(impl_handle);
+ }
ClearGlobalFunctions();
return;
}