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