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