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" |
Alexey Samsonov | e218beb | 2012-08-09 09:06:52 +0000 | [diff] [blame] | 20 | #include "asan_report.h" |
Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 21 | #include "asan_stack.h" |
| 22 | #include "asan_stats.h" |
| 23 | #include "asan_thread.h" |
| 24 | #include "asan_thread_registry.h" |
Dmitry Vyukov | fce5bd4 | 2012-06-29 16:58:33 +0000 | [diff] [blame] | 25 | #include "sanitizer_common/sanitizer_atomic.h" |
Alexey Samsonov | cb8c4dc | 2012-07-09 14:36:04 +0000 | [diff] [blame] | 26 | #include "sanitizer_common/sanitizer_flags.h" |
Alexey Samsonov | 9552db7 | 2012-06-05 07:25:47 +0000 | [diff] [blame] | 27 | #include "sanitizer_common/sanitizer_libc.h" |
Alexey Samsonov | 9c6e530 | 2012-08-23 07:32:06 +0000 | [diff] [blame] | 28 | #include "sanitizer_common/sanitizer_symbolizer.h" |
Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 29 | |
Alexey Samsonov | 47657ce | 2012-06-06 07:02:44 +0000 | [diff] [blame] | 30 | namespace __sanitizer { |
| 31 | using namespace __asan; |
| 32 | |
| 33 | void Die() { |
Dmitry Vyukov | fce5bd4 | 2012-06-29 16:58:33 +0000 | [diff] [blame] | 34 | static atomic_uint32_t num_calls; |
| 35 | if (atomic_fetch_add(&num_calls, 1, memory_order_relaxed) != 0) { |
Alexey Samsonov | 47657ce | 2012-06-06 07:02:44 +0000 | [diff] [blame] | 36 | // Don't die twice - run a busy loop. |
| 37 | while (1) { } |
| 38 | } |
Alexey Samsonov | cb8c4dc | 2012-07-09 14:36:04 +0000 | [diff] [blame] | 39 | if (flags()->sleep_before_dying) { |
Alexey Samsonov | 4e21c6b | 2012-08-06 13:00:21 +0000 | [diff] [blame] | 40 | Report("Sleeping for %d second(s)\n", flags()->sleep_before_dying); |
Alexey Samsonov | cb8c4dc | 2012-07-09 14:36:04 +0000 | [diff] [blame] | 41 | SleepForSeconds(flags()->sleep_before_dying); |
Alexey Samsonov | 47657ce | 2012-06-06 07:02:44 +0000 | [diff] [blame] | 42 | } |
Alexey Samsonov | cb8c4dc | 2012-07-09 14:36:04 +0000 | [diff] [blame] | 43 | if (flags()->unmap_shadow_on_exit) |
Alexey Samsonov | a25b346 | 2012-06-06 16:15:07 +0000 | [diff] [blame] | 44 | UnmapOrDie((void*)kLowShadowBeg, kHighShadowEnd - kLowShadowBeg); |
Alexey Samsonov | 47657ce | 2012-06-06 07:02:44 +0000 | [diff] [blame] | 45 | if (death_callback) |
| 46 | death_callback(); |
Alexey Samsonov | cb8c4dc | 2012-07-09 14:36:04 +0000 | [diff] [blame] | 47 | if (flags()->abort_on_error) |
Alexey Samsonov | 47657ce | 2012-06-06 07:02:44 +0000 | [diff] [blame] | 48 | Abort(); |
Alexey Samsonov | cb8c4dc | 2012-07-09 14:36:04 +0000 | [diff] [blame] | 49 | Exit(flags()->exitcode); |
Alexey Samsonov | 47657ce | 2012-06-06 07:02:44 +0000 | [diff] [blame] | 50 | } |
| 51 | |
Alexander Potapenko | ec3b073 | 2012-08-15 11:57:52 +0000 | [diff] [blame] | 52 | SANITIZER_INTERFACE_ATTRIBUTE |
Alexey Samsonov | 15a7761 | 2012-06-06 15:22:20 +0000 | [diff] [blame] | 53 | void CheckFailed(const char *file, int line, const char *cond, u64 v1, u64 v2) { |
Kostya Serebryany | b134ffa | 2012-07-17 07:20:13 +0000 | [diff] [blame] | 54 | AsanReport("AddressSanitizer CHECK failed: %s:%d \"%s\" (0x%zx, 0x%zx)\n", |
Alexey Samsonov | 15a7761 | 2012-06-06 15:22:20 +0000 | [diff] [blame] | 55 | file, line, cond, (uptr)v1, (uptr)v2); |
| 56 | PRINT_CURRENT_STACK(); |
| 57 | ShowStatsAndAbort(); |
| 58 | } |
| 59 | |
Alexey Samsonov | 47657ce | 2012-06-06 07:02:44 +0000 | [diff] [blame] | 60 | } // namespace __sanitizer |
| 61 | |
Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 62 | namespace __asan { |
| 63 | |
| 64 | // -------------------------- Flags ------------------------- {{{1 |
Alexey Samsonov | 9b1b101 | 2012-07-10 09:17:06 +0000 | [diff] [blame] | 65 | static const int kMallocContextSize = 30; |
Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 66 | |
Alexey Samsonov | cb8c4dc | 2012-07-09 14:36:04 +0000 | [diff] [blame] | 67 | static Flags asan_flags; |
| 68 | |
| 69 | Flags *flags() { |
| 70 | return &asan_flags; |
| 71 | } |
| 72 | |
Alexey Samsonov | cb8c4dc | 2012-07-09 14:36:04 +0000 | [diff] [blame] | 73 | static void ParseFlagsFromString(Flags *f, const char *str) { |
| 74 | ParseFlag(str, &f->quarantine_size, "quarantine_size"); |
| 75 | ParseFlag(str, &f->symbolize, "symbolize"); |
| 76 | ParseFlag(str, &f->verbosity, "verbosity"); |
| 77 | ParseFlag(str, &f->redzone, "redzone"); |
| 78 | CHECK(f->redzone >= 16); |
| 79 | CHECK(IsPowerOfTwo(f->redzone)); |
| 80 | |
| 81 | ParseFlag(str, &f->debug, "debug"); |
Alexey Samsonov | cb8c4dc | 2012-07-09 14:36:04 +0000 | [diff] [blame] | 82 | ParseFlag(str, &f->report_globals, "report_globals"); |
Kostya Serebryany | 3945c58 | 2012-08-21 14:10:25 +0000 | [diff] [blame] | 83 | ParseFlag(str, &f->check_initialization_order, "initialization_order"); |
Alexey Samsonov | cb8c4dc | 2012-07-09 14:36:04 +0000 | [diff] [blame] | 84 | ParseFlag(str, &f->malloc_context_size, "malloc_context_size"); |
| 85 | CHECK(f->malloc_context_size <= kMallocContextSize); |
| 86 | |
| 87 | ParseFlag(str, &f->replace_str, "replace_str"); |
| 88 | ParseFlag(str, &f->replace_intrin, "replace_intrin"); |
| 89 | ParseFlag(str, &f->replace_cfallocator, "replace_cfallocator"); |
| 90 | ParseFlag(str, &f->mac_ignore_invalid_free, "mac_ignore_invalid_free"); |
| 91 | ParseFlag(str, &f->use_fake_stack, "use_fake_stack"); |
| 92 | ParseFlag(str, &f->max_malloc_fill_size, "max_malloc_fill_size"); |
| 93 | ParseFlag(str, &f->exitcode, "exitcode"); |
| 94 | ParseFlag(str, &f->allow_user_poisoning, "allow_user_poisoning"); |
| 95 | ParseFlag(str, &f->sleep_before_dying, "sleep_before_dying"); |
| 96 | ParseFlag(str, &f->handle_segv, "handle_segv"); |
| 97 | ParseFlag(str, &f->use_sigaltstack, "use_sigaltstack"); |
Alexey Samsonov | cb8c4dc | 2012-07-09 14:36:04 +0000 | [diff] [blame] | 98 | ParseFlag(str, &f->check_malloc_usable_size, "check_malloc_usable_size"); |
| 99 | ParseFlag(str, &f->unmap_shadow_on_exit, "unmap_shadow_on_exit"); |
| 100 | ParseFlag(str, &f->abort_on_error, "abort_on_error"); |
| 101 | ParseFlag(str, &f->atexit, "atexit"); |
Alexey Samsonov | cb8c4dc | 2012-07-09 14:36:04 +0000 | [diff] [blame] | 102 | ParseFlag(str, &f->disable_core, "disable_core"); |
Alexey Samsonov | 4e21c6b | 2012-08-06 13:00:21 +0000 | [diff] [blame] | 103 | ParseFlag(str, &f->strip_path_prefix, "strip_path_prefix"); |
Alexander Potapenko | eb8c46e | 2012-08-24 09:22:05 +0000 | [diff] [blame] | 104 | ParseFlag(str, &f->allow_reexec, "allow_reexec"); |
Alexey Samsonov | cb8c4dc | 2012-07-09 14:36:04 +0000 | [diff] [blame] | 105 | } |
| 106 | |
Alexey Samsonov | b750c4c | 2012-07-25 10:40:57 +0000 | [diff] [blame] | 107 | extern "C" { |
Alexey Samsonov | c6b8716 | 2012-08-14 13:54:28 +0000 | [diff] [blame] | 108 | SANITIZER_WEAK_ATTRIBUTE |
| 109 | SANITIZER_INTERFACE_ATTRIBUTE |
| 110 | const char* __asan_default_options() { return ""; } |
Alexey Samsonov | b750c4c | 2012-07-25 10:40:57 +0000 | [diff] [blame] | 111 | } // extern "C" |
| 112 | |
Alexey Samsonov | cb8c4dc | 2012-07-09 14:36:04 +0000 | [diff] [blame] | 113 | void InitializeFlags(Flags *f, const char *env) { |
| 114 | internal_memset(f, 0, sizeof(*f)); |
| 115 | |
| 116 | f->quarantine_size = (ASAN_LOW_MEMORY) ? 1UL << 24 : 1UL << 28; |
| 117 | f->symbolize = false; |
| 118 | f->verbosity = 0; |
Alexey Samsonov | 7ed1d2b | 2012-07-10 07:41:27 +0000 | [diff] [blame] | 119 | f->redzone = (ASAN_LOW_MEMORY) ? 64 : 128; |
| 120 | f->debug = false; |
Alexey Samsonov | cb8c4dc | 2012-07-09 14:36:04 +0000 | [diff] [blame] | 121 | f->report_globals = 1; |
Kostya Serebryany | 3945c58 | 2012-08-21 14:10:25 +0000 | [diff] [blame] | 122 | f->check_initialization_order = true; |
Alexey Samsonov | cb8c4dc | 2012-07-09 14:36:04 +0000 | [diff] [blame] | 123 | f->malloc_context_size = kMallocContextSize; |
| 124 | f->replace_str = true; |
| 125 | f->replace_intrin = true; |
Alexey Samsonov | 7ed1d2b | 2012-07-10 07:41:27 +0000 | [diff] [blame] | 126 | f->replace_cfallocator = true; |
| 127 | f->mac_ignore_invalid_free = false; |
Alexey Samsonov | cb8c4dc | 2012-07-09 14:36:04 +0000 | [diff] [blame] | 128 | f->use_fake_stack = true; |
| 129 | f->max_malloc_fill_size = 0; |
| 130 | f->exitcode = ASAN_DEFAULT_FAILURE_EXITCODE; |
| 131 | f->allow_user_poisoning = true; |
| 132 | f->sleep_before_dying = 0; |
| 133 | f->handle_segv = ASAN_NEEDS_SEGV; |
| 134 | f->use_sigaltstack = false; |
| 135 | f->check_malloc_usable_size = true; |
| 136 | f->unmap_shadow_on_exit = false; |
| 137 | f->abort_on_error = false; |
| 138 | f->atexit = false; |
| 139 | f->disable_core = (__WORDSIZE == 64); |
Alexey Samsonov | 4e21c6b | 2012-08-06 13:00:21 +0000 | [diff] [blame] | 140 | f->strip_path_prefix = ""; |
Alexander Potapenko | eb8c46e | 2012-08-24 09:22:05 +0000 | [diff] [blame] | 141 | f->allow_reexec = true; |
Alexey Samsonov | cb8c4dc | 2012-07-09 14:36:04 +0000 | [diff] [blame] | 142 | |
Alexey Samsonov | cb8c4dc | 2012-07-09 14:36:04 +0000 | [diff] [blame] | 143 | // Override from user-specified string. |
Alexey Samsonov | b750c4c | 2012-07-25 10:40:57 +0000 | [diff] [blame] | 144 | ParseFlagsFromString(f, __asan_default_options()); |
| 145 | if (flags()->verbosity) { |
| 146 | Report("Using the defaults from __asan_default_options: %s\n", |
| 147 | __asan_default_options()); |
Alexey Samsonov | cb8c4dc | 2012-07-09 14:36:04 +0000 | [diff] [blame] | 148 | } |
Alexey Samsonov | cb8c4dc | 2012-07-09 14:36:04 +0000 | [diff] [blame] | 149 | |
| 150 | // Override from command line. |
| 151 | ParseFlagsFromString(f, env); |
| 152 | } |
Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 153 | |
| 154 | // -------------------------- Globals --------------------- {{{1 |
| 155 | int asan_inited; |
| 156 | bool asan_init_is_running; |
Alexey Samsonov | 47657ce | 2012-06-06 07:02:44 +0000 | [diff] [blame] | 157 | void (*death_callback)(void); |
Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 158 | |
Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 159 | // -------------------------- Misc ---------------- {{{1 |
| 160 | void ShowStatsAndAbort() { |
| 161 | __asan_print_accumulated_stats(); |
Alexey Samsonov | 47657ce | 2012-06-06 07:02:44 +0000 | [diff] [blame] | 162 | Die(); |
Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 163 | } |
| 164 | |
Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 165 | // ---------------------- mmap -------------------- {{{1 |
Kostya Serebryany | a874fe5 | 2011-12-28 23:28:54 +0000 | [diff] [blame] | 166 | // Reserve memory range [beg, end]. |
Kostya Serebryany | 3f4c387 | 2012-05-31 14:35:53 +0000 | [diff] [blame] | 167 | static void ReserveShadowMemoryRange(uptr beg, uptr end) { |
Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 168 | CHECK((beg % kPageSize) == 0); |
| 169 | CHECK(((end + 1) % kPageSize) == 0); |
Kostya Serebryany | 3f4c387 | 2012-05-31 14:35:53 +0000 | [diff] [blame] | 170 | uptr size = end - beg + 1; |
Alexey Samsonov | f607fc1 | 2012-06-14 14:42:58 +0000 | [diff] [blame] | 171 | void *res = MmapFixedNoReserve(beg, size); |
Kostya Serebryany | a874fe5 | 2011-12-28 23:28:54 +0000 | [diff] [blame] | 172 | CHECK(res == (void*)beg && "ReserveShadowMemoryRange failed"); |
Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 173 | } |
| 174 | |
Alexey Samsonov | 70e177e | 2012-08-27 09:30:58 +0000 | [diff] [blame] | 175 | // --------------- LowLevelAllocateCallbac ---------- {{{1 |
| 176 | static void OnLowLevelAllocate(uptr ptr, uptr size) { |
| 177 | PoisonShadow(ptr, size, kAsanInternalHeapMagic); |
Kostya Serebryany | b89567c | 2011-12-02 21:02:20 +0000 | [diff] [blame] | 178 | } |
| 179 | |
Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 180 | // -------------------------- Run-time entry ------------------- {{{1 |
Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 181 | // exported functions |
Kostya Serebryany | 51e75c4 | 2011-12-28 00:59:39 +0000 | [diff] [blame] | 182 | #define ASAN_REPORT_ERROR(type, is_write, size) \ |
Alexey Samsonov | 0a4c906 | 2012-06-05 13:50:57 +0000 | [diff] [blame] | 183 | extern "C" NOINLINE INTERFACE_ATTRIBUTE \ |
Kostya Serebryany | 3f4c387 | 2012-05-31 14:35:53 +0000 | [diff] [blame] | 184 | void __asan_report_ ## type ## size(uptr addr); \ |
| 185 | void __asan_report_ ## type ## size(uptr addr) { \ |
Kostya Serebryany | 9f311bb | 2012-03-15 01:36:00 +0000 | [diff] [blame] | 186 | GET_CALLER_PC_BP_SP; \ |
Kostya Serebryany | 51e75c4 | 2011-12-28 00:59:39 +0000 | [diff] [blame] | 187 | __asan_report_error(pc, bp, sp, addr, is_write, size); \ |
Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 188 | } |
| 189 | |
| 190 | ASAN_REPORT_ERROR(load, false, 1) |
| 191 | ASAN_REPORT_ERROR(load, false, 2) |
| 192 | ASAN_REPORT_ERROR(load, false, 4) |
| 193 | ASAN_REPORT_ERROR(load, false, 8) |
| 194 | ASAN_REPORT_ERROR(load, false, 16) |
| 195 | ASAN_REPORT_ERROR(store, true, 1) |
| 196 | ASAN_REPORT_ERROR(store, true, 2) |
| 197 | ASAN_REPORT_ERROR(store, true, 4) |
| 198 | ASAN_REPORT_ERROR(store, true, 8) |
| 199 | ASAN_REPORT_ERROR(store, true, 16) |
| 200 | |
| 201 | // Force the linker to keep the symbols for various ASan interface functions. |
| 202 | // We want to keep those in the executable in order to let the instrumented |
| 203 | // dynamic libraries access the symbol even if it is not used by the executable |
| 204 | // itself. This should help if the build system is removing dead code at link |
| 205 | // time. |
Alexander Potapenko | 3fe9135 | 2012-02-27 14:06:48 +0000 | [diff] [blame] | 206 | static NOINLINE void force_interface_symbols() { |
Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 207 | volatile int fake_condition = 0; // prevent dead condition elimination. |
Alexander Potapenko | 448fe9a | 2012-08-09 09:46:12 +0000 | [diff] [blame] | 208 | // __asan_report_* functions are noreturn, so we need a switch to prevent |
| 209 | // the compiler from removing any of them. |
| 210 | switch (fake_condition) { |
| 211 | case 1: __asan_report_load1(0); break; |
| 212 | case 2: __asan_report_load2(0); break; |
| 213 | case 3: __asan_report_load4(0); break; |
| 214 | case 4: __asan_report_load8(0); break; |
| 215 | case 5: __asan_report_load16(0); break; |
| 216 | case 6: __asan_report_store1(0); break; |
| 217 | case 7: __asan_report_store2(0); break; |
| 218 | case 8: __asan_report_store4(0); break; |
| 219 | case 9: __asan_report_store8(0); break; |
| 220 | case 10: __asan_report_store16(0); break; |
| 221 | case 11: __asan_register_global(0, 0, 0); break; |
| 222 | case 12: __asan_register_globals(0, 0); break; |
| 223 | case 13: __asan_unregister_globals(0, 0); break; |
| 224 | case 14: __asan_set_death_callback(0); break; |
| 225 | case 15: __asan_set_error_report_callback(0); break; |
| 226 | case 16: __asan_handle_no_return(); break; |
Alexander Potapenko | 5a9938d | 2012-08-09 16:05:17 +0000 | [diff] [blame] | 227 | case 17: __asan_address_is_poisoned(0); break; |
| 228 | case 18: __asan_get_allocated_size(0); break; |
| 229 | case 19: __asan_get_current_allocated_bytes(); break; |
| 230 | case 20: __asan_get_estimated_allocated_size(0); break; |
| 231 | case 21: __asan_get_free_bytes(); break; |
| 232 | case 22: __asan_get_heap_size(); break; |
| 233 | case 23: __asan_get_ownership(0); break; |
| 234 | case 24: __asan_get_unmapped_bytes(); break; |
| 235 | case 25: __asan_poison_memory_region(0, 0); break; |
| 236 | case 26: __asan_unpoison_memory_region(0, 0); break; |
| 237 | case 27: __asan_set_error_exit_code(0); break; |
| 238 | case 28: __asan_stack_free(0, 0, 0); break; |
| 239 | case 29: __asan_stack_malloc(0, 0); break; |
Alexey Samsonov | e2430d2 | 2012-08-13 14:05:00 +0000 | [diff] [blame] | 240 | case 30: __asan_set_on_error_callback(0); break; |
Alexey Samsonov | 08d9788 | 2012-08-14 15:03:24 +0000 | [diff] [blame] | 241 | case 31: __asan_default_options(); break; |
Kostya Serebryany | 128892c | 2012-08-21 14:43:36 +0000 | [diff] [blame] | 242 | case 32: __asan_before_dynamic_init(0, 0); break; |
| 243 | case 33: __asan_after_dynamic_init(); break; |
Alexey Samsonov | b21de9e | 2012-08-22 10:12:47 +0000 | [diff] [blame] | 244 | case 34: __asan_malloc_hook(0, 0); break; |
| 245 | case 35: __asan_free_hook(0); break; |
Alexey Samsonov | c93d3e2 | 2012-08-22 13:31:37 +0000 | [diff] [blame] | 246 | case 36: __asan_set_symbolize_callback(0); break; |
Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 247 | } |
| 248 | } |
| 249 | |
Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 250 | static void asan_atexit() { |
Alexey Samsonov | e954101 | 2012-06-06 13:11:29 +0000 | [diff] [blame] | 251 | AsanPrintf("AddressSanitizer exit stats:\n"); |
Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 252 | __asan_print_accumulated_stats(); |
| 253 | } |
| 254 | |
Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 255 | } // namespace __asan |
| 256 | |
Kostya Serebryany | 4803ab9 | 2012-01-09 18:53:15 +0000 | [diff] [blame] | 257 | // ---------------------- Interface ---------------- {{{1 |
Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 258 | using namespace __asan; // NOLINT |
| 259 | |
Alexander Potapenko | 5a9938d | 2012-08-09 16:05:17 +0000 | [diff] [blame] | 260 | int NOINLINE __asan_set_error_exit_code(int exit_code) { |
Alexey Samsonov | cb8c4dc | 2012-07-09 14:36:04 +0000 | [diff] [blame] | 261 | int old = flags()->exitcode; |
| 262 | flags()->exitcode = exit_code; |
Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 263 | return old; |
| 264 | } |
| 265 | |
Alexander Potapenko | dadc45d | 2012-03-06 11:45:59 +0000 | [diff] [blame] | 266 | void NOINLINE __asan_handle_no_return() { |
Kostya Serebryany | f54b1f9 | 2012-02-08 21:33:27 +0000 | [diff] [blame] | 267 | int local_stack; |
| 268 | AsanThread *curr_thread = asanThreadRegistry().GetCurrent(); |
| 269 | CHECK(curr_thread); |
Kostya Serebryany | 3f4c387 | 2012-05-31 14:35:53 +0000 | [diff] [blame] | 270 | uptr top = curr_thread->stack_top(); |
| 271 | uptr bottom = ((uptr)&local_stack - kPageSize) & ~(kPageSize-1); |
Kostya Serebryany | f54b1f9 | 2012-02-08 21:33:27 +0000 | [diff] [blame] | 272 | PoisonShadow(bottom, top - bottom, 0); |
| 273 | } |
| 274 | |
Alexander Potapenko | 2f3f962 | 2012-03-01 14:39:21 +0000 | [diff] [blame] | 275 | void NOINLINE __asan_set_death_callback(void (*callback)(void)) { |
Kostya Serebryany | e1fe0fd | 2012-02-13 21:24:29 +0000 | [diff] [blame] | 276 | death_callback = callback; |
| 277 | } |
| 278 | |
Alexander Potapenko | fca72fd | 2012-05-25 15:37:16 +0000 | [diff] [blame] | 279 | void __asan_init() { |
| 280 | if (asan_inited) return; |
Alexey Samsonov | 70e177e | 2012-08-27 09:30:58 +0000 | [diff] [blame] | 281 | CHECK(!asan_init_is_running && "ASan init calls itself!"); |
Alexander Potapenko | fca72fd | 2012-05-25 15:37:16 +0000 | [diff] [blame] | 282 | asan_init_is_running = true; |
| 283 | |
| 284 | // Make sure we are not statically linked. |
| 285 | AsanDoesNotSupportStaticLinkage(); |
| 286 | |
Alexander Potapenko | eb8c46e | 2012-08-24 09:22:05 +0000 | [diff] [blame] | 287 | // Initialize flags. This must be done early, because most of the |
| 288 | // initialization steps look at flags(). |
Alexey Samsonov | 3dbeabb | 2012-06-14 14:07:21 +0000 | [diff] [blame] | 289 | const char *options = GetEnv("ASAN_OPTIONS"); |
Alexey Samsonov | cb8c4dc | 2012-07-09 14:36:04 +0000 | [diff] [blame] | 290 | InitializeFlags(flags(), options); |
Alexander Potapenko | feb4793 | 2012-03-16 16:38:31 +0000 | [diff] [blame] | 291 | |
Alexey Samsonov | cb8c4dc | 2012-07-09 14:36:04 +0000 | [diff] [blame] | 292 | if (flags()->verbosity && options) { |
Alexander Potapenko | feb4793 | 2012-03-16 16:38:31 +0000 | [diff] [blame] | 293 | Report("Parsed ASAN_OPTIONS: %s\n", options); |
| 294 | } |
| 295 | |
Alexander Potapenko | eb8c46e | 2012-08-24 09:22:05 +0000 | [diff] [blame] | 296 | // Re-exec ourselves if we need to set additional env or command line args. |
| 297 | MaybeReexec(); |
| 298 | |
Alexey Samsonov | 947fbd1 | 2012-08-27 14:04:54 +0000 | [diff] [blame^] | 299 | // Setup internal allocator callback. |
| 300 | SetLowLevelAllocateCallback(OnLowLevelAllocate); |
| 301 | |
Alexey Samsonov | cb8c4dc | 2012-07-09 14:36:04 +0000 | [diff] [blame] | 302 | if (flags()->atexit) { |
Alexey Samsonov | b823e3c | 2012-02-22 14:07:06 +0000 | [diff] [blame] | 303 | Atexit(asan_atexit); |
Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 304 | } |
| 305 | |
Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 306 | // interceptors |
| 307 | InitializeAsanInterceptors(); |
| 308 | |
| 309 | ReplaceSystemMalloc(); |
Alexey Samsonov | 4d5f98d | 2012-04-06 08:21:08 +0000 | [diff] [blame] | 310 | ReplaceOperatorsNewAndDelete(); |
Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 311 | |
Alexey Samsonov | cb8c4dc | 2012-07-09 14:36:04 +0000 | [diff] [blame] | 312 | if (flags()->verbosity) { |
Alexey Samsonov | e4309e8 | 2012-06-06 10:54:25 +0000 | [diff] [blame] | 313 | Printf("|| `[%p, %p]` || HighMem ||\n", |
| 314 | (void*)kHighMemBeg, (void*)kHighMemEnd); |
| 315 | Printf("|| `[%p, %p]` || HighShadow ||\n", |
| 316 | (void*)kHighShadowBeg, (void*)kHighShadowEnd); |
| 317 | Printf("|| `[%p, %p]` || ShadowGap ||\n", |
| 318 | (void*)kShadowGapBeg, (void*)kShadowGapEnd); |
| 319 | Printf("|| `[%p, %p]` || LowShadow ||\n", |
| 320 | (void*)kLowShadowBeg, (void*)kLowShadowEnd); |
| 321 | Printf("|| `[%p, %p]` || LowMem ||\n", |
| 322 | (void*)kLowMemBeg, (void*)kLowMemEnd); |
Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 323 | Printf("MemToShadow(shadow): %p %p %p %p\n", |
Alexey Samsonov | e4309e8 | 2012-06-06 10:54:25 +0000 | [diff] [blame] | 324 | (void*)MEM_TO_SHADOW(kLowShadowBeg), |
| 325 | (void*)MEM_TO_SHADOW(kLowShadowEnd), |
| 326 | (void*)MEM_TO_SHADOW(kHighShadowBeg), |
| 327 | (void*)MEM_TO_SHADOW(kHighShadowEnd)); |
Alexey Samsonov | cb8c4dc | 2012-07-09 14:36:04 +0000 | [diff] [blame] | 328 | Printf("red_zone=%zu\n", (uptr)flags()->redzone); |
| 329 | Printf("malloc_context_size=%zu\n", (uptr)flags()->malloc_context_size); |
Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 330 | |
Kostya Serebryany | 3f4c387 | 2012-05-31 14:35:53 +0000 | [diff] [blame] | 331 | Printf("SHADOW_SCALE: %zx\n", (uptr)SHADOW_SCALE); |
| 332 | Printf("SHADOW_GRANULARITY: %zx\n", (uptr)SHADOW_GRANULARITY); |
| 333 | Printf("SHADOW_OFFSET: %zx\n", (uptr)SHADOW_OFFSET); |
Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 334 | CHECK(SHADOW_SCALE >= 3 && SHADOW_SCALE <= 7); |
| 335 | } |
| 336 | |
Alexey Samsonov | cb8c4dc | 2012-07-09 14:36:04 +0000 | [diff] [blame] | 337 | if (flags()->disable_core) { |
Alexey Samsonov | be7420c | 2012-06-15 06:08:19 +0000 | [diff] [blame] | 338 | DisableCoreDumper(); |
Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 339 | } |
| 340 | |
Alexey Samsonov | dd3a911 | 2012-06-15 07:29:14 +0000 | [diff] [blame] | 341 | uptr shadow_start = kLowShadowBeg; |
| 342 | if (kLowShadowBeg > 0) shadow_start -= kMmapGranularity; |
| 343 | uptr shadow_end = kHighShadowEnd; |
| 344 | if (MemoryRangeIsAvailable(shadow_start, shadow_end)) { |
Kostya Serebryany | a7e760a | 2012-01-09 19:18:27 +0000 | [diff] [blame] | 345 | if (kLowShadowBeg != kLowShadowEnd) { |
Timur Iskhodzhanov | 3e81fe4 | 2012-02-09 17:20:14 +0000 | [diff] [blame] | 346 | // mmap the low shadow plus at least one page. |
| 347 | ReserveShadowMemoryRange(kLowShadowBeg - kMmapGranularity, kLowShadowEnd); |
Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 348 | } |
Kostya Serebryany | a7e760a | 2012-01-09 19:18:27 +0000 | [diff] [blame] | 349 | // mmap the high shadow. |
| 350 | ReserveShadowMemoryRange(kHighShadowBeg, kHighShadowEnd); |
Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 351 | // protect the gap |
Alexey Samsonov | f607fc1 | 2012-06-14 14:42:58 +0000 | [diff] [blame] | 352 | void *prot = Mprotect(kShadowGapBeg, kShadowGapEnd - kShadowGapBeg + 1); |
Kostya Serebryany | a874fe5 | 2011-12-28 23:28:54 +0000 | [diff] [blame] | 353 | CHECK(prot == (void*)kShadowGapBeg); |
Alexander Potapenko | c50e835 | 2012-02-13 15:11:23 +0000 | [diff] [blame] | 354 | } else { |
| 355 | Report("Shadow memory range interleaves with an existing memory mapping. " |
| 356 | "ASan cannot proceed correctly. ABORTING.\n"); |
Alexey Samsonov | be7420c | 2012-06-15 06:08:19 +0000 | [diff] [blame] | 357 | DumpProcessMap(); |
Alexey Samsonov | 47657ce | 2012-06-06 07:02:44 +0000 | [diff] [blame] | 358 | Die(); |
Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 359 | } |
| 360 | |
Alexander Potapenko | f03d8af | 2012-04-05 10:54:52 +0000 | [diff] [blame] | 361 | InstallSignalHandlers(); |
Alexey Samsonov | 9c6e530 | 2012-08-23 07:32:06 +0000 | [diff] [blame] | 362 | // Start symbolizer process if necessary. |
| 363 | if (flags()->symbolize) { |
| 364 | const char *external_symbolizer = GetEnv("ASAN_SYMBOLIZER_PATH"); |
| 365 | if (external_symbolizer) { |
| 366 | InitializeExternalSymbolizer(external_symbolizer); |
| 367 | } |
| 368 | } |
Alexey Samsonov | c93d3e2 | 2012-08-22 13:31:37 +0000 | [diff] [blame] | 369 | #ifdef _WIN32 |
| 370 | __asan_set_symbolize_callback(WinSymbolize); |
| 371 | #endif // _WIN32 |
Alexander Potapenko | f03d8af | 2012-04-05 10:54:52 +0000 | [diff] [blame] | 372 | |
Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 373 | // On Linux AsanThread::ThreadStart() calls malloc() that's why asan_inited |
| 374 | // should be set to 1 prior to initializing the threads. |
| 375 | asan_inited = 1; |
| 376 | asan_init_is_running = false; |
| 377 | |
| 378 | asanThreadRegistry().Init(); |
| 379 | asanThreadRegistry().GetMain()->ThreadStart(); |
Kostya Serebryany | 51e75c4 | 2011-12-28 00:59:39 +0000 | [diff] [blame] | 380 | force_interface_symbols(); // no-op. |
Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 381 | |
Alexey Samsonov | cb8c4dc | 2012-07-09 14:36:04 +0000 | [diff] [blame] | 382 | if (flags()->verbosity) { |
Kostya Serebryany | d6567c5 | 2011-12-01 21:40:52 +0000 | [diff] [blame] | 383 | Report("AddressSanitizer Init done\n"); |
Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 384 | } |
| 385 | } |
Evgeniy Stepanov | 8bcc6b9 | 2012-01-11 08:17:19 +0000 | [diff] [blame] | 386 | |
| 387 | #if defined(ASAN_USE_PREINIT_ARRAY) |
Timur Iskhodzhanov | 38ed736 | 2012-02-21 16:24:23 +0000 | [diff] [blame] | 388 | // On Linux, we force __asan_init to be called before anyone else |
| 389 | // by placing it into .preinit_array section. |
| 390 | // FIXME: do we have anything like this on Mac? |
| 391 | __attribute__((section(".preinit_array"))) |
| 392 | typeof(__asan_init) *__asan_preinit =__asan_init; |
| 393 | #elif defined(_WIN32) && defined(_DLL) |
| 394 | // On Windows, when using dynamic CRT (/MD), we can put a pointer |
| 395 | // to __asan_init into the global list of C initializers. |
| 396 | // See crt0dat.c in the CRT sources for the details. |
Timur Iskhodzhanov | 39c22ee | 2012-02-22 09:28:14 +0000 | [diff] [blame] | 397 | #pragma section(".CRT$XIB", long, read) // NOLINT |
Timur Iskhodzhanov | 38ed736 | 2012-02-21 16:24:23 +0000 | [diff] [blame] | 398 | __declspec(allocate(".CRT$XIB")) void (*__asan_preinit)() = __asan_init; |
Evgeniy Stepanov | 8bcc6b9 | 2012-01-11 08:17:19 +0000 | [diff] [blame] | 399 | #endif |