blob: 7f60ca913acc91a80dc0002232d5b38604904ae4 [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 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
Kostya Serebryany6d924fa2012-09-06 10:57:03 +000036 const uptr kSummaryAllocSize = kPageSize;
Kostya Serebryanyb134ffa2012-07-17 07:20:13 +000037 CHECK_LE(sizeof(AsanThreadSummary), kSummaryAllocSize);
Kostya Serebryany6d924fa2012-09-06 10:57:03 +000038 AsanThreadSummary *summary =
39 (AsanThreadSummary*)MmapOrDie(kPageSize, "AsanThreadSummary");
Kostya Serebryanyb134ffa2012-07-17 07:20:13 +000040 summary->Init(parent_tid, stack);
Alexey Samsonov55cdfc62012-01-17 06:35:31 +000041 summary->set_thread(thread);
42 thread->set_summary(summary);
43
44 return thread;
Kostya Serebryany1e172b42011-11-30 01:07:02 +000045}
46
Kostya Serebryanyf58f9982012-02-07 00:27:15 +000047void AsanThreadSummary::TSDDtor(void *tsd) {
48 AsanThreadSummary *summary = (AsanThreadSummary*)tsd;
Alexey Samsonovcb8c4dc2012-07-09 14:36:04 +000049 if (flags()->verbosity >= 1) {
Kostya Serebryanyf58f9982012-02-07 00:27:15 +000050 Report("T%d TSDDtor\n", summary->tid());
51 }
52 if (summary->thread()) {
53 summary->thread()->Destroy();
54 }
55}
56
Kostya Serebryanya6b52262012-01-06 19:44:11 +000057void AsanThread::Destroy() {
Alexey Samsonovcb8c4dc2012-07-09 14:36:04 +000058 if (flags()->verbosity >= 1) {
Kostya Serebryanyf58f9982012-02-07 00:27:15 +000059 Report("T%d exited\n", tid());
60 }
61
62 asanThreadRegistry().UnregisterThread(this);
Kostya Serebryany3f4c3872012-05-31 14:35:53 +000063 CHECK(summary()->thread() == 0);
Kostya Serebryany1e172b42011-11-30 01:07:02 +000064 // We also clear the shadow on thread destruction because
65 // some code may still be executing in later TSD destructors
66 // and we don't want it to have any poisoned stack.
67 ClearShadowForThreadStack();
Alexey Samsonov55cdfc62012-01-17 06:35:31 +000068 fake_stack().Cleanup();
Kostya Serebryany3f4c3872012-05-31 14:35:53 +000069 uptr size = RoundUpTo(sizeof(AsanThread), kPageSize);
Alexey Samsonova25b3462012-06-06 16:15:07 +000070 UnmapOrDie(this, size);
Kostya Serebryany1e172b42011-11-30 01:07:02 +000071}
72
Kostya Serebryany69eca732011-12-16 19:13:35 +000073void AsanThread::Init() {
Kostya Serebryany1e172b42011-11-30 01:07:02 +000074 SetThreadStackTopAndBottom();
Alexey Samsonov55cdfc62012-01-17 06:35:31 +000075 CHECK(AddrIsInMem(stack_bottom_));
76 CHECK(AddrIsInMem(stack_top_));
77 ClearShadowForThreadStack();
Alexey Samsonovcb8c4dc2012-07-09 14:36:04 +000078 if (flags()->verbosity >= 1) {
Kostya Serebryany1e172b42011-11-30 01:07:02 +000079 int local = 0;
Evgeniy Stepanov739eb792012-03-21 11:32:46 +000080 Report("T%d: stack [%p,%p) size 0x%zx; local=%p\n",
Alexey Samsonov5bcca4e2012-06-06 10:46:00 +000081 tid(), (void*)stack_bottom_, (void*)stack_top_,
Kostya Serebryanya7e760a2012-01-09 19:18:27 +000082 stack_top_ - stack_bottom_, &local);
Kostya Serebryany1e172b42011-11-30 01:07:02 +000083 }
Alexey Samsonov55cdfc62012-01-17 06:35:31 +000084 fake_stack_.Init(stack_size());
Alexander Potapenko75b19eb2012-07-23 14:07:58 +000085 AsanPlatformThreadInit();
Kostya Serebryany69eca732011-12-16 19:13:35 +000086}
87
Timur Iskhodzhanov600972e2012-02-24 15:28:43 +000088thread_return_t AsanThread::ThreadStart() {
Kostya Serebryany69eca732011-12-16 19:13:35 +000089 Init();
Alexey Samsonovcb8c4dc2012-07-09 14:36:04 +000090 if (flags()->use_sigaltstack) SetAlternateSignalStack();
Kostya Serebryany1e172b42011-11-30 01:07:02 +000091
92 if (!start_routine_) {
Kostya Serebryany3f4c3872012-05-31 14:35:53 +000093 // start_routine_ == 0 if we're on the main thread or on one of the
Kostya Serebryany1e172b42011-11-30 01:07:02 +000094 // OS X libdispatch worker threads. But nobody is supposed to call
95 // ThreadStart() for the worker threads.
96 CHECK(tid() == 0);
97 return 0;
98 }
99
Timur Iskhodzhanov600972e2012-02-24 15:28:43 +0000100 thread_return_t res = start_routine_(arg_);
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000101 malloc_storage().CommitBack();
Alexey Samsonovcb8c4dc2012-07-09 14:36:04 +0000102 if (flags()->use_sigaltstack) UnsetAlternateSignalStack();
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000103
Kostya Serebryanyaf344152012-01-11 02:03:16 +0000104 this->Destroy();
105
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000106 return res;
107}
108
Alexey Samsonove5931fd2012-06-07 07:13:46 +0000109void AsanThread::SetThreadStackTopAndBottom() {
110 GetThreadStackTopAndBottom(tid() == 0, &stack_top_, &stack_bottom_);
111 int local;
112 CHECK(AddrIsInStack((uptr)&local));
113}
114
Alexey Samsonov55cdfc62012-01-17 06:35:31 +0000115void AsanThread::ClearShadowForThreadStack() {
116 PoisonShadow(stack_bottom_, stack_top_ - stack_bottom_, 0);
117}
118
Kostya Serebryany3f4c3872012-05-31 14:35:53 +0000119const char *AsanThread::GetFrameNameByAddr(uptr addr, uptr *offset) {
120 uptr bottom = 0;
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000121 if (AddrIsInStack(addr)) {
122 bottom = stack_bottom();
123 } else {
124 bottom = fake_stack().AddrIsInFakeStack(addr);
125 CHECK(bottom);
Alexander Potapenkoe406c8c2012-11-15 15:24:42 +0000126 *offset = addr - bottom;
127 return (const char *)((uptr*)bottom)[1];
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000128 }
Kostya Serebryany5af39e52012-11-21 12:38:58 +0000129 uptr aligned_addr = addr & ~(SANITIZER_WORDSIZE/8 - 1); // align addr.
Kostya Serebryanyee392552012-05-31 15:02:07 +0000130 u8 *shadow_ptr = (u8*)MemToShadow(aligned_addr);
131 u8 *shadow_bottom = (u8*)MemToShadow(bottom);
Evgeniy Stepanov3972ea02012-05-12 12:33:10 +0000132
133 while (shadow_ptr >= shadow_bottom &&
Alexander Potapenkoe406c8c2012-11-15 15:24:42 +0000134 *shadow_ptr != kAsanStackLeftRedzoneMagic) {
Evgeniy Stepanov3972ea02012-05-12 12:33:10 +0000135 shadow_ptr--;
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000136 }
Evgeniy Stepanov3972ea02012-05-12 12:33:10 +0000137
138 while (shadow_ptr >= shadow_bottom &&
Alexander Potapenkoe406c8c2012-11-15 15:24:42 +0000139 *shadow_ptr == kAsanStackLeftRedzoneMagic) {
Evgeniy Stepanov3972ea02012-05-12 12:33:10 +0000140 shadow_ptr--;
141 }
142
143 if (shadow_ptr < shadow_bottom) {
Alexander Potapenkoe406c8c2012-11-15 15:24:42 +0000144 *offset = 0;
145 return "UNKNOWN";
Evgeniy Stepanov3972ea02012-05-12 12:33:10 +0000146 }
147
Kostya Serebryany3f4c3872012-05-31 14:35:53 +0000148 uptr* ptr = (uptr*)SHADOW_TO_MEM((uptr)(shadow_ptr + 1));
Alexander Potapenkoe406c8c2012-11-15 15:24:42 +0000149 CHECK(ptr[0] == kCurrentStackFrameMagic);
Kostya Serebryany3f4c3872012-05-31 14:35:53 +0000150 *offset = addr - (uptr)ptr;
Evgeniy Stepanov3972ea02012-05-12 12:33:10 +0000151 return (const char*)ptr[1];
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000152}
153
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000154} // namespace __asan