[PATCH] D129385: [lsan][Darwin] Scan libdispatch heap

Leonard Grey via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Fri Jul 8 10:48:25 PDT 2022


lgrey created this revision.
lgrey added a reviewer: fjricci.
Herald added a subscriber: Enna1.
Herald added a project: All.
lgrey requested review of this revision.
Herald added a project: Sanitizers.
Herald added a subscriber: Sanitizers.

libdispatch uses its own heap (`_dispatch_main_heap`) for some allocations, including the `dispatch_continuation_t` that holds a dispatch source's event handler.

Scan this region so that these handlers aren't reported as leaked.

(I confirmed that VM_MEMORY_LIBDISPATCH was added in the same Darwin release as VM_MEMORY_OS_ALLOC_ONCE)


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D129385

Files:
  compiler-rt/lib/lsan/lsan_common_mac.cpp


Index: compiler-rt/lib/lsan/lsan_common_mac.cpp
===================================================================
--- compiler-rt/lib/lsan/lsan_common_mac.cpp
+++ compiler-rt/lib/lsan/lsan_common_mac.cpp
@@ -27,8 +27,10 @@
 // Only introduced in Mac OS X 10.9.
 #ifdef VM_MEMORY_OS_ALLOC_ONCE
 static const int kSanitizerVmMemoryOsAllocOnce = VM_MEMORY_OS_ALLOC_ONCE;
+static const int kSanitizerVmMemoryLibdispatch = VM_MEMORY_LIBDISPATCH;
 #else
 static const int kSanitizerVmMemoryOsAllocOnce = 73;
+static const int kSanitizerVmMemoryLibdispatch = 74;
 #endif
 
 namespace __lsan {
@@ -148,6 +150,8 @@
 
   InternalMmapVectorNoCtor<RootRegion> const *root_regions = GetRootRegions();
 
+  bool saw_alloc_once = false;
+  bool saw_libdispatch = false;
   while (err == KERN_SUCCESS) {
     vm_size_t size = 0;
     unsigned depth = 1;
@@ -157,19 +161,26 @@
                                (vm_region_info_t)&info, &count);
 
     uptr end_address = address + size;
-
-    // libxpc stashes some pointers in the Kernel Alloc Once page,
-    // make sure not to report those as leaks.
+    bool scan_region = false;
     if (info.user_tag == kSanitizerVmMemoryOsAllocOnce) {
+      scan_region = true;
+      saw_alloc_once = true;
+    } else if (info.user_tag == kSanitizerVmMemoryLibdispatch) {
+      scan_region = true;
+      saw_libdispatch = true;
+    }
+
+    if (scan_region) {
       ScanRangeForPointers(address, end_address, frontier, "GLOBAL",
                            kReachable);
-
-      // Recursing over the full memory map is very slow, break out
-      // early if we don't need the full iteration.
-      if (!flags()->use_root_regions || !root_regions->size())
-        break;
     }
 
+    // Recursing over the full memory map is very slow, break out
+    // early if we don't need the full iteration.
+    if (saw_alloc_once && saw_libdispatch &&
+        (flags()->use_root_regions || !root_regions->size()))
+      break;
+
     // This additional root region scan is required on Darwin in order to
     // detect root regions contained within mmap'd memory regions, because
     // the Darwin implementation of sanitizer_procmaps traverses images


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D129385.443287.patch
Type: text/x-patch
Size: 2183 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20220708/b82fa8c1/attachment.bin>


More information about the llvm-commits mailing list