2012-06-04 21:50:10 +08:00
|
|
|
//===-- asan_thread.cc ----------------------------------------------------===//
|
2011-11-30 09:07:02 +08:00
|
|
|
//
|
|
|
|
// 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.
|
|
|
|
//
|
|
|
|
// Thread-related code.
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
#include "asan_allocator.h"
|
|
|
|
#include "asan_interceptors.h"
|
2012-01-05 08:44:33 +08:00
|
|
|
#include "asan_procmaps.h"
|
2012-01-17 14:35:31 +08:00
|
|
|
#include "asan_stack.h"
|
2011-11-30 09:07:02 +08:00
|
|
|
#include "asan_thread.h"
|
2012-01-11 10:03:16 +08:00
|
|
|
#include "asan_thread_registry.h"
|
2011-11-30 09:07:02 +08:00
|
|
|
#include "asan_mapping.h"
|
|
|
|
|
|
|
|
namespace __asan {
|
|
|
|
|
|
|
|
AsanThread::AsanThread(LinkerInitialized x)
|
|
|
|
: fake_stack_(x),
|
|
|
|
malloc_storage_(x),
|
|
|
|
stats_(x) { }
|
|
|
|
|
2012-02-24 23:28:43 +08:00
|
|
|
AsanThread *AsanThread::Create(int parent_tid, thread_callback_t start_routine,
|
2012-01-17 14:35:31 +08:00
|
|
|
void *arg, AsanStackTrace *stack) {
|
2012-05-31 22:35:53 +08:00
|
|
|
uptr size = RoundUpTo(sizeof(AsanThread), kPageSize);
|
2012-01-17 14:35:31 +08:00
|
|
|
AsanThread *thread = (AsanThread*)AsanMmapSomewhereOrDie(size, __FUNCTION__);
|
|
|
|
thread->start_routine_ = start_routine;
|
|
|
|
thread->arg_ = arg;
|
|
|
|
|
|
|
|
AsanThreadSummary *summary = new AsanThreadSummary(parent_tid, stack);
|
|
|
|
summary->set_thread(thread);
|
|
|
|
thread->set_summary(summary);
|
|
|
|
|
|
|
|
return thread;
|
2011-11-30 09:07:02 +08:00
|
|
|
}
|
|
|
|
|
2012-02-07 08:27:15 +08:00
|
|
|
void AsanThreadSummary::TSDDtor(void *tsd) {
|
|
|
|
AsanThreadSummary *summary = (AsanThreadSummary*)tsd;
|
|
|
|
if (FLAG_v >= 1) {
|
|
|
|
Report("T%d TSDDtor\n", summary->tid());
|
|
|
|
}
|
|
|
|
if (summary->thread()) {
|
|
|
|
summary->thread()->Destroy();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-01-07 03:44:11 +08:00
|
|
|
void AsanThread::Destroy() {
|
2012-02-07 08:27:15 +08:00
|
|
|
if (FLAG_v >= 1) {
|
|
|
|
Report("T%d exited\n", tid());
|
|
|
|
}
|
|
|
|
|
|
|
|
asanThreadRegistry().UnregisterThread(this);
|
2012-05-31 22:35:53 +08:00
|
|
|
CHECK(summary()->thread() == 0);
|
2011-11-30 09:07:02 +08:00
|
|
|
// We also clear the shadow on thread destruction because
|
|
|
|
// some code may still be executing in later TSD destructors
|
|
|
|
// and we don't want it to have any poisoned stack.
|
|
|
|
ClearShadowForThreadStack();
|
2012-01-17 14:35:31 +08:00
|
|
|
fake_stack().Cleanup();
|
2012-05-31 22:35:53 +08:00
|
|
|
uptr size = RoundUpTo(sizeof(AsanThread), kPageSize);
|
2012-01-07 03:44:11 +08:00
|
|
|
AsanUnmapOrDie(this, size);
|
2011-11-30 09:07:02 +08:00
|
|
|
}
|
|
|
|
|
2011-12-17 03:13:35 +08:00
|
|
|
void AsanThread::Init() {
|
2011-11-30 09:07:02 +08:00
|
|
|
SetThreadStackTopAndBottom();
|
2012-01-17 14:35:31 +08:00
|
|
|
CHECK(AddrIsInMem(stack_bottom_));
|
|
|
|
CHECK(AddrIsInMem(stack_top_));
|
|
|
|
ClearShadowForThreadStack();
|
2011-11-30 09:07:02 +08:00
|
|
|
if (FLAG_v >= 1) {
|
|
|
|
int local = 0;
|
2012-03-21 19:32:46 +08:00
|
|
|
Report("T%d: stack [%p,%p) size 0x%zx; local=%p\n",
|
2011-11-30 09:07:02 +08:00
|
|
|
tid(), stack_bottom_, stack_top_,
|
2012-01-10 03:18:27 +08:00
|
|
|
stack_top_ - stack_bottom_, &local);
|
2011-11-30 09:07:02 +08:00
|
|
|
}
|
2012-01-17 14:35:31 +08:00
|
|
|
fake_stack_.Init(stack_size());
|
2011-12-17 03:13:35 +08:00
|
|
|
}
|
|
|
|
|
2012-02-24 23:28:43 +08:00
|
|
|
thread_return_t AsanThread::ThreadStart() {
|
2011-12-17 03:13:35 +08:00
|
|
|
Init();
|
2012-04-05 18:54:52 +08:00
|
|
|
if (FLAG_use_sigaltstack) SetAlternateSignalStack();
|
2011-11-30 09:07:02 +08:00
|
|
|
|
|
|
|
if (!start_routine_) {
|
2012-05-31 22:35:53 +08:00
|
|
|
// start_routine_ == 0 if we're on the main thread or on one of the
|
2011-11-30 09:07:02 +08:00
|
|
|
// OS X libdispatch worker threads. But nobody is supposed to call
|
|
|
|
// ThreadStart() for the worker threads.
|
|
|
|
CHECK(tid() == 0);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-02-24 23:28:43 +08:00
|
|
|
thread_return_t res = start_routine_(arg_);
|
2011-11-30 09:07:02 +08:00
|
|
|
malloc_storage().CommitBack();
|
2012-04-05 18:54:52 +08:00
|
|
|
if (FLAG_use_sigaltstack) UnsetAlternateSignalStack();
|
2011-11-30 09:07:02 +08:00
|
|
|
|
2012-01-11 10:03:16 +08:00
|
|
|
this->Destroy();
|
|
|
|
|
2011-11-30 09:07:02 +08:00
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2012-01-17 14:35:31 +08:00
|
|
|
void AsanThread::ClearShadowForThreadStack() {
|
|
|
|
PoisonShadow(stack_bottom_, stack_top_ - stack_bottom_, 0);
|
|
|
|
}
|
|
|
|
|
2012-05-31 22:35:53 +08:00
|
|
|
const char *AsanThread::GetFrameNameByAddr(uptr addr, uptr *offset) {
|
|
|
|
uptr bottom = 0;
|
2011-11-30 09:07:02 +08:00
|
|
|
bool is_fake_stack = false;
|
|
|
|
if (AddrIsInStack(addr)) {
|
|
|
|
bottom = stack_bottom();
|
|
|
|
} else {
|
|
|
|
bottom = fake_stack().AddrIsInFakeStack(addr);
|
|
|
|
CHECK(bottom);
|
|
|
|
is_fake_stack = true;
|
|
|
|
}
|
2012-05-31 22:35:53 +08:00
|
|
|
uptr aligned_addr = addr & ~(__WORDSIZE/8 - 1); // align addr.
|
2012-05-31 23:02:07 +08:00
|
|
|
u8 *shadow_ptr = (u8*)MemToShadow(aligned_addr);
|
|
|
|
u8 *shadow_bottom = (u8*)MemToShadow(bottom);
|
2012-05-12 20:33:10 +08:00
|
|
|
|
|
|
|
while (shadow_ptr >= shadow_bottom &&
|
|
|
|
*shadow_ptr != kAsanStackLeftRedzoneMagic) {
|
|
|
|
shadow_ptr--;
|
|
|
|
}
|
|
|
|
|
|
|
|
while (shadow_ptr >= shadow_bottom &&
|
|
|
|
*shadow_ptr == kAsanStackLeftRedzoneMagic) {
|
|
|
|
shadow_ptr--;
|
2011-11-30 09:07:02 +08:00
|
|
|
}
|
2012-05-12 20:33:10 +08:00
|
|
|
|
|
|
|
if (shadow_ptr < shadow_bottom) {
|
|
|
|
*offset = 0;
|
|
|
|
return "UNKNOWN";
|
|
|
|
}
|
|
|
|
|
2012-05-31 22:35:53 +08:00
|
|
|
uptr* ptr = (uptr*)SHADOW_TO_MEM((uptr)(shadow_ptr + 1));
|
2012-05-12 20:33:10 +08:00
|
|
|
CHECK((ptr[0] == kCurrentStackFrameMagic) ||
|
|
|
|
(is_fake_stack && ptr[0] == kRetiredStackFrameMagic));
|
2012-05-31 22:35:53 +08:00
|
|
|
*offset = addr - (uptr)ptr;
|
2012-05-12 20:33:10 +08:00
|
|
|
return (const char*)ptr[1];
|
2011-11-30 09:07:02 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace __asan
|