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