blob: 05a41ea9685ae8b1035cf5083dc85bcbf880f312 [file] [log] [blame]
Alexey Samsonove5f58952012-06-04 13:50:10 +00001//===-- asan_thread.cc ----------------------------------------------------===//
Kostya Serebryany1e172b42011-11-30 01:07:02 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file is a part of AddressSanitizer, an address sanity checker.
11//
12// Thread-related code.
13//===----------------------------------------------------------------------===//
14#include "asan_allocator.h"
15#include "asan_interceptors.h"
Alexey Samsonov55cdfc62012-01-17 06:35:31 +000016#include "asan_stack.h"
Kostya Serebryany1e172b42011-11-30 01:07:02 +000017#include "asan_thread.h"
Kostya Serebryanyaf344152012-01-11 02:03:16 +000018#include "asan_thread_registry.h"
Kostya Serebryany1e172b42011-11-30 01:07:02 +000019#include "asan_mapping.h"
Alexey Samsonove5931fd2012-06-07 07:13:46 +000020#include "sanitizer_common/sanitizer_common.h"
Kostya Serebryany1e172b42011-11-30 01:07:02 +000021
Kostya Serebryany1e172b42011-11-30 01:07:02 +000022namespace __asan {
23
24AsanThread::AsanThread(LinkerInitialized x)
25 : fake_stack_(x),
26 malloc_storage_(x),
27 stats_(x) { }
28
Kostya Serebryanyb134ffa2012-07-17 07:20:13 +000029static AsanLock mu_for_thread_summary(LINKER_INITIALIZED);
30static LowLevelAllocator allocator_for_thread_summary(LINKER_INITIALIZED);
31
Kostya Serebryanye0cff0b2012-06-06 15:06:58 +000032AsanThread *AsanThread::Create(u32 parent_tid, thread_callback_t start_routine,
Alexey Samsonov55cdfc62012-01-17 06:35:31 +000033 void *arg, AsanStackTrace *stack) {
Kostya Serebryany3f4c3872012-05-31 14:35:53 +000034 uptr size = RoundUpTo(sizeof(AsanThread), kPageSize);
Alexey Samsonova25b3462012-06-06 16:15:07 +000035 AsanThread *thread = (AsanThread*)MmapOrDie(size, __FUNCTION__);
Alexey Samsonov55cdfc62012-01-17 06:35:31 +000036 thread->start_routine_ = start_routine;
37 thread->arg_ = arg;
38
Kostya Serebryanyb134ffa2012-07-17 07:20:13 +000039 const uptr kSummaryAllocSize = 1024;
40 CHECK_LE(sizeof(AsanThreadSummary), kSummaryAllocSize);
41 AsanThreadSummary *summary;
42 {
43 ScopedLock lock(&mu_for_thread_summary);
44 summary = (AsanThreadSummary*)
45 allocator_for_thread_summary.Allocate(kSummaryAllocSize);
46 }
47 summary->Init(parent_tid, stack);
Alexey Samsonov55cdfc62012-01-17 06:35:31 +000048 summary->set_thread(thread);
49 thread->set_summary(summary);
50
51 return thread;
Kostya Serebryany1e172b42011-11-30 01:07:02 +000052}
53
Kostya Serebryanyf58f9982012-02-07 00:27:15 +000054void AsanThreadSummary::TSDDtor(void *tsd) {
55 AsanThreadSummary *summary = (AsanThreadSummary*)tsd;
Alexey Samsonovcb8c4dc2012-07-09 14:36:04 +000056 if (flags()->verbosity >= 1) {
Kostya Serebryanyf58f9982012-02-07 00:27:15 +000057 Report("T%d TSDDtor\n", summary->tid());
58 }
59 if (summary->thread()) {
60 summary->thread()->Destroy();
61 }
62}
63
Kostya Serebryanya6b52262012-01-06 19:44:11 +000064void AsanThread::Destroy() {
Alexey Samsonovcb8c4dc2012-07-09 14:36:04 +000065 if (flags()->verbosity >= 1) {
Kostya Serebryanyf58f9982012-02-07 00:27:15 +000066 Report("T%d exited\n", tid());
67 }
68
69 asanThreadRegistry().UnregisterThread(this);
Kostya Serebryany3f4c3872012-05-31 14:35:53 +000070 CHECK(summary()->thread() == 0);
Kostya Serebryany1e172b42011-11-30 01:07:02 +000071 // We also clear the shadow on thread destruction because
72 // some code may still be executing in later TSD destructors
73 // and we don't want it to have any poisoned stack.
74 ClearShadowForThreadStack();
Alexey Samsonov55cdfc62012-01-17 06:35:31 +000075 fake_stack().Cleanup();
Kostya Serebryany3f4c3872012-05-31 14:35:53 +000076 uptr size = RoundUpTo(sizeof(AsanThread), kPageSize);
Alexey Samsonova25b3462012-06-06 16:15:07 +000077 UnmapOrDie(this, size);
Kostya Serebryany1e172b42011-11-30 01:07:02 +000078}
79
Kostya Serebryany69eca732011-12-16 19:13:35 +000080void AsanThread::Init() {
Kostya Serebryany1e172b42011-11-30 01:07:02 +000081 SetThreadStackTopAndBottom();
Alexey Samsonov55cdfc62012-01-17 06:35:31 +000082 CHECK(AddrIsInMem(stack_bottom_));
83 CHECK(AddrIsInMem(stack_top_));
84 ClearShadowForThreadStack();
Alexey Samsonovcb8c4dc2012-07-09 14:36:04 +000085 if (flags()->verbosity >= 1) {
Kostya Serebryany1e172b42011-11-30 01:07:02 +000086 int local = 0;
Evgeniy Stepanov739eb792012-03-21 11:32:46 +000087 Report("T%d: stack [%p,%p) size 0x%zx; local=%p\n",
Alexey Samsonov5bcca4e2012-06-06 10:46:00 +000088 tid(), (void*)stack_bottom_, (void*)stack_top_,
Kostya Serebryanya7e760a2012-01-09 19:18:27 +000089 stack_top_ - stack_bottom_, &local);
Kostya Serebryany1e172b42011-11-30 01:07:02 +000090 }
Alexey Samsonov55cdfc62012-01-17 06:35:31 +000091 fake_stack_.Init(stack_size());
Alexander Potapenko75b19eb2012-07-23 14:07:58 +000092 AsanPlatformThreadInit();
Kostya Serebryany69eca732011-12-16 19:13:35 +000093}
94
Timur Iskhodzhanov600972e2012-02-24 15:28:43 +000095thread_return_t AsanThread::ThreadStart() {
Kostya Serebryany69eca732011-12-16 19:13:35 +000096 Init();
Alexey Samsonovcb8c4dc2012-07-09 14:36:04 +000097 if (flags()->use_sigaltstack) SetAlternateSignalStack();
Kostya Serebryany1e172b42011-11-30 01:07:02 +000098
99 if (!start_routine_) {
Kostya Serebryany3f4c3872012-05-31 14:35:53 +0000100 // start_routine_ == 0 if we're on the main thread or on one of the
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000101 // OS X libdispatch worker threads. But nobody is supposed to call
102 // ThreadStart() for the worker threads.
103 CHECK(tid() == 0);
104 return 0;
105 }
106
Timur Iskhodzhanov600972e2012-02-24 15:28:43 +0000107 thread_return_t res = start_routine_(arg_);
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000108 malloc_storage().CommitBack();
Alexey Samsonovcb8c4dc2012-07-09 14:36:04 +0000109 if (flags()->use_sigaltstack) UnsetAlternateSignalStack();
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000110
Kostya Serebryanyaf344152012-01-11 02:03:16 +0000111 this->Destroy();
112
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000113 return res;
114}
115
Alexey Samsonove5931fd2012-06-07 07:13:46 +0000116void AsanThread::SetThreadStackTopAndBottom() {
117 GetThreadStackTopAndBottom(tid() == 0, &stack_top_, &stack_bottom_);
118 int local;
119 CHECK(AddrIsInStack((uptr)&local));
120}
121
Alexey Samsonov55cdfc62012-01-17 06:35:31 +0000122void AsanThread::ClearShadowForThreadStack() {
123 PoisonShadow(stack_bottom_, stack_top_ - stack_bottom_, 0);
124}
125
Kostya Serebryany3f4c3872012-05-31 14:35:53 +0000126const char *AsanThread::GetFrameNameByAddr(uptr addr, uptr *offset) {
127 uptr bottom = 0;
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000128 bool is_fake_stack = false;
129 if (AddrIsInStack(addr)) {
130 bottom = stack_bottom();
131 } else {
132 bottom = fake_stack().AddrIsInFakeStack(addr);
133 CHECK(bottom);
134 is_fake_stack = true;
135 }
Kostya Serebryany3f4c3872012-05-31 14:35:53 +0000136 uptr aligned_addr = addr & ~(__WORDSIZE/8 - 1); // align addr.
Kostya Serebryanyee392552012-05-31 15:02:07 +0000137 u8 *shadow_ptr = (u8*)MemToShadow(aligned_addr);
138 u8 *shadow_bottom = (u8*)MemToShadow(bottom);
Evgeniy Stepanov3972ea02012-05-12 12:33:10 +0000139
140 while (shadow_ptr >= shadow_bottom &&
141 *shadow_ptr != kAsanStackLeftRedzoneMagic) {
142 shadow_ptr--;
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000143 }
Evgeniy Stepanov3972ea02012-05-12 12:33:10 +0000144
145 while (shadow_ptr >= shadow_bottom &&
146 *shadow_ptr == kAsanStackLeftRedzoneMagic) {
147 shadow_ptr--;
148 }
149
150 if (shadow_ptr < shadow_bottom) {
151 *offset = 0;
152 return "UNKNOWN";
153 }
154
Kostya Serebryany3f4c3872012-05-31 14:35:53 +0000155 uptr* ptr = (uptr*)SHADOW_TO_MEM((uptr)(shadow_ptr + 1));
Evgeniy Stepanov3972ea02012-05-12 12:33:10 +0000156 CHECK((ptr[0] == kCurrentStackFrameMagic) ||
157 (is_fake_stack && ptr[0] == kRetiredStackFrameMagic));
Kostya Serebryany3f4c3872012-05-31 14:35:53 +0000158 *offset = addr - (uptr)ptr;
Evgeniy Stepanov3972ea02012-05-12 12:33:10 +0000159 return (const char*)ptr[1];
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000160}
161
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000162} // namespace __asan