blob: 2f2773212f73aac171f2e73989ed951f2a90814f [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 Serebryanye0cff0b2012-06-06 15:06:58 +000029AsanThread *AsanThread::Create(u32 parent_tid, thread_callback_t start_routine,
Alexey Samsonov55cdfc62012-01-17 06:35:31 +000030 void *arg, AsanStackTrace *stack) {
Kostya Serebryany3f4c3872012-05-31 14:35:53 +000031 uptr size = RoundUpTo(sizeof(AsanThread), kPageSize);
Alexey Samsonova25b3462012-06-06 16:15:07 +000032 AsanThread *thread = (AsanThread*)MmapOrDie(size, __FUNCTION__);
Alexey Samsonov55cdfc62012-01-17 06:35:31 +000033 thread->start_routine_ = start_routine;
34 thread->arg_ = arg;
35
36 AsanThreadSummary *summary = new AsanThreadSummary(parent_tid, stack);
37 summary->set_thread(thread);
38 thread->set_summary(summary);
39
40 return thread;
Kostya Serebryany1e172b42011-11-30 01:07:02 +000041}
42
Kostya Serebryanyf58f9982012-02-07 00:27:15 +000043void AsanThreadSummary::TSDDtor(void *tsd) {
44 AsanThreadSummary *summary = (AsanThreadSummary*)tsd;
Alexey Samsonovcb8c4dc2012-07-09 14:36:04 +000045 if (flags()->verbosity >= 1) {
Kostya Serebryanyf58f9982012-02-07 00:27:15 +000046 Report("T%d TSDDtor\n", summary->tid());
47 }
48 if (summary->thread()) {
49 summary->thread()->Destroy();
50 }
51}
52
Kostya Serebryanya6b52262012-01-06 19:44:11 +000053void AsanThread::Destroy() {
Alexey Samsonovcb8c4dc2012-07-09 14:36:04 +000054 if (flags()->verbosity >= 1) {
Kostya Serebryanyf58f9982012-02-07 00:27:15 +000055 Report("T%d exited\n", tid());
56 }
57
58 asanThreadRegistry().UnregisterThread(this);
Kostya Serebryany3f4c3872012-05-31 14:35:53 +000059 CHECK(summary()->thread() == 0);
Kostya Serebryany1e172b42011-11-30 01:07:02 +000060 // We also clear the shadow on thread destruction because
61 // some code may still be executing in later TSD destructors
62 // and we don't want it to have any poisoned stack.
63 ClearShadowForThreadStack();
Alexey Samsonov55cdfc62012-01-17 06:35:31 +000064 fake_stack().Cleanup();
Kostya Serebryany3f4c3872012-05-31 14:35:53 +000065 uptr size = RoundUpTo(sizeof(AsanThread), kPageSize);
Alexey Samsonova25b3462012-06-06 16:15:07 +000066 UnmapOrDie(this, size);
Kostya Serebryany1e172b42011-11-30 01:07:02 +000067}
68
Kostya Serebryany69eca732011-12-16 19:13:35 +000069void AsanThread::Init() {
Kostya Serebryany1e172b42011-11-30 01:07:02 +000070 SetThreadStackTopAndBottom();
Alexey Samsonov55cdfc62012-01-17 06:35:31 +000071 CHECK(AddrIsInMem(stack_bottom_));
72 CHECK(AddrIsInMem(stack_top_));
73 ClearShadowForThreadStack();
Alexey Samsonovcb8c4dc2012-07-09 14:36:04 +000074 if (flags()->verbosity >= 1) {
Kostya Serebryany1e172b42011-11-30 01:07:02 +000075 int local = 0;
Evgeniy Stepanov739eb792012-03-21 11:32:46 +000076 Report("T%d: stack [%p,%p) size 0x%zx; local=%p\n",
Alexey Samsonov5bcca4e2012-06-06 10:46:00 +000077 tid(), (void*)stack_bottom_, (void*)stack_top_,
Kostya Serebryanya7e760a2012-01-09 19:18:27 +000078 stack_top_ - stack_bottom_, &local);
Kostya Serebryany1e172b42011-11-30 01:07:02 +000079 }
Alexey Samsonov55cdfc62012-01-17 06:35:31 +000080 fake_stack_.Init(stack_size());
Kostya Serebryany69eca732011-12-16 19:13:35 +000081}
82
Timur Iskhodzhanov600972e2012-02-24 15:28:43 +000083thread_return_t AsanThread::ThreadStart() {
Kostya Serebryany69eca732011-12-16 19:13:35 +000084 Init();
Alexey Samsonovcb8c4dc2012-07-09 14:36:04 +000085 if (flags()->use_sigaltstack) SetAlternateSignalStack();
Kostya Serebryany1e172b42011-11-30 01:07:02 +000086
87 if (!start_routine_) {
Kostya Serebryany3f4c3872012-05-31 14:35:53 +000088 // start_routine_ == 0 if we're on the main thread or on one of the
Kostya Serebryany1e172b42011-11-30 01:07:02 +000089 // OS X libdispatch worker threads. But nobody is supposed to call
90 // ThreadStart() for the worker threads.
91 CHECK(tid() == 0);
92 return 0;
93 }
94
Timur Iskhodzhanov600972e2012-02-24 15:28:43 +000095 thread_return_t res = start_routine_(arg_);
Kostya Serebryany1e172b42011-11-30 01:07:02 +000096 malloc_storage().CommitBack();
Alexey Samsonovcb8c4dc2012-07-09 14:36:04 +000097 if (flags()->use_sigaltstack) UnsetAlternateSignalStack();
Kostya Serebryany1e172b42011-11-30 01:07:02 +000098
Kostya Serebryanyaf344152012-01-11 02:03:16 +000099 this->Destroy();
100
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000101 return res;
102}
103
Alexey Samsonove5931fd2012-06-07 07:13:46 +0000104void AsanThread::SetThreadStackTopAndBottom() {
105 GetThreadStackTopAndBottom(tid() == 0, &stack_top_, &stack_bottom_);
106 int local;
107 CHECK(AddrIsInStack((uptr)&local));
108}
109
Alexey Samsonov55cdfc62012-01-17 06:35:31 +0000110void AsanThread::ClearShadowForThreadStack() {
111 PoisonShadow(stack_bottom_, stack_top_ - stack_bottom_, 0);
112}
113
Kostya Serebryany3f4c3872012-05-31 14:35:53 +0000114const char *AsanThread::GetFrameNameByAddr(uptr addr, uptr *offset) {
115 uptr bottom = 0;
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000116 bool is_fake_stack = false;
117 if (AddrIsInStack(addr)) {
118 bottom = stack_bottom();
119 } else {
120 bottom = fake_stack().AddrIsInFakeStack(addr);
121 CHECK(bottom);
122 is_fake_stack = true;
123 }
Kostya Serebryany3f4c3872012-05-31 14:35:53 +0000124 uptr aligned_addr = addr & ~(__WORDSIZE/8 - 1); // align addr.
Kostya Serebryanyee392552012-05-31 15:02:07 +0000125 u8 *shadow_ptr = (u8*)MemToShadow(aligned_addr);
126 u8 *shadow_bottom = (u8*)MemToShadow(bottom);
Evgeniy Stepanov3972ea02012-05-12 12:33:10 +0000127
128 while (shadow_ptr >= shadow_bottom &&
129 *shadow_ptr != kAsanStackLeftRedzoneMagic) {
130 shadow_ptr--;
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000131 }
Evgeniy Stepanov3972ea02012-05-12 12:33:10 +0000132
133 while (shadow_ptr >= shadow_bottom &&
134 *shadow_ptr == kAsanStackLeftRedzoneMagic) {
135 shadow_ptr--;
136 }
137
138 if (shadow_ptr < shadow_bottom) {
139 *offset = 0;
140 return "UNKNOWN";
141 }
142
Kostya Serebryany3f4c3872012-05-31 14:35:53 +0000143 uptr* ptr = (uptr*)SHADOW_TO_MEM((uptr)(shadow_ptr + 1));
Evgeniy Stepanov3972ea02012-05-12 12:33:10 +0000144 CHECK((ptr[0] == kCurrentStackFrameMagic) ||
145 (is_fake_stack && ptr[0] == kRetiredStackFrameMagic));
Kostya Serebryany3f4c3872012-05-31 14:35:53 +0000146 *offset = addr - (uptr)ptr;
Evgeniy Stepanov3972ea02012-05-12 12:33:10 +0000147 return (const char*)ptr[1];
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000148}
149
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000150} // namespace __asan