[PATCH] D40457: Detect thread termination in LSan/NetBSD

Kamil Rytarowski via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Sat Nov 25 08:41:46 PST 2017


krytarowski created this revision.
krytarowski added a project: Sanitizers.

Stop using the Linux solution with pthread_key_create(3).
This approach does not work on NetBSD, because calling
the thread destructor is not the latest operation on a POSIX
thread entity.

Detect _lwp_exit(2) call as it is really the latest operation
called from a detaching POSIX thread.

The pthread_key_create(3) solution also cannot be used
in early libc/libpthread initialization on NetBSD as the
system libraries are not bootstrapped enough.

Sponsored by <The NetBSD Foundation>


Repository:
  rL LLVM

https://reviews.llvm.org/D40457

Files:
  lib/lsan/lsan_interceptors.cc


Index: lib/lsan/lsan_interceptors.cc
===================================================================
--- lib/lsan/lsan_interceptors.cc
+++ lib/lsan/lsan_interceptors.cc
@@ -309,6 +309,7 @@
 
 ///// Thread initialization and finalization. /////
 
+#if !SANITIZER_NETBSD
 static unsigned g_thread_finalize_key;
 
 static void thread_finalize(void *v) {
@@ -322,6 +323,18 @@
   }
   ThreadFinish();
 }
+#endif
+
+#if SANITIZER_NETBSD
+INTERCEPTOR(void, _lwp_exit) {
+  ENSURE_LSAN_INITED;
+  ThreadFinish();
+  REAL(_lwp_exit)();
+}
+#define LSAN_MAYBE_INTERCEPT__LWP_EXIT INTERCEPT_FUNCTION(_lwp_exit)
+#else
+#define LSAN_MAYBE_INTERCEPT__LWP_EXIT
+#endif
 
 struct ThreadParam {
   void *(*callback)(void *arg);
@@ -335,11 +348,13 @@
   void *param = p->param;
   // Wait until the last iteration to maximize the chance that we are the last
   // destructor to run.
+#if !SANITIZER_NETBSD
   if (pthread_setspecific(g_thread_finalize_key,
                           (void*)GetPthreadDestructorIterations())) {
     Report("LeakSanitizer: failed to set thread key.\n");
     Die();
   }
+#endif
   int tid = 0;
   while ((tid = atomic_load(&p->tid, memory_order_acquire)) == 0)
     internal_sched_yield();
@@ -427,10 +442,14 @@
   INTERCEPT_FUNCTION(pthread_join);
   INTERCEPT_FUNCTION(_exit);
 
+  LSAN_MAYBE_INTERCEPT__LWP_EXIT;
+
+#if !SANITIZER_NETBSD
   if (pthread_key_create(&g_thread_finalize_key, &thread_finalize)) {
     Report("LeakSanitizer: failed to create thread key.\n");
     Die();
   }
+#endif
 }
 
 } // namespace __lsan


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D40457.124260.patch
Type: text/x-patch
Size: 1547 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20171125/94f1ccd9/attachment.bin>


More information about the llvm-commits mailing list