[llvm-branch-commits] [tsan] Switch to DlSymAllocator (PR #108920)

via llvm-branch-commits llvm-branch-commits at lists.llvm.org
Mon Sep 16 20:52:47 PDT 2024


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-compiler-rt-sanitizer

Author: Vitaly Buka (vitalybuka)

<details>
<summary>Changes</summary>

Existing `in_symbolizer()` tsan logic does almost
the same.

Not exactly NFC: DlSymAllocator does not allow
large allocation, which probably unlikely here
anyway. Large allocations will go to secondary
allocator, which use slow `PointerIsMine`.


---
Full diff: https://github.com/llvm/llvm-project/pull/108920.diff


2 Files Affected:

- (modified) compiler-rt/lib/tsan/rtl/tsan_interceptors_posix.cpp (+32-24) 
- (modified) compiler-rt/test/sanitizer_common/TestCases/dlsym_alloc.c (-2) 


``````````diff
diff --git a/compiler-rt/lib/tsan/rtl/tsan_interceptors_posix.cpp b/compiler-rt/lib/tsan/rtl/tsan_interceptors_posix.cpp
index 460cbacf3408cf..924339191df133 100644
--- a/compiler-rt/lib/tsan/rtl/tsan_interceptors_posix.cpp
+++ b/compiler-rt/lib/tsan/rtl/tsan_interceptors_posix.cpp
@@ -12,9 +12,11 @@
 // sanitizer_common/sanitizer_common_interceptors.inc
 //===----------------------------------------------------------------------===//
 
+#include "sanitizer_common/sanitizer_allocator_dlsym.h"
 #include "sanitizer_common/sanitizer_atomic.h"
 #include "sanitizer_common/sanitizer_errno.h"
 #include "sanitizer_common/sanitizer_glibc_version.h"
+#include "sanitizer_common/sanitizer_internal_defs.h"
 #include "sanitizer_common/sanitizer_libc.h"
 #include "sanitizer_common/sanitizer_linux.h"
 #include "sanitizer_common/sanitizer_platform_limits_netbsd.h"
@@ -252,6 +254,12 @@ SANITIZER_WEAK_CXX_DEFAULT_IMPL void OnPotentiallyBlockingRegionBegin() {}
 SANITIZER_WEAK_CXX_DEFAULT_IMPL void OnPotentiallyBlockingRegionEnd() {}
 #endif
 
+struct DlsymAlloc : public DlSymAllocator<DlsymAlloc> {
+  static bool UseImpl() {
+    return in_symbolizer() || (ctx && !ctx->initialized);
+  }
+};
+
 }  // namespace __tsan
 
 static ThreadSignalContext *SigCtx(ThreadState *thr) {
@@ -659,8 +667,8 @@ TSAN_INTERCEPTOR(void, _longjmp, uptr *env, int val) {
 
 #if !SANITIZER_APPLE
 TSAN_INTERCEPTOR(void*, malloc, uptr size) {
-  if (in_symbolizer())
-    return InternalAlloc(size);
+  if (DlsymAlloc::Use())
+    return DlsymAlloc::Allocate(size);
   void *p = 0;
   {
     SCOPED_INTERCEPTOR_RAW(malloc, size);
@@ -678,9 +686,9 @@ TSAN_INTERCEPTOR(void*, __libc_memalign, uptr align, uptr sz) {
   return user_memalign(thr, pc, align, sz);
 }
 
-TSAN_INTERCEPTOR(void *, calloc, uptr n, uptr size) {
-  if (in_symbolizer())
-    return InternalCalloc(n, size);
+TSAN_INTERCEPTOR(void*, calloc, uptr n, uptr size) {
+  if (DlsymAlloc::Use())
+    return DlsymAlloc::Callocate(n, size);
   void *p = 0;
   {
     SCOPED_INTERCEPTOR_RAW(calloc, n, size);
@@ -691,8 +699,8 @@ TSAN_INTERCEPTOR(void *, calloc, uptr n, uptr size) {
 }
 
 TSAN_INTERCEPTOR(void*, realloc, void *p, uptr size) {
-  if (in_symbolizer())
-    return InternalRealloc(p, size);
+  if (DlsymAlloc::Use() || DlsymAlloc::PointerIsMine(p))
+    return DlsymAlloc::Realloc(p, size);
   if (p)
     invoke_free_hook(p);
   {
@@ -703,9 +711,9 @@ TSAN_INTERCEPTOR(void*, realloc, void *p, uptr size) {
   return p;
 }
 
-TSAN_INTERCEPTOR(void *, reallocarray, void *p, uptr n, uptr size) {
-  if (in_symbolizer())
-    return InternalReallocArray(p, n, size);
+TSAN_INTERCEPTOR(void*, reallocarray, void *p, uptr n, uptr size) {
+  if (DlsymAlloc::Use() || DlsymAlloc::PointerIsMine(p))
+    return DlsymAlloc::ReallocArray(p, n, size);
   if (p)
     invoke_free_hook(p);
   {
@@ -717,20 +725,20 @@ TSAN_INTERCEPTOR(void *, reallocarray, void *p, uptr n, uptr size) {
 }
 
 TSAN_INTERCEPTOR(void, free, void *p) {
-  if (p == 0)
+  if (UNLIKELY(!p))
     return;
-  if (in_symbolizer())
-    return InternalFree(p);
+  if (DlsymAlloc::PointerIsMine(p))
+    return DlsymAlloc::Free(p);
   invoke_free_hook(p);
   SCOPED_INTERCEPTOR_RAW(free, p);
   user_free(thr, pc, p);
 }
 
 TSAN_INTERCEPTOR(void, cfree, void *p) {
-  if (p == 0)
+  if (UNLIKELY(!p))
     return;
-  if (in_symbolizer())
-    return InternalFree(p);
+  if (DlsymAlloc::PointerIsMine(p))
+    return DlsymAlloc::Free(p);
   invoke_free_hook(p);
   SCOPED_INTERCEPTOR_RAW(cfree, p);
   user_free(thr, pc, p);
@@ -818,15 +826,15 @@ TSAN_INTERCEPTOR(void*, memalign, uptr align, uptr sz) {
 
 #if !SANITIZER_APPLE
 TSAN_INTERCEPTOR(void*, aligned_alloc, uptr align, uptr sz) {
-  if (in_symbolizer())
-    return InternalAlloc(sz, nullptr, align);
+  if (DlsymAlloc::Use())
+    return DlsymAlloc::Allocate(sz, align);
   SCOPED_INTERCEPTOR_RAW(aligned_alloc, align, sz);
   return user_aligned_alloc(thr, pc, align, sz);
 }
 
 TSAN_INTERCEPTOR(void*, valloc, uptr sz) {
-  if (in_symbolizer())
-    return InternalAlloc(sz, nullptr, GetPageSizeCached());
+  if (DlsymAlloc::Use())
+    return DlsymAlloc::Allocate(sz, GetPageSizeCached());
   SCOPED_INTERCEPTOR_RAW(valloc, sz);
   return user_valloc(thr, pc, sz);
 }
@@ -834,10 +842,10 @@ TSAN_INTERCEPTOR(void*, valloc, uptr sz) {
 
 #if SANITIZER_LINUX
 TSAN_INTERCEPTOR(void*, pvalloc, uptr sz) {
-  if (in_symbolizer()) {
+  if (DlsymAlloc::Use()) {
     uptr PageSize = GetPageSizeCached();
     sz = sz ? RoundUpTo(sz, PageSize) : PageSize;
-    return InternalAlloc(sz, nullptr, PageSize);
+    return DlsymAlloc::Allocate(sz, PageSize);
   }
   SCOPED_INTERCEPTOR_RAW(pvalloc, sz);
   return user_pvalloc(thr, pc, sz);
@@ -849,8 +857,8 @@ TSAN_INTERCEPTOR(void*, pvalloc, uptr sz) {
 
 #if !SANITIZER_APPLE
 TSAN_INTERCEPTOR(int, posix_memalign, void **memptr, uptr align, uptr sz) {
-  if (in_symbolizer()) {
-    void *p = InternalAlloc(sz, nullptr, align);
+  if (DlsymAlloc::Use()) {
+    void *p = DlsymAlloc::Allocate(sz, align);
     if (!p)
       return errno_ENOMEM;
     *memptr = p;
diff --git a/compiler-rt/test/sanitizer_common/TestCases/dlsym_alloc.c b/compiler-rt/test/sanitizer_common/TestCases/dlsym_alloc.c
index 7b5b9cf34a90f9..4aa87afe47f4ea 100644
--- a/compiler-rt/test/sanitizer_common/TestCases/dlsym_alloc.c
+++ b/compiler-rt/test/sanitizer_common/TestCases/dlsym_alloc.c
@@ -1,7 +1,5 @@
 // RUN: %clang -O0 %s -o %t && %run %t
 
-// FIXME: TSAN does not use DlsymAlloc.
-// UNSUPPORTED: tsan
 // FIXME: investigate why this fails on macos
 // UNSUPPORTED: darwin
 

``````````

</details>


https://github.com/llvm/llvm-project/pull/108920


More information about the llvm-branch-commits mailing list