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