xref: /netbsd-src/external/gpl3/gcc.old/dist/libsanitizer/lsan/lsan_thread.cc (revision c0a68be459da21030695f60d10265c2fc49758f8)
1 //=-- lsan_thread.cc ------------------------------------------------------===//
2 //
3 // This file is distributed under the University of Illinois Open Source
4 // License. See LICENSE.TXT for details.
5 //
6 //===----------------------------------------------------------------------===//
7 //
8 // This file is a part of LeakSanitizer.
9 // See lsan_thread.h for details.
10 //
11 //===----------------------------------------------------------------------===//
12 
13 #include "lsan_thread.h"
14 
15 #include "sanitizer_common/sanitizer_common.h"
16 #include "sanitizer_common/sanitizer_placement_new.h"
17 #include "sanitizer_common/sanitizer_thread_registry.h"
18 #include "sanitizer_common/sanitizer_tls_get_addr.h"
19 #include "lsan_allocator.h"
20 #include "lsan_common.h"
21 
22 namespace __lsan {
23 
24 static ThreadRegistry *thread_registry;
25 
CreateThreadContext(u32 tid)26 static ThreadContextBase *CreateThreadContext(u32 tid) {
27   void *mem = MmapOrDie(sizeof(ThreadContext), "ThreadContext");
28   return new(mem) ThreadContext(tid);
29 }
30 
31 static const uptr kMaxThreads = 1 << 13;
32 static const uptr kThreadQuarantineSize = 64;
33 
InitializeThreadRegistry()34 void InitializeThreadRegistry() {
35   static ALIGNED(64) char thread_registry_placeholder[sizeof(ThreadRegistry)];
36   thread_registry = new(thread_registry_placeholder)
37     ThreadRegistry(CreateThreadContext, kMaxThreads, kThreadQuarantineSize);
38 }
39 
ThreadContext(int tid)40 ThreadContext::ThreadContext(int tid)
41     : ThreadContextBase(tid),
42       stack_begin_(0),
43       stack_end_(0),
44       cache_begin_(0),
45       cache_end_(0),
46       tls_begin_(0),
47       tls_end_(0),
48       dtls_(nullptr) {}
49 
50 struct OnStartedArgs {
51   uptr stack_begin, stack_end,
52        cache_begin, cache_end,
53        tls_begin, tls_end;
54   DTLS *dtls;
55 };
56 
OnStarted(void * arg)57 void ThreadContext::OnStarted(void *arg) {
58   OnStartedArgs *args = reinterpret_cast<OnStartedArgs *>(arg);
59   stack_begin_ = args->stack_begin;
60   stack_end_ = args->stack_end;
61   tls_begin_ = args->tls_begin;
62   tls_end_ = args->tls_end;
63   cache_begin_ = args->cache_begin;
64   cache_end_ = args->cache_end;
65   dtls_ = args->dtls;
66 }
67 
OnFinished()68 void ThreadContext::OnFinished() {
69   AllocatorThreadFinish();
70   DTLS_Destroy();
71 }
72 
ThreadCreate(u32 parent_tid,uptr user_id,bool detached)73 u32 ThreadCreate(u32 parent_tid, uptr user_id, bool detached) {
74   return thread_registry->CreateThread(user_id, detached, parent_tid,
75                                        /* arg */ nullptr);
76 }
77 
ThreadStart(u32 tid,tid_t os_id,bool workerthread)78 void ThreadStart(u32 tid, tid_t os_id, bool workerthread) {
79   OnStartedArgs args;
80   uptr stack_size = 0;
81   uptr tls_size = 0;
82   GetThreadStackAndTls(tid == 0, &args.stack_begin, &stack_size,
83                        &args.tls_begin, &tls_size);
84   args.stack_end = args.stack_begin + stack_size;
85   args.tls_end = args.tls_begin + tls_size;
86   GetAllocatorCacheRange(&args.cache_begin, &args.cache_end);
87   args.dtls = DTLS_Get();
88   thread_registry->StartThread(tid, os_id, workerthread, &args);
89 }
90 
ThreadFinish()91 void ThreadFinish() {
92   thread_registry->FinishThread(GetCurrentThread());
93   SetCurrentThread(kInvalidTid);
94 }
95 
CurrentThreadContext()96 ThreadContext *CurrentThreadContext() {
97   if (!thread_registry) return nullptr;
98   if (GetCurrentThread() == kInvalidTid)
99     return nullptr;
100   // No lock needed when getting current thread.
101   return (ThreadContext *)thread_registry->GetThreadLocked(GetCurrentThread());
102 }
103 
FindThreadByUid(ThreadContextBase * tctx,void * arg)104 static bool FindThreadByUid(ThreadContextBase *tctx, void *arg) {
105   uptr uid = (uptr)arg;
106   if (tctx->user_id == uid && tctx->status != ThreadStatusInvalid) {
107     return true;
108   }
109   return false;
110 }
111 
ThreadTid(uptr uid)112 u32 ThreadTid(uptr uid) {
113   return thread_registry->FindThread(FindThreadByUid, (void*)uid);
114 }
115 
ThreadJoin(u32 tid)116 void ThreadJoin(u32 tid) {
117   CHECK_NE(tid, kInvalidTid);
118   thread_registry->JoinThread(tid, /* arg */nullptr);
119 }
120 
EnsureMainThreadIDIsCorrect()121 void EnsureMainThreadIDIsCorrect() {
122   if (GetCurrentThread() == 0)
123     CurrentThreadContext()->os_id = GetTid();
124 }
125 
126 ///// Interface to the common LSan module. /////
127 
GetThreadRangesLocked(tid_t os_id,uptr * stack_begin,uptr * stack_end,uptr * tls_begin,uptr * tls_end,uptr * cache_begin,uptr * cache_end,DTLS ** dtls)128 bool GetThreadRangesLocked(tid_t os_id, uptr *stack_begin, uptr *stack_end,
129                            uptr *tls_begin, uptr *tls_end, uptr *cache_begin,
130                            uptr *cache_end, DTLS **dtls) {
131   ThreadContext *context = static_cast<ThreadContext *>(
132       thread_registry->FindThreadContextByOsIDLocked(os_id));
133   if (!context) return false;
134   *stack_begin = context->stack_begin();
135   *stack_end = context->stack_end();
136   *tls_begin = context->tls_begin();
137   *tls_end = context->tls_end();
138   *cache_begin = context->cache_begin();
139   *cache_end = context->cache_end();
140   *dtls = context->dtls();
141   return true;
142 }
143 
ForEachExtraStackRange(tid_t os_id,RangeIteratorCallback callback,void * arg)144 void ForEachExtraStackRange(tid_t os_id, RangeIteratorCallback callback,
145                             void *arg) {
146 }
147 
LockThreadRegistry()148 void LockThreadRegistry() {
149   thread_registry->Lock();
150 }
151 
UnlockThreadRegistry()152 void UnlockThreadRegistry() {
153   thread_registry->Unlock();
154 }
155 
GetThreadRegistryLocked()156 ThreadRegistry *GetThreadRegistryLocked() {
157   thread_registry->CheckLocked();
158   return thread_registry;
159 }
160 
161 } // namespace __lsan
162