Title: [288340] trunk/Source/bmalloc
Revision
288340
Author
[email protected]
Date
2022-01-20 18:11:43 -0800 (Thu, 20 Jan 2022)

Log Message

Make bmalloc work better with various MallocStackLogging modes
https://bugs.webkit.org/show_bug.cgi?id=235425

Reviewed by Yusuke Suzuki.

This conditionalizes the way system malloc is used depending on the exact value of the
MallocStackLogging environment variable:

- If the mode is "vm" or "vmlite", then we don't need to use system malloc at all, since
  those logging modes only intercept syscalls.
- If the mode is "lite", then we should use the default malloc zone (rather than our custom
  "WebKit Using System Malloc" zone), since the lite mode only intercepts allocations from
  the default zone.

Before this change, the lite mode didn't capture bmalloc/FastMalloc callstacks, which made
it less useful than it should have been.

* bmalloc/DebugHeap.cpp:
(bmalloc::shouldUseDefaultMallocZone):
(bmalloc::DebugHeap::DebugHeap):
* bmalloc/Environment.cpp:
(bmalloc::isMallocEnvironmentVariableImplyingSystemMallocSet):
(bmalloc::Environment::computeIsDebugHeapEnabled):

Modified Paths

Diff

Modified: trunk/Source/bmalloc/ChangeLog (288339 => 288340)


--- trunk/Source/bmalloc/ChangeLog	2022-01-21 01:36:40 UTC (rev 288339)
+++ trunk/Source/bmalloc/ChangeLog	2022-01-21 02:11:43 UTC (rev 288340)
@@ -1,3 +1,29 @@
+2022-01-20  Ben Nham  <[email protected]>
+
+        Make bmalloc work better with various MallocStackLogging modes
+        https://bugs.webkit.org/show_bug.cgi?id=235425
+
+        Reviewed by Yusuke Suzuki.
+
+        This conditionalizes the way system malloc is used depending on the exact value of the
+        MallocStackLogging environment variable:
+
+        - If the mode is "vm" or "vmlite", then we don't need to use system malloc at all, since
+          those logging modes only intercept syscalls.
+        - If the mode is "lite", then we should use the default malloc zone (rather than our custom
+          "WebKit Using System Malloc" zone), since the lite mode only intercepts allocations from
+          the default zone.
+
+        Before this change, the lite mode didn't capture bmalloc/FastMalloc callstacks, which made
+        it less useful than it should have been.
+
+        * bmalloc/DebugHeap.cpp:
+        (bmalloc::shouldUseDefaultMallocZone):
+        (bmalloc::DebugHeap::DebugHeap):
+        * bmalloc/Environment.cpp:
+        (bmalloc::isMallocEnvironmentVariableImplyingSystemMallocSet):
+        (bmalloc::Environment::computeIsDebugHeapEnabled):
+
 2022-01-20  Yusuke Suzuki  <[email protected]>
 
         [libpas] Ensure pas_allocation_callback and pas_deallocation_callback are initialized as NULL

Modified: trunk/Source/bmalloc/bmalloc/DebugHeap.cpp (288339 => 288340)


--- trunk/Source/bmalloc/bmalloc/DebugHeap.cpp	2022-01-21 01:36:40 UTC (rev 288339)
+++ trunk/Source/bmalloc/bmalloc/DebugHeap.cpp	2022-01-21 02:11:43 UTC (rev 288340)
@@ -44,11 +44,27 @@
 
 #if BOS(DARWIN)
 
+static bool shouldUseDefaultMallocZone()
+{
+    if (getenv("DEBUG_HEAP_USE_DEFAULT_ZONE"))
+        return true;
+
+    // The lite logging mode only intercepts allocations from the default zone.
+    const char* mallocStackLogging = getenv("MallocStackLogging");
+    if (mallocStackLogging && !strcmp(mallocStackLogging, "lite"))
+        return true;
+
+    return false;
+}
+
 DebugHeap::DebugHeap(const LockHolder&)
-    : m_zone(malloc_create_zone(0, 0))
+    : m_zone(malloc_default_zone())
     , m_pageSize(vmPageSize())
 {
-    malloc_set_zone_name(m_zone, "WebKit Using System Malloc");
+    if (!shouldUseDefaultMallocZone()) {
+        m_zone = malloc_create_zone(0, 0);
+        malloc_set_zone_name(m_zone, "WebKit Using System Malloc");
+    }
 }
 
 void* DebugHeap::malloc(size_t size, FailureAction action)

Modified: trunk/Source/bmalloc/bmalloc/Environment.cpp (288339 => 288340)


--- trunk/Source/bmalloc/bmalloc/Environment.cpp	2022-01-21 01:36:40 UTC (rev 288339)
+++ trunk/Source/bmalloc/bmalloc/Environment.cpp	2022-01-21 02:11:43 UTC (rev 288340)
@@ -59,7 +59,7 @@
 
 namespace bmalloc {
 
-static bool isMallocEnvironmentVariableSet()
+static bool isMallocEnvironmentVariableImplyingSystemMallocSet()
 {
     const char* list[] = {
         "Malloc",
@@ -67,9 +67,7 @@
         "MallocGuardEdges",
         "MallocDoNotProtectPrelude",
         "MallocDoNotProtectPostlude",
-        "MallocStackLogging",
         "MallocStackLoggingNoCompact",
-        "MallocStackLoggingDirectory",
         "MallocScribble",
         "MallocCheckHeapStart",
         "MallocCheckHeapEach",
@@ -86,6 +84,12 @@
             return true;
     }
 
+    // Use system malloc anytime MallocStackLogging is enabled, except when the "vm" or "vmlite" logging modes are enabled.
+    // Those modes only intercept syscalls rather than mallocs, so they don't necessarily imply the use of system malloc.
+    const char* mallocStackLogging = getenv("MallocStackLogging");
+    if (mallocStackLogging && strncmp(mallocStackLogging, "vm", 2))
+        return true;
+
     return false;
 }
 
@@ -165,7 +169,7 @@
 
 bool Environment::computeIsDebugHeapEnabled()
 {
-    if (isMallocEnvironmentVariableSet())
+    if (isMallocEnvironmentVariableImplyingSystemMallocSet())
         return true;
     if (isLibgmallocEnabled())
         return true;
_______________________________________________
webkit-changes mailing list
[email protected]
https://lists.webkit.org/mailman/listinfo/webkit-changes

Reply via email to