Home | History | Annotate | Line # | Download | only in lsan
      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 
     24 namespace __lsan {
     25 
     26 static ThreadRegistry *thread_registry;
     27 
     28 static ThreadContextBase *CreateThreadContext(u32 tid) {
     29   void *mem = MmapOrDie(sizeof(ThreadContext), "ThreadContext");
     30   return new (mem) ThreadContext(tid);
     31 }
     32 
     33 void InitializeThreadRegistry() {
     34   static ALIGNED(64) char thread_registry_placeholder[sizeof(ThreadRegistry)];
     35   thread_registry =
     36       new (thread_registry_placeholder) ThreadRegistry(CreateThreadContext);
     37 }
     38 
     39 ThreadContextLsanBase::ThreadContextLsanBase(int tid)
     40     : ThreadContextBase(tid) {}
     41 
     42 void ThreadContextLsanBase::OnFinished() {
     43   AllocatorThreadFinish();
     44   DTLS_Destroy();
     45 }
     46 
     47 u32 ThreadCreate(u32 parent_tid, uptr user_id, bool detached, void *arg) {
     48   return thread_registry->CreateThread(user_id, detached, parent_tid, arg);
     49 }
     50 
     51 void ThreadContextLsanBase::ThreadStart(u32 tid, tid_t os_id,
     52                                         ThreadType thread_type, void *arg) {
     53   thread_registry->StartThread(tid, os_id, thread_type, arg);
     54   SetCurrentThread(tid);
     55 }
     56 
     57 void ThreadFinish() {
     58   thread_registry->FinishThread(GetCurrentThread());
     59   SetCurrentThread(kInvalidTid);
     60 }
     61 
     62 ThreadContext *CurrentThreadContext() {
     63   if (!thread_registry)
     64     return nullptr;
     65   if (GetCurrentThread() == kInvalidTid)
     66     return nullptr;
     67   // No lock needed when getting current thread.
     68   return (ThreadContext *)thread_registry->GetThreadLocked(GetCurrentThread());
     69 }
     70 
     71 static bool FindThreadByUid(ThreadContextBase *tctx, void *arg) {
     72   uptr uid = (uptr)arg;
     73   if (tctx->user_id == uid && tctx->status != ThreadStatusInvalid) {
     74     return true;
     75   }
     76   return false;
     77 }
     78 
     79 u32 ThreadTid(uptr uid) {
     80   return thread_registry->FindThread(FindThreadByUid, (void *)uid);
     81 }
     82 
     83 void ThreadDetach(u32 tid) {
     84   CHECK_NE(tid, kInvalidTid);
     85   thread_registry->DetachThread(tid, /* arg */ nullptr);
     86 }
     87 
     88 void ThreadJoin(u32 tid) {
     89   CHECK_NE(tid, kInvalidTid);
     90   thread_registry->JoinThread(tid, /* arg */ nullptr);
     91 }
     92 
     93 void EnsureMainThreadIDIsCorrect() {
     94   if (GetCurrentThread() == kMainTid)
     95     CurrentThreadContext()->os_id = GetTid();
     96 }
     97 
     98 ///// Interface to the common LSan module. /////
     99 
    100 void ForEachExtraStackRange(tid_t os_id, RangeIteratorCallback callback,
    101                             void *arg) {}
    102 
    103 void LockThreadRegistry() { thread_registry->Lock(); }
    104 
    105 void UnlockThreadRegistry() { thread_registry->Unlock(); }
    106 
    107 ThreadRegistry *GetThreadRegistryLocked() {
    108   thread_registry->CheckLocked();
    109   return thread_registry;
    110 }
    111 
    112 }  // namespace __lsan
    113