2013-05-20 18:57:53 +08:00
|
|
|
//=-- lsan_thread.cc ------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file is a part of LeakSanitizer.
|
|
|
|
// See lsan_thread.h for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "lsan_thread.h"
|
|
|
|
|
|
|
|
#include "sanitizer_common/sanitizer_common.h"
|
|
|
|
#include "sanitizer_common/sanitizer_placement_new.h"
|
|
|
|
#include "sanitizer_common/sanitizer_thread_registry.h"
|
2016-01-15 02:50:09 +08:00
|
|
|
#include "sanitizer_common/sanitizer_tls_get_addr.h"
|
2013-05-20 18:57:53 +08:00
|
|
|
#include "lsan_allocator.h"
|
2017-02-17 11:34:56 +08:00
|
|
|
#include "lsan_common.h"
|
2013-05-20 18:57:53 +08:00
|
|
|
|
|
|
|
namespace __lsan {
|
|
|
|
|
|
|
|
static ThreadRegistry *thread_registry;
|
|
|
|
|
|
|
|
static ThreadContextBase *CreateThreadContext(u32 tid) {
|
|
|
|
void *mem = MmapOrDie(sizeof(ThreadContext), "ThreadContext");
|
|
|
|
return new(mem) ThreadContext(tid);
|
|
|
|
}
|
|
|
|
|
|
|
|
static const uptr kMaxThreads = 1 << 13;
|
|
|
|
static const uptr kThreadQuarantineSize = 64;
|
|
|
|
|
|
|
|
void InitializeThreadRegistry() {
|
2016-09-23 23:11:41 +08:00
|
|
|
static ALIGNED(64) char thread_registry_placeholder[sizeof(ThreadRegistry)];
|
2013-05-20 18:57:53 +08:00
|
|
|
thread_registry = new(thread_registry_placeholder)
|
|
|
|
ThreadRegistry(CreateThreadContext, kMaxThreads, kThreadQuarantineSize);
|
|
|
|
}
|
|
|
|
|
|
|
|
ThreadContext::ThreadContext(int tid)
|
2016-01-15 02:50:09 +08:00
|
|
|
: ThreadContextBase(tid),
|
|
|
|
stack_begin_(0),
|
|
|
|
stack_end_(0),
|
|
|
|
cache_begin_(0),
|
|
|
|
cache_end_(0),
|
|
|
|
tls_begin_(0),
|
|
|
|
tls_end_(0),
|
|
|
|
dtls_(nullptr) {}
|
2013-05-20 18:57:53 +08:00
|
|
|
|
|
|
|
struct OnStartedArgs {
|
|
|
|
uptr stack_begin, stack_end,
|
|
|
|
cache_begin, cache_end,
|
|
|
|
tls_begin, tls_end;
|
2016-01-15 02:50:09 +08:00
|
|
|
DTLS *dtls;
|
2013-05-20 18:57:53 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
void ThreadContext::OnStarted(void *arg) {
|
|
|
|
OnStartedArgs *args = reinterpret_cast<OnStartedArgs *>(arg);
|
|
|
|
stack_begin_ = args->stack_begin;
|
|
|
|
stack_end_ = args->stack_end;
|
|
|
|
tls_begin_ = args->tls_begin;
|
|
|
|
tls_end_ = args->tls_end;
|
|
|
|
cache_begin_ = args->cache_begin;
|
|
|
|
cache_end_ = args->cache_end;
|
2016-01-15 02:50:09 +08:00
|
|
|
dtls_ = args->dtls;
|
2013-05-20 18:57:53 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void ThreadContext::OnFinished() {
|
|
|
|
AllocatorThreadFinish();
|
2016-01-15 02:50:09 +08:00
|
|
|
DTLS_Destroy();
|
2013-05-20 18:57:53 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
u32 ThreadCreate(u32 parent_tid, uptr user_id, bool detached) {
|
|
|
|
return thread_registry->CreateThread(user_id, detached, parent_tid,
|
2015-10-01 08:22:21 +08:00
|
|
|
/* arg */ nullptr);
|
2013-05-20 18:57:53 +08:00
|
|
|
}
|
|
|
|
|
2017-06-20 03:21:31 +08:00
|
|
|
void ThreadStart(u32 tid, tid_t os_id, bool workerthread) {
|
2013-05-20 18:57:53 +08:00
|
|
|
OnStartedArgs args;
|
|
|
|
uptr stack_size = 0;
|
|
|
|
uptr tls_size = 0;
|
|
|
|
GetThreadStackAndTls(tid == 0, &args.stack_begin, &stack_size,
|
|
|
|
&args.tls_begin, &tls_size);
|
|
|
|
args.stack_end = args.stack_begin + stack_size;
|
|
|
|
args.tls_end = args.tls_begin + tls_size;
|
|
|
|
GetAllocatorCacheRange(&args.cache_begin, &args.cache_end);
|
2016-01-15 02:50:09 +08:00
|
|
|
args.dtls = DTLS_Get();
|
2017-06-20 03:21:31 +08:00
|
|
|
thread_registry->StartThread(tid, os_id, workerthread, &args);
|
2013-05-20 18:57:53 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void ThreadFinish() {
|
|
|
|
thread_registry->FinishThread(GetCurrentThread());
|
2017-04-12 03:57:12 +08:00
|
|
|
SetCurrentThread(kInvalidTid);
|
2013-05-20 18:57:53 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
ThreadContext *CurrentThreadContext() {
|
2015-10-01 08:22:21 +08:00
|
|
|
if (!thread_registry) return nullptr;
|
2013-05-20 18:57:53 +08:00
|
|
|
if (GetCurrentThread() == kInvalidTid)
|
2015-10-01 08:22:21 +08:00
|
|
|
return nullptr;
|
2013-05-20 18:57:53 +08:00
|
|
|
// No lock needed when getting current thread.
|
|
|
|
return (ThreadContext *)thread_registry->GetThreadLocked(GetCurrentThread());
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool FindThreadByUid(ThreadContextBase *tctx, void *arg) {
|
|
|
|
uptr uid = (uptr)arg;
|
|
|
|
if (tctx->user_id == uid && tctx->status != ThreadStatusInvalid) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
u32 ThreadTid(uptr uid) {
|
|
|
|
return thread_registry->FindThread(FindThreadByUid, (void*)uid);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ThreadJoin(u32 tid) {
|
|
|
|
CHECK_NE(tid, kInvalidTid);
|
2015-10-01 08:22:21 +08:00
|
|
|
thread_registry->JoinThread(tid, /* arg */nullptr);
|
2013-05-20 18:57:53 +08:00
|
|
|
}
|
|
|
|
|
2013-07-08 20:57:24 +08:00
|
|
|
void EnsureMainThreadIDIsCorrect() {
|
|
|
|
if (GetCurrentThread() == 0)
|
|
|
|
CurrentThreadContext()->os_id = GetTid();
|
|
|
|
}
|
|
|
|
|
2013-05-20 18:57:53 +08:00
|
|
|
///// Interface to the common LSan module. /////
|
|
|
|
|
2017-04-18 02:17:38 +08:00
|
|
|
bool GetThreadRangesLocked(tid_t os_id, uptr *stack_begin, uptr *stack_end,
|
2016-01-15 02:50:09 +08:00
|
|
|
uptr *tls_begin, uptr *tls_end, uptr *cache_begin,
|
|
|
|
uptr *cache_end, DTLS **dtls) {
|
2013-05-27 18:35:51 +08:00
|
|
|
ThreadContext *context = static_cast<ThreadContext *>(
|
|
|
|
thread_registry->FindThreadContextByOsIDLocked(os_id));
|
|
|
|
if (!context) return false;
|
2013-05-20 18:57:53 +08:00
|
|
|
*stack_begin = context->stack_begin();
|
|
|
|
*stack_end = context->stack_end();
|
|
|
|
*tls_begin = context->tls_begin();
|
|
|
|
*tls_end = context->tls_end();
|
|
|
|
*cache_begin = context->cache_begin();
|
|
|
|
*cache_end = context->cache_end();
|
2016-01-15 02:50:09 +08:00
|
|
|
*dtls = context->dtls();
|
2013-05-20 18:57:53 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-04-18 02:17:38 +08:00
|
|
|
void ForEachExtraStackRange(tid_t os_id, RangeIteratorCallback callback,
|
2013-10-14 22:04:50 +08:00
|
|
|
void *arg) {
|
|
|
|
}
|
|
|
|
|
2013-05-20 18:57:53 +08:00
|
|
|
void LockThreadRegistry() {
|
|
|
|
thread_registry->Lock();
|
|
|
|
}
|
|
|
|
|
|
|
|
void UnlockThreadRegistry() {
|
|
|
|
thread_registry->Unlock();
|
|
|
|
}
|
|
|
|
|
2015-10-01 08:22:21 +08:00
|
|
|
} // namespace __lsan
|