Merge "[malloc dispatch] Install dispatch over the top of GWP-ASan."
diff --git a/libc/bionic/malloc_common_dynamic.cpp b/libc/bionic/malloc_common_dynamic.cpp
index 3ab7d1e..d0aa10b 100644
--- a/libc/bionic/malloc_common_dynamic.cpp
+++ b/libc/bionic/malloc_common_dynamic.cpp
@@ -344,7 +344,7 @@
   // Do a pointer swap so that all of the functions become valid at once to
   // avoid any initialization order problems.
   atomic_store(&globals->default_dispatch_table, &globals->malloc_dispatch_table);
-  if (GetDispatchTable() == nullptr) {
+  if (!MallocLimitInstalled()) {
     atomic_store(&globals->current_dispatch_table, &globals->malloc_dispatch_table);
   }
 
diff --git a/libc/bionic/malloc_heapprofd.cpp b/libc/bionic/malloc_heapprofd.cpp
index 3d2dc2b..bf4c63a 100644
--- a/libc/bionic/malloc_heapprofd.cpp
+++ b/libc/bionic/malloc_heapprofd.cpp
@@ -45,6 +45,7 @@
 #include "malloc_common.h"
 #include "malloc_common_dynamic.h"
 #include "malloc_heapprofd.h"
+#include "malloc_limit.h"
 
 static constexpr char kHeapprofdSharedLib[] = "heapprofd_client.so";
 static constexpr char kHeapprofdPrefix[] = "heapprofd";
@@ -189,7 +190,7 @@
       __libc_globals.mutate([](libc_globals* globals) {
         atomic_store(&globals->default_dispatch_table, &__heapprofd_init_dispatch);
         auto dispatch_table = GetDispatchTable();
-        if (dispatch_table == nullptr || dispatch_table == &globals->malloc_dispatch_table) {
+        if (!MallocLimitInstalled() || dispatch_table == &globals->malloc_dispatch_table) {
           atomic_store(&globals->current_dispatch_table, &__heapprofd_init_dispatch);
         }
       });
diff --git a/libc/bionic/malloc_limit.cpp b/libc/bionic/malloc_limit.cpp
index ebc33ab..1405a39 100644
--- a/libc/bionic/malloc_limit.cpp
+++ b/libc/bionic/malloc_limit.cpp
@@ -253,6 +253,10 @@
 }
 #endif
 
+bool MallocLimitInstalled() {
+  return GetDispatchTable() == &__limit_dispatch;
+}
+
 #if defined(LIBC_STATIC)
 static bool EnableLimitDispatchTable() {
   // This is the only valid way to modify the dispatch tables for a
diff --git a/libc/bionic/malloc_limit.h b/libc/bionic/malloc_limit.h
index 282598f..b638e83 100644
--- a/libc/bionic/malloc_limit.h
+++ b/libc/bionic/malloc_limit.h
@@ -32,3 +32,7 @@
 
 // Function prototypes.
 bool LimitEnable(void* arg, size_t arg_size);
+
+// Returns true if malloc_limit is installed (by checking the current dispatch
+// table).
+bool MallocLimitInstalled();