10b57cec5SDimitry Andric //===-- tsd.h ---------------------------------------------------*- C++ -*-===// 20b57cec5SDimitry Andric // 30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information. 50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 60b57cec5SDimitry Andric // 70b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 80b57cec5SDimitry Andric 90b57cec5SDimitry Andric #ifndef SCUDO_TSD_H_ 100b57cec5SDimitry Andric #define SCUDO_TSD_H_ 110b57cec5SDimitry Andric 120b57cec5SDimitry Andric #include "atomic_helpers.h" 130b57cec5SDimitry Andric #include "common.h" 140b57cec5SDimitry Andric #include "mutex.h" 1506c3fb27SDimitry Andric #include "thread_annotations.h" 160b57cec5SDimitry Andric 170b57cec5SDimitry Andric #include <limits.h> // for PTHREAD_DESTRUCTOR_ITERATIONS 18480093f4SDimitry Andric #include <pthread.h> 190b57cec5SDimitry Andric 200b57cec5SDimitry Andric // With some build setups, this might still not be defined. 210b57cec5SDimitry Andric #ifndef PTHREAD_DESTRUCTOR_ITERATIONS 220b57cec5SDimitry Andric #define PTHREAD_DESTRUCTOR_ITERATIONS 4 230b57cec5SDimitry Andric #endif 240b57cec5SDimitry Andric 250b57cec5SDimitry Andric namespace scudo { 260b57cec5SDimitry Andric 275ffd83dbSDimitry Andric template <class Allocator> struct alignas(SCUDO_CACHE_LINE_SIZE) TSD { 28fe6060f1SDimitry Andric using ThisT = TSD<Allocator>; 29fe6060f1SDimitry Andric u8 DestructorIterations = 0; 300b57cec5SDimitry Andric 3106c3fb27SDimitry Andric void init(Allocator *Instance) NO_THREAD_SAFETY_ANALYSIS { 32fe6060f1SDimitry Andric DCHECK_EQ(DestructorIterations, 0U); 33fe6060f1SDimitry Andric DCHECK(isAligned(reinterpret_cast<uptr>(this), alignof(ThisT))); 340b57cec5SDimitry Andric Instance->initCache(&Cache); 350b57cec5SDimitry Andric DestructorIterations = PTHREAD_DESTRUCTOR_ITERATIONS; 360b57cec5SDimitry Andric } 370b57cec5SDimitry Andric 3806c3fb27SDimitry Andric inline bool tryLock() NO_THREAD_SAFETY_ANALYSIS { 390b57cec5SDimitry Andric if (Mutex.tryLock()) { 400b57cec5SDimitry Andric atomic_store_relaxed(&Precedence, 0); 410b57cec5SDimitry Andric return true; 420b57cec5SDimitry Andric } 430b57cec5SDimitry Andric if (atomic_load_relaxed(&Precedence) == 0) 4406c3fb27SDimitry Andric atomic_store_relaxed(&Precedence, 4506c3fb27SDimitry Andric static_cast<uptr>(getMonotonicTimeFast() >> 4606c3fb27SDimitry Andric FIRST_32_SECOND_64(16, 0))); 470b57cec5SDimitry Andric return false; 480b57cec5SDimitry Andric } 4906c3fb27SDimitry Andric inline void lock() NO_THREAD_SAFETY_ANALYSIS { 500b57cec5SDimitry Andric atomic_store_relaxed(&Precedence, 0); 510b57cec5SDimitry Andric Mutex.lock(); 520b57cec5SDimitry Andric } 5306c3fb27SDimitry Andric inline void unlock() NO_THREAD_SAFETY_ANALYSIS { Mutex.unlock(); } 54480093f4SDimitry Andric inline uptr getPrecedence() { return atomic_load_relaxed(&Precedence); } 550b57cec5SDimitry Andric 56*5f757f3fSDimitry Andric void commitBack(Allocator *Instance) { Instance->commitBack(this); } 57*5f757f3fSDimitry Andric 58*5f757f3fSDimitry Andric // As the comments attached to `getCache()`, the TSD doesn't always need to be 59*5f757f3fSDimitry Andric // locked. In that case, we would only skip the check before we have all TSDs 60*5f757f3fSDimitry Andric // locked in all paths. 61*5f757f3fSDimitry Andric void assertLocked(bool BypassCheck) ASSERT_CAPABILITY(Mutex) { 62*5f757f3fSDimitry Andric if (SCUDO_DEBUG && !BypassCheck) 63*5f757f3fSDimitry Andric Mutex.assertHeld(); 6406c3fb27SDimitry Andric } 6506c3fb27SDimitry Andric 6606c3fb27SDimitry Andric // Ideally, we may want to assert that all the operations on 6706c3fb27SDimitry Andric // Cache/QuarantineCache always have the `Mutex` acquired. However, the 6806c3fb27SDimitry Andric // current architecture of accessing TSD is not easy to cooperate with the 6906c3fb27SDimitry Andric // thread-safety analysis because of pointer aliasing. So now we just add the 7006c3fb27SDimitry Andric // assertion on the getters of Cache/QuarantineCache. 7106c3fb27SDimitry Andric // 7206c3fb27SDimitry Andric // TODO(chiahungduan): Ideally, we want to do `Mutex.assertHeld` but acquiring 7306c3fb27SDimitry Andric // TSD doesn't always require holding the lock. Add this assertion while the 7406c3fb27SDimitry Andric // lock is always acquired. 75*5f757f3fSDimitry Andric typename Allocator::CacheT &getCache() REQUIRES(Mutex) { return Cache; } 76*5f757f3fSDimitry Andric typename Allocator::QuarantineCacheT &getQuarantineCache() REQUIRES(Mutex) { 7706c3fb27SDimitry Andric return QuarantineCache; 7806c3fb27SDimitry Andric } 7906c3fb27SDimitry Andric 800b57cec5SDimitry Andric private: 810b57cec5SDimitry Andric HybridMutex Mutex; 82fe6060f1SDimitry Andric atomic_uptr Precedence = {}; 8306c3fb27SDimitry Andric 8406c3fb27SDimitry Andric typename Allocator::CacheT Cache GUARDED_BY(Mutex); 8506c3fb27SDimitry Andric typename Allocator::QuarantineCacheT QuarantineCache GUARDED_BY(Mutex); 860b57cec5SDimitry Andric }; 870b57cec5SDimitry Andric 880b57cec5SDimitry Andric } // namespace scudo 890b57cec5SDimitry Andric 900b57cec5SDimitry Andric #endif // SCUDO_TSD_H_ 91