blob: 66bb01e23a6ee9b2ff3e0b4af5ae8c9e6d63845d [file] [log] [blame]
Kostya Serebryany1e172b42011-11-30 01:07:02 +00001//===-- asan_thread.cc ------------------------------------------*- C++ -*-===//
2//
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"
Kostya Serebryanydf499b42012-01-05 00:44:33 +000016#include "asan_procmaps.h"
Alexey Samsonov55cdfc62012-01-17 06:35:31 +000017#include "asan_stack.h"
Kostya Serebryany1e172b42011-11-30 01:07:02 +000018#include "asan_thread.h"
Kostya Serebryanyaf344152012-01-11 02:03:16 +000019#include "asan_thread_registry.h"
Kostya Serebryany1e172b42011-11-30 01:07:02 +000020#include "asan_mapping.h"
21
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 Serebryanya6b52262012-01-06 19:44:11 +000029AsanThread *AsanThread::Create(int parent_tid, void *(*start_routine) (void *),
Alexey Samsonov55cdfc62012-01-17 06:35:31 +000030 void *arg, AsanStackTrace *stack) {
Kostya Serebryanya6b52262012-01-06 19:44:11 +000031 size_t size = RoundUpTo(sizeof(AsanThread), kPageSize);
Alexey Samsonov55cdfc62012-01-17 06:35:31 +000032 AsanThread *thread = (AsanThread*)AsanMmapSomewhereOrDie(size, __FUNCTION__);
33 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 Serebryanya6b52262012-01-06 19:44:11 +000043void AsanThread::Destroy() {
Kostya Serebryany1e172b42011-11-30 01:07:02 +000044 // We also clear the shadow on thread destruction because
45 // some code may still be executing in later TSD destructors
46 // and we don't want it to have any poisoned stack.
47 ClearShadowForThreadStack();
Alexey Samsonov55cdfc62012-01-17 06:35:31 +000048 fake_stack().Cleanup();
Kostya Serebryanya6b52262012-01-06 19:44:11 +000049 size_t size = RoundUpTo(sizeof(AsanThread), kPageSize);
50 AsanUnmapOrDie(this, size);
Kostya Serebryany1e172b42011-11-30 01:07:02 +000051}
52
Kostya Serebryany69eca732011-12-16 19:13:35 +000053void AsanThread::Init() {
Kostya Serebryany1e172b42011-11-30 01:07:02 +000054 SetThreadStackTopAndBottom();
Alexey Samsonov55cdfc62012-01-17 06:35:31 +000055 CHECK(AddrIsInMem(stack_bottom_));
56 CHECK(AddrIsInMem(stack_top_));
57 ClearShadowForThreadStack();
Kostya Serebryany1e172b42011-11-30 01:07:02 +000058 if (FLAG_v >= 1) {
59 int local = 0;
Kostya Serebryanya7e760a2012-01-09 19:18:27 +000060 Report("T%d: stack [%p,%p) size 0x%lx; local=%p\n",
Kostya Serebryany1e172b42011-11-30 01:07:02 +000061 tid(), stack_bottom_, stack_top_,
Kostya Serebryanya7e760a2012-01-09 19:18:27 +000062 stack_top_ - stack_bottom_, &local);
Kostya Serebryany1e172b42011-11-30 01:07:02 +000063 }
Alexey Samsonov55cdfc62012-01-17 06:35:31 +000064 fake_stack_.Init(stack_size());
Kostya Serebryany69eca732011-12-16 19:13:35 +000065}
66
67void *AsanThread::ThreadStart() {
68 Init();
Kostya Serebryany1e172b42011-11-30 01:07:02 +000069
70 if (!start_routine_) {
71 // start_routine_ == NULL if we're on the main thread or on one of the
72 // OS X libdispatch worker threads. But nobody is supposed to call
73 // ThreadStart() for the worker threads.
74 CHECK(tid() == 0);
75 return 0;
76 }
77
78 void *res = start_routine_(arg_);
79 malloc_storage().CommitBack();
80
81 if (FLAG_v >= 1) {
82 Report("T%d exited\n", tid());
83 }
84
Kostya Serebryanyaf344152012-01-11 02:03:16 +000085 asanThreadRegistry().UnregisterThread(this);
86 this->Destroy();
87
Kostya Serebryany1e172b42011-11-30 01:07:02 +000088 return res;
89}
90
Alexey Samsonov55cdfc62012-01-17 06:35:31 +000091void AsanThread::ClearShadowForThreadStack() {
92 PoisonShadow(stack_bottom_, stack_top_ - stack_bottom_, 0);
93}
94
Kostya Serebryany1e172b42011-11-30 01:07:02 +000095const char *AsanThread::GetFrameNameByAddr(uintptr_t addr, uintptr_t *offset) {
96 uintptr_t bottom = 0;
97 bool is_fake_stack = false;
98 if (AddrIsInStack(addr)) {
99 bottom = stack_bottom();
100 } else {
101 bottom = fake_stack().AddrIsInFakeStack(addr);
102 CHECK(bottom);
103 is_fake_stack = true;
104 }
105 uintptr_t aligned_addr = addr & ~(__WORDSIZE/8 - 1); // align addr.
106 uintptr_t *ptr = (uintptr_t*)aligned_addr;
107 while (ptr >= (uintptr_t*)bottom) {
108 if (ptr[0] == kCurrentStackFrameMagic ||
109 (is_fake_stack && ptr[0] == kRetiredStackFrameMagic)) {
110 *offset = addr - (uintptr_t)ptr;
111 return (const char*)ptr[1];
112 }
113 ptr--;
114 }
115 *offset = 0;
116 return "UNKNOWN";
117}
118
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000119} // namespace __asan