From 6458216b28fe2c7b2870cd91570f237441a25428 Mon Sep 17 00:00:00 2001 From: Kostya Kortchinsky Date: Thu, 9 Nov 2017 19:18:55 +0000 Subject: [PATCH] [scudo] Make getNumberOfCPUs Fuchsia compliant Summary: This change allows Fuchsia to boot properly using the Scudo allocator. Reviewers: cryptoad, alekseyshl, krytarowski Reviewed By: cryptoad, krytarowski Subscribers: rnk, krytarowski, kubamracek, llvm-commits Differential Revision: https://reviews.llvm.org/D39490 llvm-svn: 317822 --- .../lib/sanitizer_common/sanitizer_common.h | 3 +++ .../lib/sanitizer_common/sanitizer_fuchsia.cc | 4 ++++ .../sanitizer_common/sanitizer_linux_libcdep.cc | 23 ++++++++++++++++++++++ compiler-rt/lib/sanitizer_common/sanitizer_mac.cc | 5 +++++ compiler-rt/lib/sanitizer_common/sanitizer_win.cc | 5 +++++ compiler-rt/lib/scudo/scudo_tsd_shared.cpp | 9 +-------- 6 files changed, 41 insertions(+), 8 deletions(-) diff --git a/compiler-rt/lib/sanitizer_common/sanitizer_common.h b/compiler-rt/lib/sanitizer_common/sanitizer_common.h index d64d3e1..b322f47 100644 --- a/compiler-rt/lib/sanitizer_common/sanitizer_common.h +++ b/compiler-rt/lib/sanitizer_common/sanitizer_common.h @@ -933,6 +933,9 @@ void CheckNoDeepBind(const char *filename, int flag); // be used to seed a PRNG. Defaults to blocking like the underlying syscall. bool GetRandom(void *buffer, uptr length, bool blocking = true); +// Returns the number of logical processors on the system. +u32 GetNumberOfCPUs(); + } // namespace __sanitizer inline void *operator new(__sanitizer::operator_new_size_type size, diff --git a/compiler-rt/lib/sanitizer_common/sanitizer_fuchsia.cc b/compiler-rt/lib/sanitizer_common/sanitizer_fuchsia.cc index 97dff0d..45104e5 100644 --- a/compiler-rt/lib/sanitizer_common/sanitizer_fuchsia.cc +++ b/compiler-rt/lib/sanitizer_common/sanitizer_fuchsia.cc @@ -520,6 +520,10 @@ bool GetRandom(void *buffer, uptr length, bool blocking) { return true; } +u32 GetNumberOfCPUs() { + return zx_system_get_num_cpus(); +} + } // namespace __sanitizer using namespace __sanitizer; // NOLINT diff --git a/compiler-rt/lib/sanitizer_common/sanitizer_linux_libcdep.cc b/compiler-rt/lib/sanitizer_common/sanitizer_linux_libcdep.cc index 0dc4375..5f9404e 100644 --- a/compiler-rt/lib/sanitizer_common/sanitizer_linux_libcdep.cc +++ b/compiler-rt/lib/sanitizer_common/sanitizer_linux_libcdep.cc @@ -30,6 +30,7 @@ #include // for dlsym() #include #include +#include #include #include #include @@ -37,9 +38,14 @@ #if SANITIZER_FREEBSD #include #include +#include #define pthread_getattr_np pthread_attr_get_np #endif +#if SANITIZER_NETBSD +#include +#endif + #if SANITIZER_LINUX #include #endif @@ -538,6 +544,23 @@ uptr GetRSS() { return rss * GetPageSizeCached(); } +// sysconf(_SC_NPROCESSORS_{CONF,ONLN}) cannot be used as they allocate memory. +u32 GetNumberOfCPUs() { +#if SANITIZER_FREEBSD || SANITIZER_NETBSD + u32 ncpu; + int req[2]; + size_t len = sizeof(ncpu); + req[0] = CTL_HW; + req[1] = HW_NCPU; + CHECK_EQ(sysctl(req, 2, &ncpu, &len, NULL, 0), 0); + return ncpu; +#else + cpu_set_t CPUs; + CHECK_EQ(sched_getaffinity(0, sizeof(cpu_set_t), &CPUs), 0); + return CPU_COUNT(&CPUs); +#endif +} + // 64-bit Android targets don't provide the deprecated __android_log_write. // Starting with the L release, syslog() works and is preferable to // __android_log_write. diff --git a/compiler-rt/lib/sanitizer_common/sanitizer_mac.cc b/compiler-rt/lib/sanitizer_common/sanitizer_mac.cc index 7a93c11..fbc8cdd 100644 --- a/compiler-rt/lib/sanitizer_common/sanitizer_mac.cc +++ b/compiler-rt/lib/sanitizer_common/sanitizer_mac.cc @@ -1000,6 +1000,11 @@ bool GetRandom(void *buffer, uptr length, bool blocking) { UNIMPLEMENTED(); } +// FIXME: implement on this platform. +u32 GetNumberOfCPUs() { + UNIMPLEMENTED(); +} + } // namespace __sanitizer #endif // SANITIZER_MAC diff --git a/compiler-rt/lib/sanitizer_common/sanitizer_win.cc b/compiler-rt/lib/sanitizer_common/sanitizer_win.cc index 1186971..7a9a947 100644 --- a/compiler-rt/lib/sanitizer_common/sanitizer_win.cc +++ b/compiler-rt/lib/sanitizer_common/sanitizer_win.cc @@ -1093,6 +1093,11 @@ bool GetRandom(void *buffer, uptr length, bool blocking) { UNIMPLEMENTED(); } +// FIXME: implement on this platform. +u32 GetNumberOfCPUs() { + UNIMPLEMENTED(); +} + } // namespace __sanitizer #endif // _WIN32 diff --git a/compiler-rt/lib/scudo/scudo_tsd_shared.cpp b/compiler-rt/lib/scudo/scudo_tsd_shared.cpp index 191c9ff..25575af 100644 --- a/compiler-rt/lib/scudo/scudo_tsd_shared.cpp +++ b/compiler-rt/lib/scudo/scudo_tsd_shared.cpp @@ -24,17 +24,10 @@ static atomic_uint32_t CurrentIndex; static ScudoTSD *TSDs; static u32 NumberOfTSDs; -// sysconf(_SC_NPROCESSORS_{CONF,ONLN}) cannot be used as they allocate memory. -static u32 getNumberOfCPUs() { - cpu_set_t CPUs; - CHECK_EQ(sched_getaffinity(0, sizeof(cpu_set_t), &CPUs), 0); - return CPU_COUNT(&CPUs); -} - static void initOnce() { CHECK_EQ(pthread_key_create(&PThreadKey, NULL), 0); initScudo(); - NumberOfTSDs = Min(Max(1U, getNumberOfCPUs()), + NumberOfTSDs = Min(Max(1U, GetNumberOfCPUs()), static_cast(SCUDO_SHARED_TSD_POOL_SIZE)); TSDs = reinterpret_cast( MmapOrDie(sizeof(ScudoTSD) * NumberOfTSDs, "ScudoTSDs")); -- 2.7.4