blob: eb47197344ad63b35875eb64a39c8ead161e9a46 [file] [log] [blame]
Kostya Kortchinskyb59abb22017-09-26 17:20:02 +00001//===-- scudo_tsd_exclusive.cpp ---------------------------------*- C++ -*-===//
Kostya Kortchinsky36b34342017-04-27 20:21:16 +00002//
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 Kortchinskyb59abb22017-09-26 17:20:02 +000010/// Scudo exclusive TSD implementation.
Kostya Kortchinsky36b34342017-04-27 20:21:16 +000011///
12//===----------------------------------------------------------------------===//
13
Kostya Kortchinskyb59abb22017-09-26 17:20:02 +000014#include "scudo_tsd.h"
Kostya Kortchinsky36b34342017-04-27 20:21:16 +000015
Kostya Kortchinskyb59abb22017-09-26 17:20:02 +000016#if SCUDO_TSD_EXCLUSIVE
Kostya Kortchinsky36b34342017-04-27 20:21:16 +000017
Kostya Kortchinsky36b34342017-04-27 20:21:16 +000018#include <pthread.h>
19
20namespace __scudo {
21
22static pthread_once_t GlobalInitialized = PTHREAD_ONCE_INIT;
23static pthread_key_t PThreadKey;
24
Kostya Kortchinskyee0695762017-05-05 21:38:22 +000025__attribute__((tls_model("initial-exec")))
26THREADLOCAL ThreadState ScudoThreadState = ThreadNotInitialized;
27__attribute__((tls_model("initial-exec")))
Kostya Kortchinsky39248092017-09-22 15:35:37 +000028THREADLOCAL ScudoTSD TSD;
Kostya Kortchinsky36b34342017-04-27 20:21:16 +000029
Kostya Kortchinsky22396c22017-09-25 15:12:08 +000030// Fallback TSD for when the thread isn't initialized yet or is torn down. It
31// can be shared between multiple threads and as such must be locked.
32ScudoTSD FallbackTSD;
33
Kostya Kortchinsky36b34342017-04-27 20:21:16 +000034static void teardownThread(void *Ptr) {
Kostya Kortchinskydb18e4d2017-05-26 15:39:22 +000035 uptr I = reinterpret_cast<uptr>(Ptr);
Kostya Kortchinsky36b34342017-04-27 20:21:16 +000036 // The glibc POSIX thread-local-storage deallocation routine calls user
37 // provided destructors in a loop of PTHREAD_DESTRUCTOR_ITERATIONS.
38 // We want to be called last since other destructors might call free and the
39 // like, so we wait until PTHREAD_DESTRUCTOR_ITERATIONS before draining the
40 // quarantine and swallowing the cache.
Kostya Kortchinskydb18e4d2017-05-26 15:39:22 +000041 if (I > 1) {
42 // If pthread_setspecific fails, we will go ahead with the teardown.
43 if (LIKELY(pthread_setspecific(PThreadKey,
44 reinterpret_cast<void *>(I - 1)) == 0))
45 return;
Kostya Kortchinsky36b34342017-04-27 20:21:16 +000046 }
Kostya Kortchinsky39248092017-09-22 15:35:37 +000047 TSD.commitBack();
Kostya Kortchinsky36b34342017-04-27 20:21:16 +000048 ScudoThreadState = ThreadTornDown;
49}
50
51
52static void initOnce() {
53 CHECK_EQ(pthread_key_create(&PThreadKey, teardownThread), 0);
54 initScudo();
Kostya Kortchinsky22396c22017-09-25 15:12:08 +000055 FallbackTSD.init(/*Shared=*/true);
Kostya Kortchinsky36b34342017-04-27 20:21:16 +000056}
57
Kostya Kortchinsky040c2112017-09-11 19:59:40 +000058void initThread(bool MinimalInit) {
Kostya Kortchinskydb18e4d2017-05-26 15:39:22 +000059 CHECK_EQ(pthread_once(&GlobalInitialized, initOnce), 0);
Kostya Kortchinsky040c2112017-09-11 19:59:40 +000060 if (UNLIKELY(MinimalInit))
61 return;
Kostya Kortchinskydb18e4d2017-05-26 15:39:22 +000062 CHECK_EQ(pthread_setspecific(PThreadKey, reinterpret_cast<void *>(
63 GetPthreadDestructorIterations())), 0);
Kostya Kortchinsky22396c22017-09-25 15:12:08 +000064 TSD.init(/*Shared=*/false);
Kostya Kortchinsky36b34342017-04-27 20:21:16 +000065 ScudoThreadState = ThreadInitialized;
66}
67
68} // namespace __scudo
69
Kostya Kortchinskyb59abb22017-09-26 17:20:02 +000070#endif // SCUDO_TSD_EXCLUSIVE