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