| Alexey Samsonov | e5f5895 | 2012-06-04 13:50:10 +0000 | [diff] [blame] | 1 | //===-- asan_rtl.cc -------------------------------------------------------===// |
| Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 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 | // Main file of the ASan run-time library. |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | #include "asan_allocator.h" |
| 15 | #include "asan_interceptors.h" |
| 16 | #include "asan_interface.h" |
| 17 | #include "asan_internal.h" |
| 18 | #include "asan_lock.h" |
| Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 19 | #include "asan_mapping.h" |
| 20 | #include "asan_stack.h" |
| 21 | #include "asan_stats.h" |
| 22 | #include "asan_thread.h" |
| 23 | #include "asan_thread_registry.h" |
| Dmitry Vyukov | fce5bd4 | 2012-06-29 16:58:33 +0000 | [diff] [blame] | 24 | #include "sanitizer_common/sanitizer_atomic.h" |
| Alexey Samsonov | cb8c4dc | 2012-07-09 14:36:04 +0000 | [diff] [blame^] | 25 | #include "sanitizer_common/sanitizer_flags.h" |
| Alexey Samsonov | 9552db7 | 2012-06-05 07:25:47 +0000 | [diff] [blame] | 26 | #include "sanitizer_common/sanitizer_libc.h" |
| Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 27 | |
| Alexey Samsonov | 47657ce | 2012-06-06 07:02:44 +0000 | [diff] [blame] | 28 | namespace __sanitizer { |
| 29 | using namespace __asan; |
| 30 | |
| 31 | void Die() { |
| Dmitry Vyukov | fce5bd4 | 2012-06-29 16:58:33 +0000 | [diff] [blame] | 32 | static atomic_uint32_t num_calls; |
| 33 | if (atomic_fetch_add(&num_calls, 1, memory_order_relaxed) != 0) { |
| Alexey Samsonov | 47657ce | 2012-06-06 07:02:44 +0000 | [diff] [blame] | 34 | // Don't die twice - run a busy loop. |
| 35 | while (1) { } |
| 36 | } |
| Alexey Samsonov | cb8c4dc | 2012-07-09 14:36:04 +0000 | [diff] [blame^] | 37 | if (flags()->sleep_before_dying) { |
| 38 | Report("Sleeping for %zd second(s)\n", flags()->sleep_before_dying); |
| 39 | SleepForSeconds(flags()->sleep_before_dying); |
| Alexey Samsonov | 47657ce | 2012-06-06 07:02:44 +0000 | [diff] [blame] | 40 | } |
| Alexey Samsonov | cb8c4dc | 2012-07-09 14:36:04 +0000 | [diff] [blame^] | 41 | if (flags()->unmap_shadow_on_exit) |
| Alexey Samsonov | a25b346 | 2012-06-06 16:15:07 +0000 | [diff] [blame] | 42 | UnmapOrDie((void*)kLowShadowBeg, kHighShadowEnd - kLowShadowBeg); |
| Alexey Samsonov | 47657ce | 2012-06-06 07:02:44 +0000 | [diff] [blame] | 43 | if (death_callback) |
| 44 | death_callback(); |
| Alexey Samsonov | cb8c4dc | 2012-07-09 14:36:04 +0000 | [diff] [blame^] | 45 | if (flags()->abort_on_error) |
| Alexey Samsonov | 47657ce | 2012-06-06 07:02:44 +0000 | [diff] [blame] | 46 | Abort(); |
| Alexey Samsonov | cb8c4dc | 2012-07-09 14:36:04 +0000 | [diff] [blame^] | 47 | Exit(flags()->exitcode); |
| Alexey Samsonov | 47657ce | 2012-06-06 07:02:44 +0000 | [diff] [blame] | 48 | } |
| 49 | |
| Alexey Samsonov | 15a7761 | 2012-06-06 15:22:20 +0000 | [diff] [blame] | 50 | void CheckFailed(const char *file, int line, const char *cond, u64 v1, u64 v2) { |
| 51 | AsanReport("AddressSanitizer CHECK failed: %s:%d \"%s\" (%zx, %zx)\n", |
| 52 | file, line, cond, (uptr)v1, (uptr)v2); |
| 53 | PRINT_CURRENT_STACK(); |
| 54 | ShowStatsAndAbort(); |
| 55 | } |
| 56 | |
| Alexey Samsonov | 47657ce | 2012-06-06 07:02:44 +0000 | [diff] [blame] | 57 | } // namespace __sanitizer |
| 58 | |
| Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 59 | namespace __asan { |
| 60 | |
| 61 | // -------------------------- Flags ------------------------- {{{1 |
| Kostya Serebryany | 3f4c387 | 2012-05-31 14:35:53 +0000 | [diff] [blame] | 62 | static const uptr kMallocContextSize = 30; |
| Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 63 | |
| Alexey Samsonov | cb8c4dc | 2012-07-09 14:36:04 +0000 | [diff] [blame^] | 64 | static Flags asan_flags; |
| 65 | |
| 66 | Flags *flags() { |
| 67 | return &asan_flags; |
| 68 | } |
| 69 | |
| 70 | // Can be overriden in frontend. |
| 71 | void WEAK OverrideFlags(Flags *f) { |
| 72 | (void)f; |
| 73 | } |
| 74 | |
| 75 | static void ParseFlagsFromString(Flags *f, const char *str) { |
| 76 | ParseFlag(str, &f->quarantine_size, "quarantine_size"); |
| 77 | ParseFlag(str, &f->symbolize, "symbolize"); |
| 78 | ParseFlag(str, &f->verbosity, "verbosity"); |
| 79 | ParseFlag(str, &f->redzone, "redzone"); |
| 80 | CHECK(f->redzone >= 16); |
| 81 | CHECK(IsPowerOfTwo(f->redzone)); |
| 82 | |
| 83 | ParseFlag(str, &f->debug, "debug"); |
| 84 | ParseFlag(str, &f->poison_shadow, "poison_shadow"); |
| 85 | ParseFlag(str, &f->report_globals, "report_globals"); |
| 86 | ParseFlag(str, &f->malloc_context_size, "malloc_context_size"); |
| 87 | CHECK(f->malloc_context_size <= kMallocContextSize); |
| 88 | |
| 89 | ParseFlag(str, &f->replace_str, "replace_str"); |
| 90 | ParseFlag(str, &f->replace_intrin, "replace_intrin"); |
| 91 | ParseFlag(str, &f->replace_cfallocator, "replace_cfallocator"); |
| 92 | ParseFlag(str, &f->mac_ignore_invalid_free, "mac_ignore_invalid_free"); |
| 93 | ParseFlag(str, &f->use_fake_stack, "use_fake_stack"); |
| 94 | ParseFlag(str, &f->max_malloc_fill_size, "max_malloc_fill_size"); |
| 95 | ParseFlag(str, &f->exitcode, "exitcode"); |
| 96 | ParseFlag(str, &f->allow_user_poisoning, "allow_user_poisoning"); |
| 97 | ParseFlag(str, &f->sleep_before_dying, "sleep_before_dying"); |
| 98 | ParseFlag(str, &f->handle_segv, "handle_segv"); |
| 99 | ParseFlag(str, &f->use_sigaltstack, "use_sigaltstack"); |
| 100 | // Allow the users to work around the bug in Nvidia drivers prior to 295.*. |
| 101 | ParseFlag(str, &f->check_malloc_usable_size, "check_malloc_usable_size"); |
| 102 | ParseFlag(str, &f->unmap_shadow_on_exit, "unmap_shadow_on_exit"); |
| 103 | ParseFlag(str, &f->abort_on_error, "abort_on_error"); |
| 104 | ParseFlag(str, &f->atexit, "atexit"); |
| 105 | // By default, disable core dumper on 64-bit -- |
| 106 | // it makes little sense to dump 16T+ core. |
| 107 | ParseFlag(str, &f->disable_core, "disable_core"); |
| 108 | } |
| 109 | |
| 110 | void InitializeFlags(Flags *f, const char *env) { |
| 111 | internal_memset(f, 0, sizeof(*f)); |
| 112 | |
| 113 | f->quarantine_size = (ASAN_LOW_MEMORY) ? 1UL << 24 : 1UL << 28; |
| 114 | f->symbolize = false; |
| 115 | f->verbosity = 0; |
| 116 | f->redzone = (ASAN_LOW_MEMORY) ? 64 : 128; // power of two, >= 32. |
| 117 | f->debug = 0; |
| 118 | f->poison_shadow = true; |
| 119 | f->report_globals = 1; |
| 120 | f->malloc_context_size = kMallocContextSize; |
| 121 | f->replace_str = true; |
| 122 | f->replace_intrin = true; |
| 123 | f->replace_cfallocator = true; // Used on Mac only. |
| 124 | f->mac_ignore_invalid_free = false; // Used on Mac only. |
| 125 | f->use_fake_stack = true; |
| 126 | f->max_malloc_fill_size = 0; |
| 127 | f->exitcode = ASAN_DEFAULT_FAILURE_EXITCODE; |
| 128 | f->allow_user_poisoning = true; |
| 129 | f->sleep_before_dying = 0; |
| 130 | f->handle_segv = ASAN_NEEDS_SEGV; |
| 131 | f->use_sigaltstack = false; |
| 132 | f->check_malloc_usable_size = true; |
| 133 | f->unmap_shadow_on_exit = false; |
| 134 | f->abort_on_error = false; |
| 135 | f->atexit = false; |
| 136 | f->disable_core = (__WORDSIZE == 64); |
| 137 | |
| 138 | // Let a frontend override. |
| 139 | OverrideFlags(f); |
| 140 | |
| 141 | // Override from user-specified string. |
| 142 | #if !defined(_WIN32) |
| 143 | if (__asan_default_options) { |
| 144 | ParseFlagsFromString(f, __asan_default_options); |
| 145 | if (flags()->verbosity) { |
| 146 | Report("Using the defaults from __asan_default_options: %s\n", |
| 147 | __asan_default_options); |
| 148 | } |
| 149 | } |
| 150 | #endif |
| 151 | |
| 152 | // Override from command line. |
| 153 | ParseFlagsFromString(f, env); |
| 154 | } |
| Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 155 | |
| 156 | // -------------------------- Globals --------------------- {{{1 |
| 157 | int asan_inited; |
| 158 | bool asan_init_is_running; |
| Alexey Samsonov | 47657ce | 2012-06-06 07:02:44 +0000 | [diff] [blame] | 159 | void (*death_callback)(void); |
| Alexander Potapenko | 3fe9135 | 2012-02-27 14:06:48 +0000 | [diff] [blame] | 160 | static void (*error_report_callback)(const char*); |
| Kostya Serebryany | 3f4c387 | 2012-05-31 14:35:53 +0000 | [diff] [blame] | 161 | char *error_message_buffer = 0; |
| 162 | uptr error_message_buffer_pos = 0; |
| 163 | uptr error_message_buffer_size = 0; |
| Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 164 | |
| Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 165 | // -------------------------- Misc ---------------- {{{1 |
| 166 | void ShowStatsAndAbort() { |
| 167 | __asan_print_accumulated_stats(); |
| Alexey Samsonov | 47657ce | 2012-06-06 07:02:44 +0000 | [diff] [blame] | 168 | Die(); |
| Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 169 | } |
| 170 | |
| Kostya Serebryany | 3f4c387 | 2012-05-31 14:35:53 +0000 | [diff] [blame] | 171 | static void PrintBytes(const char *before, uptr *a) { |
| Kostya Serebryany | ee39255 | 2012-05-31 15:02:07 +0000 | [diff] [blame] | 172 | u8 *bytes = (u8*)a; |
| Kostya Serebryany | 3f4c387 | 2012-05-31 14:35:53 +0000 | [diff] [blame] | 173 | uptr byte_num = (__WORDSIZE) / 8; |
| Alexey Samsonov | e954101 | 2012-06-06 13:11:29 +0000 | [diff] [blame] | 174 | AsanPrintf("%s%p:", before, (void*)a); |
| Kostya Serebryany | 3f4c387 | 2012-05-31 14:35:53 +0000 | [diff] [blame] | 175 | for (uptr i = 0; i < byte_num; i++) { |
| Alexey Samsonov | e954101 | 2012-06-06 13:11:29 +0000 | [diff] [blame] | 176 | AsanPrintf(" %x%x", bytes[i] >> 4, bytes[i] & 15); |
| Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 177 | } |
| Alexey Samsonov | e954101 | 2012-06-06 13:11:29 +0000 | [diff] [blame] | 178 | AsanPrintf("\n"); |
| Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 179 | } |
| 180 | |
| Alexey Samsonov | e954101 | 2012-06-06 13:11:29 +0000 | [diff] [blame] | 181 | void AppendToErrorMessageBuffer(const char *buffer) { |
| 182 | if (error_message_buffer) { |
| Kostya Serebryany | 94c54f1 | 2012-06-23 16:30:48 +0000 | [diff] [blame] | 183 | uptr length = internal_strlen(buffer); |
| 184 | CHECK_GE(error_message_buffer_size, error_message_buffer_pos); |
| 185 | uptr remaining = error_message_buffer_size - error_message_buffer_pos; |
| Alexey Samsonov | e954101 | 2012-06-06 13:11:29 +0000 | [diff] [blame] | 186 | internal_strncpy(error_message_buffer + error_message_buffer_pos, |
| 187 | buffer, remaining); |
| 188 | error_message_buffer[error_message_buffer_size - 1] = '\0'; |
| 189 | // FIXME: reallocate the buffer instead of truncating the message. |
| 190 | error_message_buffer_pos += remaining > length ? length : remaining; |
| 191 | } |
| 192 | } |
| 193 | |
| Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 194 | // ---------------------- mmap -------------------- {{{1 |
| Kostya Serebryany | a874fe5 | 2011-12-28 23:28:54 +0000 | [diff] [blame] | 195 | // Reserve memory range [beg, end]. |
| Kostya Serebryany | 3f4c387 | 2012-05-31 14:35:53 +0000 | [diff] [blame] | 196 | static void ReserveShadowMemoryRange(uptr beg, uptr end) { |
| Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 197 | CHECK((beg % kPageSize) == 0); |
| 198 | CHECK(((end + 1) % kPageSize) == 0); |
| Kostya Serebryany | 3f4c387 | 2012-05-31 14:35:53 +0000 | [diff] [blame] | 199 | uptr size = end - beg + 1; |
| Alexey Samsonov | f607fc1 | 2012-06-14 14:42:58 +0000 | [diff] [blame] | 200 | void *res = MmapFixedNoReserve(beg, size); |
| Kostya Serebryany | a874fe5 | 2011-12-28 23:28:54 +0000 | [diff] [blame] | 201 | CHECK(res == (void*)beg && "ReserveShadowMemoryRange failed"); |
| Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 202 | } |
| 203 | |
| Kostya Serebryany | b89567c | 2011-12-02 21:02:20 +0000 | [diff] [blame] | 204 | // ---------------------- LowLevelAllocator ------------- {{{1 |
| Kostya Serebryany | 3f4c387 | 2012-05-31 14:35:53 +0000 | [diff] [blame] | 205 | void *LowLevelAllocator::Allocate(uptr size) { |
| Kostya Serebryany | b89567c | 2011-12-02 21:02:20 +0000 | [diff] [blame] | 206 | CHECK((size & (size - 1)) == 0 && "size must be a power of two"); |
| Kostya Serebryany | 94c54f1 | 2012-06-23 16:30:48 +0000 | [diff] [blame] | 207 | if (allocated_end_ - allocated_current_ < (sptr)size) { |
| Kostya Serebryany | 3f4c387 | 2012-05-31 14:35:53 +0000 | [diff] [blame] | 208 | uptr size_to_allocate = Max(size, kPageSize); |
| Kostya Serebryany | de496f4 | 2011-12-28 22:58:01 +0000 | [diff] [blame] | 209 | allocated_current_ = |
| Alexey Samsonov | a25b346 | 2012-06-06 16:15:07 +0000 | [diff] [blame] | 210 | (char*)MmapOrDie(size_to_allocate, __FUNCTION__); |
| Kostya Serebryany | b89567c | 2011-12-02 21:02:20 +0000 | [diff] [blame] | 211 | allocated_end_ = allocated_current_ + size_to_allocate; |
| Kostya Serebryany | 3f4c387 | 2012-05-31 14:35:53 +0000 | [diff] [blame] | 212 | PoisonShadow((uptr)allocated_current_, size_to_allocate, |
| Kostya Serebryany | 6b30e2c | 2011-12-15 17:41:30 +0000 | [diff] [blame] | 213 | kAsanInternalHeapMagic); |
| Kostya Serebryany | b89567c | 2011-12-02 21:02:20 +0000 | [diff] [blame] | 214 | } |
| Kostya Serebryany | 94c54f1 | 2012-06-23 16:30:48 +0000 | [diff] [blame] | 215 | CHECK(allocated_end_ - allocated_current_ >= (sptr)size); |
| Kostya Serebryany | b89567c | 2011-12-02 21:02:20 +0000 | [diff] [blame] | 216 | void *res = allocated_current_; |
| 217 | allocated_current_ += size; |
| 218 | return res; |
| 219 | } |
| 220 | |
| Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 221 | // ---------------------- DescribeAddress -------------------- {{{1 |
| Kostya Serebryany | 3f4c387 | 2012-05-31 14:35:53 +0000 | [diff] [blame] | 222 | static bool DescribeStackAddress(uptr addr, uptr access_size) { |
| Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 223 | AsanThread *t = asanThreadRegistry().FindThreadByStackAddress(addr); |
| 224 | if (!t) return false; |
| Kostya Serebryany | ee39255 | 2012-05-31 15:02:07 +0000 | [diff] [blame] | 225 | const sptr kBufSize = 4095; |
| Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 226 | char buf[kBufSize]; |
| Kostya Serebryany | 3f4c387 | 2012-05-31 14:35:53 +0000 | [diff] [blame] | 227 | uptr offset = 0; |
| Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 228 | const char *frame_descr = t->GetFrameNameByAddr(addr, &offset); |
| 229 | // This string is created by the compiler and has the following form: |
| 230 | // "FunctioName n alloc_1 alloc_2 ... alloc_n" |
| 231 | // where alloc_i looks like "offset size len ObjectName ". |
| 232 | CHECK(frame_descr); |
| 233 | // Report the function name and the offset. |
| Timur Iskhodzhanov | 5362717 | 2012-02-14 19:33:04 +0000 | [diff] [blame] | 234 | const char *name_end = internal_strchr(frame_descr, ' '); |
| Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 235 | CHECK(name_end); |
| 236 | buf[0] = 0; |
| Kostya Serebryany | a4ccf87 | 2012-01-09 22:20:49 +0000 | [diff] [blame] | 237 | internal_strncat(buf, frame_descr, |
| 238 | Min(kBufSize, |
| Kostya Serebryany | ee39255 | 2012-05-31 15:02:07 +0000 | [diff] [blame] | 239 | static_cast<sptr>(name_end - frame_descr))); |
| Alexey Samsonov | e954101 | 2012-06-06 13:11:29 +0000 | [diff] [blame] | 240 | AsanPrintf("Address %p is located at offset %zu " |
| 241 | "in frame <%s> of T%d's stack:\n", |
| 242 | (void*)addr, offset, buf, t->tid()); |
| Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 243 | // Report the number of stack objects. |
| 244 | char *p; |
| Kostya Serebryany | 3f4c387 | 2012-05-31 14:35:53 +0000 | [diff] [blame] | 245 | uptr n_objects = internal_simple_strtoll(name_end, &p, 10); |
| Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 246 | CHECK(n_objects > 0); |
| Alexey Samsonov | e954101 | 2012-06-06 13:11:29 +0000 | [diff] [blame] | 247 | AsanPrintf(" This frame has %zu object(s):\n", n_objects); |
| Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 248 | // Report all objects in this frame. |
| Kostya Serebryany | 3f4c387 | 2012-05-31 14:35:53 +0000 | [diff] [blame] | 249 | for (uptr i = 0; i < n_objects; i++) { |
| 250 | uptr beg, size; |
| Kostya Serebryany | ee39255 | 2012-05-31 15:02:07 +0000 | [diff] [blame] | 251 | sptr len; |
| Alexey Samsonov | 8898102 | 2012-02-17 16:15:09 +0000 | [diff] [blame] | 252 | beg = internal_simple_strtoll(p, &p, 10); |
| 253 | size = internal_simple_strtoll(p, &p, 10); |
| 254 | len = internal_simple_strtoll(p, &p, 10); |
| Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 255 | if (beg <= 0 || size <= 0 || len < 0 || *p != ' ') { |
| Alexey Samsonov | e954101 | 2012-06-06 13:11:29 +0000 | [diff] [blame] | 256 | AsanPrintf("AddressSanitizer can't parse the stack frame " |
| 257 | "descriptor: |%s|\n", frame_descr); |
| Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 258 | break; |
| 259 | } |
| 260 | p++; |
| 261 | buf[0] = 0; |
| Kostya Serebryany | a4ccf87 | 2012-01-09 22:20:49 +0000 | [diff] [blame] | 262 | internal_strncat(buf, p, Min(kBufSize, len)); |
| Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 263 | p += len; |
| Alexey Samsonov | e954101 | 2012-06-06 13:11:29 +0000 | [diff] [blame] | 264 | AsanPrintf(" [%zu, %zu) '%s'\n", beg, beg + size, buf); |
| Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 265 | } |
| Alexey Samsonov | e954101 | 2012-06-06 13:11:29 +0000 | [diff] [blame] | 266 | AsanPrintf("HINT: this may be a false positive if your program uses " |
| 267 | "some custom stack unwind mechanism\n" |
| 268 | " (longjmp and C++ exceptions *are* supported)\n"); |
| Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 269 | t->summary()->Announce(); |
| 270 | return true; |
| 271 | } |
| 272 | |
| Kostya Serebryany | 3f4c387 | 2012-05-31 14:35:53 +0000 | [diff] [blame] | 273 | static NOINLINE void DescribeAddress(uptr addr, uptr access_size) { |
| Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 274 | // Check if this is a global. |
| 275 | if (DescribeAddrIfGlobal(addr)) |
| 276 | return; |
| 277 | |
| 278 | if (DescribeStackAddress(addr, access_size)) |
| 279 | return; |
| 280 | |
| 281 | // finally, check if this is a heap. |
| 282 | DescribeHeapAddress(addr, access_size); |
| 283 | } |
| 284 | |
| 285 | // -------------------------- Run-time entry ------------------- {{{1 |
| Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 286 | // exported functions |
| Kostya Serebryany | 51e75c4 | 2011-12-28 00:59:39 +0000 | [diff] [blame] | 287 | #define ASAN_REPORT_ERROR(type, is_write, size) \ |
| Alexey Samsonov | 0a4c906 | 2012-06-05 13:50:57 +0000 | [diff] [blame] | 288 | extern "C" NOINLINE INTERFACE_ATTRIBUTE \ |
| Kostya Serebryany | 3f4c387 | 2012-05-31 14:35:53 +0000 | [diff] [blame] | 289 | void __asan_report_ ## type ## size(uptr addr); \ |
| 290 | void __asan_report_ ## type ## size(uptr addr) { \ |
| Kostya Serebryany | 9f311bb | 2012-03-15 01:36:00 +0000 | [diff] [blame] | 291 | GET_CALLER_PC_BP_SP; \ |
| Kostya Serebryany | 51e75c4 | 2011-12-28 00:59:39 +0000 | [diff] [blame] | 292 | __asan_report_error(pc, bp, sp, addr, is_write, size); \ |
| Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 293 | } |
| 294 | |
| 295 | ASAN_REPORT_ERROR(load, false, 1) |
| 296 | ASAN_REPORT_ERROR(load, false, 2) |
| 297 | ASAN_REPORT_ERROR(load, false, 4) |
| 298 | ASAN_REPORT_ERROR(load, false, 8) |
| 299 | ASAN_REPORT_ERROR(load, false, 16) |
| 300 | ASAN_REPORT_ERROR(store, true, 1) |
| 301 | ASAN_REPORT_ERROR(store, true, 2) |
| 302 | ASAN_REPORT_ERROR(store, true, 4) |
| 303 | ASAN_REPORT_ERROR(store, true, 8) |
| 304 | ASAN_REPORT_ERROR(store, true, 16) |
| 305 | |
| 306 | // Force the linker to keep the symbols for various ASan interface functions. |
| 307 | // We want to keep those in the executable in order to let the instrumented |
| 308 | // dynamic libraries access the symbol even if it is not used by the executable |
| 309 | // itself. This should help if the build system is removing dead code at link |
| 310 | // time. |
| Alexander Potapenko | 3fe9135 | 2012-02-27 14:06:48 +0000 | [diff] [blame] | 311 | static NOINLINE void force_interface_symbols() { |
| Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 312 | volatile int fake_condition = 0; // prevent dead condition elimination. |
| 313 | if (fake_condition) { |
| Kostya Serebryany | f0977db | 2012-03-14 22:48:09 +0000 | [diff] [blame] | 314 | __asan_report_load1(0); |
| 315 | __asan_report_load2(0); |
| 316 | __asan_report_load4(0); |
| 317 | __asan_report_load8(0); |
| 318 | __asan_report_load16(0); |
| 319 | __asan_report_store1(0); |
| 320 | __asan_report_store2(0); |
| 321 | __asan_report_store4(0); |
| 322 | __asan_report_store8(0); |
| 323 | __asan_report_store16(0); |
| Kostya Serebryany | 3f4c387 | 2012-05-31 14:35:53 +0000 | [diff] [blame] | 324 | __asan_register_global(0, 0, 0); |
| 325 | __asan_register_globals(0, 0); |
| 326 | __asan_unregister_globals(0, 0); |
| 327 | __asan_set_death_callback(0); |
| 328 | __asan_set_error_report_callback(0); |
| Alexander Potapenko | dadc45d | 2012-03-06 11:45:59 +0000 | [diff] [blame] | 329 | __asan_handle_no_return(); |
| Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 330 | } |
| 331 | } |
| 332 | |
| 333 | // -------------------------- Init ------------------- {{{1 |
| Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 334 | static void asan_atexit() { |
| Alexey Samsonov | e954101 | 2012-06-06 13:11:29 +0000 | [diff] [blame] | 335 | AsanPrintf("AddressSanitizer exit stats:\n"); |
| Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 336 | __asan_print_accumulated_stats(); |
| 337 | } |
| 338 | |
| Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 339 | } // namespace __asan |
| 340 | |
| Kostya Serebryany | 4803ab9 | 2012-01-09 18:53:15 +0000 | [diff] [blame] | 341 | // ---------------------- Interface ---------------- {{{1 |
| Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 342 | using namespace __asan; // NOLINT |
| 343 | |
| Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 344 | int __asan_set_error_exit_code(int exit_code) { |
| Alexey Samsonov | cb8c4dc | 2012-07-09 14:36:04 +0000 | [diff] [blame^] | 345 | int old = flags()->exitcode; |
| 346 | flags()->exitcode = exit_code; |
| Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 347 | return old; |
| 348 | } |
| 349 | |
| Alexander Potapenko | dadc45d | 2012-03-06 11:45:59 +0000 | [diff] [blame] | 350 | void NOINLINE __asan_handle_no_return() { |
| Kostya Serebryany | f54b1f9 | 2012-02-08 21:33:27 +0000 | [diff] [blame] | 351 | int local_stack; |
| 352 | AsanThread *curr_thread = asanThreadRegistry().GetCurrent(); |
| 353 | CHECK(curr_thread); |
| Kostya Serebryany | 3f4c387 | 2012-05-31 14:35:53 +0000 | [diff] [blame] | 354 | uptr top = curr_thread->stack_top(); |
| 355 | uptr bottom = ((uptr)&local_stack - kPageSize) & ~(kPageSize-1); |
| Kostya Serebryany | f54b1f9 | 2012-02-08 21:33:27 +0000 | [diff] [blame] | 356 | PoisonShadow(bottom, top - bottom, 0); |
| 357 | } |
| 358 | |
| Alexander Potapenko | 2f3f962 | 2012-03-01 14:39:21 +0000 | [diff] [blame] | 359 | void NOINLINE __asan_set_death_callback(void (*callback)(void)) { |
| Kostya Serebryany | e1fe0fd | 2012-02-13 21:24:29 +0000 | [diff] [blame] | 360 | death_callback = callback; |
| 361 | } |
| 362 | |
| Alexander Potapenko | 3fe9135 | 2012-02-27 14:06:48 +0000 | [diff] [blame] | 363 | void NOINLINE __asan_set_error_report_callback(void (*callback)(const char*)) { |
| 364 | error_report_callback = callback; |
| 365 | if (callback) { |
| Alexander Potapenko | 8ca0278 | 2012-05-12 12:33:41 +0000 | [diff] [blame] | 366 | error_message_buffer_size = 1 << 16; |
| Alexander Potapenko | 3fe9135 | 2012-02-27 14:06:48 +0000 | [diff] [blame] | 367 | error_message_buffer = |
| Alexey Samsonov | a25b346 | 2012-06-06 16:15:07 +0000 | [diff] [blame] | 368 | (char*)MmapOrDie(error_message_buffer_size, __FUNCTION__); |
| Alexander Potapenko | 3fe9135 | 2012-02-27 14:06:48 +0000 | [diff] [blame] | 369 | error_message_buffer_pos = 0; |
| 370 | } |
| 371 | } |
| 372 | |
| Kostya Serebryany | 9aead37 | 2012-05-31 14:11:07 +0000 | [diff] [blame] | 373 | void __asan_report_error(uptr pc, uptr bp, uptr sp, |
| 374 | uptr addr, bool is_write, uptr access_size) { |
| Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 375 | // Do not print more than one report, otherwise they will mix up. |
| Dmitry Vyukov | fce5bd4 | 2012-06-29 16:58:33 +0000 | [diff] [blame] | 376 | static atomic_uint32_t num_calls; |
| 377 | if (atomic_fetch_add(&num_calls, 1, memory_order_relaxed) != 0) return; |
| Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 378 | |
| Alexey Samsonov | e954101 | 2012-06-06 13:11:29 +0000 | [diff] [blame] | 379 | AsanPrintf("====================================================" |
| 380 | "=============\n"); |
| Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 381 | const char *bug_descr = "unknown-crash"; |
| 382 | if (AddrIsInMem(addr)) { |
| Kostya Serebryany | ee39255 | 2012-05-31 15:02:07 +0000 | [diff] [blame] | 383 | u8 *shadow_addr = (u8*)MemToShadow(addr); |
| Kostya Serebryany | acd5c61 | 2011-12-07 21:30:20 +0000 | [diff] [blame] | 384 | // If we are accessing 16 bytes, look at the second shadow byte. |
| 385 | if (*shadow_addr == 0 && access_size > SHADOW_GRANULARITY) |
| 386 | shadow_addr++; |
| 387 | // If we are in the partial right redzone, look at the next shadow byte. |
| 388 | if (*shadow_addr > 0 && *shadow_addr < 128) |
| 389 | shadow_addr++; |
| 390 | switch (*shadow_addr) { |
| Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 391 | case kAsanHeapLeftRedzoneMagic: |
| 392 | case kAsanHeapRightRedzoneMagic: |
| 393 | bug_descr = "heap-buffer-overflow"; |
| 394 | break; |
| 395 | case kAsanHeapFreeMagic: |
| 396 | bug_descr = "heap-use-after-free"; |
| 397 | break; |
| 398 | case kAsanStackLeftRedzoneMagic: |
| 399 | bug_descr = "stack-buffer-underflow"; |
| 400 | break; |
| 401 | case kAsanStackMidRedzoneMagic: |
| 402 | case kAsanStackRightRedzoneMagic: |
| 403 | case kAsanStackPartialRedzoneMagic: |
| 404 | bug_descr = "stack-buffer-overflow"; |
| 405 | break; |
| 406 | case kAsanStackAfterReturnMagic: |
| 407 | bug_descr = "stack-use-after-return"; |
| 408 | break; |
| 409 | case kAsanUserPoisonedMemoryMagic: |
| 410 | bug_descr = "use-after-poison"; |
| 411 | break; |
| 412 | case kAsanGlobalRedzoneMagic: |
| 413 | bug_descr = "global-buffer-overflow"; |
| 414 | break; |
| 415 | } |
| 416 | } |
| 417 | |
| Kostya Serebryany | c4b34d9 | 2011-12-09 01:49:31 +0000 | [diff] [blame] | 418 | AsanThread *curr_thread = asanThreadRegistry().GetCurrent(); |
| Kostya Serebryany | e0cff0b | 2012-06-06 15:06:58 +0000 | [diff] [blame] | 419 | u32 curr_tid = asanThreadRegistry().GetCurrentTidOrInvalid(); |
| Kostya Serebryany | c4b34d9 | 2011-12-09 01:49:31 +0000 | [diff] [blame] | 420 | |
| 421 | if (curr_thread) { |
| 422 | // We started reporting an error message. Stop using the fake stack |
| 423 | // in case we will call an instrumented function from a symbolizer. |
| 424 | curr_thread->fake_stack().StopUsingFakeStack(); |
| 425 | } |
| 426 | |
| Alexey Samsonov | e954101 | 2012-06-06 13:11:29 +0000 | [diff] [blame] | 427 | AsanReport("ERROR: AddressSanitizer %s on address " |
| 428 | "%p at pc 0x%zx bp 0x%zx sp 0x%zx\n", |
| 429 | bug_descr, (void*)addr, pc, bp, sp); |
| Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 430 | |
| Alexey Samsonov | e954101 | 2012-06-06 13:11:29 +0000 | [diff] [blame] | 431 | AsanPrintf("%s of size %zu at %p thread T%d\n", |
| 432 | access_size ? (is_write ? "WRITE" : "READ") : "ACCESS", |
| 433 | access_size, (void*)addr, curr_tid); |
| Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 434 | |
| Alexey Samsonov | cb8c4dc | 2012-07-09 14:36:04 +0000 | [diff] [blame^] | 435 | if (flags()->debug) { |
| Kostya Serebryany | 3f4c387 | 2012-05-31 14:35:53 +0000 | [diff] [blame] | 436 | PrintBytes("PC: ", (uptr*)pc); |
| Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 437 | } |
| 438 | |
| Evgeniy Stepanov | 9cfa194 | 2012-01-19 11:34:18 +0000 | [diff] [blame] | 439 | GET_STACK_TRACE_WITH_PC_AND_BP(kStackTraceMax, pc, bp); |
| Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 440 | stack.PrintStack(); |
| 441 | |
| 442 | CHECK(AddrIsInMem(addr)); |
| 443 | |
| 444 | DescribeAddress(addr, access_size); |
| 445 | |
| Kostya Serebryany | 3f4c387 | 2012-05-31 14:35:53 +0000 | [diff] [blame] | 446 | uptr shadow_addr = MemToShadow(addr); |
| Alexey Samsonov | e954101 | 2012-06-06 13:11:29 +0000 | [diff] [blame] | 447 | AsanReport("ABORTING\n"); |
| Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 448 | __asan_print_accumulated_stats(); |
| Alexey Samsonov | e954101 | 2012-06-06 13:11:29 +0000 | [diff] [blame] | 449 | AsanPrintf("Shadow byte and word:\n"); |
| 450 | AsanPrintf(" %p: %x\n", (void*)shadow_addr, *(unsigned char*)shadow_addr); |
| Kostya Serebryany | 3f4c387 | 2012-05-31 14:35:53 +0000 | [diff] [blame] | 451 | uptr aligned_shadow = shadow_addr & ~(kWordSize - 1); |
| 452 | PrintBytes(" ", (uptr*)(aligned_shadow)); |
| Alexey Samsonov | e954101 | 2012-06-06 13:11:29 +0000 | [diff] [blame] | 453 | AsanPrintf("More shadow bytes:\n"); |
| Kostya Serebryany | 3f4c387 | 2012-05-31 14:35:53 +0000 | [diff] [blame] | 454 | PrintBytes(" ", (uptr*)(aligned_shadow-4*kWordSize)); |
| 455 | PrintBytes(" ", (uptr*)(aligned_shadow-3*kWordSize)); |
| 456 | PrintBytes(" ", (uptr*)(aligned_shadow-2*kWordSize)); |
| 457 | PrintBytes(" ", (uptr*)(aligned_shadow-1*kWordSize)); |
| 458 | PrintBytes("=>", (uptr*)(aligned_shadow+0*kWordSize)); |
| 459 | PrintBytes(" ", (uptr*)(aligned_shadow+1*kWordSize)); |
| 460 | PrintBytes(" ", (uptr*)(aligned_shadow+2*kWordSize)); |
| 461 | PrintBytes(" ", (uptr*)(aligned_shadow+3*kWordSize)); |
| 462 | PrintBytes(" ", (uptr*)(aligned_shadow+4*kWordSize)); |
| Alexander Potapenko | 3fe9135 | 2012-02-27 14:06:48 +0000 | [diff] [blame] | 463 | if (error_report_callback) { |
| 464 | error_report_callback(error_message_buffer); |
| 465 | } |
| Alexey Samsonov | 47657ce | 2012-06-06 07:02:44 +0000 | [diff] [blame] | 466 | Die(); |
| Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 467 | } |
| 468 | |
| Alexander Potapenko | fca72fd | 2012-05-25 15:37:16 +0000 | [diff] [blame] | 469 | |
| 470 | void __asan_init() { |
| 471 | if (asan_inited) return; |
| 472 | asan_init_is_running = true; |
| 473 | |
| 474 | // Make sure we are not statically linked. |
| 475 | AsanDoesNotSupportStaticLinkage(); |
| 476 | |
| Alexey Samsonov | cb8c4dc | 2012-07-09 14:36:04 +0000 | [diff] [blame^] | 477 | // Initialize flags. |
| Alexey Samsonov | 3dbeabb | 2012-06-14 14:07:21 +0000 | [diff] [blame] | 478 | const char *options = GetEnv("ASAN_OPTIONS"); |
| Alexey Samsonov | cb8c4dc | 2012-07-09 14:36:04 +0000 | [diff] [blame^] | 479 | InitializeFlags(flags(), options); |
| Alexander Potapenko | feb4793 | 2012-03-16 16:38:31 +0000 | [diff] [blame] | 480 | |
| Alexey Samsonov | cb8c4dc | 2012-07-09 14:36:04 +0000 | [diff] [blame^] | 481 | if (flags()->verbosity && options) { |
| Alexander Potapenko | feb4793 | 2012-03-16 16:38:31 +0000 | [diff] [blame] | 482 | Report("Parsed ASAN_OPTIONS: %s\n", options); |
| 483 | } |
| 484 | |
| Alexey Samsonov | cb8c4dc | 2012-07-09 14:36:04 +0000 | [diff] [blame^] | 485 | if (flags()->atexit) { |
| Alexey Samsonov | b823e3c | 2012-02-22 14:07:06 +0000 | [diff] [blame] | 486 | Atexit(asan_atexit); |
| Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 487 | } |
| 488 | |
| Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 489 | // interceptors |
| 490 | InitializeAsanInterceptors(); |
| 491 | |
| 492 | ReplaceSystemMalloc(); |
| Alexey Samsonov | 4d5f98d | 2012-04-06 08:21:08 +0000 | [diff] [blame] | 493 | ReplaceOperatorsNewAndDelete(); |
| Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 494 | |
| Alexey Samsonov | cb8c4dc | 2012-07-09 14:36:04 +0000 | [diff] [blame^] | 495 | if (flags()->verbosity) { |
| Alexey Samsonov | e4309e8 | 2012-06-06 10:54:25 +0000 | [diff] [blame] | 496 | Printf("|| `[%p, %p]` || HighMem ||\n", |
| 497 | (void*)kHighMemBeg, (void*)kHighMemEnd); |
| 498 | Printf("|| `[%p, %p]` || HighShadow ||\n", |
| 499 | (void*)kHighShadowBeg, (void*)kHighShadowEnd); |
| 500 | Printf("|| `[%p, %p]` || ShadowGap ||\n", |
| 501 | (void*)kShadowGapBeg, (void*)kShadowGapEnd); |
| 502 | Printf("|| `[%p, %p]` || LowShadow ||\n", |
| 503 | (void*)kLowShadowBeg, (void*)kLowShadowEnd); |
| 504 | Printf("|| `[%p, %p]` || LowMem ||\n", |
| 505 | (void*)kLowMemBeg, (void*)kLowMemEnd); |
| Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 506 | Printf("MemToShadow(shadow): %p %p %p %p\n", |
| Alexey Samsonov | e4309e8 | 2012-06-06 10:54:25 +0000 | [diff] [blame] | 507 | (void*)MEM_TO_SHADOW(kLowShadowBeg), |
| 508 | (void*)MEM_TO_SHADOW(kLowShadowEnd), |
| 509 | (void*)MEM_TO_SHADOW(kHighShadowBeg), |
| 510 | (void*)MEM_TO_SHADOW(kHighShadowEnd)); |
| Alexey Samsonov | cb8c4dc | 2012-07-09 14:36:04 +0000 | [diff] [blame^] | 511 | Printf("red_zone=%zu\n", (uptr)flags()->redzone); |
| 512 | Printf("malloc_context_size=%zu\n", (uptr)flags()->malloc_context_size); |
| Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 513 | |
| Kostya Serebryany | 3f4c387 | 2012-05-31 14:35:53 +0000 | [diff] [blame] | 514 | Printf("SHADOW_SCALE: %zx\n", (uptr)SHADOW_SCALE); |
| 515 | Printf("SHADOW_GRANULARITY: %zx\n", (uptr)SHADOW_GRANULARITY); |
| 516 | Printf("SHADOW_OFFSET: %zx\n", (uptr)SHADOW_OFFSET); |
| Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 517 | CHECK(SHADOW_SCALE >= 3 && SHADOW_SCALE <= 7); |
| 518 | } |
| 519 | |
| Alexey Samsonov | cb8c4dc | 2012-07-09 14:36:04 +0000 | [diff] [blame^] | 520 | if (flags()->disable_core) { |
| Alexey Samsonov | be7420c | 2012-06-15 06:08:19 +0000 | [diff] [blame] | 521 | DisableCoreDumper(); |
| Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 522 | } |
| 523 | |
| Alexey Samsonov | dd3a911 | 2012-06-15 07:29:14 +0000 | [diff] [blame] | 524 | uptr shadow_start = kLowShadowBeg; |
| 525 | if (kLowShadowBeg > 0) shadow_start -= kMmapGranularity; |
| 526 | uptr shadow_end = kHighShadowEnd; |
| 527 | if (MemoryRangeIsAvailable(shadow_start, shadow_end)) { |
| Kostya Serebryany | a7e760a | 2012-01-09 19:18:27 +0000 | [diff] [blame] | 528 | if (kLowShadowBeg != kLowShadowEnd) { |
| Timur Iskhodzhanov | 3e81fe4 | 2012-02-09 17:20:14 +0000 | [diff] [blame] | 529 | // mmap the low shadow plus at least one page. |
| 530 | ReserveShadowMemoryRange(kLowShadowBeg - kMmapGranularity, kLowShadowEnd); |
| Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 531 | } |
| Kostya Serebryany | a7e760a | 2012-01-09 19:18:27 +0000 | [diff] [blame] | 532 | // mmap the high shadow. |
| 533 | ReserveShadowMemoryRange(kHighShadowBeg, kHighShadowEnd); |
| Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 534 | // protect the gap |
| Alexey Samsonov | f607fc1 | 2012-06-14 14:42:58 +0000 | [diff] [blame] | 535 | void *prot = Mprotect(kShadowGapBeg, kShadowGapEnd - kShadowGapBeg + 1); |
| Kostya Serebryany | a874fe5 | 2011-12-28 23:28:54 +0000 | [diff] [blame] | 536 | CHECK(prot == (void*)kShadowGapBeg); |
| Alexander Potapenko | c50e835 | 2012-02-13 15:11:23 +0000 | [diff] [blame] | 537 | } else { |
| 538 | Report("Shadow memory range interleaves with an existing memory mapping. " |
| 539 | "ASan cannot proceed correctly. ABORTING.\n"); |
| Alexey Samsonov | be7420c | 2012-06-15 06:08:19 +0000 | [diff] [blame] | 540 | DumpProcessMap(); |
| Alexey Samsonov | 47657ce | 2012-06-06 07:02:44 +0000 | [diff] [blame] | 541 | Die(); |
| Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 542 | } |
| 543 | |
| Alexander Potapenko | f03d8af | 2012-04-05 10:54:52 +0000 | [diff] [blame] | 544 | InstallSignalHandlers(); |
| 545 | |
| Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 546 | // On Linux AsanThread::ThreadStart() calls malloc() that's why asan_inited |
| 547 | // should be set to 1 prior to initializing the threads. |
| 548 | asan_inited = 1; |
| 549 | asan_init_is_running = false; |
| 550 | |
| 551 | asanThreadRegistry().Init(); |
| 552 | asanThreadRegistry().GetMain()->ThreadStart(); |
| Kostya Serebryany | 51e75c4 | 2011-12-28 00:59:39 +0000 | [diff] [blame] | 553 | force_interface_symbols(); // no-op. |
| Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 554 | |
| Alexey Samsonov | cb8c4dc | 2012-07-09 14:36:04 +0000 | [diff] [blame^] | 555 | if (flags()->verbosity) { |
| Kostya Serebryany | d6567c5 | 2011-12-01 21:40:52 +0000 | [diff] [blame] | 556 | Report("AddressSanitizer Init done\n"); |
| Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 557 | } |
| 558 | } |
| Evgeniy Stepanov | 8bcc6b9 | 2012-01-11 08:17:19 +0000 | [diff] [blame] | 559 | |
| 560 | #if defined(ASAN_USE_PREINIT_ARRAY) |
| Timur Iskhodzhanov | 38ed736 | 2012-02-21 16:24:23 +0000 | [diff] [blame] | 561 | // On Linux, we force __asan_init to be called before anyone else |
| 562 | // by placing it into .preinit_array section. |
| 563 | // FIXME: do we have anything like this on Mac? |
| 564 | __attribute__((section(".preinit_array"))) |
| 565 | typeof(__asan_init) *__asan_preinit =__asan_init; |
| 566 | #elif defined(_WIN32) && defined(_DLL) |
| 567 | // On Windows, when using dynamic CRT (/MD), we can put a pointer |
| 568 | // to __asan_init into the global list of C initializers. |
| 569 | // See crt0dat.c in the CRT sources for the details. |
| Timur Iskhodzhanov | 39c22ee | 2012-02-22 09:28:14 +0000 | [diff] [blame] | 570 | #pragma section(".CRT$XIB", long, read) // NOLINT |
| Timur Iskhodzhanov | 38ed736 | 2012-02-21 16:24:23 +0000 | [diff] [blame] | 571 | __declspec(allocate(".CRT$XIB")) void (*__asan_preinit)() = __asan_init; |
| Evgeniy Stepanov | 8bcc6b9 | 2012-01-11 08:17:19 +0000 | [diff] [blame] | 572 | #endif |