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"
17 #include "lsan_allocator.h"
18 #include "lsan_common.h"
19 #include "sanitizer_common/sanitizer_common.h"
20 #include "sanitizer_common/sanitizer_placement_new.h"
21 #include "sanitizer_common/sanitizer_thread_registry.h"
22 #include "sanitizer_common/sanitizer_tls_get_addr.h"
26 static ThreadRegistry
*thread_registry
;
28 static ThreadContextBase
*CreateThreadContext(u32 tid
) {
29 void *mem
= MmapOrDie(sizeof(ThreadContext
), "ThreadContext");
30 return new (mem
) ThreadContext(tid
);
33 static const uptr kMaxThreads
= 1 << 13;
34 static const uptr kThreadQuarantineSize
= 64;
36 void InitializeThreadRegistry() {
37 static ALIGNED(64) char thread_registry_placeholder
[sizeof(ThreadRegistry
)];
38 thread_registry
= new (thread_registry_placeholder
)
39 ThreadRegistry(CreateThreadContext
, kMaxThreads
, kThreadQuarantineSize
);
42 ThreadContextLsanBase::ThreadContextLsanBase(int tid
)
43 : ThreadContextBase(tid
) {}
45 void ThreadContextLsanBase::OnFinished() {
46 AllocatorThreadFinish();
50 u32
ThreadCreate(u32 parent_tid
, uptr user_id
, bool detached
, void *arg
) {
51 return thread_registry
->CreateThread(user_id
, detached
, parent_tid
, arg
);
54 void ThreadContextLsanBase::ThreadStart(u32 tid
, tid_t os_id
,
55 ThreadType thread_type
, void *arg
) {
56 thread_registry
->StartThread(tid
, os_id
, thread_type
, arg
);
57 SetCurrentThread(tid
);
61 thread_registry
->FinishThread(GetCurrentThread());
62 SetCurrentThread(kInvalidTid
);
65 ThreadContext
*CurrentThreadContext() {
68 if (GetCurrentThread() == kInvalidTid
)
70 // No lock needed when getting current thread.
71 return (ThreadContext
*)thread_registry
->GetThreadLocked(GetCurrentThread());
74 static bool FindThreadByUid(ThreadContextBase
*tctx
, void *arg
) {
76 if (tctx
->user_id
== uid
&& tctx
->status
!= ThreadStatusInvalid
) {
82 u32
ThreadTid(uptr uid
) {
83 return thread_registry
->FindThread(FindThreadByUid
, (void *)uid
);
86 void ThreadJoin(u32 tid
) {
87 CHECK_NE(tid
, kInvalidTid
);
88 thread_registry
->JoinThread(tid
, /* arg */ nullptr);
91 void EnsureMainThreadIDIsCorrect() {
92 if (GetCurrentThread() == 0)
93 CurrentThreadContext()->os_id
= GetTid();
96 ///// Interface to the common LSan module. /////
98 void ForEachExtraStackRange(tid_t os_id
, RangeIteratorCallback callback
,
101 void LockThreadRegistry() { thread_registry
->Lock(); }
103 void UnlockThreadRegistry() { thread_registry
->Unlock(); }
105 ThreadRegistry
*GetThreadRegistryLocked() {
106 thread_registry
->CheckLocked();
107 return thread_registry
;
110 } // namespace __lsan