[PATCH] D40159: Make TLS/NetBSD handling more generic

Kamil Rytarowski via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Thu Nov 16 16:41:58 PST 2017


krytarowski created this revision.
krytarowski added a project: Sanitizers.
Herald added subscribers: kubamracek, emaste.

Include <sys/tls.h> for:

- struct tls_tcb - thread control block structure
- __HAVE___LWP_GETTCB_FAST - __lwp_gettcb_fast() is available
- __HAVE___LWP_GETPRIVATE_FAST -  __lwp_getprivate_fast() is available
- __HAVE_TLS_VARIANT_I - TLS Variant I for this architecture
- __HAVE_TLS_VARIANT_II - TLS Variant II for this architecture

Switch ThreadSelfSegbase() to retrieve in a portable way TCB.
Switch ThreadSelf() to retrieve in a portable way pthread.
Switch GetTls() to use Variant I / Variant II of TLS.
Variant I is temporarily not implemented and has no users.

Stop sharing the same code for ThreadSelfSegbase(), ThreadSelf()
and GetTls() with FreeBSD.

Sponsored by <The NetBSD Foundation>


Repository:
  rL LLVM

https://reviews.llvm.org/D40159

Files:
  lib/sanitizer_common/sanitizer_linux_libcdep.cc


Index: lib/sanitizer_common/sanitizer_linux_libcdep.cc
===================================================================
--- lib/sanitizer_common/sanitizer_linux_libcdep.cc
+++ lib/sanitizer_common/sanitizer_linux_libcdep.cc
@@ -40,6 +40,10 @@
 #define pthread_getattr_np pthread_attr_get_np
 #endif
 
+#if SANITIZER_NETBSD
+#include <sys/tls.h>
+#endif
+
 #if SANITIZER_LINUX
 #include <sys/prctl.h>
 #endif
@@ -312,7 +316,7 @@
 }
 #endif  // (x86_64 || i386 || MIPS) && SANITIZER_LINUX
 
-#if SANITIZER_FREEBSD || SANITIZER_NETBSD
+#if SANITIZER_FREEBSD
 static void **ThreadSelfSegbase() {
   void **segbase = 0;
 # if defined(__i386__)
@@ -330,7 +334,25 @@
 uptr ThreadSelf() {
   return (uptr)ThreadSelfSegbase()[2];
 }
-#endif  // SANITIZER_FREEBSD || SANITIZER_NETBSD
+#endif  // SANITIZER_FREEBSD
+
+#if SANITIZER_NETBSD
+static struct tls_tcb * ThreadSelfSegbase() {
+  struct tls_tcb * tcb;
+# ifdef __HAVE___LWP_GETTCB_FAST
+  tcb = (struct tls_tcb *)__lwp_gettcb_fast();
+# elif defined(__HAVE___LWP_GETPRIVATE_FAST)
+  tcb = (struct tls_tcb *)__lwp_getprivate_fast();
+# else
+#  error "unsupported CPU arch"
+# endif
+  return tcb;
+}
+
+uptr ThreadSelf() {
+  return (uptr)ThreadSelfSegbase()->tcb_pthread;
+}
+#endif  // SANITIZER_NETBSD
 
 #if !SANITIZER_GO
 static void GetTls(uptr *addr, uptr *size) {
@@ -348,7 +370,7 @@
   *addr = 0;
   *size = 0;
 # endif
-#elif SANITIZER_FREEBSD || SANITIZER_NETBSD
+#elif SANITIZER_FREEBSD
   void** segbase = ThreadSelfSegbase();
   *addr = 0;
   *size = 0;
@@ -361,6 +383,25 @@
     *addr = (uptr) dtv[2];
     *size = (*addr == 0) ? 0 : ((uptr) segbase[0] - (uptr) dtv[2]);
   }
+#elif SANITIZER_NETBSD
+  struct tls_tcb * const tcb = ThreadSelfSegbase();
+  *addr = 0;
+  *size = 0;
+  if (tcb != 0) {
+# ifdef __HAVE_TLS_VARIANT_I
+#  error "Not implemented"
+# elif defined(__HAVE_TLS_VARIANT_II)
+    // tcbalign = 16
+    // tls_size = round(tls_static_space, tcbalign);
+    // dtv = segbase[1];
+    // dtv[2] = segbase - tls_static_space;
+    void **dtv = tcb->tcb_dtv;
+    *addr = (uptr) dtv[2];
+    *size = (*addr == 0) ? 0 : ((uptr) tcb->tcb_self - (uptr) dtv[2]);
+# else
+#  error "Not implemented"
+# endif
+  }
 #elif SANITIZER_ANDROID
   *addr = 0;
   *size = 0;


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D40159.123270.patch
Type: text/x-patch
Size: 2245 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20171117/e08ec464/attachment.bin>


More information about the llvm-commits mailing list