Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 1 | //===-- 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 Serebryany | df499b4 | 2012-01-05 00:44:33 +0000 | [diff] [blame] | 16 | #include "asan_procmaps.h" |
Alexey Samsonov | 55cdfc6 | 2012-01-17 06:35:31 +0000 | [diff] [blame^] | 17 | #include "asan_stack.h" |
Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 18 | #include "asan_thread.h" |
Kostya Serebryany | af34415 | 2012-01-11 02:03:16 +0000 | [diff] [blame] | 19 | #include "asan_thread_registry.h" |
Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 20 | #include "asan_mapping.h" |
| 21 | |
Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 22 | namespace __asan { |
| 23 | |
| 24 | AsanThread::AsanThread(LinkerInitialized x) |
| 25 | : fake_stack_(x), |
| 26 | malloc_storage_(x), |
| 27 | stats_(x) { } |
| 28 | |
Kostya Serebryany | a6b5226 | 2012-01-06 19:44:11 +0000 | [diff] [blame] | 29 | AsanThread *AsanThread::Create(int parent_tid, void *(*start_routine) (void *), |
Alexey Samsonov | 55cdfc6 | 2012-01-17 06:35:31 +0000 | [diff] [blame^] | 30 | void *arg, AsanStackTrace *stack) { |
Kostya Serebryany | a6b5226 | 2012-01-06 19:44:11 +0000 | [diff] [blame] | 31 | size_t size = RoundUpTo(sizeof(AsanThread), kPageSize); |
Alexey Samsonov | 55cdfc6 | 2012-01-17 06:35:31 +0000 | [diff] [blame^] | 32 | 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 Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 41 | } |
| 42 | |
Kostya Serebryany | a6b5226 | 2012-01-06 19:44:11 +0000 | [diff] [blame] | 43 | void AsanThread::Destroy() { |
Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 44 | // 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 Samsonov | 55cdfc6 | 2012-01-17 06:35:31 +0000 | [diff] [blame^] | 48 | fake_stack().Cleanup(); |
Kostya Serebryany | a6b5226 | 2012-01-06 19:44:11 +0000 | [diff] [blame] | 49 | size_t size = RoundUpTo(sizeof(AsanThread), kPageSize); |
| 50 | AsanUnmapOrDie(this, size); |
Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 51 | } |
| 52 | |
Kostya Serebryany | 69eca73 | 2011-12-16 19:13:35 +0000 | [diff] [blame] | 53 | void AsanThread::Init() { |
Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 54 | SetThreadStackTopAndBottom(); |
Alexey Samsonov | 55cdfc6 | 2012-01-17 06:35:31 +0000 | [diff] [blame^] | 55 | CHECK(AddrIsInMem(stack_bottom_)); |
| 56 | CHECK(AddrIsInMem(stack_top_)); |
| 57 | ClearShadowForThreadStack(); |
Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 58 | if (FLAG_v >= 1) { |
| 59 | int local = 0; |
Kostya Serebryany | a7e760a | 2012-01-09 19:18:27 +0000 | [diff] [blame] | 60 | Report("T%d: stack [%p,%p) size 0x%lx; local=%p\n", |
Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 61 | tid(), stack_bottom_, stack_top_, |
Kostya Serebryany | a7e760a | 2012-01-09 19:18:27 +0000 | [diff] [blame] | 62 | stack_top_ - stack_bottom_, &local); |
Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 63 | } |
Alexey Samsonov | 55cdfc6 | 2012-01-17 06:35:31 +0000 | [diff] [blame^] | 64 | fake_stack_.Init(stack_size()); |
Kostya Serebryany | 69eca73 | 2011-12-16 19:13:35 +0000 | [diff] [blame] | 65 | } |
| 66 | |
| 67 | void *AsanThread::ThreadStart() { |
| 68 | Init(); |
Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 69 | |
| 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 Serebryany | af34415 | 2012-01-11 02:03:16 +0000 | [diff] [blame] | 85 | asanThreadRegistry().UnregisterThread(this); |
| 86 | this->Destroy(); |
| 87 | |
Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 88 | return res; |
| 89 | } |
| 90 | |
Alexey Samsonov | 55cdfc6 | 2012-01-17 06:35:31 +0000 | [diff] [blame^] | 91 | void AsanThread::ClearShadowForThreadStack() { |
| 92 | PoisonShadow(stack_bottom_, stack_top_ - stack_bottom_, 0); |
| 93 | } |
| 94 | |
Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 95 | const 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 Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 119 | } // namespace __asan |