1 //=-- lsan_thread.cpp -----------------------------------------------------===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 // This file is a part of LeakSanitizer.
10 // See lsan_thread.h for details.
12 //===----------------------------------------------------------------------===//
14 #include "lsan_thread.h"
16 #include "sanitizer_common/sanitizer_common.h"
17 #include "sanitizer_common/sanitizer_placement_new.h"
18 #include "sanitizer_common/sanitizer_thread_registry.h"
19 #include "sanitizer_common/sanitizer_tls_get_addr.h"
20 #include "lsan_allocator.h"
21 #include "lsan_common.h"
25 static ThreadRegistry *thread_registry;
27 static ThreadContextBase *CreateThreadContext(u32 tid) {
28 void *mem = MmapOrDie(sizeof(ThreadContext), "ThreadContext");
29 return new(mem) ThreadContext(tid);
32 static const uptr kMaxThreads = 1 << 13;
33 static const uptr kThreadQuarantineSize = 64;
35 void InitializeThreadRegistry() {
36 static ALIGNED(64) char thread_registry_placeholder[sizeof(ThreadRegistry)];
37 thread_registry = new(thread_registry_placeholder)
38 ThreadRegistry(CreateThreadContext, kMaxThreads, kThreadQuarantineSize);
41 ThreadContext::ThreadContext(int tid)
42 : ThreadContextBase(tid),
51 struct OnStartedArgs {
52 uptr stack_begin, stack_end,
53 cache_begin, cache_end,
58 void ThreadContext::OnStarted(void *arg) {
59 OnStartedArgs *args = reinterpret_cast<OnStartedArgs *>(arg);
60 stack_begin_ = args->stack_begin;
61 stack_end_ = args->stack_end;
62 tls_begin_ = args->tls_begin;
63 tls_end_ = args->tls_end;
64 cache_begin_ = args->cache_begin;
65 cache_end_ = args->cache_end;
69 void ThreadContext::OnFinished() {
70 AllocatorThreadFinish();
74 u32 ThreadCreate(u32 parent_tid, uptr user_id, bool detached) {
75 return thread_registry->CreateThread(user_id, detached, parent_tid,
79 void ThreadStart(u32 tid, tid_t os_id, ThreadType thread_type) {
83 GetThreadStackAndTls(tid == 0, &args.stack_begin, &stack_size,
84 &args.tls_begin, &tls_size);
85 args.stack_end = args.stack_begin + stack_size;
86 args.tls_end = args.tls_begin + tls_size;
87 GetAllocatorCacheRange(&args.cache_begin, &args.cache_end);
88 args.dtls = DTLS_Get();
89 thread_registry->StartThread(tid, os_id, thread_type, &args);
93 thread_registry->FinishThread(GetCurrentThread());
94 SetCurrentThread(kInvalidTid);
97 ThreadContext *CurrentThreadContext() {
98 if (!thread_registry) return nullptr;
99 if (GetCurrentThread() == kInvalidTid)
101 // No lock needed when getting current thread.
102 return (ThreadContext *)thread_registry->GetThreadLocked(GetCurrentThread());
105 static bool FindThreadByUid(ThreadContextBase *tctx, void *arg) {
106 uptr uid = (uptr)arg;
107 if (tctx->user_id == uid && tctx->status != ThreadStatusInvalid) {
113 u32 ThreadTid(uptr uid) {
114 return thread_registry->FindThread(FindThreadByUid, (void*)uid);
117 void ThreadJoin(u32 tid) {
118 CHECK_NE(tid, kInvalidTid);
119 thread_registry->JoinThread(tid, /* arg */nullptr);
122 void EnsureMainThreadIDIsCorrect() {
123 if (GetCurrentThread() == 0)
124 CurrentThreadContext()->os_id = GetTid();
127 ///// Interface to the common LSan module. /////
129 bool GetThreadRangesLocked(tid_t os_id, uptr *stack_begin, uptr *stack_end,
130 uptr *tls_begin, uptr *tls_end, uptr *cache_begin,
131 uptr *cache_end, DTLS **dtls) {
132 ThreadContext *context = static_cast<ThreadContext *>(
133 thread_registry->FindThreadContextByOsIDLocked(os_id));
134 if (!context) return false;
135 *stack_begin = context->stack_begin();
136 *stack_end = context->stack_end();
137 *tls_begin = context->tls_begin();
138 *tls_end = context->tls_end();
139 *cache_begin = context->cache_begin();
140 *cache_end = context->cache_end();
141 *dtls = context->dtls();
145 void ForEachExtraStackRange(tid_t os_id, RangeIteratorCallback callback,
149 void LockThreadRegistry() {
150 thread_registry->Lock();
153 void UnlockThreadRegistry() {
154 thread_registry->Unlock();
157 ThreadRegistry *GetThreadRegistryLocked() {
158 thread_registry->CheckLocked();
159 return thread_registry;
162 } // namespace __lsan