xref: /openbsd-src/gnu/llvm/compiler-rt/lib/lsan/lsan_thread.cpp (revision 810390e339a5425391477d5d41c78d7cab2424ac)
13cab2bb3Spatrick //=-- lsan_thread.cpp -----------------------------------------------------===//
23cab2bb3Spatrick //
33cab2bb3Spatrick // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
43cab2bb3Spatrick // See https://llvm.org/LICENSE.txt for license information.
53cab2bb3Spatrick // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
63cab2bb3Spatrick //
73cab2bb3Spatrick //===----------------------------------------------------------------------===//
83cab2bb3Spatrick //
93cab2bb3Spatrick // This file is a part of LeakSanitizer.
103cab2bb3Spatrick // See lsan_thread.h for details.
113cab2bb3Spatrick //
123cab2bb3Spatrick //===----------------------------------------------------------------------===//
133cab2bb3Spatrick 
143cab2bb3Spatrick #include "lsan_thread.h"
153cab2bb3Spatrick 
161f9cb04fSpatrick #include "lsan.h"
171f9cb04fSpatrick #include "lsan_allocator.h"
181f9cb04fSpatrick #include "lsan_common.h"
193cab2bb3Spatrick #include "sanitizer_common/sanitizer_common.h"
203cab2bb3Spatrick #include "sanitizer_common/sanitizer_placement_new.h"
213cab2bb3Spatrick #include "sanitizer_common/sanitizer_thread_registry.h"
223cab2bb3Spatrick #include "sanitizer_common/sanitizer_tls_get_addr.h"
233cab2bb3Spatrick 
243cab2bb3Spatrick namespace __lsan {
253cab2bb3Spatrick 
263cab2bb3Spatrick static ThreadRegistry *thread_registry;
273cab2bb3Spatrick 
CreateThreadContext(u32 tid)283cab2bb3Spatrick static ThreadContextBase *CreateThreadContext(u32 tid) {
293cab2bb3Spatrick   void *mem = MmapOrDie(sizeof(ThreadContext), "ThreadContext");
303cab2bb3Spatrick   return new (mem) ThreadContext(tid);
313cab2bb3Spatrick }
323cab2bb3Spatrick 
InitializeThreadRegistry()333cab2bb3Spatrick void InitializeThreadRegistry() {
343cab2bb3Spatrick   static ALIGNED(64) char thread_registry_placeholder[sizeof(ThreadRegistry)];
35d89ec533Spatrick   thread_registry =
36d89ec533Spatrick       new (thread_registry_placeholder) ThreadRegistry(CreateThreadContext);
373cab2bb3Spatrick }
383cab2bb3Spatrick 
ThreadContextLsanBase(int tid)391f9cb04fSpatrick ThreadContextLsanBase::ThreadContextLsanBase(int tid)
401f9cb04fSpatrick     : ThreadContextBase(tid) {}
413cab2bb3Spatrick 
OnFinished()421f9cb04fSpatrick void ThreadContextLsanBase::OnFinished() {
433cab2bb3Spatrick   AllocatorThreadFinish();
443cab2bb3Spatrick   DTLS_Destroy();
453cab2bb3Spatrick }
463cab2bb3Spatrick 
ThreadCreate(u32 parent_tid,bool detached,void * arg)47*810390e3Srobert u32 ThreadCreate(u32 parent_tid, bool detached, void *arg) {
48*810390e3Srobert   return thread_registry->CreateThread(0, detached, parent_tid, arg);
493cab2bb3Spatrick }
503cab2bb3Spatrick 
ThreadStart(u32 tid,tid_t os_id,ThreadType thread_type,void * arg)511f9cb04fSpatrick void ThreadContextLsanBase::ThreadStart(u32 tid, tid_t os_id,
521f9cb04fSpatrick                                         ThreadType thread_type, void *arg) {
531f9cb04fSpatrick   thread_registry->StartThread(tid, os_id, thread_type, arg);
541f9cb04fSpatrick   SetCurrentThread(tid);
553cab2bb3Spatrick }
563cab2bb3Spatrick 
ThreadFinish()573cab2bb3Spatrick void ThreadFinish() {
583cab2bb3Spatrick   thread_registry->FinishThread(GetCurrentThread());
593cab2bb3Spatrick   SetCurrentThread(kInvalidTid);
603cab2bb3Spatrick }
613cab2bb3Spatrick 
CurrentThreadContext()623cab2bb3Spatrick ThreadContext *CurrentThreadContext() {
631f9cb04fSpatrick   if (!thread_registry)
641f9cb04fSpatrick     return nullptr;
653cab2bb3Spatrick   if (GetCurrentThread() == kInvalidTid)
663cab2bb3Spatrick     return nullptr;
673cab2bb3Spatrick   // No lock needed when getting current thread.
683cab2bb3Spatrick   return (ThreadContext *)thread_registry->GetThreadLocked(GetCurrentThread());
693cab2bb3Spatrick }
703cab2bb3Spatrick 
EnsureMainThreadIDIsCorrect()713cab2bb3Spatrick void EnsureMainThreadIDIsCorrect() {
72d89ec533Spatrick   if (GetCurrentThread() == kMainTid)
733cab2bb3Spatrick     CurrentThreadContext()->os_id = GetTid();
743cab2bb3Spatrick }
753cab2bb3Spatrick 
763cab2bb3Spatrick ///// Interface to the common LSan module. /////
773cab2bb3Spatrick 
GetThreadExtraStackRangesLocked(tid_t os_id,InternalMmapVector<Range> * ranges)78*810390e3Srobert void GetThreadExtraStackRangesLocked(tid_t os_id,
79*810390e3Srobert                                      InternalMmapVector<Range> *ranges) {}
GetThreadExtraStackRangesLocked(InternalMmapVector<Range> * ranges)80*810390e3Srobert void GetThreadExtraStackRangesLocked(InternalMmapVector<Range> *ranges) {}
813cab2bb3Spatrick 
LockThreadRegistry()821f9cb04fSpatrick void LockThreadRegistry() { thread_registry->Lock(); }
833cab2bb3Spatrick 
UnlockThreadRegistry()841f9cb04fSpatrick void UnlockThreadRegistry() { thread_registry->Unlock(); }
853cab2bb3Spatrick 
GetLsanThreadRegistryLocked()86*810390e3Srobert ThreadRegistry *GetLsanThreadRegistryLocked() {
873cab2bb3Spatrick   thread_registry->CheckLocked();
883cab2bb3Spatrick   return thread_registry;
893cab2bb3Spatrick }
903cab2bb3Spatrick 
GetRunningThreadsLocked(InternalMmapVector<tid_t> * threads)91*810390e3Srobert void GetRunningThreadsLocked(InternalMmapVector<tid_t> *threads) {
92*810390e3Srobert   GetLsanThreadRegistryLocked()->RunCallbackForEachThreadLocked(
93*810390e3Srobert       [](ThreadContextBase *tctx, void *threads) {
94*810390e3Srobert         if (tctx->status == ThreadStatusRunning) {
95*810390e3Srobert           reinterpret_cast<InternalMmapVector<tid_t> *>(threads)->push_back(
96*810390e3Srobert               tctx->os_id);
97*810390e3Srobert         }
98*810390e3Srobert       },
99*810390e3Srobert       threads);
100*810390e3Srobert }
101*810390e3Srobert 
1023cab2bb3Spatrick }  // namespace __lsan
103