Kostya Serebryany | 019b76f | 2011-11-30 01:07:02 | [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 | cd271f5 | 2012-01-05 00:44:33 | [diff] [blame] | 16 | #include "asan_procmaps.h" |
Kostya Serebryany | 019b76f | 2011-11-30 01:07:02 | [diff] [blame] | 17 | #include "asan_thread.h" |
Kostya Serebryany | 332923b | 2012-01-11 02:03:16 | [diff] [blame] | 18 | #include "asan_thread_registry.h" |
Kostya Serebryany | 019b76f | 2011-11-30 01:07:02 | [diff] [blame] | 19 | #include "asan_mapping.h" |
| 20 | |
Kostya Serebryany | 019b76f | 2011-11-30 01:07:02 | [diff] [blame] | 21 | namespace __asan { |
| 22 | |
| 23 | AsanThread::AsanThread(LinkerInitialized x) |
| 24 | : fake_stack_(x), |
| 25 | malloc_storage_(x), |
| 26 | stats_(x) { } |
| 27 | |
Kostya Serebryany | 3f4b9bb | 2012-01-06 19:44:11 | [diff] [blame] | 28 | AsanThread *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 Serebryany | 019b76f | 2011-11-30 01:07:02 | [diff] [blame] | 35 | } |
| 36 | |
Kostya Serebryany | 3f4b9bb | 2012-01-06 19:44:11 | [diff] [blame] | 37 | void AsanThread::Destroy() { |
Kostya Serebryany | 019b76f | 2011-11-30 01:07:02 | [diff] [blame] | 38 | 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 Serebryany | 3f4b9bb | 2012-01-06 19:44:11 | [diff] [blame] | 43 | size_t size = RoundUpTo(sizeof(AsanThread), kPageSize); |
| 44 | AsanUnmapOrDie(this, size); |
Kostya Serebryany | 019b76f | 2011-11-30 01:07:02 | [diff] [blame] | 45 | } |
| 46 | |
| 47 | void 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 Serebryany | 6bb2f1d | 2011-12-16 19:13:35 | [diff] [blame] | 53 | void AsanThread::Init() { |
Kostya Serebryany | 019b76f | 2011-11-30 01:07:02 | [diff] [blame] | 54 | SetThreadStackTopAndBottom(); |
| 55 | fake_stack_.Init(stack_size()); |
| 56 | if (FLAG_v >= 1) { |
| 57 | int local = 0; |
Kostya Serebryany | 5be458c | 2012-01-09 19:18:27 | [diff] [blame] | 58 | Report("T%d: stack [%p,%p) size 0x%lx; local=%p\n", |
Kostya Serebryany | 019b76f | 2011-11-30 01:07:02 | [diff] [blame] | 59 | tid(), stack_bottom_, stack_top_, |
Kostya Serebryany | 5be458c | 2012-01-09 19:18:27 | [diff] [blame] | 60 | stack_top_ - stack_bottom_, &local); |
Kostya Serebryany | 019b76f | 2011-11-30 01:07:02 | [diff] [blame] | 61 | } |
| 62 | |
| 63 | CHECK(AddrIsInMem(stack_bottom_)); |
| 64 | CHECK(AddrIsInMem(stack_top_)); |
| 65 | |
| 66 | ClearShadowForThreadStack(); |
Kostya Serebryany | 6bb2f1d | 2011-12-16 19:13:35 | [diff] [blame] | 67 | } |
| 68 | |
| 69 | void *AsanThread::ThreadStart() { |
| 70 | Init(); |
Kostya Serebryany | 019b76f | 2011-11-30 01:07:02 | [diff] [blame] | 71 | |
| 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 Serebryany | 332923b | 2012-01-11 02:03:16 | [diff] [blame] | 87 | asanThreadRegistry().UnregisterThread(this); |
| 88 | this->Destroy(); |
| 89 | |
Kostya Serebryany | 019b76f | 2011-11-30 01:07:02 | [diff] [blame] | 90 | return res; |
| 91 | } |
| 92 | |
| 93 | const 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 Serebryany | 019b76f | 2011-11-30 01:07:02 | [diff] [blame] | 117 | } // namespace __asan |