[compiler-rt] r305695 - Add lsan interceptors for libdispatch functions on darwin

Francis Ricci via llvm-commits llvm-commits at lists.llvm.org
Mon Jun 19 07:09:11 PDT 2017


Author: fjricci
Date: Mon Jun 19 09:09:10 2017
New Revision: 305695

URL: http://llvm.org/viewvc/llvm-project?rev=305695&view=rev
Log:
Add lsan interceptors for libdispatch functions on darwin

Summary:
This is required for standalone LSan to work with libdispatch worker threads,
and is a slimmed down version of the functionality provided for ASan
in asan_mac.cc.

Reviewers: alekseyshl, kubamracek, glider, kcc

Subscribers: mgorny, llvm-commits

Differential Revision: https://reviews.llvm.org/D34247

Added:
    compiler-rt/trunk/lib/lsan/lsan_mac.cc
    compiler-rt/trunk/test/lsan/TestCases/Darwin/
    compiler-rt/trunk/test/lsan/TestCases/Darwin/dispatch.mm
    compiler-rt/trunk/test/lsan/TestCases/Darwin/lit.local.cfg
Modified:
    compiler-rt/trunk/lib/lsan/CMakeLists.txt
    compiler-rt/trunk/lib/lsan/lsan.h
    compiler-rt/trunk/lib/lsan/lsan_common_mac.cc
    compiler-rt/trunk/lib/lsan/lsan_thread.cc
    compiler-rt/trunk/lib/lsan/lsan_thread.h
    compiler-rt/trunk/test/lsan/lit.common.cfg

Modified: compiler-rt/trunk/lib/lsan/CMakeLists.txt
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/lsan/CMakeLists.txt?rev=305695&r1=305694&r2=305695&view=diff
==============================================================================
--- compiler-rt/trunk/lib/lsan/CMakeLists.txt (original)
+++ compiler-rt/trunk/lib/lsan/CMakeLists.txt Mon Jun 19 09:09:10 2017
@@ -13,6 +13,7 @@ set(LSAN_SOURCES
   lsan_allocator.cc
   lsan_linux.cc
   lsan_interceptors.cc
+  lsan_mac.cc
   lsan_malloc_mac.cc
   lsan_preinit.cc
   lsan_thread.cc)

Modified: compiler-rt/trunk/lib/lsan/lsan.h
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/lsan/lsan.h?rev=305695&r1=305694&r2=305695&view=diff
==============================================================================
--- compiler-rt/trunk/lib/lsan/lsan.h (original)
+++ compiler-rt/trunk/lib/lsan/lsan.h Mon Jun 19 09:09:10 2017
@@ -38,6 +38,8 @@
   GET_STACK_TRACE(__sanitizer::common_flags()->malloc_context_size, \
                   common_flags()->fast_unwind_on_malloc)
 
+#define GET_STACK_TRACE_THREAD GET_STACK_TRACE(kStackTraceMax, true)
+
 namespace __lsan {
 
 void InitializeInterceptors();

Modified: compiler-rt/trunk/lib/lsan/lsan_common_mac.cc
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/lsan/lsan_common_mac.cc?rev=305695&r1=305694&r2=305695&view=diff
==============================================================================
--- compiler-rt/trunk/lib/lsan/lsan_common_mac.cc (original)
+++ compiler-rt/trunk/lib/lsan/lsan_common_mac.cc Mon Jun 19 09:09:10 2017
@@ -79,8 +79,7 @@ void EnableInThisThread() {
 
 u32 GetCurrentThread() {
   thread_local_data_t *data = get_tls_val(false);
-  CHECK(data);
-  return data->current_thread_id;
+  return data ? data->current_thread_id : kInvalidTid;
 }
 
 void SetCurrentThread(u32 tid) { get_tls_val(true)->current_thread_id = tid; }

Added: compiler-rt/trunk/lib/lsan/lsan_mac.cc
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/lsan/lsan_mac.cc?rev=305695&view=auto
==============================================================================
--- compiler-rt/trunk/lib/lsan/lsan_mac.cc (added)
+++ compiler-rt/trunk/lib/lsan/lsan_mac.cc Mon Jun 19 09:09:10 2017
@@ -0,0 +1,192 @@
+//===-- lsan_mac.cc -------------------------------------------------------===//
+//
+//                     The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+//
+// This file is a part of LeakSanitizer, a memory leak checker.
+//
+// Mac-specific details.
+//===----------------------------------------------------------------------===//
+
+#include "sanitizer_common/sanitizer_platform.h"
+#if SANITIZER_MAC
+
+#include "interception/interception.h"
+#include "lsan.h"
+#include "lsan_allocator.h"
+#include "lsan_thread.h"
+
+#include <pthread.h>
+
+namespace __lsan {
+// Support for the following functions from libdispatch on Mac OS:
+//   dispatch_async_f()
+//   dispatch_async()
+//   dispatch_sync_f()
+//   dispatch_sync()
+//   dispatch_after_f()
+//   dispatch_after()
+//   dispatch_group_async_f()
+//   dispatch_group_async()
+// TODO(glider): libdispatch API contains other functions that we don't support
+// yet.
+//
+// dispatch_sync() and dispatch_sync_f() are synchronous, although chances are
+// they can cause jobs to run on a thread different from the current one.
+// TODO(glider): if so, we need a test for this (otherwise we should remove
+// them).
+//
+// The following functions use dispatch_barrier_async_f() (which isn't a library
+// function but is exported) and are thus supported:
+//   dispatch_source_set_cancel_handler_f()
+//   dispatch_source_set_cancel_handler()
+//   dispatch_source_set_event_handler_f()
+//   dispatch_source_set_event_handler()
+//
+// The reference manual for Grand Central Dispatch is available at
+//   http://developer.apple.com/library/mac/#documentation/Performance/Reference/GCD_libdispatch_Ref/Reference/reference.html
+// The implementation details are at
+//   http://libdispatch.macosforge.org/trac/browser/trunk/src/queue.c
+
+typedef void *dispatch_group_t;
+typedef void *dispatch_queue_t;
+typedef void *dispatch_source_t;
+typedef u64 dispatch_time_t;
+typedef void (*dispatch_function_t)(void *block);
+typedef void *(*worker_t)(void *block);
+
+// A wrapper for the ObjC blocks used to support libdispatch.
+typedef struct {
+  void *block;
+  dispatch_function_t func;
+  u32 parent_tid;
+} lsan_block_context_t;
+
+ALWAYS_INLINE
+void lsan_register_worker_thread(int parent_tid) {
+  if (GetCurrentThread() == kInvalidTid) {
+    u32 tid = ThreadCreate(parent_tid, 0, true);
+    ThreadStart(tid, GetTid());
+    SetCurrentThread(tid);
+  }
+}
+
+// For use by only those functions that allocated the context via
+// alloc_lsan_context().
+extern "C" void lsan_dispatch_call_block_and_release(void *block) {
+  lsan_block_context_t *context = (lsan_block_context_t *)block;
+  VReport(2,
+          "lsan_dispatch_call_block_and_release(): "
+          "context: %p, pthread_self: %p\n",
+          block, pthread_self());
+  lsan_register_worker_thread(context->parent_tid);
+  // Call the original dispatcher for the block.
+  context->func(context->block);
+  lsan_free(context);
+}
+
+}  // namespace __lsan
+
+using namespace __lsan;  // NOLINT
+
+// Wrap |ctxt| and |func| into an lsan_block_context_t.
+// The caller retains control of the allocated context.
+extern "C" lsan_block_context_t *alloc_lsan_context(void *ctxt,
+                                                    dispatch_function_t func) {
+  GET_STACK_TRACE_THREAD;
+  lsan_block_context_t *lsan_ctxt =
+      (lsan_block_context_t *)lsan_malloc(sizeof(lsan_block_context_t), stack);
+  lsan_ctxt->block = ctxt;
+  lsan_ctxt->func = func;
+  lsan_ctxt->parent_tid = GetCurrentThread();
+  return lsan_ctxt;
+}
+
+// Define interceptor for dispatch_*_f function with the three most common
+// parameters: dispatch_queue_t, context, dispatch_function_t.
+#define INTERCEPT_DISPATCH_X_F_3(dispatch_x_f)                        \
+  INTERCEPTOR(void, dispatch_x_f, dispatch_queue_t dq, void *ctxt,    \
+              dispatch_function_t func) {                             \
+    lsan_block_context_t *lsan_ctxt = alloc_lsan_context(ctxt, func); \
+    return REAL(dispatch_x_f)(dq, (void *)lsan_ctxt,                  \
+                              lsan_dispatch_call_block_and_release);  \
+  }
+
+INTERCEPT_DISPATCH_X_F_3(dispatch_async_f)
+INTERCEPT_DISPATCH_X_F_3(dispatch_sync_f)
+INTERCEPT_DISPATCH_X_F_3(dispatch_barrier_async_f)
+
+INTERCEPTOR(void, dispatch_after_f, dispatch_time_t when, dispatch_queue_t dq,
+            void *ctxt, dispatch_function_t func) {
+  lsan_block_context_t *lsan_ctxt = alloc_lsan_context(ctxt, func);
+  return REAL(dispatch_after_f)(when, dq, (void *)lsan_ctxt,
+                                lsan_dispatch_call_block_and_release);
+}
+
+INTERCEPTOR(void, dispatch_group_async_f, dispatch_group_t group,
+            dispatch_queue_t dq, void *ctxt, dispatch_function_t func) {
+  lsan_block_context_t *lsan_ctxt = alloc_lsan_context(ctxt, func);
+  REAL(dispatch_group_async_f)
+  (group, dq, (void *)lsan_ctxt, lsan_dispatch_call_block_and_release);
+}
+
+#if !defined(MISSING_BLOCKS_SUPPORT)
+extern "C" {
+void dispatch_async(dispatch_queue_t dq, void (^work)(void));
+void dispatch_group_async(dispatch_group_t dg, dispatch_queue_t dq,
+                          void (^work)(void));
+void dispatch_after(dispatch_time_t when, dispatch_queue_t queue,
+                    void (^work)(void));
+void dispatch_source_set_cancel_handler(dispatch_source_t ds,
+                                        void (^work)(void));
+void dispatch_source_set_event_handler(dispatch_source_t ds,
+                                       void (^work)(void));
+}
+
+#define GET_LSAN_BLOCK(work)                 \
+  void (^lsan_block)(void);                  \
+  int parent_tid = GetCurrentThread();       \
+  lsan_block = ^(void) {                     \
+    lsan_register_worker_thread(parent_tid); \
+    work();                                  \
+  }
+
+INTERCEPTOR(void, dispatch_async, dispatch_queue_t dq, void (^work)(void)) {
+  GET_LSAN_BLOCK(work);
+  REAL(dispatch_async)(dq, lsan_block);
+}
+
+INTERCEPTOR(void, dispatch_group_async, dispatch_group_t dg,
+            dispatch_queue_t dq, void (^work)(void)) {
+  GET_LSAN_BLOCK(work);
+  REAL(dispatch_group_async)(dg, dq, lsan_block);
+}
+
+INTERCEPTOR(void, dispatch_after, dispatch_time_t when, dispatch_queue_t queue,
+            void (^work)(void)) {
+  GET_LSAN_BLOCK(work);
+  REAL(dispatch_after)(when, queue, lsan_block);
+}
+
+INTERCEPTOR(void, dispatch_source_set_cancel_handler, dispatch_source_t ds,
+            void (^work)(void)) {
+  if (!work) {
+    REAL(dispatch_source_set_cancel_handler)(ds, work);
+    return;
+  }
+  GET_LSAN_BLOCK(work);
+  REAL(dispatch_source_set_cancel_handler)(ds, lsan_block);
+}
+
+INTERCEPTOR(void, dispatch_source_set_event_handler, dispatch_source_t ds,
+            void (^work)(void)) {
+  GET_LSAN_BLOCK(work);
+  REAL(dispatch_source_set_event_handler)(ds, lsan_block);
+}
+#endif
+
+#endif  // SANITIZER_MAC

Modified: compiler-rt/trunk/lib/lsan/lsan_thread.cc
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/lsan/lsan_thread.cc?rev=305695&r1=305694&r2=305695&view=diff
==============================================================================
--- compiler-rt/trunk/lib/lsan/lsan_thread.cc (original)
+++ compiler-rt/trunk/lib/lsan/lsan_thread.cc Mon Jun 19 09:09:10 2017
@@ -77,7 +77,7 @@ u32 ThreadCreate(u32 parent_tid, uptr us
                                        /* arg */ nullptr);
 }
 
-void ThreadStart(u32 tid, tid_t os_id) {
+void ThreadStart(u32 tid, tid_t os_id, bool workerthread) {
   OnStartedArgs args;
   uptr stack_size = 0;
   uptr tls_size = 0;
@@ -87,7 +87,7 @@ void ThreadStart(u32 tid, tid_t os_id) {
   args.tls_end = args.tls_begin + tls_size;
   GetAllocatorCacheRange(&args.cache_begin, &args.cache_end);
   args.dtls = DTLS_Get();
-  thread_registry->StartThread(tid, os_id, /*workerthread*/ false, &args);
+  thread_registry->StartThread(tid, os_id, workerthread, &args);
 }
 
 void ThreadFinish() {

Modified: compiler-rt/trunk/lib/lsan/lsan_thread.h
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/lsan/lsan_thread.h?rev=305695&r1=305694&r2=305695&view=diff
==============================================================================
--- compiler-rt/trunk/lib/lsan/lsan_thread.h (original)
+++ compiler-rt/trunk/lib/lsan/lsan_thread.h Mon Jun 19 09:09:10 2017
@@ -45,7 +45,7 @@ class ThreadContext : public ThreadConte
 
 void InitializeThreadRegistry();
 
-void ThreadStart(u32 tid, tid_t os_id);
+void ThreadStart(u32 tid, tid_t os_id, bool workerthread = false);
 void ThreadFinish();
 u32 ThreadCreate(u32 tid, uptr uid, bool detached);
 void ThreadJoin(u32 tid);

Added: compiler-rt/trunk/test/lsan/TestCases/Darwin/dispatch.mm
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/test/lsan/TestCases/Darwin/dispatch.mm?rev=305695&view=auto
==============================================================================
--- compiler-rt/trunk/test/lsan/TestCases/Darwin/dispatch.mm (added)
+++ compiler-rt/trunk/test/lsan/TestCases/Darwin/dispatch.mm Mon Jun 19 09:09:10 2017
@@ -0,0 +1,59 @@
+// Test for threads spawned with wqthread_start
+// RUN: LSAN_BASE="report_objects=1"
+// RUN: %clangxx_lsan %s -DDISPATCH_ASYNC -o %t-async -framework Foundation
+// RUN: %clangxx_lsan %s -DDISPATCH_SYNC -o %t-sync -framework Foundation
+// RUN: %env_lsan_opts=$LSAN_BASE not %run %t-async 2>&1 | FileCheck %s
+// RUN: %env_lsan_opts=$LSAN_BASE not %run %t-sync 2>&1 | FileCheck %s
+
+#include <dispatch/dispatch.h>
+#include <pthread.h>
+#include <stdlib.h>
+
+#include "sanitizer_common/print_address.h"
+
+bool done = false;
+
+void worker_do_leak(int size) {
+  void *p = malloc(size);
+  print_address("Test alloc: ", 1, p);
+  done = true;
+}
+
+#if DISPATCH_ASYNC
+// Tests for the Grand Central Dispatch. See
+// http://developer.apple.com/library/mac/#documentation/Performance/Reference/GCD_libdispatch_Ref/Reference/reference.html
+// for the reference.
+void TestGCDDispatch() {
+  dispatch_queue_t queue = dispatch_get_global_queue(0, 0);
+  dispatch_block_t block = ^{
+    worker_do_leak(1337);
+  };
+  // dispatch_async() runs the task on a worker thread that does not go through
+  // pthread_create(). We need to verify that LeakSanitizer notices that the
+  // thread has started.
+  dispatch_async(queue, block);
+  while (!done)
+    pthread_yield_np();
+}
+#elif DISPATCH_SYNC
+void TestGCDDispatch() {
+  dispatch_queue_t queue = dispatch_get_global_queue(2, 0);
+  dispatch_block_t block = ^{
+    worker_do_leak(1337);
+  };
+  // dispatch_sync() runs the task on a worker thread that does not go through
+  // pthread_create(). We need to verify that LeakSanitizer notices that the
+  // thread has started.
+  dispatch_sync(queue, block);
+}
+#endif
+
+int main() {
+  TestGCDDispatch();
+  return 0;
+}
+
+// CHECK: Test alloc: [[addr:0x[0-9,a-f]+]]
+// CHECK: LeakSanitizer: detected memory leaks
+// CHECK: [[addr]] (1337 bytes)
+// CHECK: SUMMARY: {{(Leak|Address)}}Sanitizer:

Added: compiler-rt/trunk/test/lsan/TestCases/Darwin/lit.local.cfg
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/test/lsan/TestCases/Darwin/lit.local.cfg?rev=305695&view=auto
==============================================================================
--- compiler-rt/trunk/test/lsan/TestCases/Darwin/lit.local.cfg (added)
+++ compiler-rt/trunk/test/lsan/TestCases/Darwin/lit.local.cfg Mon Jun 19 09:09:10 2017
@@ -0,0 +1,9 @@
+def getRoot(config):
+  if not config.parent:
+    return config
+  return getRoot(config.parent)
+
+root = getRoot(config)
+
+if root.host_os not in ['Darwin']:
+  config.unsupported = True

Modified: compiler-rt/trunk/test/lsan/lit.common.cfg
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/test/lsan/lit.common.cfg?rev=305695&r1=305694&r2=305695&view=diff
==============================================================================
--- compiler-rt/trunk/test/lsan/lit.common.cfg (original)
+++ compiler-rt/trunk/test/lsan/lit.common.cfg Mon Jun 19 09:09:10 2017
@@ -77,4 +77,4 @@ if not (supported_linux or supported_dar
 if re.search('mthumb', config.target_cflags) is not None:
   config.unsupported = True
 
-config.suffixes = ['.c', '.cc', '.cpp']
+config.suffixes = ['.c', '.cc', '.cpp', '.mm']




More information about the llvm-commits mailing list