[compiler-rt] 561fa84 - [scudo][standalone] Allow sched_getaffinity to fail

Kostya Kortchinsky via llvm-commits llvm-commits at lists.llvm.org
Tue Jan 21 11:20:18 PST 2020


Author: Kostya Kortchinsky
Date: 2020-01-21T11:18:18-08:00
New Revision: 561fa844777c1bd9c0c6937be272e6ea73ff94f6

URL: https://github.com/llvm/llvm-project/commit/561fa844777c1bd9c0c6937be272e6ea73ff94f6
DIFF: https://github.com/llvm/llvm-project/commit/561fa844777c1bd9c0c6937be272e6ea73ff94f6.diff

LOG: [scudo][standalone] Allow sched_getaffinity to fail

Summary:
In some configuration, `sched_getaffinity` can fail. Some reasons for
that being the lack of `CAP_SYS_NICE` capability or some syscall
filtering and so on.

This should not be fatal to the allocator, so in this situation, we
will fallback to the `MaxTSDCount` value specified in the allocator
configuration.

Reviewers: cferris, eugenis, hctim, morehouse, pcc

Subscribers: #sanitizers, llvm-commits

Tags: #sanitizers, #llvm

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

Added: 
    

Modified: 
    compiler-rt/lib/scudo/standalone/common.h
    compiler-rt/lib/scudo/standalone/linux.cpp
    compiler-rt/lib/scudo/standalone/tsd_shared.h

Removed: 
    


################################################################################
diff  --git a/compiler-rt/lib/scudo/standalone/common.h b/compiler-rt/lib/scudo/standalone/common.h
index a700eb5eaa3f..e026e34c0045 100644
--- a/compiler-rt/lib/scudo/standalone/common.h
+++ b/compiler-rt/lib/scudo/standalone/common.h
@@ -126,6 +126,7 @@ inline uptr getPageSizeCached() {
   return getPageSizeSlow();
 }
 
+// Returns 0 if the number of CPUs could not be determined.
 u32 getNumberOfCPUs();
 
 const char *getEnv(const char *Name);

diff  --git a/compiler-rt/lib/scudo/standalone/linux.cpp b/compiler-rt/lib/scudo/standalone/linux.cpp
index 3c120845c2d8..3b7562844d72 100644
--- a/compiler-rt/lib/scudo/standalone/linux.cpp
+++ b/compiler-rt/lib/scudo/standalone/linux.cpp
@@ -132,7 +132,10 @@ u64 getMonotonicTime() {
 
 u32 getNumberOfCPUs() {
   cpu_set_t CPUs;
-  CHECK_EQ(sched_getaffinity(0, sizeof(cpu_set_t), &CPUs), 0);
+  // sched_getaffinity can fail for a variety of legitimate reasons (lack of
+  // CAP_SYS_NICE, syscall filtering, etc), in which case we shall return 0.
+  if (sched_getaffinity(0, sizeof(cpu_set_t), &CPUs) != 0)
+    return 0;
   return static_cast<u32>(CPU_COUNT(&CPUs));
 }
 

diff  --git a/compiler-rt/lib/scudo/standalone/tsd_shared.h b/compiler-rt/lib/scudo/standalone/tsd_shared.h
index 5ab8269519a9..1626732c70f4 100644
--- a/compiler-rt/lib/scudo/standalone/tsd_shared.h
+++ b/compiler-rt/lib/scudo/standalone/tsd_shared.h
@@ -18,7 +18,9 @@ template <class Allocator, u32 MaxTSDCount> struct TSDRegistrySharedT {
   void initLinkerInitialized(Allocator *Instance) {
     Instance->initLinkerInitialized();
     CHECK_EQ(pthread_key_create(&PThreadKey, nullptr), 0); // For non-TLS
-    NumberOfTSDs = Min(Max(1U, getNumberOfCPUs()), MaxTSDCount);
+    const u32 NumberOfCPUs = getNumberOfCPUs();
+    NumberOfTSDs =
+        (NumberOfCPUs == 0) ? MaxTSDCount : Min(NumberOfCPUs, MaxTSDCount);
     TSDs = reinterpret_cast<TSD<Allocator> *>(
         map(nullptr, sizeof(TSD<Allocator>) * NumberOfTSDs, "scudo:tsd"));
     for (u32 I = 0; I < NumberOfTSDs; I++)


        


More information about the llvm-commits mailing list