2011-11-30 09:07:02 +08:00
|
|
|
//===-- asan_thread.h -------------------------------------------*- C++ -*-===//
|
|
|
|
//
|
|
|
|
// 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 AddressSanitizer, an address sanity checker.
|
|
|
|
//
|
|
|
|
// ASan-private header for asan_thread.cc.
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
#ifndef ASAN_THREAD_H
|
|
|
|
#define ASAN_THREAD_H
|
|
|
|
|
|
|
|
#include "asan_allocator.h"
|
|
|
|
#include "asan_internal.h"
|
2013-04-11 19:39:19 +08:00
|
|
|
#include "asan_fake_stack.h"
|
2011-11-30 09:07:02 +08:00
|
|
|
#include "asan_stats.h"
|
2013-10-14 20:01:05 +08:00
|
|
|
#include "sanitizer_common/sanitizer_common.h"
|
2012-07-17 15:20:13 +08:00
|
|
|
#include "sanitizer_common/sanitizer_libc.h"
|
2013-03-21 19:23:41 +08:00
|
|
|
#include "sanitizer_common/sanitizer_thread_registry.h"
|
2011-11-30 09:07:02 +08:00
|
|
|
|
|
|
|
namespace __asan {
|
|
|
|
|
2012-06-06 23:06:58 +08:00
|
|
|
const u32 kInvalidTid = 0xffffff; // Must fit into 24 bits.
|
2013-03-21 19:23:41 +08:00
|
|
|
const u32 kMaxNumberOfThreads = (1 << 22); // 4M
|
2012-06-06 23:06:58 +08:00
|
|
|
|
2011-11-30 09:07:02 +08:00
|
|
|
class AsanThread;
|
|
|
|
|
|
|
|
// These objects are created for every thread and are never deleted,
|
|
|
|
// so we can find them by tid even if the thread is long dead.
|
2013-03-21 19:23:41 +08:00
|
|
|
class AsanThreadContext : public ThreadContextBase {
|
2011-11-30 09:07:02 +08:00
|
|
|
public:
|
2013-03-21 19:23:41 +08:00
|
|
|
explicit AsanThreadContext(int tid)
|
|
|
|
: ThreadContextBase(tid),
|
|
|
|
announced(false),
|
2013-10-14 20:01:05 +08:00
|
|
|
destructor_iterations(kPthreadDestructorIterations),
|
2013-10-18 22:50:44 +08:00
|
|
|
stack_id(0),
|
2013-03-21 19:23:41 +08:00
|
|
|
thread(0) {
|
2011-11-30 09:07:02 +08:00
|
|
|
}
|
2013-03-21 19:23:41 +08:00
|
|
|
bool announced;
|
2013-10-18 22:50:44 +08:00
|
|
|
u8 destructor_iterations;
|
|
|
|
u32 stack_id;
|
2013-03-21 19:23:41 +08:00
|
|
|
AsanThread *thread;
|
2012-02-07 08:27:15 +08:00
|
|
|
|
2013-03-21 19:23:41 +08:00
|
|
|
void OnCreated(void *arg);
|
|
|
|
void OnFinished();
|
2011-11-30 09:07:02 +08:00
|
|
|
};
|
|
|
|
|
2013-03-21 19:23:41 +08:00
|
|
|
// AsanThreadContext objects are never freed, so we need many of them.
|
2013-10-18 22:50:44 +08:00
|
|
|
COMPILER_CHECK(sizeof(AsanThreadContext) <= 256);
|
2012-12-07 23:15:01 +08:00
|
|
|
|
2011-11-30 09:07:02 +08:00
|
|
|
// AsanThread are stored in TSD and destroyed when the thread dies.
|
|
|
|
class AsanThread {
|
|
|
|
public:
|
2013-03-21 19:23:41 +08:00
|
|
|
static AsanThread *Create(thread_callback_t start_routine, void *arg);
|
|
|
|
static void TSDDtor(void *tsd);
|
2012-01-07 03:44:11 +08:00
|
|
|
void Destroy();
|
2011-11-30 09:07:02 +08:00
|
|
|
|
2011-12-17 03:13:35 +08:00
|
|
|
void Init(); // Should be called from the thread itself.
|
2014-12-05 08:10:15 +08:00
|
|
|
thread_return_t ThreadStart(uptr os_id,
|
|
|
|
atomic_uintptr_t *signal_thread_is_registered);
|
2011-11-30 09:07:02 +08:00
|
|
|
|
2012-05-31 22:35:53 +08:00
|
|
|
uptr stack_top() { return stack_top_; }
|
|
|
|
uptr stack_bottom() { return stack_bottom_; }
|
2013-09-19 22:59:52 +08:00
|
|
|
uptr stack_size() { return stack_size_; }
|
2013-05-29 21:09:44 +08:00
|
|
|
uptr tls_begin() { return tls_begin_; }
|
|
|
|
uptr tls_end() { return tls_end_; }
|
2013-03-21 19:23:41 +08:00
|
|
|
u32 tid() { return context_->tid; }
|
|
|
|
AsanThreadContext *context() { return context_; }
|
|
|
|
void set_context(AsanThreadContext *context) { context_ = context; }
|
2011-11-30 09:07:02 +08:00
|
|
|
|
2014-10-02 05:13:00 +08:00
|
|
|
struct StackFrameAccess {
|
|
|
|
uptr offset;
|
|
|
|
uptr frame_pc;
|
|
|
|
const char *frame_descr;
|
|
|
|
};
|
|
|
|
bool GetStackFrameAccessByAddr(uptr addr, StackFrameAccess *access);
|
2011-11-30 09:07:02 +08:00
|
|
|
|
2012-05-31 22:35:53 +08:00
|
|
|
bool AddrIsInStack(uptr addr) {
|
2011-11-30 09:07:02 +08:00
|
|
|
return addr >= stack_bottom_ && addr < stack_top_;
|
|
|
|
}
|
|
|
|
|
2013-12-11 21:54:01 +08:00
|
|
|
void DeleteFakeStack(int tid) {
|
2013-06-26 20:16:05 +08:00
|
|
|
if (!fake_stack_) return;
|
2013-09-13 14:32:26 +08:00
|
|
|
FakeStack *t = fake_stack_;
|
|
|
|
fake_stack_ = 0;
|
|
|
|
SetTLSFakeStack(0);
|
2013-12-11 21:54:01 +08:00
|
|
|
t->Destroy(tid);
|
2013-09-12 15:11:58 +08:00
|
|
|
}
|
|
|
|
|
2013-09-12 16:43:44 +08:00
|
|
|
bool has_fake_stack() {
|
|
|
|
return (reinterpret_cast<uptr>(fake_stack_) > 1);
|
|
|
|
}
|
|
|
|
|
2013-09-12 15:11:58 +08:00
|
|
|
FakeStack *fake_stack() {
|
2013-09-18 18:35:12 +08:00
|
|
|
if (!__asan_option_detect_stack_use_after_return)
|
|
|
|
return 0;
|
2013-09-12 16:43:44 +08:00
|
|
|
if (!has_fake_stack())
|
2013-09-12 16:34:50 +08:00
|
|
|
return AsyncSignalSafeLazyInitFakeStack();
|
2013-09-12 15:11:58 +08:00
|
|
|
return fake_stack_;
|
2013-06-26 20:16:05 +08:00
|
|
|
}
|
|
|
|
|
2013-09-12 16:16:28 +08:00
|
|
|
// True is this thread is currently unwinding stack (i.e. collecting a stack
|
|
|
|
// trace). Used to prevent deadlocks on platforms where libc unwinder calls
|
|
|
|
// malloc internally. See PR17116 for more details.
|
2013-11-27 21:22:21 +08:00
|
|
|
bool isUnwinding() const { return unwinding_; }
|
|
|
|
void setUnwinding(bool b) { unwinding_ = b; }
|
2013-09-12 16:16:28 +08:00
|
|
|
|
2014-11-10 23:22:04 +08:00
|
|
|
// True if we are in a deadly signal handler.
|
|
|
|
bool isInDeadlySignal() const { return in_deadly_signal_; }
|
|
|
|
void setInDeadlySignal(bool b) { in_deadly_signal_ = b; }
|
|
|
|
|
2011-11-30 09:07:02 +08:00
|
|
|
AsanThreadLocalMallocStorage &malloc_storage() { return malloc_storage_; }
|
|
|
|
AsanStats &stats() { return stats_; }
|
|
|
|
|
|
|
|
private:
|
2013-11-27 21:22:21 +08:00
|
|
|
// NOTE: There is no AsanThread constructor. It is allocated
|
|
|
|
// via mmap() and *must* be valid in zero-initialized state.
|
2013-05-29 21:09:44 +08:00
|
|
|
void SetThreadStackAndTls();
|
|
|
|
void ClearShadowForThreadStackAndTLS();
|
2013-09-12 16:34:50 +08:00
|
|
|
FakeStack *AsyncSignalSafeLazyInitFakeStack();
|
|
|
|
|
2013-03-21 19:23:41 +08:00
|
|
|
AsanThreadContext *context_;
|
2012-02-24 23:28:43 +08:00
|
|
|
thread_callback_t start_routine_;
|
2011-11-30 09:07:02 +08:00
|
|
|
void *arg_;
|
2013-11-27 21:22:21 +08:00
|
|
|
uptr stack_top_;
|
|
|
|
uptr stack_bottom_;
|
2013-09-19 22:59:52 +08:00
|
|
|
// stack_size_ == stack_top_ - stack_bottom_;
|
|
|
|
// It needs to be set in a async-signal-safe manner.
|
2013-11-27 21:22:21 +08:00
|
|
|
uptr stack_size_;
|
2013-05-29 21:09:44 +08:00
|
|
|
uptr tls_begin_;
|
|
|
|
uptr tls_end_;
|
2011-11-30 09:07:02 +08:00
|
|
|
|
2013-06-26 20:16:05 +08:00
|
|
|
FakeStack *fake_stack_;
|
2011-11-30 09:07:02 +08:00
|
|
|
AsanThreadLocalMallocStorage malloc_storage_;
|
|
|
|
AsanStats stats_;
|
2013-11-27 21:22:21 +08:00
|
|
|
bool unwinding_;
|
2014-11-10 23:22:04 +08:00
|
|
|
bool in_deadly_signal_;
|
2013-09-12 16:16:28 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
// ScopedUnwinding is a scope for stacktracing member of a context
|
|
|
|
class ScopedUnwinding {
|
|
|
|
public:
|
|
|
|
explicit ScopedUnwinding(AsanThread *t) : thread(t) {
|
|
|
|
t->setUnwinding(true);
|
|
|
|
}
|
|
|
|
~ScopedUnwinding() { thread->setUnwinding(false); }
|
|
|
|
|
|
|
|
private:
|
|
|
|
AsanThread *thread;
|
2011-11-30 09:07:02 +08:00
|
|
|
};
|
|
|
|
|
2014-11-10 23:22:04 +08:00
|
|
|
// ScopedDeadlySignal is a scope for handling deadly signals.
|
|
|
|
class ScopedDeadlySignal {
|
|
|
|
public:
|
|
|
|
explicit ScopedDeadlySignal(AsanThread *t) : thread(t) {
|
|
|
|
if (thread) thread->setInDeadlySignal(true);
|
|
|
|
}
|
|
|
|
~ScopedDeadlySignal() {
|
|
|
|
if (thread) thread->setInDeadlySignal(false);
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
AsanThread *thread;
|
|
|
|
};
|
|
|
|
|
2013-03-21 19:23:41 +08:00
|
|
|
struct CreateThreadContextArgs {
|
|
|
|
AsanThread *thread;
|
|
|
|
StackTrace *stack;
|
|
|
|
};
|
|
|
|
|
|
|
|
// Returns a single instance of registry.
|
|
|
|
ThreadRegistry &asanThreadRegistry();
|
|
|
|
|
|
|
|
// Must be called under ThreadRegistryLock.
|
|
|
|
AsanThreadContext *GetThreadContextByTidLocked(u32 tid);
|
|
|
|
|
2013-03-20 17:23:28 +08:00
|
|
|
// Get the current thread. May return 0.
|
|
|
|
AsanThread *GetCurrentThread();
|
|
|
|
void SetCurrentThread(AsanThread *t);
|
|
|
|
u32 GetCurrentTidOrInvalid();
|
2013-03-21 19:23:41 +08:00
|
|
|
AsanThread *FindThreadByStackAddress(uptr addr);
|
2013-03-20 17:23:28 +08:00
|
|
|
|
2013-07-08 20:57:24 +08:00
|
|
|
// Used to handle fork().
|
|
|
|
void EnsureMainThreadIDIsCorrect();
|
2011-11-30 09:07:02 +08:00
|
|
|
} // namespace __asan
|
|
|
|
|
|
|
|
#endif // ASAN_THREAD_H
|