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"
|
|
|
|
#include "asan_stack.h"
|
|
|
|
#include "asan_stats.h"
|
2012-07-17 15:20:13 +08:00
|
|
|
#include "sanitizer_common/sanitizer_libc.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.
|
|
|
|
|
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.
|
|
|
|
class AsanThreadSummary {
|
|
|
|
public:
|
|
|
|
explicit AsanThreadSummary(LinkerInitialized) { } // for T0.
|
2012-08-28 19:54:30 +08:00
|
|
|
void Init(u32 parent_tid, StackTrace *stack) {
|
2012-07-17 15:20:13 +08:00
|
|
|
parent_tid_ = parent_tid;
|
|
|
|
announced_ = false;
|
2012-06-06 23:06:58 +08:00
|
|
|
tid_ = kInvalidTid;
|
2011-11-30 09:07:02 +08:00
|
|
|
if (stack) {
|
2012-07-17 15:20:13 +08:00
|
|
|
internal_memcpy(&stack_, stack, sizeof(*stack));
|
2011-11-30 09:07:02 +08:00
|
|
|
}
|
|
|
|
thread_ = 0;
|
|
|
|
}
|
|
|
|
void Announce() {
|
|
|
|
if (tid_ == 0) return; // no need to announce the main thread.
|
|
|
|
if (!announced_) {
|
|
|
|
announced_ = true;
|
2012-08-28 19:34:40 +08:00
|
|
|
Printf("Thread T%d created by T%d here:\n", tid_, parent_tid_);
|
2012-08-28 21:49:49 +08:00
|
|
|
PrintStack(&stack_);
|
2011-11-30 09:07:02 +08:00
|
|
|
}
|
|
|
|
}
|
2012-06-06 23:06:58 +08:00
|
|
|
u32 tid() { return tid_; }
|
|
|
|
void set_tid(u32 tid) { tid_ = tid; }
|
2011-11-30 09:07:02 +08:00
|
|
|
AsanThread *thread() { return thread_; }
|
|
|
|
void set_thread(AsanThread *thread) { thread_ = thread; }
|
2012-02-07 08:27:15 +08:00
|
|
|
static void TSDDtor(void *tsd);
|
|
|
|
|
2011-11-30 09:07:02 +08:00
|
|
|
private:
|
2012-06-06 23:06:58 +08:00
|
|
|
u32 tid_;
|
|
|
|
u32 parent_tid_;
|
2011-11-30 09:07:02 +08:00
|
|
|
bool announced_;
|
2012-08-28 19:54:30 +08:00
|
|
|
StackTrace stack_;
|
2011-11-30 09:07:02 +08:00
|
|
|
AsanThread *thread_;
|
|
|
|
};
|
|
|
|
|
|
|
|
// AsanThread are stored in TSD and destroyed when the thread dies.
|
|
|
|
class AsanThread {
|
|
|
|
public:
|
|
|
|
explicit AsanThread(LinkerInitialized); // for T0.
|
2012-06-06 23:06:58 +08:00
|
|
|
static AsanThread *Create(u32 parent_tid, thread_callback_t start_routine,
|
2012-08-28 19:54:30 +08:00
|
|
|
void *arg, StackTrace *stack);
|
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.
|
2012-02-24 23:28:43 +08:00
|
|
|
thread_return_t ThreadStart();
|
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_; }
|
|
|
|
uptr stack_size() { return stack_top_ - stack_bottom_; }
|
2012-06-06 23:06:58 +08:00
|
|
|
u32 tid() { return summary_->tid(); }
|
2011-11-30 09:07:02 +08:00
|
|
|
AsanThreadSummary *summary() { return summary_; }
|
|
|
|
void set_summary(AsanThreadSummary *summary) { summary_ = summary; }
|
|
|
|
|
2012-05-31 22:35:53 +08:00
|
|
|
const char *GetFrameNameByAddr(uptr addr, uptr *offset);
|
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_;
|
|
|
|
}
|
|
|
|
|
|
|
|
FakeStack &fake_stack() { return fake_stack_; }
|
|
|
|
AsanThreadLocalMallocStorage &malloc_storage() { return malloc_storage_; }
|
|
|
|
AsanStats &stats() { return stats_; }
|
|
|
|
|
|
|
|
private:
|
|
|
|
void SetThreadStackTopAndBottom();
|
|
|
|
void ClearShadowForThreadStack();
|
|
|
|
AsanThreadSummary *summary_;
|
2012-02-24 23:28:43 +08:00
|
|
|
thread_callback_t start_routine_;
|
2011-11-30 09:07:02 +08:00
|
|
|
void *arg_;
|
2012-05-31 22:35:53 +08:00
|
|
|
uptr stack_top_;
|
|
|
|
uptr stack_bottom_;
|
2011-11-30 09:07:02 +08:00
|
|
|
|
|
|
|
FakeStack fake_stack_;
|
|
|
|
AsanThreadLocalMallocStorage malloc_storage_;
|
|
|
|
AsanStats stats_;
|
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace __asan
|
|
|
|
|
|
|
|
#endif // ASAN_THREAD_H
|