bmalloc should support system memory analysis tools (part 2)
authorggaren@apple.com <ggaren@apple.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Fri, 12 Dec 2014 00:22:41 +0000 (00:22 +0000)
committerggaren@apple.com <ggaren@apple.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Fri, 12 Dec 2014 00:22:41 +0000 (00:22 +0000)
https://bugs.webkit.org/show_bug.cgi?id=139565

Reviewed by Mark Lam.

This patch actually queries the environment to see if memory analysis
tools have been enabled.

* bmalloc/Deallocator.cpp:
(bmalloc::Deallocator::scavenge): Don't process the object log if
we've disabled bmalloc because it will be full of invalid nullptrs.

* bmalloc/Environment.cpp:
(bmalloc::isMallocEnvironmentVariableSet): Test for the list of known
Malloc debugging flags. I also added a plain "Malloc" catch-all for
when you want to disable bmalloc without enabling any kind of funny
business.

It would be slightly nicer just to iterate the list of environment
variables and strstr them, but getenv is the more portable option,
and performance here doesn't really matter.

(bmalloc::isLibgmallocEnabled): Test for the libgmalloc insertion
environment variable.

(bmalloc::Environment::computeIsBmallocEnabled):

git-svn-id: https://svn.webkit.org/repository/webkit/trunk@177191 268f45cc-cd09-0410-ab3c-d52691b4dbfc

Source/bmalloc/ChangeLog
Source/bmalloc/bmalloc/Allocator.cpp
Source/bmalloc/bmalloc/Deallocator.cpp
Source/bmalloc/bmalloc/Environment.cpp
Source/bmalloc/bmalloc/Range.h

index d8b51e7..49a6e3e 100644 (file)
@@ -1,5 +1,34 @@
 2014-12-11  Geoffrey Garen  <ggaren@apple.com>
 
+        bmalloc should support system memory analysis tools (part 2)
+        https://bugs.webkit.org/show_bug.cgi?id=139565
+
+        Reviewed by Mark Lam.
+
+        This patch actually queries the environment to see if memory analysis
+        tools have been enabled.
+
+        * bmalloc/Deallocator.cpp:
+        (bmalloc::Deallocator::scavenge): Don't process the object log if
+        we've disabled bmalloc because it will be full of invalid nullptrs.
+
+        * bmalloc/Environment.cpp:
+        (bmalloc::isMallocEnvironmentVariableSet): Test for the list of known
+        Malloc debugging flags. I also added a plain "Malloc" catch-all for
+        when you want to disable bmalloc without enabling any kind of funny
+        business.
+
+        It would be slightly nicer just to iterate the list of environment
+        variables and strstr them, but getenv is the more portable option,
+        and performance here doesn't really matter.
+
+        (bmalloc::isLibgmallocEnabled): Test for the libgmalloc insertion
+        environment variable.
+
+        (bmalloc::Environment::computeIsBmallocEnabled):
+
+2014-12-11  Geoffrey Garen  <ggaren@apple.com>
+
         Try to fix the iOS simulator build.
 
         #include the declaration of malloc / free.
index 8aa8d33..5d68ce6 100644 (file)
@@ -30,7 +30,7 @@
 #include "PerProcess.h"
 #include "Sizes.h"
 #include <algorithm>
-#include <stdlib.h>
+#include <cstdlib>
 
 using namespace std;
 
index d7959ec..d7964c4 100644 (file)
@@ -32,7 +32,7 @@
 #include "PerProcess.h"
 #include "SmallChunk.h"
 #include <algorithm>
-#include <stdlib.h>
+#include <cstdlib>
 #include <sys/mman.h>
 
 using namespace std;
@@ -56,7 +56,8 @@ Deallocator::~Deallocator()
     
 void Deallocator::scavenge()
 {
-    processObjectLog();
+    if (m_isBmallocEnabled)
+        processObjectLog();
 }
 
 void Deallocator::deallocateLarge(void* object)
index 4e3536a..184c305 100644 (file)
  */
 
 #include "Environment.h"
+#include <cstdlib>
+#include <cstring>
 
 namespace bmalloc {
 
+static bool isMallocEnvironmentVariableSet()
+{
+    const char* list[] = {
+        "Malloc",
+        "MallocLogFile",
+        "MallocGuardEdges",
+        "MallocDoNotProtectPrelude",
+        "MallocDoNotProtectPostlude",
+        "MallocStackLogging",
+        "MallocStackLoggingNoCompact",
+        "MallocStackLoggingDirectory",
+        "MallocScribble",
+        "MallocCheckHeapStart",
+        "MallocCheckHeapEach",
+        "MallocCheckHeapSleep",
+        "MallocCheckHeapAbort",
+        "MallocErrorAbort",
+        "MallocCorruptionAbort",
+        "MallocHelp"
+    };
+    size_t size = sizeof(list) / sizeof(const char*);
+    
+    for (size_t i = 0; i < size; ++i) {
+        if (getenv(list[i]))
+            return true;
+    }
+
+    return false;
+}
+
+static bool isLibgmallocEnabled()
+{
+    char* variable = getenv("DYLD_INSERT_LIBRARIES");
+    if (!variable)
+        return false;
+    if (!strstr(variable, "libgmalloc"))
+        return false;
+    return true;
+}
+
 Environment::Environment()
     : m_isBmallocEnabled(computeIsBmallocEnabled())
 {
@@ -34,6 +76,10 @@ Environment::Environment()
 
 bool Environment::computeIsBmallocEnabled()
 {
+    if (isMallocEnvironmentVariableSet())
+        return false;
+    if (isLibgmallocEnabled())
+        return false;
     return true;
 }
 
index 83d14da..1ee6411 100644 (file)
@@ -26,7 +26,7 @@
 #ifndef Range_h
 #define Range_h
 
-#include <stddef.h>
+#include <cstddef>
 
 namespace bmalloc {