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" |
Kostya Serebryany | 230e52f | 2013-09-18 10:35:12 +0000 | [diff] [blame] | 16 | #include "asan_interface_internal.h" |
Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 17 | #include "asan_internal.h" |
Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 18 | #include "asan_mapping.h" |
Alexey Samsonov | 7e84349 | 2013-03-28 15:42:43 +0000 | [diff] [blame] | 19 | #include "asan_poisoning.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" |
Dmitry Vyukov | fce5bd4 | 2012-06-29 16:58:33 +0000 | [diff] [blame] | 24 | #include "sanitizer_common/sanitizer_atomic.h" |
Alexey Samsonov | cb8c4dc | 2012-07-09 14:36:04 +0000 | [diff] [blame] | 25 | #include "sanitizer_common/sanitizer_flags.h" |
Alexey Samsonov | 9552db7 | 2012-06-05 07:25:47 +0000 | [diff] [blame] | 26 | #include "sanitizer_common/sanitizer_libc.h" |
Alexey Samsonov | 9c6e530 | 2012-08-23 07:32:06 +0000 | [diff] [blame] | 27 | #include "sanitizer_common/sanitizer_symbolizer.h" |
Sergey Matveev | 71f0411 | 2013-05-21 14:15:35 +0000 | [diff] [blame] | 28 | #include "lsan/lsan_common.h" |
Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 29 | |
Kostya Serebryany | 230e52f | 2013-09-18 10:35:12 +0000 | [diff] [blame] | 30 | int __asan_option_detect_stack_use_after_return; // Global interface symbol. |
| 31 | |
Alexey Samsonov | 591616d | 2012-09-11 09:44:48 +0000 | [diff] [blame] | 32 | namespace __asan { |
Alexey Samsonov | 47657ce | 2012-06-06 07:02:44 +0000 | [diff] [blame] | 33 | |
Kostya Serebryany | e31eca9 | 2013-02-15 12:00:24 +0000 | [diff] [blame] | 34 | uptr AsanMappingProfile[kAsanMappingProfileSize]; |
| 35 | |
Alexey Samsonov | 591616d | 2012-09-11 09:44:48 +0000 | [diff] [blame] | 36 | static void AsanDie() { |
Dmitry Vyukov | fce5bd4 | 2012-06-29 16:58:33 +0000 | [diff] [blame] | 37 | static atomic_uint32_t num_calls; |
| 38 | if (atomic_fetch_add(&num_calls, 1, memory_order_relaxed) != 0) { |
Alexey Samsonov | 47657ce | 2012-06-06 07:02:44 +0000 | [diff] [blame] | 39 | // Don't die twice - run a busy loop. |
| 40 | while (1) { } |
| 41 | } |
Alexey Samsonov | cb8c4dc | 2012-07-09 14:36:04 +0000 | [diff] [blame] | 42 | if (flags()->sleep_before_dying) { |
Alexey Samsonov | 4e21c6b | 2012-08-06 13:00:21 +0000 | [diff] [blame] | 43 | Report("Sleeping for %d second(s)\n", flags()->sleep_before_dying); |
Alexey Samsonov | cb8c4dc | 2012-07-09 14:36:04 +0000 | [diff] [blame] | 44 | SleepForSeconds(flags()->sleep_before_dying); |
Alexey Samsonov | 47657ce | 2012-06-06 07:02:44 +0000 | [diff] [blame] | 45 | } |
Kostya Serebryany | e31eca9 | 2013-02-15 12:00:24 +0000 | [diff] [blame] | 46 | if (flags()->unmap_shadow_on_exit) { |
| 47 | if (kMidMemBeg) { |
| 48 | UnmapOrDie((void*)kLowShadowBeg, kMidMemBeg - kLowShadowBeg); |
| 49 | UnmapOrDie((void*)kMidMemEnd, kHighShadowEnd - kMidMemEnd); |
| 50 | } else { |
| 51 | UnmapOrDie((void*)kLowShadowBeg, kHighShadowEnd - kLowShadowBeg); |
| 52 | } |
| 53 | } |
Alexey Samsonov | 47657ce | 2012-06-06 07:02:44 +0000 | [diff] [blame] | 54 | if (death_callback) |
| 55 | death_callback(); |
Alexey Samsonov | cb8c4dc | 2012-07-09 14:36:04 +0000 | [diff] [blame] | 56 | if (flags()->abort_on_error) |
Alexey Samsonov | 47657ce | 2012-06-06 07:02:44 +0000 | [diff] [blame] | 57 | Abort(); |
Alexey Samsonov | f882247 | 2013-02-20 13:54:32 +0000 | [diff] [blame] | 58 | internal__exit(flags()->exitcode); |
Alexey Samsonov | 47657ce | 2012-06-06 07:02:44 +0000 | [diff] [blame] | 59 | } |
| 60 | |
Alexey Samsonov | 591616d | 2012-09-11 09:44:48 +0000 | [diff] [blame] | 61 | static void AsanCheckFailed(const char *file, int line, const char *cond, |
| 62 | u64 v1, u64 v2) { |
Kostya Serebryany | 283c296 | 2012-08-28 11:34:40 +0000 | [diff] [blame] | 63 | Report("AddressSanitizer CHECK failed: %s:%d \"%s\" (0x%zx, 0x%zx)\n", |
Alexey Samsonov | 15a7761 | 2012-06-06 15:22:20 +0000 | [diff] [blame] | 64 | file, line, cond, (uptr)v1, (uptr)v2); |
Alexander Potapenko | 8e23d27 | 2012-09-07 15:50:19 +0000 | [diff] [blame] | 65 | // FIXME: check for infinite recursion without a thread-local counter here. |
Alexey Samsonov | 15a7761 | 2012-06-06 15:22:20 +0000 | [diff] [blame] | 66 | PRINT_CURRENT_STACK(); |
Alexey Samsonov | 570c2a0 | 2012-12-13 11:09:26 +0000 | [diff] [blame] | 67 | Die(); |
Alexey Samsonov | 15a7761 | 2012-06-06 15:22:20 +0000 | [diff] [blame] | 68 | } |
| 69 | |
Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 70 | // -------------------------- Flags ------------------------- {{{1 |
Sergey Matveev | 3f5528b | 2013-04-16 14:58:45 +0000 | [diff] [blame] | 71 | static const int kDefaultMallocContextSize = 30; |
Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 72 | |
Kostya Serebryany | 16cc10d | 2013-04-11 18:36:04 +0000 | [diff] [blame] | 73 | Flags asan_flags_dont_use_directly; // use via flags(). |
Alexey Samsonov | cb8c4dc | 2012-07-09 14:36:04 +0000 | [diff] [blame] | 74 | |
Alexey Samsonov | 6a08d29 | 2012-12-07 22:01:28 +0000 | [diff] [blame] | 75 | static const char *MaybeCallAsanDefaultOptions() { |
| 76 | return (&__asan_default_options) ? __asan_default_options() : ""; |
| 77 | } |
| 78 | |
Alexey Samsonov | 36ea94d | 2013-02-19 13:14:48 +0000 | [diff] [blame] | 79 | static const char *MaybeUseAsanDefaultOptionsCompileDefiniton() { |
| 80 | #ifdef ASAN_DEFAULT_OPTIONS |
| 81 | // Stringize the macro value. |
| 82 | # define ASAN_STRINGIZE(x) #x |
| 83 | # define ASAN_STRINGIZE_OPTIONS(options) ASAN_STRINGIZE(options) |
| 84 | return ASAN_STRINGIZE_OPTIONS(ASAN_DEFAULT_OPTIONS); |
| 85 | #else |
| 86 | return ""; |
| 87 | #endif |
| 88 | } |
| 89 | |
Alexey Samsonov | cb8c4dc | 2012-07-09 14:36:04 +0000 | [diff] [blame] | 90 | static void ParseFlagsFromString(Flags *f, const char *str) { |
Sergey Matveev | ed20ebe | 2013-05-06 11:27:58 +0000 | [diff] [blame] | 91 | ParseCommonFlagsFromString(str); |
| 92 | CHECK((uptr)common_flags()->malloc_context_size <= kStackTraceMax); |
| 93 | |
Alexey Samsonov | cb8c4dc | 2012-07-09 14:36:04 +0000 | [diff] [blame] | 94 | ParseFlag(str, &f->quarantine_size, "quarantine_size"); |
Alexey Samsonov | cb8c4dc | 2012-07-09 14:36:04 +0000 | [diff] [blame] | 95 | ParseFlag(str, &f->redzone, "redzone"); |
Kostya Serebryany | a27bdf7 | 2013-04-05 14:40:25 +0000 | [diff] [blame] | 96 | CHECK_GE(f->redzone, 16); |
Alexey Samsonov | cb8c4dc | 2012-07-09 14:36:04 +0000 | [diff] [blame] | 97 | CHECK(IsPowerOfTwo(f->redzone)); |
| 98 | |
| 99 | ParseFlag(str, &f->debug, "debug"); |
Alexey Samsonov | cb8c4dc | 2012-07-09 14:36:04 +0000 | [diff] [blame] | 100 | ParseFlag(str, &f->report_globals, "report_globals"); |
Alexey Samsonov | 6e7dca7 | 2013-03-28 09:11:20 +0000 | [diff] [blame] | 101 | ParseFlag(str, &f->check_initialization_order, "check_initialization_order"); |
Alexey Samsonov | cb8c4dc | 2012-07-09 14:36:04 +0000 | [diff] [blame] | 102 | |
| 103 | ParseFlag(str, &f->replace_str, "replace_str"); |
| 104 | ParseFlag(str, &f->replace_intrin, "replace_intrin"); |
Alexey Samsonov | cb8c4dc | 2012-07-09 14:36:04 +0000 | [diff] [blame] | 105 | ParseFlag(str, &f->mac_ignore_invalid_free, "mac_ignore_invalid_free"); |
Kostya Serebryany | 230e52f | 2013-09-18 10:35:12 +0000 | [diff] [blame] | 106 | ParseFlag(str, &f->detect_stack_use_after_return, |
| 107 | "detect_stack_use_after_return"); |
Kostya Serebryany | e1c68c3 | 2013-09-27 11:37:23 +0000 | [diff] [blame] | 108 | ParseFlag(str, &f->uar_stack_size_log, "uar_stack_size_log"); |
Alexey Samsonov | cb8c4dc | 2012-07-09 14:36:04 +0000 | [diff] [blame] | 109 | ParseFlag(str, &f->max_malloc_fill_size, "max_malloc_fill_size"); |
Kostya Serebryany | 2a3619e | 2013-04-04 11:17:14 +0000 | [diff] [blame] | 110 | ParseFlag(str, &f->malloc_fill_byte, "malloc_fill_byte"); |
Alexey Samsonov | cb8c4dc | 2012-07-09 14:36:04 +0000 | [diff] [blame] | 111 | ParseFlag(str, &f->exitcode, "exitcode"); |
| 112 | ParseFlag(str, &f->allow_user_poisoning, "allow_user_poisoning"); |
| 113 | ParseFlag(str, &f->sleep_before_dying, "sleep_before_dying"); |
| 114 | ParseFlag(str, &f->handle_segv, "handle_segv"); |
Alexey Samsonov | 332bf33 | 2013-04-25 10:52:15 +0000 | [diff] [blame] | 115 | ParseFlag(str, &f->allow_user_segv_handler, "allow_user_segv_handler"); |
Alexey Samsonov | cb8c4dc | 2012-07-09 14:36:04 +0000 | [diff] [blame] | 116 | ParseFlag(str, &f->use_sigaltstack, "use_sigaltstack"); |
Alexey Samsonov | cb8c4dc | 2012-07-09 14:36:04 +0000 | [diff] [blame] | 117 | ParseFlag(str, &f->check_malloc_usable_size, "check_malloc_usable_size"); |
| 118 | ParseFlag(str, &f->unmap_shadow_on_exit, "unmap_shadow_on_exit"); |
| 119 | ParseFlag(str, &f->abort_on_error, "abort_on_error"); |
Kostya Serebryany | 95f630a | 2013-01-28 07:34:22 +0000 | [diff] [blame] | 120 | ParseFlag(str, &f->print_stats, "print_stats"); |
| 121 | ParseFlag(str, &f->print_legend, "print_legend"); |
Alexey Samsonov | cb8c4dc | 2012-07-09 14:36:04 +0000 | [diff] [blame] | 122 | ParseFlag(str, &f->atexit, "atexit"); |
Bob Wilson | dbd69cc | 2013-11-15 07:18:15 +0000 | [diff] [blame] | 123 | ParseFlag(str, &f->coverage, "coverage"); |
Alexey Samsonov | cb8c4dc | 2012-07-09 14:36:04 +0000 | [diff] [blame] | 124 | ParseFlag(str, &f->disable_core, "disable_core"); |
Alexander Potapenko | eb8c46e | 2012-08-24 09:22:05 +0000 | [diff] [blame] | 125 | ParseFlag(str, &f->allow_reexec, "allow_reexec"); |
Alexey Samsonov | 71b42c9 | 2012-09-05 07:37:15 +0000 | [diff] [blame] | 126 | ParseFlag(str, &f->print_full_thread_history, "print_full_thread_history"); |
Kostya Serebryany | 73bad81 | 2012-12-20 11:54:21 +0000 | [diff] [blame] | 127 | ParseFlag(str, &f->poison_heap, "poison_heap"); |
Kostya Serebryany | cca8e78 | 2013-10-16 13:49:01 +0000 | [diff] [blame] | 128 | ParseFlag(str, &f->poison_partial, "poison_partial"); |
Kostya Serebryany | fe6d916 | 2012-12-21 08:53:59 +0000 | [diff] [blame] | 129 | ParseFlag(str, &f->alloc_dealloc_mismatch, "alloc_dealloc_mismatch"); |
Alexander Potapenko | 8bd5e74 | 2013-02-28 14:09:30 +0000 | [diff] [blame] | 130 | ParseFlag(str, &f->strict_memcmp, "strict_memcmp"); |
Alexey Samsonov | dfeef67 | 2013-04-19 08:35:16 +0000 | [diff] [blame] | 131 | ParseFlag(str, &f->strict_init_order, "strict_init_order"); |
Alexey Samsonov | cb8c4dc | 2012-07-09 14:36:04 +0000 | [diff] [blame] | 132 | } |
| 133 | |
| 134 | void InitializeFlags(Flags *f, const char *env) { |
Sergey Matveev | ed20ebe | 2013-05-06 11:27:58 +0000 | [diff] [blame] | 135 | CommonFlags *cf = common_flags(); |
Alexey Samsonov | 55c7947 | 2013-11-12 13:59:08 +0000 | [diff] [blame] | 136 | SetCommonFlagDefaults(); |
Sergey Matveev | ed20ebe | 2013-05-06 11:27:58 +0000 | [diff] [blame] | 137 | cf->external_symbolizer_path = GetEnv("ASAN_SYMBOLIZER_PATH"); |
Sergey Matveev | ed20ebe | 2013-05-06 11:27:58 +0000 | [diff] [blame] | 138 | cf->malloc_context_size = kDefaultMallocContextSize; |
Alexey Samsonov | cb8c4dc | 2012-07-09 14:36:04 +0000 | [diff] [blame] | 139 | |
Sergey Matveev | ed20ebe | 2013-05-06 11:27:58 +0000 | [diff] [blame] | 140 | internal_memset(f, 0, sizeof(*f)); |
Evgeniy Stepanov | 7315c26 | 2012-09-07 12:13:52 +0000 | [diff] [blame] | 141 | f->quarantine_size = (ASAN_LOW_MEMORY) ? 1UL << 26 : 1UL << 28; |
Kostya Serebryany | b1971ca | 2013-04-04 11:32:49 +0000 | [diff] [blame] | 142 | f->redzone = 16; |
Alexey Samsonov | 7ed1d2b | 2012-07-10 07:41:27 +0000 | [diff] [blame] | 143 | f->debug = false; |
Alexey Samsonov | cb8c4dc | 2012-07-09 14:36:04 +0000 | [diff] [blame] | 144 | f->report_globals = 1; |
Alexey Samsonov | e9e2b7f | 2013-11-06 04:27:09 +0000 | [diff] [blame] | 145 | f->check_initialization_order = false; |
Alexey Samsonov | cb8c4dc | 2012-07-09 14:36:04 +0000 | [diff] [blame] | 146 | f->replace_str = true; |
| 147 | f->replace_intrin = true; |
Alexey Samsonov | 7ed1d2b | 2012-07-10 07:41:27 +0000 | [diff] [blame] | 148 | f->mac_ignore_invalid_free = false; |
Kostya Serebryany | b079602 | 2013-09-23 08:58:14 +0000 | [diff] [blame] | 149 | f->detect_stack_use_after_return = false; // Also needs the compiler flag. |
Kostya Serebryany | e1c68c3 | 2013-09-27 11:37:23 +0000 | [diff] [blame] | 150 | f->uar_stack_size_log = 0; |
Kostya Serebryany | 2a3619e | 2013-04-04 11:17:14 +0000 | [diff] [blame] | 151 | f->max_malloc_fill_size = 0x1000; // By default, fill only the first 4K. |
| 152 | f->malloc_fill_byte = 0xbe; |
Alexey Samsonov | cb8c4dc | 2012-07-09 14:36:04 +0000 | [diff] [blame] | 153 | f->exitcode = ASAN_DEFAULT_FAILURE_EXITCODE; |
| 154 | f->allow_user_poisoning = true; |
| 155 | f->sleep_before_dying = 0; |
| 156 | f->handle_segv = ASAN_NEEDS_SEGV; |
Alexey Samsonov | 332bf33 | 2013-04-25 10:52:15 +0000 | [diff] [blame] | 157 | f->allow_user_segv_handler = false; |
Alexey Samsonov | cb8c4dc | 2012-07-09 14:36:04 +0000 | [diff] [blame] | 158 | f->use_sigaltstack = false; |
| 159 | f->check_malloc_usable_size = true; |
| 160 | f->unmap_shadow_on_exit = false; |
| 161 | f->abort_on_error = false; |
Kostya Serebryany | 95f630a | 2013-01-28 07:34:22 +0000 | [diff] [blame] | 162 | f->print_stats = false; |
| 163 | f->print_legend = true; |
Alexey Samsonov | cb8c4dc | 2012-07-09 14:36:04 +0000 | [diff] [blame] | 164 | f->atexit = false; |
Bob Wilson | dbd69cc | 2013-11-15 07:18:15 +0000 | [diff] [blame] | 165 | f->coverage = false; |
Kostya Serebryany | 5af39e5 | 2012-11-21 12:38:58 +0000 | [diff] [blame] | 166 | f->disable_core = (SANITIZER_WORDSIZE == 64); |
Alexander Potapenko | eb8c46e | 2012-08-24 09:22:05 +0000 | [diff] [blame] | 167 | f->allow_reexec = true; |
Alexey Samsonov | 71b42c9 | 2012-09-05 07:37:15 +0000 | [diff] [blame] | 168 | f->print_full_thread_history = true; |
Kostya Serebryany | 73bad81 | 2012-12-20 11:54:21 +0000 | [diff] [blame] | 169 | f->poison_heap = true; |
Kostya Serebryany | cca8e78 | 2013-10-16 13:49:01 +0000 | [diff] [blame] | 170 | f->poison_partial = true; |
Timur Iskhodzhanov | a0e78c9 | 2013-08-16 11:04:59 +0000 | [diff] [blame] | 171 | // Turn off alloc/dealloc mismatch checker on Mac and Windows for now. |
| 172 | // TODO(glider,timurrrr): Fix known issues and enable this back. |
| 173 | f->alloc_dealloc_mismatch = (SANITIZER_MAC == 0) && (SANITIZER_WINDOWS == 0); |
Alexander Potapenko | 8bd5e74 | 2013-02-28 14:09:30 +0000 | [diff] [blame] | 174 | f->strict_memcmp = true; |
Alexey Samsonov | dfeef67 | 2013-04-19 08:35:16 +0000 | [diff] [blame] | 175 | f->strict_init_order = false; |
Alexey Samsonov | cb8c4dc | 2012-07-09 14:36:04 +0000 | [diff] [blame] | 176 | |
Alexey Samsonov | 36ea94d | 2013-02-19 13:14:48 +0000 | [diff] [blame] | 177 | // Override from compile definition. |
| 178 | ParseFlagsFromString(f, MaybeUseAsanDefaultOptionsCompileDefiniton()); |
| 179 | |
Alexey Samsonov | cb8c4dc | 2012-07-09 14:36:04 +0000 | [diff] [blame] | 180 | // Override from user-specified string. |
Alexey Samsonov | 6a08d29 | 2012-12-07 22:01:28 +0000 | [diff] [blame] | 181 | ParseFlagsFromString(f, MaybeCallAsanDefaultOptions()); |
Dmitry Vyukov | 6866dba | 2013-10-15 13:28:51 +0000 | [diff] [blame] | 182 | if (common_flags()->verbosity) { |
Alexey Samsonov | b750c4c | 2012-07-25 10:40:57 +0000 | [diff] [blame] | 183 | Report("Using the defaults from __asan_default_options: %s\n", |
Alexey Samsonov | 6a08d29 | 2012-12-07 22:01:28 +0000 | [diff] [blame] | 184 | MaybeCallAsanDefaultOptions()); |
Alexey Samsonov | cb8c4dc | 2012-07-09 14:36:04 +0000 | [diff] [blame] | 185 | } |
Alexey Samsonov | cb8c4dc | 2012-07-09 14:36:04 +0000 | [diff] [blame] | 186 | |
| 187 | // Override from command line. |
| 188 | ParseFlagsFromString(f, env); |
Sergey Matveev | 71f0411 | 2013-05-21 14:15:35 +0000 | [diff] [blame] | 189 | |
| 190 | #if !CAN_SANITIZE_LEAKS |
Sergey Matveev | 62519eb | 2013-07-22 12:38:17 +0000 | [diff] [blame] | 191 | if (cf->detect_leaks) { |
Sergey Matveev | 71f0411 | 2013-05-21 14:15:35 +0000 | [diff] [blame] | 192 | Report("%s: detect_leaks is not supported on this platform.\n", |
| 193 | SanitizerToolName); |
Sergey Matveev | 62519eb | 2013-07-22 12:38:17 +0000 | [diff] [blame] | 194 | cf->detect_leaks = false; |
Sergey Matveev | 71f0411 | 2013-05-21 14:15:35 +0000 | [diff] [blame] | 195 | } |
| 196 | #endif |
Alexey Samsonov | e9e2b7f | 2013-11-06 04:27:09 +0000 | [diff] [blame] | 197 | |
| 198 | // Make "strict_init_order" imply "check_initialization_order". |
| 199 | // TODO(samsonov): Use a single runtime flag for an init-order checker. |
| 200 | if (f->strict_init_order) { |
| 201 | f->check_initialization_order = true; |
| 202 | } |
Alexey Samsonov | cb8c4dc | 2012-07-09 14:36:04 +0000 | [diff] [blame] | 203 | } |
Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 204 | |
| 205 | // -------------------------- Globals --------------------- {{{1 |
| 206 | int asan_inited; |
| 207 | bool asan_init_is_running; |
Alexey Samsonov | 47657ce | 2012-06-06 07:02:44 +0000 | [diff] [blame] | 208 | void (*death_callback)(void); |
Kostya Serebryany | e31eca9 | 2013-02-15 12:00:24 +0000 | [diff] [blame] | 209 | |
| 210 | #if !ASAN_FIXED_MAPPING |
| 211 | uptr kHighMemEnd, kMidMemBeg, kMidMemEnd; |
| 212 | #endif |
Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 213 | |
Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 214 | // -------------------------- Misc ---------------- {{{1 |
| 215 | void ShowStatsAndAbort() { |
| 216 | __asan_print_accumulated_stats(); |
Alexey Samsonov | 47657ce | 2012-06-06 07:02:44 +0000 | [diff] [blame] | 217 | Die(); |
Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 218 | } |
| 219 | |
Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 220 | // ---------------------- mmap -------------------- {{{1 |
Kostya Serebryany | a874fe5 | 2011-12-28 23:28:54 +0000 | [diff] [blame] | 221 | // Reserve memory range [beg, end]. |
Kostya Serebryany | 3f4c387 | 2012-05-31 14:35:53 +0000 | [diff] [blame] | 222 | static void ReserveShadowMemoryRange(uptr beg, uptr end) { |
Kostya Serebryany | a27bdf7 | 2013-04-05 14:40:25 +0000 | [diff] [blame] | 223 | CHECK_EQ((beg % GetPageSizeCached()), 0); |
| 224 | CHECK_EQ(((end + 1) % GetPageSizeCached()), 0); |
Kostya Serebryany | 3f4c387 | 2012-05-31 14:35:53 +0000 | [diff] [blame] | 225 | uptr size = end - beg + 1; |
Alexey Samsonov | f607fc1 | 2012-06-14 14:42:58 +0000 | [diff] [blame] | 226 | void *res = MmapFixedNoReserve(beg, size); |
Alexander Potapenko | 5aabcb5 | 2012-09-07 15:34:40 +0000 | [diff] [blame] | 227 | if (res != (void*)beg) { |
| 228 | Report("ReserveShadowMemoryRange failed while trying to map 0x%zx bytes. " |
| 229 | "Perhaps you're using ulimit -v\n", size); |
| 230 | Abort(); |
| 231 | } |
Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 232 | } |
| 233 | |
Alexey Samsonov | 70e177e | 2012-08-27 09:30:58 +0000 | [diff] [blame] | 234 | // --------------- LowLevelAllocateCallbac ---------- {{{1 |
| 235 | static void OnLowLevelAllocate(uptr ptr, uptr size) { |
| 236 | PoisonShadow(ptr, size, kAsanInternalHeapMagic); |
Kostya Serebryany | b89567c | 2011-12-02 21:02:20 +0000 | [diff] [blame] | 237 | } |
| 238 | |
Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 239 | // -------------------------- Run-time entry ------------------- {{{1 |
Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 240 | // exported functions |
Kostya Serebryany | 51e75c4 | 2011-12-28 00:59:39 +0000 | [diff] [blame] | 241 | #define ASAN_REPORT_ERROR(type, is_write, size) \ |
Alexey Samsonov | 0a4c906 | 2012-06-05 13:50:57 +0000 | [diff] [blame] | 242 | extern "C" NOINLINE INTERFACE_ATTRIBUTE \ |
Kostya Serebryany | 3f4c387 | 2012-05-31 14:35:53 +0000 | [diff] [blame] | 243 | void __asan_report_ ## type ## size(uptr addr); \ |
| 244 | void __asan_report_ ## type ## size(uptr addr) { \ |
Kostya Serebryany | 9f311bb | 2012-03-15 01:36:00 +0000 | [diff] [blame] | 245 | GET_CALLER_PC_BP_SP; \ |
Kostya Serebryany | 51e75c4 | 2011-12-28 00:59:39 +0000 | [diff] [blame] | 246 | __asan_report_error(pc, bp, sp, addr, is_write, size); \ |
Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 247 | } |
| 248 | |
| 249 | ASAN_REPORT_ERROR(load, false, 1) |
| 250 | ASAN_REPORT_ERROR(load, false, 2) |
| 251 | ASAN_REPORT_ERROR(load, false, 4) |
| 252 | ASAN_REPORT_ERROR(load, false, 8) |
| 253 | ASAN_REPORT_ERROR(load, false, 16) |
| 254 | ASAN_REPORT_ERROR(store, true, 1) |
| 255 | ASAN_REPORT_ERROR(store, true, 2) |
| 256 | ASAN_REPORT_ERROR(store, true, 4) |
| 257 | ASAN_REPORT_ERROR(store, true, 8) |
| 258 | ASAN_REPORT_ERROR(store, true, 16) |
| 259 | |
Kostya Serebryany | 366984e | 2013-02-19 11:30:25 +0000 | [diff] [blame] | 260 | #define ASAN_REPORT_ERROR_N(type, is_write) \ |
| 261 | extern "C" NOINLINE INTERFACE_ATTRIBUTE \ |
| 262 | void __asan_report_ ## type ## _n(uptr addr, uptr size); \ |
| 263 | void __asan_report_ ## type ## _n(uptr addr, uptr size) { \ |
| 264 | GET_CALLER_PC_BP_SP; \ |
| 265 | __asan_report_error(pc, bp, sp, addr, is_write, size); \ |
| 266 | } |
| 267 | |
| 268 | ASAN_REPORT_ERROR_N(load, false) |
| 269 | ASAN_REPORT_ERROR_N(store, true) |
| 270 | |
Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 271 | // Force the linker to keep the symbols for various ASan interface functions. |
| 272 | // We want to keep those in the executable in order to let the instrumented |
| 273 | // dynamic libraries access the symbol even if it is not used by the executable |
| 274 | // itself. This should help if the build system is removing dead code at link |
| 275 | // time. |
Alexander Potapenko | 3fe9135 | 2012-02-27 14:06:48 +0000 | [diff] [blame] | 276 | static NOINLINE void force_interface_symbols() { |
Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 277 | volatile int fake_condition = 0; // prevent dead condition elimination. |
Alexander Potapenko | 448fe9a | 2012-08-09 09:46:12 +0000 | [diff] [blame] | 278 | // __asan_report_* functions are noreturn, so we need a switch to prevent |
| 279 | // the compiler from removing any of them. |
| 280 | switch (fake_condition) { |
| 281 | case 1: __asan_report_load1(0); break; |
| 282 | case 2: __asan_report_load2(0); break; |
| 283 | case 3: __asan_report_load4(0); break; |
| 284 | case 4: __asan_report_load8(0); break; |
| 285 | case 5: __asan_report_load16(0); break; |
| 286 | case 6: __asan_report_store1(0); break; |
| 287 | case 7: __asan_report_store2(0); break; |
| 288 | case 8: __asan_report_store4(0); break; |
| 289 | case 9: __asan_report_store8(0); break; |
| 290 | case 10: __asan_report_store16(0); break; |
Alexander Potapenko | 448fe9a | 2012-08-09 09:46:12 +0000 | [diff] [blame] | 291 | case 12: __asan_register_globals(0, 0); break; |
| 292 | case 13: __asan_unregister_globals(0, 0); break; |
| 293 | case 14: __asan_set_death_callback(0); break; |
| 294 | case 15: __asan_set_error_report_callback(0); break; |
| 295 | case 16: __asan_handle_no_return(); break; |
Alexander Potapenko | 5a9938d | 2012-08-09 16:05:17 +0000 | [diff] [blame] | 296 | case 17: __asan_address_is_poisoned(0); break; |
| 297 | case 18: __asan_get_allocated_size(0); break; |
| 298 | case 19: __asan_get_current_allocated_bytes(); break; |
| 299 | case 20: __asan_get_estimated_allocated_size(0); break; |
| 300 | case 21: __asan_get_free_bytes(); break; |
| 301 | case 22: __asan_get_heap_size(); break; |
| 302 | case 23: __asan_get_ownership(0); break; |
| 303 | case 24: __asan_get_unmapped_bytes(); break; |
| 304 | case 25: __asan_poison_memory_region(0, 0); break; |
| 305 | case 26: __asan_unpoison_memory_region(0, 0); break; |
| 306 | case 27: __asan_set_error_exit_code(0); break; |
Alexey Samsonov | 05e16a0 | 2013-03-26 13:06:12 +0000 | [diff] [blame] | 307 | case 30: __asan_before_dynamic_init(0); break; |
Alexey Samsonov | 6a08d29 | 2012-12-07 22:01:28 +0000 | [diff] [blame] | 308 | case 31: __asan_after_dynamic_init(); break; |
| 309 | case 32: __asan_poison_stack_memory(0, 0); break; |
| 310 | case 33: __asan_unpoison_stack_memory(0, 0); break; |
Kostya Serebryany | eb28093 | 2012-12-28 15:24:16 +0000 | [diff] [blame] | 311 | case 34: __asan_region_is_poisoned(0, 0); break; |
Kostya Serebryany | 17a7c67 | 2012-12-29 10:18:31 +0000 | [diff] [blame] | 312 | case 35: __asan_describe_address(0); break; |
Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 313 | } |
| 314 | } |
| 315 | |
Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 316 | static void asan_atexit() { |
Kostya Serebryany | 283c296 | 2012-08-28 11:34:40 +0000 | [diff] [blame] | 317 | Printf("AddressSanitizer exit stats:\n"); |
Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 318 | __asan_print_accumulated_stats(); |
Kostya Serebryany | e31eca9 | 2013-02-15 12:00:24 +0000 | [diff] [blame] | 319 | // Print AsanMappingProfile. |
| 320 | for (uptr i = 0; i < kAsanMappingProfileSize; i++) { |
| 321 | if (AsanMappingProfile[i] == 0) continue; |
| 322 | Printf("asan_mapping.h:%zd -- %zd\n", i, AsanMappingProfile[i]); |
| 323 | } |
Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 324 | } |
| 325 | |
Kostya Serebryany | e5ab968 | 2013-01-23 13:27:43 +0000 | [diff] [blame] | 326 | static void InitializeHighMemEnd() { |
Kostya Serebryany | e31eca9 | 2013-02-15 12:00:24 +0000 | [diff] [blame] | 327 | #if !ASAN_FIXED_MAPPING |
Timur Iskhodzhanov | bb7f2d8 | 2013-07-16 09:47:39 +0000 | [diff] [blame] | 328 | kHighMemEnd = GetMaxVirtualAddress(); |
| 329 | // Increase kHighMemEnd to make sure it's properly |
| 330 | // aligned together with kHighMemBeg: |
| 331 | kHighMemEnd |= SHADOW_GRANULARITY * GetPageSizeCached() - 1; |
Kostya Serebryany | e31eca9 | 2013-02-15 12:00:24 +0000 | [diff] [blame] | 332 | #endif // !ASAN_FIXED_MAPPING |
Timur Iskhodzhanov | bb7f2d8 | 2013-07-16 09:47:39 +0000 | [diff] [blame] | 333 | CHECK_EQ((kHighMemBeg % GetPageSizeCached()), 0); |
Kostya Serebryany | e31eca9 | 2013-02-15 12:00:24 +0000 | [diff] [blame] | 334 | } |
| 335 | |
| 336 | static void ProtectGap(uptr a, uptr size) { |
| 337 | CHECK_EQ(a, (uptr)Mprotect(a, size)); |
| 338 | } |
| 339 | |
| 340 | static void PrintAddressSpaceLayout() { |
| 341 | Printf("|| `[%p, %p]` || HighMem ||\n", |
| 342 | (void*)kHighMemBeg, (void*)kHighMemEnd); |
| 343 | Printf("|| `[%p, %p]` || HighShadow ||\n", |
| 344 | (void*)kHighShadowBeg, (void*)kHighShadowEnd); |
| 345 | if (kMidMemBeg) { |
| 346 | Printf("|| `[%p, %p]` || ShadowGap3 ||\n", |
| 347 | (void*)kShadowGap3Beg, (void*)kShadowGap3End); |
| 348 | Printf("|| `[%p, %p]` || MidMem ||\n", |
| 349 | (void*)kMidMemBeg, (void*)kMidMemEnd); |
| 350 | Printf("|| `[%p, %p]` || ShadowGap2 ||\n", |
| 351 | (void*)kShadowGap2Beg, (void*)kShadowGap2End); |
| 352 | Printf("|| `[%p, %p]` || MidShadow ||\n", |
| 353 | (void*)kMidShadowBeg, (void*)kMidShadowEnd); |
| 354 | } |
| 355 | Printf("|| `[%p, %p]` || ShadowGap ||\n", |
| 356 | (void*)kShadowGapBeg, (void*)kShadowGapEnd); |
| 357 | if (kLowShadowBeg) { |
| 358 | Printf("|| `[%p, %p]` || LowShadow ||\n", |
| 359 | (void*)kLowShadowBeg, (void*)kLowShadowEnd); |
| 360 | Printf("|| `[%p, %p]` || LowMem ||\n", |
| 361 | (void*)kLowMemBeg, (void*)kLowMemEnd); |
| 362 | } |
| 363 | Printf("MemToShadow(shadow): %p %p %p %p", |
| 364 | (void*)MEM_TO_SHADOW(kLowShadowBeg), |
| 365 | (void*)MEM_TO_SHADOW(kLowShadowEnd), |
| 366 | (void*)MEM_TO_SHADOW(kHighShadowBeg), |
| 367 | (void*)MEM_TO_SHADOW(kHighShadowEnd)); |
| 368 | if (kMidMemBeg) { |
| 369 | Printf(" %p %p", |
| 370 | (void*)MEM_TO_SHADOW(kMidShadowBeg), |
| 371 | (void*)MEM_TO_SHADOW(kMidShadowEnd)); |
| 372 | } |
| 373 | Printf("\n"); |
| 374 | Printf("red_zone=%zu\n", (uptr)flags()->redzone); |
Kostya Serebryany | 34e3ed1 | 2013-09-10 13:16:26 +0000 | [diff] [blame] | 375 | Printf("quarantine_size=%zuM\n", (uptr)flags()->quarantine_size >> 20); |
Sergey Matveev | ed20ebe | 2013-05-06 11:27:58 +0000 | [diff] [blame] | 376 | Printf("malloc_context_size=%zu\n", |
| 377 | (uptr)common_flags()->malloc_context_size); |
Kostya Serebryany | e31eca9 | 2013-02-15 12:00:24 +0000 | [diff] [blame] | 378 | |
| 379 | Printf("SHADOW_SCALE: %zx\n", (uptr)SHADOW_SCALE); |
| 380 | Printf("SHADOW_GRANULARITY: %zx\n", (uptr)SHADOW_GRANULARITY); |
| 381 | Printf("SHADOW_OFFSET: %zx\n", (uptr)SHADOW_OFFSET); |
| 382 | CHECK(SHADOW_SCALE >= 3 && SHADOW_SCALE <= 7); |
| 383 | if (kMidMemBeg) |
| 384 | CHECK(kMidShadowBeg > kLowShadowEnd && |
| 385 | kMidMemBeg > kMidShadowEnd && |
| 386 | kHighShadowBeg > kMidMemEnd); |
Kostya Serebryany | e5ab968 | 2013-01-23 13:27:43 +0000 | [diff] [blame] | 387 | } |
| 388 | |
Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 389 | } // namespace __asan |
| 390 | |
Kostya Serebryany | 4803ab9 | 2012-01-09 18:53:15 +0000 | [diff] [blame] | 391 | // ---------------------- Interface ---------------- {{{1 |
Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 392 | using namespace __asan; // NOLINT |
| 393 | |
Alexey Samsonov | 6a08d29 | 2012-12-07 22:01:28 +0000 | [diff] [blame] | 394 | #if !SANITIZER_SUPPORTS_WEAK_HOOKS |
| 395 | extern "C" { |
Timur Iskhodzhanov | 3c80c6c | 2013-08-13 11:42:45 +0000 | [diff] [blame] | 396 | SANITIZER_INTERFACE_ATTRIBUTE SANITIZER_WEAK_ATTRIBUTE |
Alexey Samsonov | 6a08d29 | 2012-12-07 22:01:28 +0000 | [diff] [blame] | 397 | const char* __asan_default_options() { return ""; } |
| 398 | } // extern "C" |
| 399 | #endif |
| 400 | |
Alexander Potapenko | 5a9938d | 2012-08-09 16:05:17 +0000 | [diff] [blame] | 401 | int NOINLINE __asan_set_error_exit_code(int exit_code) { |
Alexey Samsonov | cb8c4dc | 2012-07-09 14:36:04 +0000 | [diff] [blame] | 402 | int old = flags()->exitcode; |
| 403 | flags()->exitcode = exit_code; |
Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 404 | return old; |
| 405 | } |
| 406 | |
Alexander Potapenko | dadc45d | 2012-03-06 11:45:59 +0000 | [diff] [blame] | 407 | void NOINLINE __asan_handle_no_return() { |
Kostya Serebryany | f54b1f9 | 2012-02-08 21:33:27 +0000 | [diff] [blame] | 408 | int local_stack; |
Alexey Samsonov | 89c1384 | 2013-03-20 09:23:28 +0000 | [diff] [blame] | 409 | AsanThread *curr_thread = GetCurrentThread(); |
Kostya Serebryany | f54b1f9 | 2012-02-08 21:33:27 +0000 | [diff] [blame] | 410 | CHECK(curr_thread); |
Kostya Serebryany | f67ec2b | 2012-11-23 15:38:49 +0000 | [diff] [blame] | 411 | uptr PageSize = GetPageSizeCached(); |
Kostya Serebryany | 3f4c387 | 2012-05-31 14:35:53 +0000 | [diff] [blame] | 412 | uptr top = curr_thread->stack_top(); |
Kostya Serebryany | f67ec2b | 2012-11-23 15:38:49 +0000 | [diff] [blame] | 413 | uptr bottom = ((uptr)&local_stack - PageSize) & ~(PageSize-1); |
Kostya Serebryany | 30e970f | 2013-05-22 08:54:30 +0000 | [diff] [blame] | 414 | static const uptr kMaxExpectedCleanupSize = 64 << 20; // 64M |
| 415 | if (top - bottom > kMaxExpectedCleanupSize) { |
| 416 | static bool reported_warning = false; |
| 417 | if (reported_warning) |
| 418 | return; |
| 419 | reported_warning = true; |
| 420 | Report("WARNING: ASan is ignoring requested __asan_handle_no_return: " |
| 421 | "stack top: %p; bottom %p; size: %p (%zd)\n" |
| 422 | "False positive error reports may follow\n" |
| 423 | "For details see " |
| 424 | "http://code.google.com/p/address-sanitizer/issues/detail?id=189\n", |
| 425 | top, bottom, top - bottom, top - bottom); |
| 426 | return; |
| 427 | } |
Kostya Serebryany | f54b1f9 | 2012-02-08 21:33:27 +0000 | [diff] [blame] | 428 | PoisonShadow(bottom, top - bottom, 0); |
Kostya Serebryany | 89de457 | 2013-09-12 13:25:29 +0000 | [diff] [blame] | 429 | if (curr_thread->has_fake_stack()) |
| 430 | curr_thread->fake_stack()->HandleNoReturn(); |
Kostya Serebryany | f54b1f9 | 2012-02-08 21:33:27 +0000 | [diff] [blame] | 431 | } |
| 432 | |
Alexander Potapenko | 2f3f962 | 2012-03-01 14:39:21 +0000 | [diff] [blame] | 433 | void NOINLINE __asan_set_death_callback(void (*callback)(void)) { |
Kostya Serebryany | e1fe0fd | 2012-02-13 21:24:29 +0000 | [diff] [blame] | 434 | death_callback = callback; |
| 435 | } |
| 436 | |
Alexander Potapenko | fca72fd | 2012-05-25 15:37:16 +0000 | [diff] [blame] | 437 | void __asan_init() { |
| 438 | if (asan_inited) return; |
Kostya Serebryany | 859778a | 2013-01-31 14:11:21 +0000 | [diff] [blame] | 439 | SanitizerToolName = "AddressSanitizer"; |
Alexey Samsonov | 70e177e | 2012-08-27 09:30:58 +0000 | [diff] [blame] | 440 | CHECK(!asan_init_is_running && "ASan init calls itself!"); |
Alexander Potapenko | fca72fd | 2012-05-25 15:37:16 +0000 | [diff] [blame] | 441 | asan_init_is_running = true; |
Kostya Serebryany | e5ab968 | 2013-01-23 13:27:43 +0000 | [diff] [blame] | 442 | InitializeHighMemEnd(); |
Alexander Potapenko | fca72fd | 2012-05-25 15:37:16 +0000 | [diff] [blame] | 443 | |
| 444 | // Make sure we are not statically linked. |
| 445 | AsanDoesNotSupportStaticLinkage(); |
| 446 | |
Alexey Samsonov | 591616d | 2012-09-11 09:44:48 +0000 | [diff] [blame] | 447 | // Install tool-specific callbacks in sanitizer_common. |
| 448 | SetDieCallback(AsanDie); |
| 449 | SetCheckFailedCallback(AsanCheckFailed); |
Kostya Serebryany | 283c296 | 2012-08-28 11:34:40 +0000 | [diff] [blame] | 450 | SetPrintfAndReportCallback(AppendToErrorMessageBuffer); |
| 451 | |
Alexander Potapenko | eb8c46e | 2012-08-24 09:22:05 +0000 | [diff] [blame] | 452 | // Initialize flags. This must be done early, because most of the |
| 453 | // initialization steps look at flags(). |
Alexey Samsonov | 3dbeabb | 2012-06-14 14:07:21 +0000 | [diff] [blame] | 454 | const char *options = GetEnv("ASAN_OPTIONS"); |
Alexey Samsonov | cb8c4dc | 2012-07-09 14:36:04 +0000 | [diff] [blame] | 455 | InitializeFlags(flags(), options); |
Evgeniy Stepanov | b624606 | 2013-06-25 13:50:44 +0000 | [diff] [blame] | 456 | __sanitizer_set_report_path(common_flags()->log_path); |
Kostya Serebryany | 230e52f | 2013-09-18 10:35:12 +0000 | [diff] [blame] | 457 | __asan_option_detect_stack_use_after_return = |
| 458 | flags()->detect_stack_use_after_return; |
Alexander Potapenko | feb4793 | 2012-03-16 16:38:31 +0000 | [diff] [blame] | 459 | |
Dmitry Vyukov | 6866dba | 2013-10-15 13:28:51 +0000 | [diff] [blame] | 460 | if (common_flags()->verbosity && options) { |
Alexander Potapenko | feb4793 | 2012-03-16 16:38:31 +0000 | [diff] [blame] | 461 | Report("Parsed ASAN_OPTIONS: %s\n", options); |
| 462 | } |
| 463 | |
Alexander Potapenko | eb8c46e | 2012-08-24 09:22:05 +0000 | [diff] [blame] | 464 | // Re-exec ourselves if we need to set additional env or command line args. |
| 465 | MaybeReexec(); |
| 466 | |
Alexey Samsonov | 947fbd1 | 2012-08-27 14:04:54 +0000 | [diff] [blame] | 467 | // Setup internal allocator callback. |
| 468 | SetLowLevelAllocateCallback(OnLowLevelAllocate); |
| 469 | |
Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 470 | InitializeAsanInterceptors(); |
| 471 | |
| 472 | ReplaceSystemMalloc(); |
Alexey Samsonov | 4d5f98d | 2012-04-06 08:21:08 +0000 | [diff] [blame] | 473 | ReplaceOperatorsNewAndDelete(); |
Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 474 | |
Kostya Serebryany | e31eca9 | 2013-02-15 12:00:24 +0000 | [diff] [blame] | 475 | uptr shadow_start = kLowShadowBeg; |
Timur Iskhodzhanov | bb7f2d8 | 2013-07-16 09:47:39 +0000 | [diff] [blame] | 476 | if (kLowShadowBeg) |
| 477 | shadow_start -= GetMmapGranularity(); |
Kostya Serebryany | e31eca9 | 2013-02-15 12:00:24 +0000 | [diff] [blame] | 478 | bool full_shadow_is_available = |
Timur Iskhodzhanov | bb7f2d8 | 2013-07-16 09:47:39 +0000 | [diff] [blame] | 479 | MemoryRangeIsAvailable(shadow_start, kHighShadowEnd); |
Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 480 | |
Evgeniy Stepanov | 83cb787 | 2013-03-19 13:54:41 +0000 | [diff] [blame] | 481 | #if SANITIZER_LINUX && defined(__x86_64__) && !ASAN_FIXED_MAPPING |
Kostya Serebryany | e31eca9 | 2013-02-15 12:00:24 +0000 | [diff] [blame] | 482 | if (!full_shadow_is_available) { |
| 483 | kMidMemBeg = kLowMemEnd < 0x3000000000ULL ? 0x3000000000ULL : 0; |
Kostya Serebryany | 13577fe | 2013-02-28 12:28:37 +0000 | [diff] [blame] | 484 | kMidMemEnd = kLowMemEnd < 0x3000000000ULL ? 0x4fffffffffULL : 0; |
Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 485 | } |
Kostya Serebryany | e31eca9 | 2013-02-15 12:00:24 +0000 | [diff] [blame] | 486 | #endif |
| 487 | |
Dmitry Vyukov | 6866dba | 2013-10-15 13:28:51 +0000 | [diff] [blame] | 488 | if (common_flags()->verbosity) |
Kostya Serebryany | e31eca9 | 2013-02-15 12:00:24 +0000 | [diff] [blame] | 489 | PrintAddressSpaceLayout(); |
Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 490 | |
Alexey Samsonov | cb8c4dc | 2012-07-09 14:36:04 +0000 | [diff] [blame] | 491 | if (flags()->disable_core) { |
Alexey Samsonov | be7420c | 2012-06-15 06:08:19 +0000 | [diff] [blame] | 492 | DisableCoreDumper(); |
Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 493 | } |
| 494 | |
Kostya Serebryany | e31eca9 | 2013-02-15 12:00:24 +0000 | [diff] [blame] | 495 | if (full_shadow_is_available) { |
| 496 | // mmap the low shadow plus at least one page at the left. |
| 497 | if (kLowShadowBeg) |
| 498 | ReserveShadowMemoryRange(shadow_start, kLowShadowEnd); |
Kostya Serebryany | a7e760a | 2012-01-09 19:18:27 +0000 | [diff] [blame] | 499 | // mmap the high shadow. |
| 500 | ReserveShadowMemoryRange(kHighShadowBeg, kHighShadowEnd); |
Kostya Serebryany | e31eca9 | 2013-02-15 12:00:24 +0000 | [diff] [blame] | 501 | // protect the gap. |
| 502 | ProtectGap(kShadowGapBeg, kShadowGapEnd - kShadowGapBeg + 1); |
| 503 | } else if (kMidMemBeg && |
| 504 | MemoryRangeIsAvailable(shadow_start, kMidMemBeg - 1) && |
Timur Iskhodzhanov | bb7f2d8 | 2013-07-16 09:47:39 +0000 | [diff] [blame] | 505 | MemoryRangeIsAvailable(kMidMemEnd + 1, kHighShadowEnd)) { |
Kostya Serebryany | e31eca9 | 2013-02-15 12:00:24 +0000 | [diff] [blame] | 506 | CHECK(kLowShadowBeg != kLowShadowEnd); |
| 507 | // mmap the low shadow plus at least one page at the left. |
| 508 | ReserveShadowMemoryRange(shadow_start, kLowShadowEnd); |
| 509 | // mmap the mid shadow. |
| 510 | ReserveShadowMemoryRange(kMidShadowBeg, kMidShadowEnd); |
| 511 | // mmap the high shadow. |
| 512 | ReserveShadowMemoryRange(kHighShadowBeg, kHighShadowEnd); |
| 513 | // protect the gaps. |
| 514 | ProtectGap(kShadowGapBeg, kShadowGapEnd - kShadowGapBeg + 1); |
| 515 | ProtectGap(kShadowGap2Beg, kShadowGap2End - kShadowGap2Beg + 1); |
| 516 | ProtectGap(kShadowGap3Beg, kShadowGap3End - kShadowGap3Beg + 1); |
Alexander Potapenko | c50e835 | 2012-02-13 15:11:23 +0000 | [diff] [blame] | 517 | } else { |
| 518 | Report("Shadow memory range interleaves with an existing memory mapping. " |
| 519 | "ASan cannot proceed correctly. ABORTING.\n"); |
Alexey Samsonov | be7420c | 2012-06-15 06:08:19 +0000 | [diff] [blame] | 520 | DumpProcessMap(); |
Alexey Samsonov | 47657ce | 2012-06-06 07:02:44 +0000 | [diff] [blame] | 521 | Die(); |
Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 522 | } |
| 523 | |
Kostya Serebryany | 13521a2 | 2013-10-17 13:18:21 +0000 | [diff] [blame] | 524 | AsanTSDInit(PlatformTSDDtor); |
Alexander Potapenko | f03d8af | 2012-04-05 10:54:52 +0000 | [diff] [blame] | 525 | InstallSignalHandlers(); |
Alexey Samsonov | d64bcf4 | 2013-06-11 08:13:36 +0000 | [diff] [blame] | 526 | |
Alexey Samsonov | d64bcf4 | 2013-06-11 08:13:36 +0000 | [diff] [blame] | 527 | // Allocator should be initialized before starting external symbolizer, as |
| 528 | // fork() on Mac locks the allocator. |
| 529 | InitializeAllocator(); |
| 530 | |
Alexey Samsonov | 9c6e530 | 2012-08-23 07:32:06 +0000 | [diff] [blame] | 531 | // Start symbolizer process if necessary. |
Peter Collingbourne | c1a1ed6 | 2013-10-25 23:03:29 +0000 | [diff] [blame] | 532 | if (common_flags()->symbolize) { |
| 533 | Symbolizer::Init(common_flags()->external_symbolizer_path); |
| 534 | } else { |
| 535 | Symbolizer::Disable(); |
Alexey Samsonov | 9c6e530 | 2012-08-23 07:32:06 +0000 | [diff] [blame] | 536 | } |
Alexander Potapenko | f03d8af | 2012-04-05 10:54:52 +0000 | [diff] [blame] | 537 | |
Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 538 | // On Linux AsanThread::ThreadStart() calls malloc() that's why asan_inited |
| 539 | // should be set to 1 prior to initializing the threads. |
| 540 | asan_inited = 1; |
| 541 | asan_init_is_running = false; |
| 542 | |
Kostya Serebryany | a2e70d9 | 2013-10-18 07:57:59 +0000 | [diff] [blame] | 543 | if (flags()->atexit) |
| 544 | Atexit(asan_atexit); |
| 545 | |
Bob Wilson | dbd69cc | 2013-11-15 07:18:15 +0000 | [diff] [blame] | 546 | if (flags()->coverage) |
| 547 | Atexit(__sanitizer_cov_dump); |
| 548 | |
Kostya Serebryany | a2e70d9 | 2013-10-18 07:57:59 +0000 | [diff] [blame] | 549 | // interceptors |
Sergey Matveev | 12d01ba | 2013-05-29 13:09:44 +0000 | [diff] [blame] | 550 | InitTlsSize(); |
| 551 | |
Alexey Samsonov | def1be9 | 2013-03-21 11:23:41 +0000 | [diff] [blame] | 552 | // Create main thread. |
Alexey Samsonov | def1be9 | 2013-03-21 11:23:41 +0000 | [diff] [blame] | 553 | AsanThread *main_thread = AsanThread::Create(0, 0); |
| 554 | CreateThreadContextArgs create_main_args = { main_thread, 0 }; |
| 555 | u32 main_tid = asanThreadRegistry().CreateThread( |
| 556 | 0, true, 0, &create_main_args); |
| 557 | CHECK_EQ(0, main_tid); |
| 558 | SetCurrentThread(main_thread); |
Peter Collingbourne | 0b694fc | 2013-05-17 16:56:53 +0000 | [diff] [blame] | 559 | main_thread->ThreadStart(internal_getpid()); |
Kostya Serebryany | 51e75c4 | 2011-12-28 00:59:39 +0000 | [diff] [blame] | 560 | force_interface_symbols(); // no-op. |
Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 561 | |
Alexey Samsonov | 975a329 | 2013-05-22 13:20:37 +0000 | [diff] [blame] | 562 | #if CAN_SANITIZE_LEAKS |
Sergey Matveev | 71f0411 | 2013-05-21 14:15:35 +0000 | [diff] [blame] | 563 | __lsan::InitCommonLsan(); |
Sergey Matveev | 41f1540 | 2013-08-01 14:57:07 +0000 | [diff] [blame] | 564 | if (common_flags()->detect_leaks && common_flags()->leak_check_at_exit) { |
Sergey Matveev | 4cacb4f | 2013-05-21 17:56:45 +0000 | [diff] [blame] | 565 | Atexit(__lsan::DoLeakCheck); |
| 566 | } |
Alexey Samsonov | 975a329 | 2013-05-22 13:20:37 +0000 | [diff] [blame] | 567 | #endif // CAN_SANITIZE_LEAKS |
Sergey Matveev | 71f0411 | 2013-05-21 14:15:35 +0000 | [diff] [blame] | 568 | |
Dmitry Vyukov | 6866dba | 2013-10-15 13:28:51 +0000 | [diff] [blame] | 569 | if (common_flags()->verbosity) { |
Kostya Serebryany | d6567c5 | 2011-12-01 21:40:52 +0000 | [diff] [blame] | 570 | Report("AddressSanitizer Init done\n"); |
Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 571 | } |
| 572 | } |