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_dl.h"
21#include "sanitizer_common/sanitizer_placement_new.h"
22#include "sanitizer_common/sanitizer_thread_history.h"
23#include "sanitizer_common/sanitizer_thread_registry.h"
24#include "sanitizer_common/sanitizer_tls_get_addr.h"
25
26namespace __lsan {
27
28static ThreadRegistry *thread_registry;
29static ThreadArgRetval *thread_arg_retval;
30
31static Mutex mu_for_thread_context;
32static LowLevelAllocator allocator_for_thread_context;
33
34static ThreadContextBase *CreateThreadContext(u32 tid) {
35 Lock lock(&mu_for_thread_context);
36 return new (allocator_for_thread_context) ThreadContext(tid);
37}
38
39void InitializeThreads() {
40 alignas(alignof(ThreadRegistry)) static char
41 thread_registry_placeholder[sizeof(ThreadRegistry)];
42 thread_registry =
43 new (thread_registry_placeholder) ThreadRegistry(CreateThreadContext);
44
45 alignas(alignof(ThreadArgRetval)) static char
46 thread_arg_retval_placeholder[sizeof(ThreadArgRetval)];
47 thread_arg_retval = new (thread_arg_retval_placeholder) ThreadArgRetval();
48}
49
50ThreadArgRetval &GetThreadArgRetval() { return *thread_arg_retval; }
51
52ThreadContextLsanBase::ThreadContextLsanBase(int tid)
53 : ThreadContextBase(tid) {}
54
55void ThreadContextLsanBase::OnStarted(void *arg) {
56 SetCurrentThread(this);
57 AllocatorThreadStart();
58}
59
60void ThreadContextLsanBase::OnFinished() {
61 AllocatorThreadFinish();
62 DTLS_Destroy();
63 SetCurrentThread(nullptr);
64}
65
66u32 ThreadCreate(u32 parent_tid, bool detached, void *arg) {
67 return thread_registry->CreateThread(user_id: 0, detached, parent_tid, arg);
68}
69
70void ThreadContextLsanBase::ThreadStart(u32 tid, ThreadID os_id,
71 ThreadType thread_type, void *arg) {
72 thread_registry->StartThread(tid, os_id, thread_type, arg);
73}
74
75void ThreadFinish() {
76 ClearDlerror();
77 thread_registry->FinishThread(tid: GetCurrentThreadId());
78}
79
80void EnsureMainThreadIDIsCorrect() {
81 if (GetCurrentThreadId() == kMainTid)
82 GetCurrentThread()->os_id = GetTid();
83}
84
85///// Interface to the common LSan module. /////
86
87void GetThreadExtraStackRangesLocked(ThreadID os_id,
88 InternalMmapVector<Range> *ranges) {}
89void GetThreadExtraStackRangesLocked(InternalMmapVector<Range> *ranges) {}
90
91void LockThreads() {
92 thread_registry->Lock();
93 thread_arg_retval->Lock();
94}
95
96void UnlockThreads() {
97 thread_arg_retval->Unlock();
98 thread_registry->Unlock();
99}
100
101ThreadRegistry *GetLsanThreadRegistryLocked() {
102 thread_registry->CheckLocked();
103 return thread_registry;
104}
105
106void GetRunningThreadsLocked(InternalMmapVector<ThreadID> *threads) {
107 GetLsanThreadRegistryLocked()->RunCallbackForEachThreadLocked(
108 cb: [](ThreadContextBase *tctx, void *threads) {
109 if (tctx->status == ThreadStatusRunning) {
110 reinterpret_cast<InternalMmapVector<ThreadID> *>(threads)->push_back(
111 element: tctx->os_id);
112 }
113 },
114 arg: threads);
115}
116
117void PrintThreads() {
118 InternalScopedString out;
119 PrintThreadHistory(registry&: *thread_registry, out);
120 Report(format: "%s\n", out.data());
121}
122
123void GetAdditionalThreadContextPtrsLocked(InternalMmapVector<uptr> *ptrs) {
124 GetThreadArgRetval().GetAllPtrsLocked(ptrs);
125}
126
127} // namespace __lsan
128