blob: 778e91932ed534e3c09cdaff64e555468adeb264 [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,
Kostya Serebryanyc3390df2012-08-28 11:54:30 +000030 void *arg, StackTrace *stack) {
Kostya Serebryanyf67ec2b2012-11-23 15:38:49 +000031 uptr PageSize = GetPageSizeCached();
32 uptr size = RoundUpTo(sizeof(AsanThread), PageSize);
Alexey Samsonova25b3462012-06-06 16:15:07 +000033 AsanThread *thread = (AsanThread*)MmapOrDie(size, __FUNCTION__);
Alexey Samsonov55cdfc62012-01-17 06:35:31 +000034 thread->start_routine_ = start_routine;
35 thread->arg_ = arg;
36
Kostya Serebryanyf67ec2b2012-11-23 15:38:49 +000037 const uptr kSummaryAllocSize = PageSize;
Kostya Serebryanyb134ffa2012-07-17 07:20:13 +000038 CHECK_LE(sizeof(AsanThreadSummary), kSummaryAllocSize);
Kostya Serebryany6d924fa2012-09-06 10:57:03 +000039 AsanThreadSummary *summary =
Kostya Serebryanyf67ec2b2012-11-23 15:38:49 +000040 (AsanThreadSummary*)MmapOrDie(PageSize, "AsanThreadSummary");
Kostya Serebryanyb134ffa2012-07-17 07:20:13 +000041 summary->Init(parent_tid, stack);
Alexey Samsonov55cdfc62012-01-17 06:35:31 +000042 summary->set_thread(thread);
43 thread->set_summary(summary);
44
45 return thread;
Kostya Serebryany1e172b42011-11-30 01:07:02 +000046}
47
Kostya Serebryanyf58f9982012-02-07 00:27:15 +000048void AsanThreadSummary::TSDDtor(void *tsd) {
49 AsanThreadSummary *summary = (AsanThreadSummary*)tsd;
Alexey Samsonovcb8c4dc2012-07-09 14:36:04 +000050 if (flags()->verbosity >= 1) {
Kostya Serebryanyf58f9982012-02-07 00:27:15 +000051 Report("T%d TSDDtor\n", summary->tid());
52 }
53 if (summary->thread()) {
54 summary->thread()->Destroy();
55 }
56}
57
Kostya Serebryanya6b52262012-01-06 19:44:11 +000058void AsanThread::Destroy() {
Alexey Samsonovcb8c4dc2012-07-09 14:36:04 +000059 if (flags()->verbosity >= 1) {
Kostya Serebryanyf58f9982012-02-07 00:27:15 +000060 Report("T%d exited\n", tid());
61 }
62
63 asanThreadRegistry().UnregisterThread(this);
Kostya Serebryany3f4c3872012-05-31 14:35:53 +000064 CHECK(summary()->thread() == 0);
Kostya Serebryany1e172b42011-11-30 01:07:02 +000065 // We also clear the shadow on thread destruction because
66 // some code may still be executing in later TSD destructors
67 // and we don't want it to have any poisoned stack.
68 ClearShadowForThreadStack();
Alexey Samsonov55cdfc62012-01-17 06:35:31 +000069 fake_stack().Cleanup();
Kostya Serebryanyf67ec2b2012-11-23 15:38:49 +000070 uptr size = RoundUpTo(sizeof(AsanThread), GetPageSizeCached());
Alexey Samsonova25b3462012-06-06 16:15:07 +000071 UnmapOrDie(this, size);
Kostya Serebryany1e172b42011-11-30 01:07:02 +000072}
73
Kostya Serebryany69eca732011-12-16 19:13:35 +000074void AsanThread::Init() {
Kostya Serebryany1e172b42011-11-30 01:07:02 +000075 SetThreadStackTopAndBottom();
Alexey Samsonov55cdfc62012-01-17 06:35:31 +000076 CHECK(AddrIsInMem(stack_bottom_));
Kostya Serebryany541cfb12013-01-18 11:30:36 +000077 CHECK(AddrIsInMem(stack_top_ - 1));
Alexey Samsonov55cdfc62012-01-17 06:35:31 +000078 ClearShadowForThreadStack();
Alexey Samsonovcb8c4dc2012-07-09 14:36:04 +000079 if (flags()->verbosity >= 1) {
Kostya Serebryany1e172b42011-11-30 01:07:02 +000080 int local = 0;
Evgeniy Stepanov739eb792012-03-21 11:32:46 +000081 Report("T%d: stack [%p,%p) size 0x%zx; local=%p\n",
Alexey Samsonov5bcca4e2012-06-06 10:46:00 +000082 tid(), (void*)stack_bottom_, (void*)stack_top_,
Kostya Serebryanya7e760a2012-01-09 19:18:27 +000083 stack_top_ - stack_bottom_, &local);
Kostya Serebryany1e172b42011-11-30 01:07:02 +000084 }
Alexey Samsonov55cdfc62012-01-17 06:35:31 +000085 fake_stack_.Init(stack_size());
Alexander Potapenko75b19eb2012-07-23 14:07:58 +000086 AsanPlatformThreadInit();
Kostya Serebryany69eca732011-12-16 19:13:35 +000087}
88
Timur Iskhodzhanov600972e2012-02-24 15:28:43 +000089thread_return_t AsanThread::ThreadStart() {
Kostya Serebryany69eca732011-12-16 19:13:35 +000090 Init();
Alexey Samsonovcb8c4dc2012-07-09 14:36:04 +000091 if (flags()->use_sigaltstack) SetAlternateSignalStack();
Kostya Serebryany1e172b42011-11-30 01:07:02 +000092
93 if (!start_routine_) {
Kostya Serebryany3f4c3872012-05-31 14:35:53 +000094 // start_routine_ == 0 if we're on the main thread or on one of the
Kostya Serebryany1e172b42011-11-30 01:07:02 +000095 // OS X libdispatch worker threads. But nobody is supposed to call
96 // ThreadStart() for the worker threads.
97 CHECK(tid() == 0);
98 return 0;
99 }
100
Timur Iskhodzhanov600972e2012-02-24 15:28:43 +0000101 thread_return_t res = start_routine_(arg_);
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000102 malloc_storage().CommitBack();
Alexey Samsonovcb8c4dc2012-07-09 14:36:04 +0000103 if (flags()->use_sigaltstack) UnsetAlternateSignalStack();
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000104
Kostya Serebryanyaf344152012-01-11 02:03:16 +0000105 this->Destroy();
106
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000107 return res;
108}
109
Alexey Samsonove5931fd2012-06-07 07:13:46 +0000110void AsanThread::SetThreadStackTopAndBottom() {
111 GetThreadStackTopAndBottom(tid() == 0, &stack_top_, &stack_bottom_);
112 int local;
113 CHECK(AddrIsInStack((uptr)&local));
114}
115
Alexey Samsonov55cdfc62012-01-17 06:35:31 +0000116void AsanThread::ClearShadowForThreadStack() {
117 PoisonShadow(stack_bottom_, stack_top_ - stack_bottom_, 0);
118}
119
Kostya Serebryany3f4c3872012-05-31 14:35:53 +0000120const char *AsanThread::GetFrameNameByAddr(uptr addr, uptr *offset) {
121 uptr bottom = 0;
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000122 if (AddrIsInStack(addr)) {
123 bottom = stack_bottom();
124 } else {
125 bottom = fake_stack().AddrIsInFakeStack(addr);
126 CHECK(bottom);
Alexander Potapenkoe406c8c2012-11-15 15:24:42 +0000127 *offset = addr - bottom;
128 return (const char *)((uptr*)bottom)[1];
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000129 }
Kostya Serebryany5af39e52012-11-21 12:38:58 +0000130 uptr aligned_addr = addr & ~(SANITIZER_WORDSIZE/8 - 1); // align addr.
Kostya Serebryanyee392552012-05-31 15:02:07 +0000131 u8 *shadow_ptr = (u8*)MemToShadow(aligned_addr);
132 u8 *shadow_bottom = (u8*)MemToShadow(bottom);
Evgeniy Stepanov3972ea02012-05-12 12:33:10 +0000133
134 while (shadow_ptr >= shadow_bottom &&
Alexander Potapenkoe406c8c2012-11-15 15:24:42 +0000135 *shadow_ptr != kAsanStackLeftRedzoneMagic) {
Evgeniy Stepanov3972ea02012-05-12 12:33:10 +0000136 shadow_ptr--;
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000137 }
Evgeniy Stepanov3972ea02012-05-12 12:33:10 +0000138
139 while (shadow_ptr >= shadow_bottom &&
Alexander Potapenkoe406c8c2012-11-15 15:24:42 +0000140 *shadow_ptr == kAsanStackLeftRedzoneMagic) {
Evgeniy Stepanov3972ea02012-05-12 12:33:10 +0000141 shadow_ptr--;
142 }
143
144 if (shadow_ptr < shadow_bottom) {
Alexander Potapenkoe406c8c2012-11-15 15:24:42 +0000145 *offset = 0;
146 return "UNKNOWN";
Evgeniy Stepanov3972ea02012-05-12 12:33:10 +0000147 }
148
Kostya Serebryany3f4c3872012-05-31 14:35:53 +0000149 uptr* ptr = (uptr*)SHADOW_TO_MEM((uptr)(shadow_ptr + 1));
Alexander Potapenkoe406c8c2012-11-15 15:24:42 +0000150 CHECK(ptr[0] == kCurrentStackFrameMagic);
Kostya Serebryany3f4c3872012-05-31 14:35:53 +0000151 *offset = addr - (uptr)ptr;
Evgeniy Stepanov3972ea02012-05-12 12:33:10 +0000152 return (const char*)ptr[1];
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000153}
154
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000155} // namespace __asan