blob: c61b2b4bd958565d4ca414ba85ca4aa330cc1d76 [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"
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"
20
Kostya Serebryany1e172b42011-11-30 01:07:02 +000021namespace __asan {
22
23AsanThread::AsanThread(LinkerInitialized x)
24 : fake_stack_(x),
25 malloc_storage_(x),
26 stats_(x) { }
27
Kostya Serebryanya6b52262012-01-06 19:44:11 +000028AsanThread *AsanThread::Create(int parent_tid, void *(*start_routine) (void *),
29 void *arg) {
30 size_t size = RoundUpTo(sizeof(AsanThread), kPageSize);
31 AsanThread *res = (AsanThread*)AsanMmapSomewhereOrDie(size, __FUNCTION__);
32 res->start_routine_ = start_routine;
33 res->arg_ = arg;
34 return res;
Kostya Serebryany1e172b42011-11-30 01:07:02 +000035}
36
Kostya Serebryanya6b52262012-01-06 19:44:11 +000037void AsanThread::Destroy() {
Kostya Serebryany1e172b42011-11-30 01:07:02 +000038 fake_stack().Cleanup();
39 // We also clear the shadow on thread destruction because
40 // some code may still be executing in later TSD destructors
41 // and we don't want it to have any poisoned stack.
42 ClearShadowForThreadStack();
Kostya Serebryanya6b52262012-01-06 19:44:11 +000043 size_t size = RoundUpTo(sizeof(AsanThread), kPageSize);
44 AsanUnmapOrDie(this, size);
Kostya Serebryany1e172b42011-11-30 01:07:02 +000045}
46
47void AsanThread::ClearShadowForThreadStack() {
48 uintptr_t shadow_bot = MemToShadow(stack_bottom_);
49 uintptr_t shadow_top = MemToShadow(stack_top_);
50 real_memset((void*)shadow_bot, 0, shadow_top - shadow_bot);
51}
52
Kostya Serebryany69eca732011-12-16 19:13:35 +000053void AsanThread::Init() {
Kostya Serebryany1e172b42011-11-30 01:07:02 +000054 SetThreadStackTopAndBottom();
55 fake_stack_.Init(stack_size());
56 if (FLAG_v >= 1) {
57 int local = 0;
Kostya Serebryanya7e760a2012-01-09 19:18:27 +000058 Report("T%d: stack [%p,%p) size 0x%lx; local=%p\n",
Kostya Serebryany1e172b42011-11-30 01:07:02 +000059 tid(), stack_bottom_, stack_top_,
Kostya Serebryanya7e760a2012-01-09 19:18:27 +000060 stack_top_ - stack_bottom_, &local);
Kostya Serebryany1e172b42011-11-30 01:07:02 +000061 }
62
63 CHECK(AddrIsInMem(stack_bottom_));
64 CHECK(AddrIsInMem(stack_top_));
65
66 ClearShadowForThreadStack();
Kostya Serebryany69eca732011-12-16 19:13:35 +000067}
68
69void *AsanThread::ThreadStart() {
70 Init();
Kostya Serebryany1e172b42011-11-30 01:07:02 +000071
72 if (!start_routine_) {
73 // start_routine_ == NULL if we're on the main thread or on one of the
74 // OS X libdispatch worker threads. But nobody is supposed to call
75 // ThreadStart() for the worker threads.
76 CHECK(tid() == 0);
77 return 0;
78 }
79
80 void *res = start_routine_(arg_);
81 malloc_storage().CommitBack();
82
83 if (FLAG_v >= 1) {
84 Report("T%d exited\n", tid());
85 }
86
Kostya Serebryanyaf344152012-01-11 02:03:16 +000087 asanThreadRegistry().UnregisterThread(this);
88 this->Destroy();
89
Kostya Serebryany1e172b42011-11-30 01:07:02 +000090 return res;
91}
92
93const char *AsanThread::GetFrameNameByAddr(uintptr_t addr, uintptr_t *offset) {
94 uintptr_t bottom = 0;
95 bool is_fake_stack = false;
96 if (AddrIsInStack(addr)) {
97 bottom = stack_bottom();
98 } else {
99 bottom = fake_stack().AddrIsInFakeStack(addr);
100 CHECK(bottom);
101 is_fake_stack = true;
102 }
103 uintptr_t aligned_addr = addr & ~(__WORDSIZE/8 - 1); // align addr.
104 uintptr_t *ptr = (uintptr_t*)aligned_addr;
105 while (ptr >= (uintptr_t*)bottom) {
106 if (ptr[0] == kCurrentStackFrameMagic ||
107 (is_fake_stack && ptr[0] == kRetiredStackFrameMagic)) {
108 *offset = addr - (uintptr_t)ptr;
109 return (const char*)ptr[1];
110 }
111 ptr--;
112 }
113 *offset = 0;
114 return "UNKNOWN";
115}
116
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000117} // namespace __asan