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