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;
|
2023-05-08 12:42:50 -07:00
|
|
|
static ThreadArgRetval *thread_arg_retval;
|
2013-05-20 10:57:53 +00:00
|
|
|
|
2023-04-14 22:50:28 -07:00
|
|
|
static Mutex mu_for_thread_context;
|
|
|
|
static LowLevelAllocator allocator_for_thread_context;
|
|
|
|
|
2013-05-20 10:57:53 +00:00
|
|
|
static ThreadContextBase *CreateThreadContext(u32 tid) {
|
2023-04-14 22:50:28 -07:00
|
|
|
Lock lock(&mu_for_thread_context);
|
|
|
|
return new (allocator_for_thread_context) ThreadContext(tid);
|
2013-05-20 10:57:53 +00:00
|
|
|
}
|
|
|
|
|
2023-05-11 16:53:30 -07:00
|
|
|
void InitializeThreads() {
|
2023-05-08 12:42:50 -07:00
|
|
|
static ALIGNED(alignof(
|
|
|
|
ThreadRegistry)) char thread_registry_placeholder[sizeof(ThreadRegistry)];
|
2021-07-12 12:06:28 -07:00
|
|
|
thread_registry =
|
|
|
|
new (thread_registry_placeholder) ThreadRegistry(CreateThreadContext);
|
2023-05-08 12:42:50 -07:00
|
|
|
|
|
|
|
static ALIGNED(alignof(ThreadArgRetval)) char
|
|
|
|
thread_arg_retval_placeholder[sizeof(ThreadArgRetval)];
|
|
|
|
thread_arg_retval = new (thread_arg_retval_placeholder) ThreadArgRetval();
|
2013-05-20 10:57:53 +00:00
|
|
|
}
|
|
|
|
|
2023-05-08 12:42:50 -07:00
|
|
|
ThreadArgRetval &GetThreadArgRetval() { return *thread_arg_retval; }
|
|
|
|
|
2020-01-24 16:55:11 -08:00
|
|
|
ThreadContextLsanBase::ThreadContextLsanBase(int tid)
|
|
|
|
: ThreadContextBase(tid) {}
|
2013-05-20 10:57:53 +00:00
|
|
|
|
2023-05-24 00:31:15 -07:00
|
|
|
void ThreadContextLsanBase::OnStarted(void *arg) {
|
|
|
|
SetCurrentThread(this);
|
|
|
|
AllocatorThreadStart();
|
|
|
|
}
|
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
|
|
|
|
2023-05-11 16:53:30 -07:00
|
|
|
void LockThreads() {
|
2023-05-08 12:42:50 -07:00
|
|
|
thread_registry->Lock();
|
|
|
|
thread_arg_retval->Lock();
|
|
|
|
}
|
2013-05-20 10:57:53 +00:00
|
|
|
|
2023-05-11 16:53:30 -07:00
|
|
|
void UnlockThreads() {
|
2023-05-08 12:42:50 -07:00
|
|
|
thread_arg_retval->Unlock();
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2023-05-08 12:19:35 -07:00
|
|
|
void GetAdditionalThreadContextPtrsLocked(InternalMmapVector<uptr> *ptrs) {
|
2023-05-08 12:42:50 -07:00
|
|
|
GetThreadArgRetval().GetAllPtrsLocked(ptrs);
|
2023-05-08 12:19:35 -07:00
|
|
|
}
|
|
|
|
|
2020-01-24 16:55:11 -08:00
|
|
|
} // namespace __lsan
|