[compiler-rt] r315583 - [scudo] Allow for non-Android Shared TSD platforms, part 1

Kostya Kortchinsky via llvm-commits llvm-commits at lists.llvm.org
Thu Oct 12 08:01:09 PDT 2017


Author: cryptoad
Date: Thu Oct 12 08:01:09 2017
New Revision: 315583

URL: http://llvm.org/viewvc/llvm-project?rev=315583&view=rev
Log:
[scudo] Allow for non-Android Shared TSD platforms, part 1

Summary:
This first part just prepares the grounds for part 2 and doesn't add any new
functionality. It mostly consists of small refactors:
- move the `pthread.h` include higher as it will be used in the headers;
- use `errno.h` in `scudo_allocator.cpp` instead of the sanitizer one, update
  the `errno` assignments accordingly (otherwise it creates conflicts on some
  platforms due to `pthread.h` including `errno.h`);
- introduce and use `getCurrentTSD` and `setCurrentTSD` for the shared TSD
  model code;

Reviewers: alekseyshl

Reviewed By: alekseyshl

Subscribers: llvm-commits, srhines

Differential Revision: https://reviews.llvm.org/D38826

Modified:
    compiler-rt/trunk/lib/scudo/scudo_allocator.cpp
    compiler-rt/trunk/lib/scudo/scudo_tsd.h
    compiler-rt/trunk/lib/scudo/scudo_tsd_exclusive.cpp
    compiler-rt/trunk/lib/scudo/scudo_tsd_shared.cpp
    compiler-rt/trunk/lib/scudo/scudo_tsd_shared.inc

Modified: compiler-rt/trunk/lib/scudo/scudo_allocator.cpp
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/scudo/scudo_allocator.cpp?rev=315583&r1=315582&r2=315583&view=diff
==============================================================================
--- compiler-rt/trunk/lib/scudo/scudo_allocator.cpp (original)
+++ compiler-rt/trunk/lib/scudo/scudo_allocator.cpp Thu Oct 12 08:01:09 2017
@@ -22,9 +22,9 @@
 
 #include "sanitizer_common/sanitizer_allocator_checks.h"
 #include "sanitizer_common/sanitizer_allocator_interface.h"
-#include "sanitizer_common/sanitizer_errno.h"
 #include "sanitizer_common/sanitizer_quarantine.h"
 
+#include <errno.h>
 #include <string.h>
 
 namespace __scudo {
@@ -640,7 +640,7 @@ void *scudoValloc(uptr Size) {
 void *scudoPvalloc(uptr Size) {
   uptr PageSize = GetPageSizeCached();
   if (UNLIKELY(CheckForPvallocOverflow(Size, PageSize))) {
-    errno = errno_ENOMEM;
+    errno = ENOMEM;
     return Instance.handleBadRequest();
   }
   // pvalloc(0) should allocate one page.
@@ -650,7 +650,7 @@ void *scudoPvalloc(uptr Size) {
 
 void *scudoMemalign(uptr Alignment, uptr Size) {
   if (UNLIKELY(!IsPowerOfTwo(Alignment))) {
-    errno = errno_EINVAL;
+    errno = EINVAL;
     return Instance.handleBadRequest();
   }
   return SetErrnoOnNull(Instance.allocate(Size, Alignment, FromMemalign));
@@ -659,18 +659,18 @@ void *scudoMemalign(uptr Alignment, uptr
 int scudoPosixMemalign(void **MemPtr, uptr Alignment, uptr Size) {
   if (UNLIKELY(!CheckPosixMemalignAlignment(Alignment))) {
     Instance.handleBadRequest();
-    return errno_EINVAL;
+    return EINVAL;
   }
   void *Ptr = Instance.allocate(Size, Alignment, FromMemalign);
   if (UNLIKELY(!Ptr))
-    return errno_ENOMEM;
+    return ENOMEM;
   *MemPtr = Ptr;
   return 0;
 }
 
 void *scudoAlignedAlloc(uptr Alignment, uptr Size) {
   if (UNLIKELY(!CheckAlignedAllocAlignmentAndSize(Alignment, Size))) {
-    errno = errno_EINVAL;
+    errno = EINVAL;
     return Instance.handleBadRequest();
   }
   return SetErrnoOnNull(Instance.allocate(Size, Alignment, FromMalloc));

Modified: compiler-rt/trunk/lib/scudo/scudo_tsd.h
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/scudo/scudo_tsd.h?rev=315583&r1=315582&r2=315583&view=diff
==============================================================================
--- compiler-rt/trunk/lib/scudo/scudo_tsd.h (original)
+++ compiler-rt/trunk/lib/scudo/scudo_tsd.h Thu Oct 12 08:01:09 2017
@@ -19,6 +19,8 @@
 #include "scudo_allocator.h"
 #include "scudo_utils.h"
 
+#include <pthread.h>
+
 namespace __scudo {
 
 struct ALIGNED(64) ScudoTSD {

Modified: compiler-rt/trunk/lib/scudo/scudo_tsd_exclusive.cpp
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/scudo/scudo_tsd_exclusive.cpp?rev=315583&r1=315582&r2=315583&view=diff
==============================================================================
--- compiler-rt/trunk/lib/scudo/scudo_tsd_exclusive.cpp (original)
+++ compiler-rt/trunk/lib/scudo/scudo_tsd_exclusive.cpp Thu Oct 12 08:01:09 2017
@@ -15,8 +15,6 @@
 
 #if SCUDO_TSD_EXCLUSIVE
 
-#include <pthread.h>
-
 namespace __scudo {
 
 static pthread_once_t GlobalInitialized = PTHREAD_ONCE_INIT;

Modified: compiler-rt/trunk/lib/scudo/scudo_tsd_shared.cpp
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/scudo/scudo_tsd_shared.cpp?rev=315583&r1=315582&r2=315583&view=diff
==============================================================================
--- compiler-rt/trunk/lib/scudo/scudo_tsd_shared.cpp (original)
+++ compiler-rt/trunk/lib/scudo/scudo_tsd_shared.cpp Thu Oct 12 08:01:09 2017
@@ -15,8 +15,6 @@
 
 #if !SCUDO_TSD_EXCLUSIVE
 
-#include <pthread.h>
-
 namespace __scudo {
 
 static pthread_once_t GlobalInitialized = PTHREAD_ONCE_INIT;
@@ -51,12 +49,15 @@ static void initOnce() {
     TSDs[i].init(/*Shared=*/true);
 }
 
+ALWAYS_INLINE void setCurrentTSD(ScudoTSD *TSD) {
+  *get_android_tls_ptr() = reinterpret_cast<uptr>(TSD);
+}
+
 void initThread(bool MinimalInit) {
   pthread_once(&GlobalInitialized, initOnce);
   // Initial context assignment is done in a plain round-robin fashion.
   u32 Index = atomic_fetch_add(&CurrentIndex, 1, memory_order_relaxed);
-  ScudoTSD *TSD = &TSDs[Index % NumberOfTSDs];
-  *get_android_tls_ptr() = reinterpret_cast<uptr>(TSD);
+  setCurrentTSD(&TSDs[Index % NumberOfTSDs]);
 }
 
 ScudoTSD *getTSDAndLockSlow() {
@@ -66,7 +67,7 @@ ScudoTSD *getTSDAndLockSlow() {
     for (u32 i = 0; i < NumberOfTSDs; i++) {
       TSD = &TSDs[i];
       if (TSD->tryLock()) {
-        *get_android_tls_ptr() = reinterpret_cast<uptr>(TSD);
+        setCurrentTSD(TSD);
         return TSD;
       }
     }
@@ -81,12 +82,12 @@ ScudoTSD *getTSDAndLockSlow() {
     }
     if (LIKELY(LowestPrecedence != UINT64_MAX)) {
       TSD->lock();
-      *get_android_tls_ptr() = reinterpret_cast<uptr>(TSD);
+      setCurrentTSD(TSD);
       return TSD;
     }
   }
   // Last resort, stick with the current one.
-  TSD = reinterpret_cast<ScudoTSD *>(*get_android_tls_ptr());
+  TSD = getCurrentTSD();
   TSD->lock();
   return TSD;
 }

Modified: compiler-rt/trunk/lib/scudo/scudo_tsd_shared.inc
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/scudo/scudo_tsd_shared.inc?rev=315583&r1=315582&r2=315583&view=diff
==============================================================================
--- compiler-rt/trunk/lib/scudo/scudo_tsd_shared.inc (original)
+++ compiler-rt/trunk/lib/scudo/scudo_tsd_shared.inc Thu Oct 12 08:01:09 2017
@@ -17,8 +17,12 @@
 
 #if !SCUDO_TSD_EXCLUSIVE
 
+ALWAYS_INLINE ScudoTSD* getCurrentTSD() {
+  return reinterpret_cast<ScudoTSD *>(*get_android_tls_ptr());
+}
+
 ALWAYS_INLINE void initThreadMaybe(bool MinimalInit = false) {
-  if (LIKELY(*get_android_tls_ptr()))
+  if (LIKELY(getCurrentTSD()))
     return;
   initThread(MinimalInit);
 }
@@ -26,7 +30,7 @@ ALWAYS_INLINE void initThreadMaybe(bool
 ScudoTSD *getTSDAndLockSlow();
 
 ALWAYS_INLINE ScudoTSD *getTSDAndLock() {
-  ScudoTSD *TSD = reinterpret_cast<ScudoTSD *>(*get_android_tls_ptr());
+  ScudoTSD *TSD = getCurrentTSD();
   CHECK(TSD && "No TSD associated with the current thread!");
   // Try to lock the currently associated context.
   if (TSD->tryLock())




More information about the llvm-commits mailing list