From 84c2c4977dfe89112fd564a69c693d271663229c Mon Sep 17 00:00:00 2001 From: Peter Collingbourne Date: Wed, 9 Sep 2020 15:15:46 -0700 Subject: [PATCH] scudo: Introduce a new mechanism to let Scudo access a platform-specific TLS slot An upcoming change to Scudo will change how we use the TLS slot in tsd_shared.h, which will be a little easier to deal with if we can remove the code path that calls pthread_getspecific and pthread_setspecific. The only known user of this code path is Fuchsia. We can't eliminate this code path by making Fuchsia use ELF TLS because although Fuchsia supports ELF TLS, it is not supported within libc itself. To address this, Roland McGrath on the Fuchsia team has proposed that Scudo will optionally call a platform-provided function to access a TLS slot reserved for Scudo. Android also has a reserved TLS slot, but the code that accesses the TLS slot lives in Scudo. We can eliminate some complexity and duplicated code by having Android use the same mechanism that was proposed for Fuchsia, which is what this change does. A separate change to Android implements it. Differential Revision: https://reviews.llvm.org/D87420 --- compiler-rt/lib/scudo/standalone/linux.h | 45 ------------------- compiler-rt/lib/scudo/standalone/tsd_shared.h | 44 ++++++++---------- 2 files changed, 19 insertions(+), 70 deletions(-) diff --git a/compiler-rt/lib/scudo/standalone/linux.h b/compiler-rt/lib/scudo/standalone/linux.h index c8e41484c851..72acb6da83a7 100644 --- a/compiler-rt/lib/scudo/standalone/linux.h +++ b/compiler-rt/lib/scudo/standalone/linux.h @@ -18,51 +18,6 @@ namespace scudo { // MapPlatformData is unused on Linux, define it as a minimally sized structure. struct MapPlatformData {}; -#if SCUDO_ANDROID - -#if defined(__aarch64__) -#define __get_tls() \ - ({ \ - void **__v; \ - __asm__("mrs %0, tpidr_el0" : "=r"(__v)); \ - __v; \ - }) -#elif defined(__arm__) -#define __get_tls() \ - ({ \ - void **__v; \ - __asm__("mrc p15, 0, %0, c13, c0, 3" : "=r"(__v)); \ - __v; \ - }) -#elif defined(__i386__) -#define __get_tls() \ - ({ \ - void **__v; \ - __asm__("movl %%gs:0, %0" : "=r"(__v)); \ - __v; \ - }) -#elif defined(__x86_64__) -#define __get_tls() \ - ({ \ - void **__v; \ - __asm__("mov %%fs:0, %0" : "=r"(__v)); \ - __v; \ - }) -#else -#error "Unsupported architecture." -#endif - -// The Android Bionic team has allocated a TLS slot for sanitizers starting -// with Q, given that Android currently doesn't support ELF TLS. It is used to -// store sanitizer thread specific data. -static const int TLS_SLOT_SANITIZER = 6; - -ALWAYS_INLINE uptr *getAndroidTlsPtr() { - return reinterpret_cast(&__get_tls()[TLS_SLOT_SANITIZER]); -} - -#endif // SCUDO_ANDROID - } // namespace scudo #endif // SCUDO_LINUX diff --git a/compiler-rt/lib/scudo/standalone/tsd_shared.h b/compiler-rt/lib/scudo/standalone/tsd_shared.h index 25ba191826c3..041b834c7485 100644 --- a/compiler-rt/lib/scudo/standalone/tsd_shared.h +++ b/compiler-rt/lib/scudo/standalone/tsd_shared.h @@ -9,9 +9,17 @@ #ifndef SCUDO_TSD_SHARED_H_ #define SCUDO_TSD_SHARED_H_ -#include "linux.h" // for getAndroidTlsPtr() #include "tsd.h" +#if SCUDO_HAS_PLATFORM_TLS_SLOT +// This is a platform-provided header that needs to be on the include path when +// Scudo is compiled. It must declare a function with the prototype: +// uintptr_t *getPlatformAllocatorTlsSlot() +// that returns the address of a thread-local word of storage reserved for +// Scudo, that must be zero-initialized in newly created threads. +#include "scudo_platform_tls_slot.h" +#endif + namespace scudo { template @@ -80,26 +88,21 @@ struct TSDRegistrySharedT { } private: - ALWAYS_INLINE void setCurrentTSD(TSD *CurrentTSD) { -#if _BIONIC - *getAndroidTlsPtr() = reinterpret_cast(CurrentTSD); -#elif SCUDO_LINUX - ThreadTSD = CurrentTSD; + ALWAYS_INLINE uptr *getTlsPtr() const { +#if SCUDO_HAS_PLATFORM_TLS_SLOT + return reinterpret_cast(getPlatformAllocatorTlsSlot()); #else - CHECK_EQ( - pthread_setspecific(PThreadKey, reinterpret_cast(CurrentTSD)), - 0); + static thread_local uptr ThreadTSD; + return &ThreadTSD; #endif } + ALWAYS_INLINE void setCurrentTSD(TSD *CurrentTSD) { + *getTlsPtr() = reinterpret_cast(CurrentTSD); + } + ALWAYS_INLINE TSD *getCurrentTSD() { -#if _BIONIC - return reinterpret_cast *>(*getAndroidTlsPtr()); -#elif SCUDO_LINUX - return ThreadTSD; -#else - return reinterpret_cast *>(pthread_getspecific(PThreadKey)); -#endif + return reinterpret_cast *>(*getTlsPtr()); } bool setNumberOfTSDs(u32 N) { @@ -195,17 +198,8 @@ private: HybridMutex Mutex; HybridMutex MutexTSDs; TSD TSDs[TSDsArraySize]; -#if SCUDO_LINUX && !_BIONIC - static THREADLOCAL TSD *ThreadTSD; -#endif }; -#if SCUDO_LINUX && !_BIONIC -template -THREADLOCAL TSD - *TSDRegistrySharedT::ThreadTSD; -#endif - } // namespace scudo #endif // SCUDO_TSD_SHARED_H_