xref: /freebsd/contrib/llvm-project/compiler-rt/lib/sanitizer_common/sanitizer_thread_registry.cpp (revision 43a5ec4eb41567cc92586503212743d89686d78f)
1 //===-- sanitizer_thread_registry.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 shared between sanitizer tools.
10 //
11 // General thread bookkeeping functionality.
12 //===----------------------------------------------------------------------===//
13 
14 #include "sanitizer_thread_registry.h"
15 
16 namespace __sanitizer {
17 
18 ThreadContextBase::ThreadContextBase(u32 tid)
19     : tid(tid), unique_id(0), reuse_count(), os_id(0), user_id(0),
20       status(ThreadStatusInvalid), detached(false),
21       thread_type(ThreadType::Regular), parent_tid(0), next(0) {
22   name[0] = '\0';
23   atomic_store(&thread_destroyed, 0, memory_order_release);
24 }
25 
26 ThreadContextBase::~ThreadContextBase() {
27   // ThreadContextBase should never be deleted.
28   CHECK(0);
29 }
30 
31 void ThreadContextBase::SetName(const char *new_name) {
32   name[0] = '\0';
33   if (new_name) {
34     internal_strncpy(name, new_name, sizeof(name));
35     name[sizeof(name) - 1] = '\0';
36   }
37 }
38 
39 void ThreadContextBase::SetDead() {
40   CHECK(status == ThreadStatusRunning ||
41         status == ThreadStatusFinished);
42   status = ThreadStatusDead;
43   user_id = 0;
44   OnDead();
45 }
46 
47 void ThreadContextBase::SetDestroyed() {
48   atomic_store(&thread_destroyed, 1, memory_order_release);
49 }
50 
51 bool ThreadContextBase::GetDestroyed() {
52   return !!atomic_load(&thread_destroyed, memory_order_acquire);
53 }
54 
55 void ThreadContextBase::SetJoined(void *arg) {
56   // FIXME(dvyukov): print message and continue (it's user error).
57   CHECK_EQ(false, detached);
58   CHECK_EQ(ThreadStatusFinished, status);
59   status = ThreadStatusDead;
60   user_id = 0;
61   OnJoined(arg);
62 }
63 
64 void ThreadContextBase::SetFinished() {
65   // ThreadRegistry::FinishThread calls here in ThreadStatusCreated state
66   // for a thread that never actually started.  In that case the thread
67   // should go to ThreadStatusFinished regardless of whether it was created
68   // as detached.
69   if (!detached || status == ThreadStatusCreated) status = ThreadStatusFinished;
70   OnFinished();
71 }
72 
73 void ThreadContextBase::SetStarted(tid_t _os_id, ThreadType _thread_type,
74                                    void *arg) {
75   status = ThreadStatusRunning;
76   os_id = _os_id;
77   thread_type = _thread_type;
78   OnStarted(arg);
79 }
80 
81 void ThreadContextBase::SetCreated(uptr _user_id, u64 _unique_id,
82                                    bool _detached, u32 _parent_tid, void *arg) {
83   status = ThreadStatusCreated;
84   user_id = _user_id;
85   unique_id = _unique_id;
86   detached = _detached;
87   // Parent tid makes no sense for the main thread.
88   if (tid != kMainTid)
89     parent_tid = _parent_tid;
90   OnCreated(arg);
91 }
92 
93 void ThreadContextBase::Reset() {
94   status = ThreadStatusInvalid;
95   SetName(0);
96   atomic_store(&thread_destroyed, 0, memory_order_release);
97   OnReset();
98 }
99 
100 // ThreadRegistry implementation.
101 
102 ThreadRegistry::ThreadRegistry(ThreadContextFactory factory)
103     : ThreadRegistry(factory, UINT32_MAX, UINT32_MAX, 0) {}
104 
105 ThreadRegistry::ThreadRegistry(ThreadContextFactory factory, u32 max_threads,
106                                u32 thread_quarantine_size, u32 max_reuse)
107     : context_factory_(factory),
108       max_threads_(max_threads),
109       thread_quarantine_size_(thread_quarantine_size),
110       max_reuse_(max_reuse),
111       mtx_(),
112       total_threads_(0),
113       alive_threads_(0),
114       max_alive_threads_(0),
115       running_threads_(0) {
116   dead_threads_.clear();
117   invalid_threads_.clear();
118 }
119 
120 void ThreadRegistry::GetNumberOfThreads(uptr *total, uptr *running,
121                                         uptr *alive) {
122   BlockingMutexLock l(&mtx_);
123   if (total)
124     *total = threads_.size();
125   if (running) *running = running_threads_;
126   if (alive) *alive = alive_threads_;
127 }
128 
129 uptr ThreadRegistry::GetMaxAliveThreads() {
130   BlockingMutexLock l(&mtx_);
131   return max_alive_threads_;
132 }
133 
134 u32 ThreadRegistry::CreateThread(uptr user_id, bool detached, u32 parent_tid,
135                                  void *arg) {
136   BlockingMutexLock l(&mtx_);
137   u32 tid = kInvalidTid;
138   ThreadContextBase *tctx = QuarantinePop();
139   if (tctx) {
140     tid = tctx->tid;
141   } else if (threads_.size() < max_threads_) {
142     // Allocate new thread context and tid.
143     tid = threads_.size();
144     tctx = context_factory_(tid);
145     threads_.push_back(tctx);
146   } else {
147 #if !SANITIZER_GO
148     Report("%s: Thread limit (%u threads) exceeded. Dying.\n",
149            SanitizerToolName, max_threads_);
150 #else
151     Printf("race: limit on %u simultaneously alive goroutines is exceeded,"
152         " dying\n", max_threads_);
153 #endif
154     Die();
155   }
156   CHECK_NE(tctx, 0);
157   CHECK_NE(tid, kInvalidTid);
158   CHECK_LT(tid, max_threads_);
159   CHECK_EQ(tctx->status, ThreadStatusInvalid);
160   alive_threads_++;
161   if (max_alive_threads_ < alive_threads_) {
162     max_alive_threads_++;
163     CHECK_EQ(alive_threads_, max_alive_threads_);
164   }
165   tctx->SetCreated(user_id, total_threads_++, detached,
166                    parent_tid, arg);
167   return tid;
168 }
169 
170 void ThreadRegistry::RunCallbackForEachThreadLocked(ThreadCallback cb,
171                                                     void *arg) {
172   CheckLocked();
173   for (u32 tid = 0; tid < threads_.size(); tid++) {
174     ThreadContextBase *tctx = threads_[tid];
175     if (tctx == 0)
176       continue;
177     cb(tctx, arg);
178   }
179 }
180 
181 u32 ThreadRegistry::FindThread(FindThreadCallback cb, void *arg) {
182   BlockingMutexLock l(&mtx_);
183   for (u32 tid = 0; tid < threads_.size(); tid++) {
184     ThreadContextBase *tctx = threads_[tid];
185     if (tctx != 0 && cb(tctx, arg))
186       return tctx->tid;
187   }
188   return kInvalidTid;
189 }
190 
191 ThreadContextBase *
192 ThreadRegistry::FindThreadContextLocked(FindThreadCallback cb, void *arg) {
193   CheckLocked();
194   for (u32 tid = 0; tid < threads_.size(); tid++) {
195     ThreadContextBase *tctx = threads_[tid];
196     if (tctx != 0 && cb(tctx, arg))
197       return tctx;
198   }
199   return 0;
200 }
201 
202 static bool FindThreadContextByOsIdCallback(ThreadContextBase *tctx,
203                                             void *arg) {
204   return (tctx->os_id == (uptr)arg && tctx->status != ThreadStatusInvalid &&
205       tctx->status != ThreadStatusDead);
206 }
207 
208 ThreadContextBase *ThreadRegistry::FindThreadContextByOsIDLocked(tid_t os_id) {
209   return FindThreadContextLocked(FindThreadContextByOsIdCallback,
210                                  (void *)os_id);
211 }
212 
213 void ThreadRegistry::SetThreadName(u32 tid, const char *name) {
214   BlockingMutexLock l(&mtx_);
215   ThreadContextBase *tctx = threads_[tid];
216   CHECK_NE(tctx, 0);
217   CHECK_EQ(SANITIZER_FUCHSIA ? ThreadStatusCreated : ThreadStatusRunning,
218            tctx->status);
219   tctx->SetName(name);
220 }
221 
222 void ThreadRegistry::SetThreadNameByUserId(uptr user_id, const char *name) {
223   BlockingMutexLock l(&mtx_);
224   for (u32 tid = 0; tid < threads_.size(); tid++) {
225     ThreadContextBase *tctx = threads_[tid];
226     if (tctx != 0 && tctx->user_id == user_id &&
227         tctx->status != ThreadStatusInvalid) {
228       tctx->SetName(name);
229       return;
230     }
231   }
232 }
233 
234 void ThreadRegistry::DetachThread(u32 tid, void *arg) {
235   BlockingMutexLock l(&mtx_);
236   ThreadContextBase *tctx = threads_[tid];
237   CHECK_NE(tctx, 0);
238   if (tctx->status == ThreadStatusInvalid) {
239     Report("%s: Detach of non-existent thread\n", SanitizerToolName);
240     return;
241   }
242   tctx->OnDetached(arg);
243   if (tctx->status == ThreadStatusFinished) {
244     tctx->SetDead();
245     QuarantinePush(tctx);
246   } else {
247     tctx->detached = true;
248   }
249 }
250 
251 void ThreadRegistry::JoinThread(u32 tid, void *arg) {
252   bool destroyed = false;
253   do {
254     {
255       BlockingMutexLock l(&mtx_);
256       ThreadContextBase *tctx = threads_[tid];
257       CHECK_NE(tctx, 0);
258       if (tctx->status == ThreadStatusInvalid) {
259         Report("%s: Join of non-existent thread\n", SanitizerToolName);
260         return;
261       }
262       if ((destroyed = tctx->GetDestroyed())) {
263         tctx->SetJoined(arg);
264         QuarantinePush(tctx);
265       }
266     }
267     if (!destroyed)
268       internal_sched_yield();
269   } while (!destroyed);
270 }
271 
272 // Normally this is called when the thread is about to exit.  If
273 // called in ThreadStatusCreated state, then this thread was never
274 // really started.  We just did CreateThread for a prospective new
275 // thread before trying to create it, and then failed to actually
276 // create it, and so never called StartThread.
277 ThreadStatus ThreadRegistry::FinishThread(u32 tid) {
278   BlockingMutexLock l(&mtx_);
279   CHECK_GT(alive_threads_, 0);
280   alive_threads_--;
281   ThreadContextBase *tctx = threads_[tid];
282   CHECK_NE(tctx, 0);
283   bool dead = tctx->detached;
284   ThreadStatus prev_status = tctx->status;
285   if (tctx->status == ThreadStatusRunning) {
286     CHECK_GT(running_threads_, 0);
287     running_threads_--;
288   } else {
289     // The thread never really existed.
290     CHECK_EQ(tctx->status, ThreadStatusCreated);
291     dead = true;
292   }
293   tctx->SetFinished();
294   if (dead) {
295     tctx->SetDead();
296     QuarantinePush(tctx);
297   }
298   tctx->SetDestroyed();
299   return prev_status;
300 }
301 
302 void ThreadRegistry::StartThread(u32 tid, tid_t os_id, ThreadType thread_type,
303                                  void *arg) {
304   BlockingMutexLock l(&mtx_);
305   running_threads_++;
306   ThreadContextBase *tctx = threads_[tid];
307   CHECK_NE(tctx, 0);
308   CHECK_EQ(ThreadStatusCreated, tctx->status);
309   tctx->SetStarted(os_id, thread_type, arg);
310 }
311 
312 void ThreadRegistry::QuarantinePush(ThreadContextBase *tctx) {
313   if (tctx->tid == 0)
314     return;  // Don't reuse the main thread.  It's a special snowflake.
315   dead_threads_.push_back(tctx);
316   if (dead_threads_.size() <= thread_quarantine_size_)
317     return;
318   tctx = dead_threads_.front();
319   dead_threads_.pop_front();
320   CHECK_EQ(tctx->status, ThreadStatusDead);
321   tctx->Reset();
322   tctx->reuse_count++;
323   if (max_reuse_ > 0 && tctx->reuse_count >= max_reuse_)
324     return;
325   invalid_threads_.push_back(tctx);
326 }
327 
328 ThreadContextBase *ThreadRegistry::QuarantinePop() {
329   if (invalid_threads_.size() == 0)
330     return 0;
331   ThreadContextBase *tctx = invalid_threads_.front();
332   invalid_threads_.pop_front();
333   return tctx;
334 }
335 
336 void ThreadRegistry::SetThreadUserId(u32 tid, uptr user_id) {
337   BlockingMutexLock l(&mtx_);
338   ThreadContextBase *tctx = threads_[tid];
339   CHECK_NE(tctx, 0);
340   CHECK_NE(tctx->status, ThreadStatusInvalid);
341   CHECK_NE(tctx->status, ThreadStatusDead);
342   CHECK_EQ(tctx->user_id, 0);
343   tctx->user_id = user_id;
344 }
345 
346 }  // namespace __sanitizer
347