blob: 78ce64c13b581a57024ef16d4c12261d57fa9133 [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 Serebryany1e172b42011-11-30 01:07:02 +000018#include "asan_mapping.h"
19
Kostya Serebryany1e172b42011-11-30 01:07:02 +000020#include <pthread.h>
21#include <stdlib.h>
22#include <string.h>
23
24namespace __asan {
25
26AsanThread::AsanThread(LinkerInitialized x)
27 : fake_stack_(x),
28 malloc_storage_(x),
29 stats_(x) { }
30
Kostya Serebryanya6b52262012-01-06 19:44:11 +000031AsanThread *AsanThread::Create(int parent_tid, void *(*start_routine) (void *),
32 void *arg) {
33 size_t size = RoundUpTo(sizeof(AsanThread), kPageSize);
34 AsanThread *res = (AsanThread*)AsanMmapSomewhereOrDie(size, __FUNCTION__);
35 res->start_routine_ = start_routine;
36 res->arg_ = arg;
37 return res;
Kostya Serebryany1e172b42011-11-30 01:07:02 +000038}
39
Kostya Serebryanya6b52262012-01-06 19:44:11 +000040void AsanThread::Destroy() {
Kostya Serebryany1e172b42011-11-30 01:07:02 +000041 fake_stack().Cleanup();
42 // We also clear the shadow on thread destruction because
43 // some code may still be executing in later TSD destructors
44 // and we don't want it to have any poisoned stack.
45 ClearShadowForThreadStack();
Kostya Serebryanya6b52262012-01-06 19:44:11 +000046 size_t size = RoundUpTo(sizeof(AsanThread), kPageSize);
47 AsanUnmapOrDie(this, size);
Kostya Serebryany1e172b42011-11-30 01:07:02 +000048}
49
50void AsanThread::ClearShadowForThreadStack() {
51 uintptr_t shadow_bot = MemToShadow(stack_bottom_);
52 uintptr_t shadow_top = MemToShadow(stack_top_);
53 real_memset((void*)shadow_bot, 0, shadow_top - shadow_bot);
54}
55
Kostya Serebryany69eca732011-12-16 19:13:35 +000056void AsanThread::Init() {
Kostya Serebryany1e172b42011-11-30 01:07:02 +000057 SetThreadStackTopAndBottom();
58 fake_stack_.Init(stack_size());
59 if (FLAG_v >= 1) {
60 int local = 0;
61 Report("T%d: stack [%p,%p) size 0x%lx; local=%p, pthread_self=%p\n",
62 tid(), stack_bottom_, stack_top_,
63 stack_top_ - stack_bottom_, &local, pthread_self());
64 }
65
66 CHECK(AddrIsInMem(stack_bottom_));
67 CHECK(AddrIsInMem(stack_top_));
68
69 ClearShadowForThreadStack();
Kostya Serebryany69eca732011-12-16 19:13:35 +000070}
71
72void *AsanThread::ThreadStart() {
73 Init();
Kostya Serebryany1e172b42011-11-30 01:07:02 +000074
75 if (!start_routine_) {
76 // start_routine_ == NULL if we're on the main thread or on one of the
77 // OS X libdispatch worker threads. But nobody is supposed to call
78 // ThreadStart() for the worker threads.
79 CHECK(tid() == 0);
80 return 0;
81 }
82
83 void *res = start_routine_(arg_);
84 malloc_storage().CommitBack();
85
86 if (FLAG_v >= 1) {
87 Report("T%d exited\n", tid());
88 }
89
90 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