2019-08-01 14:01:30 +00:00
|
|
|
//=-- lsan_thread.cpp -----------------------------------------------------===//
|
2013-05-20 10:57:53 +00:00
|
|
|
//
|
2019-01-19 08:50:56 +00:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2013-05-20 10:57:53 +00:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file is a part of LeakSanitizer.
|
|
|
|
// See lsan_thread.h for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "lsan_thread.h"
|
|
|
|
|
2020-01-24 16:55:11 -08:00
|
|
|
#include "lsan.h"
|
|
|
|
#include "lsan_allocator.h"
|
|
|
|
#include "lsan_common.h"
|
2013-05-20 10:57:53 +00:00
|
|
|
#include "sanitizer_common/sanitizer_common.h"
|
|
|
|
#include "sanitizer_common/sanitizer_placement_new.h"
|
|
|
|
#include "sanitizer_common/sanitizer_thread_registry.h"
|
2016-01-14 18:50:09 +00:00
|
|
|
#include "sanitizer_common/sanitizer_tls_get_addr.h"
|
2013-05-20 10:57:53 +00:00
|
|
|
|
|
|
|
namespace __lsan {
|
|
|
|
|
|
|
|
static ThreadRegistry *thread_registry;
|
|
|
|
|
|
|
|
static ThreadContextBase *CreateThreadContext(u32 tid) {
|
|
|
|
void *mem = MmapOrDie(sizeof(ThreadContext), "ThreadContext");
|
2020-01-24 16:55:11 -08:00
|
|
|
return new (mem) ThreadContext(tid);
|
2013-05-20 10:57:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void InitializeThreadRegistry() {
|
2016-09-23 15:11:41 +00:00
|
|
|
static ALIGNED(64) char thread_registry_placeholder[sizeof(ThreadRegistry)];
|
2021-07-12 12:06:28 -07:00
|
|
|
thread_registry =
|
|
|
|
new (thread_registry_placeholder) ThreadRegistry(CreateThreadContext);
|
2013-05-20 10:57:53 +00:00
|
|
|
}
|
|
|
|
|
2020-01-24 16:55:11 -08:00
|
|
|
ThreadContextLsanBase::ThreadContextLsanBase(int tid)
|
|
|
|
: ThreadContextBase(tid) {}
|
2013-05-20 10:57:53 +00:00
|
|
|
|
2023-04-14 16:27:19 -07:00
|
|
|
void ThreadContextLsanBase::OnStarted(void *arg) { SetCurrentThread(this); }
|
2023-04-14 15:43:56 -07:00
|
|
|
|
2020-01-24 16:55:11 -08:00
|
|
|
void ThreadContextLsanBase::OnFinished() {
|
2013-05-20 10:57:53 +00:00
|
|
|
AllocatorThreadFinish();
|
2016-01-14 18:50:09 +00:00
|
|
|
DTLS_Destroy();
|
2023-04-14 16:27:19 -07:00
|
|
|
SetCurrentThread(nullptr);
|
2013-05-20 10:57:53 +00:00
|
|
|
}
|
|
|
|
|
2021-11-15 19:20:00 +01:00
|
|
|
u32 ThreadCreate(u32 parent_tid, bool detached, void *arg) {
|
|
|
|
return thread_registry->CreateThread(0, detached, parent_tid, arg);
|
2013-05-20 10:57:53 +00:00
|
|
|
}
|
|
|
|
|
2020-01-24 16:55:11 -08:00
|
|
|
void ThreadContextLsanBase::ThreadStart(u32 tid, tid_t os_id,
|
|
|
|
ThreadType thread_type, void *arg) {
|
|
|
|
thread_registry->StartThread(tid, os_id, thread_type, arg);
|
2013-05-20 10:57:53 +00:00
|
|
|
}
|
|
|
|
|
2023-04-14 16:22:54 -07:00
|
|
|
void ThreadFinish() { thread_registry->FinishThread(GetCurrentThreadId()); }
|
2013-05-20 10:57:53 +00:00
|
|
|
|
2013-07-08 12:57:24 +00:00
|
|
|
void EnsureMainThreadIDIsCorrect() {
|
2023-04-14 16:22:54 -07:00
|
|
|
if (GetCurrentThreadId() == kMainTid)
|
2023-04-14 16:27:19 -07:00
|
|
|
GetCurrentThread()->os_id = GetTid();
|
2013-07-08 12:57:24 +00:00
|
|
|
}
|
|
|
|
|
2013-05-20 10:57:53 +00:00
|
|
|
///// Interface to the common LSan module. /////
|
|
|
|
|
2023-01-13 20:19:46 -08:00
|
|
|
void GetThreadExtraStackRangesLocked(tid_t os_id,
|
|
|
|
InternalMmapVector<Range> *ranges) {}
|
|
|
|
void GetThreadExtraStackRangesLocked(InternalMmapVector<Range> *ranges) {}
|
2013-10-14 14:04:50 +00:00
|
|
|
|
2020-01-24 16:55:11 -08:00
|
|
|
void LockThreadRegistry() { thread_registry->Lock(); }
|
2013-05-20 10:57:53 +00:00
|
|
|
|
2020-01-24 16:55:11 -08:00
|
|
|
void UnlockThreadRegistry() { thread_registry->Unlock(); }
|
2013-05-20 10:57:53 +00:00
|
|
|
|
2022-12-13 23:03:48 +00:00
|
|
|
ThreadRegistry *GetLsanThreadRegistryLocked() {
|
2018-05-09 23:02:14 +00:00
|
|
|
thread_registry->CheckLocked();
|
|
|
|
return thread_registry;
|
|
|
|
}
|
|
|
|
|
2023-01-12 13:31:49 -08:00
|
|
|
void GetRunningThreadsLocked(InternalMmapVector<tid_t> *threads) {
|
|
|
|
GetLsanThreadRegistryLocked()->RunCallbackForEachThreadLocked(
|
|
|
|
[](ThreadContextBase *tctx, void *threads) {
|
|
|
|
if (tctx->status == ThreadStatusRunning) {
|
|
|
|
reinterpret_cast<InternalMmapVector<tid_t> *>(threads)->push_back(
|
|
|
|
tctx->os_id);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
threads);
|
2022-12-13 23:03:48 +00:00
|
|
|
}
|
|
|
|
|
2020-01-24 16:55:11 -08:00
|
|
|
} // namespace __lsan
|