[llvm-branch-commits] [compiler-rt] [nsan] Use sanitizer allocator (PR #102764)

Fangrui Song via llvm-branch-commits llvm-branch-commits at lists.llvm.org
Mon Aug 12 13:55:42 PDT 2024


https://github.com/MaskRay updated https://github.com/llvm/llvm-project/pull/102764

>From 6ec669e2206a29bce0c28213e82c2694f03bfad9 Mon Sep 17 00:00:00 2001
From: Fangrui Song <i at maskray.me>
Date: Sun, 11 Aug 2024 13:27:34 -0700
Subject: [PATCH] remove GetCurrentThread check for NsanAllocate

Created using spr 1.3.5-bogner
---
 compiler-rt/lib/nsan/nsan_allocator.cpp | 13 ++++---------
 1 file changed, 4 insertions(+), 9 deletions(-)

diff --git a/compiler-rt/lib/nsan/nsan_allocator.cpp b/compiler-rt/lib/nsan/nsan_allocator.cpp
index 3fa58513e9b8c9..3ba71d7e9f9e26 100644
--- a/compiler-rt/lib/nsan/nsan_allocator.cpp
+++ b/compiler-rt/lib/nsan/nsan_allocator.cpp
@@ -108,15 +108,8 @@ static void *NsanAllocate(uptr size, uptr alignment, bool zero) {
     ReportRssLimitExceeded(&stack);
   }
   NsanThread *t = GetCurrentThread();
-  void *allocated;
-  if (t) {
-    AllocatorCache *cache = GetAllocatorCache(&t->malloc_storage());
-    allocated = allocator.Allocate(cache, size, alignment);
-  } else {
-    SpinMutexLock l(&fallback_mutex);
-    AllocatorCache *cache = &fallback_allocator_cache;
-    allocated = allocator.Allocate(cache, size, alignment);
-  }
+  void *allocated = allocator.Allocate(GetAllocatorCache(&t->malloc_storage()),
+                                       size, alignment);
   if (UNLIKELY(!allocated)) {
     SetAllocatorOutOfMemory();
     if (AllocatorMayReturnNull())
@@ -146,6 +139,8 @@ void __nsan::NsanDeallocate(void *p) {
     AllocatorCache *cache = GetAllocatorCache(&t->malloc_storage());
     allocator.Deallocate(cache, p);
   } else {
+    // In a just created thread, glibc's _dl_deallocate_tls might reach here
+    // before nsan_current_thread is set.
     SpinMutexLock l(&fallback_mutex);
     AllocatorCache *cache = &fallback_allocator_cache;
     allocator.Deallocate(cache, p);



More information about the llvm-branch-commits mailing list