1//=-- lsan_thread.cpp -----------------------------------------------------===//
2//
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
6//
7//===----------------------------------------------------------------------===//
8//
9// This file is a part of LeakSanitizer.
10// See lsan_thread.h for details.
11//
12//===----------------------------------------------------------------------===//
13
14#include "lsan_thread.h"
15
16#include "lsan.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"
23
24namespace __lsan {
25
26static ThreadRegistry *thread_registry;
27static ThreadArgRetval *thread_arg_retval;
28
29static Mutex mu_for_thread_context;
30static LowLevelAllocator allocator_for_thread_context;
31
32static ThreadContextBase *CreateThreadContext(u32 tid) {
33 Lock lock(&mu_for_thread_context);
34 return new (allocator_for_thread_context) ThreadContext(tid);
35}
36
37void InitializeThreads() {
38 alignas(alignof(ThreadRegistry)) static char
39 thread_registry_placeholder[sizeof(ThreadRegistry)];
40 thread_registry =
41 new (thread_registry_placeholder) ThreadRegistry(CreateThreadContext);
42
43 alignas(alignof(ThreadArgRetval)) static char
44 thread_arg_retval_placeholder[sizeof(ThreadArgRetval)];
45 thread_arg_retval = new (thread_arg_retval_placeholder) ThreadArgRetval();
46}
47
48ThreadArgRetval &GetThreadArgRetval() { return *thread_arg_retval; }
49
50ThreadContextLsanBase::ThreadContextLsanBase(int tid)
51 : ThreadContextBase(tid) {}
52
53void ThreadContextLsanBase::OnStarted(void *arg) {
54 SetCurrentThread(this);
55 AllocatorThreadStart();
56}
57
58void ThreadContextLsanBase::OnFinished() {
59 AllocatorThreadFinish();
60 DTLS_Destroy();
61 SetCurrentThread(nullptr);
62}
63
64u32 ThreadCreate(u32 parent_tid, bool detached, void *arg) {
65 return thread_registry->CreateThread(user_id: 0, detached, parent_tid, arg);
66}
67
68void ThreadContextLsanBase::ThreadStart(u32 tid, tid_t os_id,
69 ThreadType thread_type, void *arg) {
70 thread_registry->StartThread(tid, os_id, thread_type, arg);
71}
72
73void ThreadFinish() { thread_registry->FinishThread(tid: GetCurrentThreadId()); }
74
75void EnsureMainThreadIDIsCorrect() {
76 if (GetCurrentThreadId() == kMainTid)
77 GetCurrentThread()->os_id = GetTid();
78}
79
80///// Interface to the common LSan module. /////
81
82void GetThreadExtraStackRangesLocked(tid_t os_id,
83 InternalMmapVector<Range> *ranges) {}
84void GetThreadExtraStackRangesLocked(InternalMmapVector<Range> *ranges) {}
85
86void LockThreads() {
87 thread_registry->Lock();
88 thread_arg_retval->Lock();
89}
90
91void UnlockThreads() {
92 thread_arg_retval->Unlock();
93 thread_registry->Unlock();
94}
95
96ThreadRegistry *GetLsanThreadRegistryLocked() {
97 thread_registry->CheckLocked();
98 return thread_registry;
99}
100
101void GetRunningThreadsLocked(InternalMmapVector<tid_t> *threads) {
102 GetLsanThreadRegistryLocked()->RunCallbackForEachThreadLocked(
103 cb: [](ThreadContextBase *tctx, void *threads) {
104 if (tctx->status == ThreadStatusRunning) {
105 reinterpret_cast<InternalMmapVector<tid_t> *>(threads)->push_back(
106 element: tctx->os_id);
107 }
108 },
109 arg: threads);
110}
111
112void GetAdditionalThreadContextPtrsLocked(InternalMmapVector<uptr> *ptrs) {
113 GetThreadArgRetval().GetAllPtrsLocked(ptrs);
114}
115
116} // namespace __lsan
117