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" |
Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 17 | #include "asan_thread.h" |
Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 18 | #include "asan_mapping.h" |
| 19 | |
Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 20 | #include <pthread.h> |
| 21 | #include <stdlib.h> |
| 22 | #include <string.h> |
| 23 | |
| 24 | namespace __asan { |
| 25 | |
| 26 | AsanThread::AsanThread(LinkerInitialized x) |
| 27 | : fake_stack_(x), |
| 28 | malloc_storage_(x), |
| 29 | stats_(x) { } |
| 30 | |
Kostya Serebryany | a6b5226 | 2012-01-06 19:44:11 +0000 | [diff] [blame^] | 31 | AsanThread *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 Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 38 | } |
| 39 | |
Kostya Serebryany | a6b5226 | 2012-01-06 19:44:11 +0000 | [diff] [blame^] | 40 | void AsanThread::Destroy() { |
Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 41 | 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 Serebryany | a6b5226 | 2012-01-06 19:44:11 +0000 | [diff] [blame^] | 46 | size_t size = RoundUpTo(sizeof(AsanThread), kPageSize); |
| 47 | AsanUnmapOrDie(this, size); |
Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 48 | } |
| 49 | |
| 50 | void 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 Serebryany | 69eca73 | 2011-12-16 19:13:35 +0000 | [diff] [blame] | 56 | void AsanThread::Init() { |
Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 57 | 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 Serebryany | 69eca73 | 2011-12-16 19:13:35 +0000 | [diff] [blame] | 70 | } |
| 71 | |
| 72 | void *AsanThread::ThreadStart() { |
| 73 | Init(); |
Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 74 | |
| 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 | |
| 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 | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 117 | } // namespace __asan |