Kostya Kortchinsky | b59abb2 | 2017-09-26 17:20:02 +0000 | [diff] [blame] | 1 | //===-- scudo_tsd_exclusive.cpp ---------------------------------*- C++ -*-===// |
Kostya Kortchinsky | 36b3434 | 2017-04-27 20:21:16 +0000 | [diff] [blame] | 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | /// |
Kostya Kortchinsky | b59abb2 | 2017-09-26 17:20:02 +0000 | [diff] [blame] | 10 | /// Scudo exclusive TSD implementation. |
Kostya Kortchinsky | 36b3434 | 2017-04-27 20:21:16 +0000 | [diff] [blame] | 11 | /// |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
Kostya Kortchinsky | b59abb2 | 2017-09-26 17:20:02 +0000 | [diff] [blame] | 14 | #include "scudo_tsd.h" |
Kostya Kortchinsky | 36b3434 | 2017-04-27 20:21:16 +0000 | [diff] [blame] | 15 | |
Kostya Kortchinsky | b59abb2 | 2017-09-26 17:20:02 +0000 | [diff] [blame] | 16 | #if SCUDO_TSD_EXCLUSIVE |
Kostya Kortchinsky | 36b3434 | 2017-04-27 20:21:16 +0000 | [diff] [blame] | 17 | |
Kostya Kortchinsky | 36b3434 | 2017-04-27 20:21:16 +0000 | [diff] [blame] | 18 | namespace __scudo { |
| 19 | |
| 20 | static pthread_once_t GlobalInitialized = PTHREAD_ONCE_INIT; |
| 21 | static pthread_key_t PThreadKey; |
| 22 | |
Kostya Kortchinsky | ee069576 | 2017-05-05 21:38:22 +0000 | [diff] [blame] | 23 | __attribute__((tls_model("initial-exec"))) |
| 24 | THREADLOCAL ThreadState ScudoThreadState = ThreadNotInitialized; |
| 25 | __attribute__((tls_model("initial-exec"))) |
Kostya Kortchinsky | 3924809 | 2017-09-22 15:35:37 +0000 | [diff] [blame] | 26 | THREADLOCAL ScudoTSD TSD; |
Kostya Kortchinsky | 36b3434 | 2017-04-27 20:21:16 +0000 | [diff] [blame] | 27 | |
Kostya Kortchinsky | 22396c2 | 2017-09-25 15:12:08 +0000 | [diff] [blame] | 28 | // Fallback TSD for when the thread isn't initialized yet or is torn down. It |
| 29 | // can be shared between multiple threads and as such must be locked. |
| 30 | ScudoTSD FallbackTSD; |
| 31 | |
Kostya Kortchinsky | 36b3434 | 2017-04-27 20:21:16 +0000 | [diff] [blame] | 32 | static void teardownThread(void *Ptr) { |
Kostya Kortchinsky | db18e4d | 2017-05-26 15:39:22 +0000 | [diff] [blame] | 33 | uptr I = reinterpret_cast<uptr>(Ptr); |
Kostya Kortchinsky | 36b3434 | 2017-04-27 20:21:16 +0000 | [diff] [blame] | 34 | // The glibc POSIX thread-local-storage deallocation routine calls user |
| 35 | // provided destructors in a loop of PTHREAD_DESTRUCTOR_ITERATIONS. |
| 36 | // We want to be called last since other destructors might call free and the |
| 37 | // like, so we wait until PTHREAD_DESTRUCTOR_ITERATIONS before draining the |
| 38 | // quarantine and swallowing the cache. |
Kostya Kortchinsky | db18e4d | 2017-05-26 15:39:22 +0000 | [diff] [blame] | 39 | if (I > 1) { |
| 40 | // If pthread_setspecific fails, we will go ahead with the teardown. |
| 41 | if (LIKELY(pthread_setspecific(PThreadKey, |
| 42 | reinterpret_cast<void *>(I - 1)) == 0)) |
| 43 | return; |
Kostya Kortchinsky | 36b3434 | 2017-04-27 20:21:16 +0000 | [diff] [blame] | 44 | } |
Kostya Kortchinsky | 3924809 | 2017-09-22 15:35:37 +0000 | [diff] [blame] | 45 | TSD.commitBack(); |
Kostya Kortchinsky | 36b3434 | 2017-04-27 20:21:16 +0000 | [diff] [blame] | 46 | ScudoThreadState = ThreadTornDown; |
| 47 | } |
| 48 | |
| 49 | |
| 50 | static void initOnce() { |
| 51 | CHECK_EQ(pthread_key_create(&PThreadKey, teardownThread), 0); |
| 52 | initScudo(); |
Kostya Kortchinsky | 22396c2 | 2017-09-25 15:12:08 +0000 | [diff] [blame] | 53 | FallbackTSD.init(/*Shared=*/true); |
Kostya Kortchinsky | 36b3434 | 2017-04-27 20:21:16 +0000 | [diff] [blame] | 54 | } |
| 55 | |
Kostya Kortchinsky | 040c211 | 2017-09-11 19:59:40 +0000 | [diff] [blame] | 56 | void initThread(bool MinimalInit) { |
Kostya Kortchinsky | db18e4d | 2017-05-26 15:39:22 +0000 | [diff] [blame] | 57 | CHECK_EQ(pthread_once(&GlobalInitialized, initOnce), 0); |
Kostya Kortchinsky | 040c211 | 2017-09-11 19:59:40 +0000 | [diff] [blame] | 58 | if (UNLIKELY(MinimalInit)) |
| 59 | return; |
Kostya Kortchinsky | db18e4d | 2017-05-26 15:39:22 +0000 | [diff] [blame] | 60 | CHECK_EQ(pthread_setspecific(PThreadKey, reinterpret_cast<void *>( |
| 61 | GetPthreadDestructorIterations())), 0); |
Kostya Kortchinsky | 22396c2 | 2017-09-25 15:12:08 +0000 | [diff] [blame] | 62 | TSD.init(/*Shared=*/false); |
Kostya Kortchinsky | 36b3434 | 2017-04-27 20:21:16 +0000 | [diff] [blame] | 63 | ScudoThreadState = ThreadInitialized; |
| 64 | } |
| 65 | |
| 66 | } // namespace __scudo |
| 67 | |
Kostya Kortchinsky | b59abb2 | 2017-09-26 17:20:02 +0000 | [diff] [blame] | 68 | #endif // SCUDO_TSD_EXCLUSIVE |