[PATCH] D65221: [Sanitizer][ASAN][MSAN] Fix infinite recursion on FreeBSD

Alexander Richardson via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Wed Jul 24 09:49:01 PDT 2019


arichardson created this revision.
arichardson added reviewers: vitalybuka, krytarowski, devnexen.
Herald added subscribers: llvm-commits, Sanitizers, jfb, kubamracek, emaste.
Herald added projects: Sanitizers, LLVM.

MSAN was broken on FreeBSD by https://reviews.llvm.org/D55703: after this
change accesses to the key variable call __tls_get_addr, which is
intercepted. The interceptor then calls GetCurrentThread which calls
MsanTSDGet which again calls __tls_get_addr, etc...
Using the default implementation in the SANITIZER_FREEBSD case fixes MSAN
for me.

I then applied the same change to ASAN (introduced in https://reviews.llvm.org/D55596)
but that did not work yet. In the ASAN case, we get infinite recursion
again during initialization, this time because calling pthread_key_create() early on
results in infinite recursion. pthread_key_create() calls sysctlbyname()
which is intercepted but COMMON_INTERCEPTOR_NOTHING_IS_INITIALIZED returns
true, so the interceptor calls internal_sysctlbyname() which then ends up
calling the interceptor again. I fixed this issue by using dlsym() to get
the libc version of sysctlbyname() instead.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D65221

Files:
  compiler-rt/lib/asan/asan_posix.cc
  compiler-rt/lib/msan/msan_linux.cc
  compiler-rt/lib/sanitizer_common/sanitizer_linux.cc


Index: compiler-rt/lib/sanitizer_common/sanitizer_linux.cc
===================================================================
--- compiler-rt/lib/sanitizer_common/sanitizer_linux.cc
+++ compiler-rt/lib/sanitizer_common/sanitizer_linux.cc
@@ -777,9 +777,17 @@
 }
 
 #if SANITIZER_FREEBSD
+typedef int (*syctlbyname_ptr)(const char *sname, void *oldp, size_t *oldlenp,
+                          const void *newp, size_t newlen);
 int internal_sysctlbyname(const char *sname, void *oldp, uptr *oldlenp,
                           const void *newp, uptr newlen) {
-  return sysctlbyname(sname, oldp, (size_t *)oldlenp, newp, (size_t)newlen);
+  static syctlbyname_ptr real_sysctlbyname = nullptr;
+  if (!real_sysctlbyname)
+    real_sysctlbyname = (syctlbyname_ptr)dlfunc(RTLD_NEXT, "sysctlbyname");
+  if (!real_sysctlbyname)
+    real_sysctlbyname = (syctlbyname_ptr)dlfunc(RTLD_DEFAULT, "sysctlbyname");
+  CHECK(real_sysctlbyname);
+  return real_sysctlbyname(sname, oldp, (size_t *)oldlenp, newp, (size_t)newlen);
 }
 #endif
 #endif
Index: compiler-rt/lib/msan/msan_linux.cc
===================================================================
--- compiler-rt/lib/msan/msan_linux.cc
+++ compiler-rt/lib/msan/msan_linux.cc
@@ -174,7 +174,7 @@
 
 // ---------------------- TSD ---------------- {{{1
 
-#if SANITIZER_NETBSD || SANITIZER_FREEBSD
+#if SANITIZER_NETBSD
 // Thread Static Data cannot be used in early init on NetBSD and FreeBSD.
 // Reuse the MSan TSD API for compatibility with existing code
 // with an alternative implementation.
Index: compiler-rt/lib/asan/asan_posix.cc
===================================================================
--- compiler-rt/lib/asan/asan_posix.cc
+++ compiler-rt/lib/asan/asan_posix.cc
@@ -39,7 +39,7 @@
 
 // ---------------------- TSD ---------------- {{{1
 
-#if SANITIZER_NETBSD || SANITIZER_FREEBSD
+#if SANITIZER_NETBSD
 // Thread Static Data cannot be used in early init on NetBSD and FreeBSD.
 // Reuse the Asan TSD API for compatibility with existing code
 // with an alternative implementation.


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D65221.211535.patch
Type: text/x-patch
Size: 2053 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20190724/8f84c0c1/attachment.bin>


More information about the llvm-commits mailing list