blob: df5c1fe3650d909579a7a1e1e9196437d91dc4c0 [file] [log] [blame]
Alexey Samsonove5f58952012-06-04 13:50:10 +00001//===-- asan_rtl.cc -------------------------------------------------------===//
Kostya Serebryany1e172b42011-11-30 01:07:02 +00002//
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 Serebryany1e172b42011-11-30 01:07:02 +000019#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"
Alexey Samsonov9552db72012-06-05 07:25:47 +000024#include "sanitizer_common/sanitizer_libc.h"
Kostya Serebryany1e172b42011-11-30 01:07:02 +000025
Alexey Samsonov47657ce2012-06-06 07:02:44 +000026namespace __sanitizer {
27using namespace __asan;
28
29void Die() {
30 static int num_calls = 0;
31 if (AtomicInc(&num_calls) > 1) {
32 // Don't die twice - run a busy loop.
33 while (1) { }
34 }
35 if (FLAG_sleep_before_dying) {
Alexey Samsonov5bcca4e2012-06-06 10:46:00 +000036 Report("Sleeping for %zd second(s)\n", FLAG_sleep_before_dying);
Alexey Samsonov47657ce2012-06-06 07:02:44 +000037 SleepForSeconds(FLAG_sleep_before_dying);
38 }
39 if (FLAG_unmap_shadow_on_exit)
Alexey Samsonova25b3462012-06-06 16:15:07 +000040 UnmapOrDie((void*)kLowShadowBeg, kHighShadowEnd - kLowShadowBeg);
Alexey Samsonov47657ce2012-06-06 07:02:44 +000041 if (death_callback)
42 death_callback();
43 if (FLAG_abort_on_error)
44 Abort();
45 Exit(FLAG_exitcode);
46}
47
Alexey Samsonov15a77612012-06-06 15:22:20 +000048void CheckFailed(const char *file, int line, const char *cond, u64 v1, u64 v2) {
49 AsanReport("AddressSanitizer CHECK failed: %s:%d \"%s\" (%zx, %zx)\n",
50 file, line, cond, (uptr)v1, (uptr)v2);
51 PRINT_CURRENT_STACK();
52 ShowStatsAndAbort();
53}
54
Alexey Samsonov47657ce2012-06-06 07:02:44 +000055} // namespace __sanitizer
56
Kostya Serebryany1e172b42011-11-30 01:07:02 +000057namespace __asan {
58
59// -------------------------- Flags ------------------------- {{{1
Kostya Serebryany3f4c3872012-05-31 14:35:53 +000060static const uptr kMallocContextSize = 30;
Kostya Serebryany1e172b42011-11-30 01:07:02 +000061
Kostya Serebryany3f4c3872012-05-31 14:35:53 +000062uptr FLAG_malloc_context_size = kMallocContextSize;
63uptr FLAG_max_malloc_fill_size = 0;
Kostya Serebryanyee392552012-05-31 15:02:07 +000064s64 FLAG_v = 0;
Kostya Serebryany3f4c3872012-05-31 14:35:53 +000065uptr FLAG_redzone = (ASAN_LOW_MEMORY) ? 64 : 128; // power of two, >= 32
66uptr FLAG_quarantine_size = (ASAN_LOW_MEMORY) ? 1UL << 24 : 1UL << 28;
Kostya Serebryanyee392552012-05-31 15:02:07 +000067static s64 FLAG_atexit = 0;
Alexander Potapenko62f10e72012-05-28 16:21:19 +000068bool FLAG_poison_shadow = 1;
Kostya Serebryanyee392552012-05-31 15:02:07 +000069s64 FLAG_report_globals = 1;
Alexander Potapenko62f10e72012-05-28 16:21:19 +000070bool FLAG_handle_segv = ASAN_NEEDS_SEGV;
71bool FLAG_use_sigaltstack = 0;
Alexey Samsonov5f2fe372012-06-04 11:20:17 +000072bool FLAG_symbolize = 0;
Kostya Serebryanyee392552012-05-31 15:02:07 +000073s64 FLAG_demangle = 1;
74s64 FLAG_debug = 0;
Alexander Potapenko62f10e72012-05-28 16:21:19 +000075bool FLAG_replace_cfallocator = 1; // Used on Mac only.
76bool FLAG_replace_str = 1;
77bool FLAG_replace_intrin = 1;
78bool FLAG_use_fake_stack = 1;
Kostya Serebryanyee392552012-05-31 15:02:07 +000079s64 FLAG_exitcode = ASAN_DEFAULT_FAILURE_EXITCODE;
Alexander Potapenko62f10e72012-05-28 16:21:19 +000080bool FLAG_allow_user_poisoning = 1;
Kostya Serebryanyee392552012-05-31 15:02:07 +000081s64 FLAG_sleep_before_dying = 0;
Alexander Potapenko62f10e72012-05-28 16:21:19 +000082bool FLAG_abort_on_error = 0;
83bool FLAG_unmap_shadow_on_exit = 0;
84bool FLAG_disable_core = __WORDSIZE == 64;
85bool FLAG_check_malloc_usable_size = 1;
Kostya Serebryany1e172b42011-11-30 01:07:02 +000086
87// -------------------------- Globals --------------------- {{{1
88int asan_inited;
89bool asan_init_is_running;
Alexey Samsonov47657ce2012-06-06 07:02:44 +000090void (*death_callback)(void);
Alexander Potapenko3fe91352012-02-27 14:06:48 +000091static void (*error_report_callback)(const char*);
Kostya Serebryany3f4c3872012-05-31 14:35:53 +000092char *error_message_buffer = 0;
93uptr error_message_buffer_pos = 0;
94uptr error_message_buffer_size = 0;
Kostya Serebryany1e172b42011-11-30 01:07:02 +000095
Kostya Serebryany1e172b42011-11-30 01:07:02 +000096// -------------------------- Misc ---------------- {{{1
97void ShowStatsAndAbort() {
98 __asan_print_accumulated_stats();
Alexey Samsonov47657ce2012-06-06 07:02:44 +000099 Die();
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000100}
101
Kostya Serebryany3f4c3872012-05-31 14:35:53 +0000102static void PrintBytes(const char *before, uptr *a) {
Kostya Serebryanyee392552012-05-31 15:02:07 +0000103 u8 *bytes = (u8*)a;
Kostya Serebryany3f4c3872012-05-31 14:35:53 +0000104 uptr byte_num = (__WORDSIZE) / 8;
Alexey Samsonove9541012012-06-06 13:11:29 +0000105 AsanPrintf("%s%p:", before, (void*)a);
Kostya Serebryany3f4c3872012-05-31 14:35:53 +0000106 for (uptr i = 0; i < byte_num; i++) {
Alexey Samsonove9541012012-06-06 13:11:29 +0000107 AsanPrintf(" %x%x", bytes[i] >> 4, bytes[i] & 15);
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000108 }
Alexey Samsonove9541012012-06-06 13:11:29 +0000109 AsanPrintf("\n");
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000110}
111
Kostya Serebryany3f4c3872012-05-31 14:35:53 +0000112uptr ReadFileToBuffer(const char *file_name, char **buff,
113 uptr *buff_size, uptr max_len) {
114 const uptr kMinFileLen = kPageSize;
115 uptr read_len = 0;
Kostya Serebryanyde496f42011-12-28 22:58:01 +0000116 *buff = 0;
Kostya Serebryanydf499b42012-01-05 00:44:33 +0000117 *buff_size = 0;
Kostya Serebryanyde496f42011-12-28 22:58:01 +0000118 // The files we usually open are not seekable, so try different buffer sizes.
Kostya Serebryany3f4c3872012-05-31 14:35:53 +0000119 for (uptr size = kMinFileLen; size <= max_len; size *= 2) {
Alexey Samsonov9552db72012-06-05 07:25:47 +0000120 fd_t fd = internal_open(file_name, /*write*/ false);
Alexander Potapenkof2b1df72012-05-10 12:03:09 +0000121 if (fd < 0) return 0;
Alexey Samsonova25b3462012-06-06 16:15:07 +0000122 UnmapOrDie(*buff, *buff_size);
123 *buff = (char*)MmapOrDie(size, __FUNCTION__);
Kostya Serebryanydf499b42012-01-05 00:44:33 +0000124 *buff_size = size;
Kostya Serebryany454a0642012-01-17 18:00:07 +0000125 // Read up to one page at a time.
126 read_len = 0;
127 bool reached_eof = false;
128 while (read_len + kPageSize <= size) {
Alexey Samsonov2221f552012-06-05 08:48:10 +0000129 uptr just_read = internal_read(fd, *buff + read_len, kPageSize);
Kostya Serebryany454a0642012-01-17 18:00:07 +0000130 if (just_read == 0) {
131 reached_eof = true;
132 break;
133 }
134 read_len += just_read;
135 }
Alexey Samsonov2221f552012-06-05 08:48:10 +0000136 internal_close(fd);
Kostya Serebryany454a0642012-01-17 18:00:07 +0000137 if (reached_eof) // We've read the whole file.
Kostya Serebryanyde496f42011-12-28 22:58:01 +0000138 break;
139 }
140 return read_len;
141}
142
Alexey Samsonove9541012012-06-06 13:11:29 +0000143void AppendToErrorMessageBuffer(const char *buffer) {
144 if (error_message_buffer) {
145 uptr length = (uptr)internal_strlen(buffer);
146 int remaining = error_message_buffer_size - error_message_buffer_pos;
147 internal_strncpy(error_message_buffer + error_message_buffer_pos,
148 buffer, remaining);
149 error_message_buffer[error_message_buffer_size - 1] = '\0';
150 // FIXME: reallocate the buffer instead of truncating the message.
151 error_message_buffer_pos += remaining > length ? length : remaining;
152 }
153}
154
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000155// ---------------------- mmap -------------------- {{{1
Kostya Serebryanya874fe52011-12-28 23:28:54 +0000156// Reserve memory range [beg, end].
Kostya Serebryany3f4c3872012-05-31 14:35:53 +0000157static void ReserveShadowMemoryRange(uptr beg, uptr end) {
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000158 CHECK((beg % kPageSize) == 0);
159 CHECK(((end + 1) % kPageSize) == 0);
Kostya Serebryany3f4c3872012-05-31 14:35:53 +0000160 uptr size = end - beg + 1;
Kostya Serebryanya874fe52011-12-28 23:28:54 +0000161 void *res = AsanMmapFixedNoReserve(beg, size);
162 CHECK(res == (void*)beg && "ReserveShadowMemoryRange failed");
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000163}
164
Kostya Serebryanyb89567c2011-12-02 21:02:20 +0000165// ---------------------- LowLevelAllocator ------------- {{{1
Kostya Serebryany3f4c3872012-05-31 14:35:53 +0000166void *LowLevelAllocator::Allocate(uptr size) {
Kostya Serebryanyb89567c2011-12-02 21:02:20 +0000167 CHECK((size & (size - 1)) == 0 && "size must be a power of two");
168 if (allocated_end_ - allocated_current_ < size) {
Kostya Serebryany3f4c3872012-05-31 14:35:53 +0000169 uptr size_to_allocate = Max(size, kPageSize);
Kostya Serebryanyde496f42011-12-28 22:58:01 +0000170 allocated_current_ =
Alexey Samsonova25b3462012-06-06 16:15:07 +0000171 (char*)MmapOrDie(size_to_allocate, __FUNCTION__);
Kostya Serebryanyb89567c2011-12-02 21:02:20 +0000172 allocated_end_ = allocated_current_ + size_to_allocate;
Kostya Serebryany3f4c3872012-05-31 14:35:53 +0000173 PoisonShadow((uptr)allocated_current_, size_to_allocate,
Kostya Serebryany6b30e2c2011-12-15 17:41:30 +0000174 kAsanInternalHeapMagic);
Kostya Serebryanyb89567c2011-12-02 21:02:20 +0000175 }
176 CHECK(allocated_end_ - allocated_current_ >= size);
177 void *res = allocated_current_;
178 allocated_current_ += size;
179 return res;
180}
181
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000182// ---------------------- DescribeAddress -------------------- {{{1
Kostya Serebryany3f4c3872012-05-31 14:35:53 +0000183static bool DescribeStackAddress(uptr addr, uptr access_size) {
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000184 AsanThread *t = asanThreadRegistry().FindThreadByStackAddress(addr);
185 if (!t) return false;
Kostya Serebryanyee392552012-05-31 15:02:07 +0000186 const sptr kBufSize = 4095;
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000187 char buf[kBufSize];
Kostya Serebryany3f4c3872012-05-31 14:35:53 +0000188 uptr offset = 0;
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000189 const char *frame_descr = t->GetFrameNameByAddr(addr, &offset);
190 // This string is created by the compiler and has the following form:
191 // "FunctioName n alloc_1 alloc_2 ... alloc_n"
192 // where alloc_i looks like "offset size len ObjectName ".
193 CHECK(frame_descr);
194 // Report the function name and the offset.
Timur Iskhodzhanov53627172012-02-14 19:33:04 +0000195 const char *name_end = internal_strchr(frame_descr, ' ');
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000196 CHECK(name_end);
197 buf[0] = 0;
Kostya Serebryanya4ccf872012-01-09 22:20:49 +0000198 internal_strncat(buf, frame_descr,
199 Min(kBufSize,
Kostya Serebryanyee392552012-05-31 15:02:07 +0000200 static_cast<sptr>(name_end - frame_descr)));
Alexey Samsonove9541012012-06-06 13:11:29 +0000201 AsanPrintf("Address %p is located at offset %zu "
202 "in frame <%s> of T%d's stack:\n",
203 (void*)addr, offset, buf, t->tid());
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000204 // Report the number of stack objects.
205 char *p;
Kostya Serebryany3f4c3872012-05-31 14:35:53 +0000206 uptr n_objects = internal_simple_strtoll(name_end, &p, 10);
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000207 CHECK(n_objects > 0);
Alexey Samsonove9541012012-06-06 13:11:29 +0000208 AsanPrintf(" This frame has %zu object(s):\n", n_objects);
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000209 // Report all objects in this frame.
Kostya Serebryany3f4c3872012-05-31 14:35:53 +0000210 for (uptr i = 0; i < n_objects; i++) {
211 uptr beg, size;
Kostya Serebryanyee392552012-05-31 15:02:07 +0000212 sptr len;
Alexey Samsonov88981022012-02-17 16:15:09 +0000213 beg = internal_simple_strtoll(p, &p, 10);
214 size = internal_simple_strtoll(p, &p, 10);
215 len = internal_simple_strtoll(p, &p, 10);
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000216 if (beg <= 0 || size <= 0 || len < 0 || *p != ' ') {
Alexey Samsonove9541012012-06-06 13:11:29 +0000217 AsanPrintf("AddressSanitizer can't parse the stack frame "
218 "descriptor: |%s|\n", frame_descr);
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000219 break;
220 }
221 p++;
222 buf[0] = 0;
Kostya Serebryanya4ccf872012-01-09 22:20:49 +0000223 internal_strncat(buf, p, Min(kBufSize, len));
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000224 p += len;
Alexey Samsonove9541012012-06-06 13:11:29 +0000225 AsanPrintf(" [%zu, %zu) '%s'\n", beg, beg + size, buf);
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000226 }
Alexey Samsonove9541012012-06-06 13:11:29 +0000227 AsanPrintf("HINT: this may be a false positive if your program uses "
228 "some custom stack unwind mechanism\n"
229 " (longjmp and C++ exceptions *are* supported)\n");
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000230 t->summary()->Announce();
231 return true;
232}
233
Kostya Serebryany3f4c3872012-05-31 14:35:53 +0000234static NOINLINE void DescribeAddress(uptr addr, uptr access_size) {
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000235 // Check if this is a global.
236 if (DescribeAddrIfGlobal(addr))
237 return;
238
239 if (DescribeStackAddress(addr, access_size))
240 return;
241
242 // finally, check if this is a heap.
243 DescribeHeapAddress(addr, access_size);
244}
245
246// -------------------------- Run-time entry ------------------- {{{1
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000247// exported functions
Kostya Serebryany51e75c42011-12-28 00:59:39 +0000248#define ASAN_REPORT_ERROR(type, is_write, size) \
Alexey Samsonov0a4c9062012-06-05 13:50:57 +0000249extern "C" NOINLINE INTERFACE_ATTRIBUTE \
Kostya Serebryany3f4c3872012-05-31 14:35:53 +0000250void __asan_report_ ## type ## size(uptr addr); \
251void __asan_report_ ## type ## size(uptr addr) { \
Kostya Serebryany9f311bb2012-03-15 01:36:00 +0000252 GET_CALLER_PC_BP_SP; \
Kostya Serebryany51e75c42011-12-28 00:59:39 +0000253 __asan_report_error(pc, bp, sp, addr, is_write, size); \
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000254}
255
256ASAN_REPORT_ERROR(load, false, 1)
257ASAN_REPORT_ERROR(load, false, 2)
258ASAN_REPORT_ERROR(load, false, 4)
259ASAN_REPORT_ERROR(load, false, 8)
260ASAN_REPORT_ERROR(load, false, 16)
261ASAN_REPORT_ERROR(store, true, 1)
262ASAN_REPORT_ERROR(store, true, 2)
263ASAN_REPORT_ERROR(store, true, 4)
264ASAN_REPORT_ERROR(store, true, 8)
265ASAN_REPORT_ERROR(store, true, 16)
266
267// Force the linker to keep the symbols for various ASan interface functions.
268// We want to keep those in the executable in order to let the instrumented
269// dynamic libraries access the symbol even if it is not used by the executable
270// itself. This should help if the build system is removing dead code at link
271// time.
Alexander Potapenko3fe91352012-02-27 14:06:48 +0000272static NOINLINE void force_interface_symbols() {
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000273 volatile int fake_condition = 0; // prevent dead condition elimination.
274 if (fake_condition) {
Kostya Serebryanyf0977db2012-03-14 22:48:09 +0000275 __asan_report_load1(0);
276 __asan_report_load2(0);
277 __asan_report_load4(0);
278 __asan_report_load8(0);
279 __asan_report_load16(0);
280 __asan_report_store1(0);
281 __asan_report_store2(0);
282 __asan_report_store4(0);
283 __asan_report_store8(0);
284 __asan_report_store16(0);
Kostya Serebryany3f4c3872012-05-31 14:35:53 +0000285 __asan_register_global(0, 0, 0);
286 __asan_register_globals(0, 0);
287 __asan_unregister_globals(0, 0);
288 __asan_set_death_callback(0);
289 __asan_set_error_report_callback(0);
Alexander Potapenkodadc45d2012-03-06 11:45:59 +0000290 __asan_handle_no_return();
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000291 }
292}
293
294// -------------------------- Init ------------------- {{{1
Alexander Potapenko62f10e72012-05-28 16:21:19 +0000295static void IntFlagValue(const char *flags, const char *flag,
Kostya Serebryanyee392552012-05-31 15:02:07 +0000296 s64 *out_val) {
Alexander Potapenko62f10e72012-05-28 16:21:19 +0000297 if (!flags) return;
Kostya Serebryanya4ccf872012-01-09 22:20:49 +0000298 const char *str = internal_strstr(flags, flag);
Alexander Potapenko62f10e72012-05-28 16:21:19 +0000299 if (!str) return;
300 *out_val = internal_atoll(str + internal_strlen(flag));
301}
302
303static void BoolFlagValue(const char *flags, const char *flag,
304 bool *out_val) {
305 if (!flags) return;
306 const char *str = internal_strstr(flags, flag);
307 if (!str) return;
Alexander Potapenko6b6dc572012-05-30 15:28:45 +0000308 const char *suffix = str + internal_strlen(flag);
Alexander Potapenko62f10e72012-05-28 16:21:19 +0000309 if (!internal_atoll(str + internal_strlen(flag))) {
Alexander Potapenko6b6dc572012-05-30 15:28:45 +0000310 if (suffix[0] == '0') {
Alexander Potapenko62f10e72012-05-28 16:21:19 +0000311 *out_val = false;
312 return;
313 }
314 } else {
315 *out_val = true;
316 return;
317 }
Alexander Potapenko6b6dc572012-05-30 15:28:45 +0000318 switch (suffix[0]) {
Alexander Potapenko62f10e72012-05-28 16:21:19 +0000319 case 'y':
320 case 't': {
321 *out_val = true;
322 break;
323 }
324 case 'n':
325 case 'f': {
326 *out_val = false;
327 break;
328 }
329 default: return;
330 }
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000331}
332
333static void asan_atexit() {
Alexey Samsonove9541012012-06-06 13:11:29 +0000334 AsanPrintf("AddressSanitizer exit stats:\n");
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000335 __asan_print_accumulated_stats();
336}
337
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000338} // namespace __asan
339
Kostya Serebryany4803ab92012-01-09 18:53:15 +0000340// ---------------------- Interface ---------------- {{{1
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000341using namespace __asan; // NOLINT
342
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000343int __asan_set_error_exit_code(int exit_code) {
344 int old = FLAG_exitcode;
345 FLAG_exitcode = exit_code;
346 return old;
347}
348
Alexander Potapenkodadc45d2012-03-06 11:45:59 +0000349void NOINLINE __asan_handle_no_return() {
Kostya Serebryanyf54b1f92012-02-08 21:33:27 +0000350 int local_stack;
351 AsanThread *curr_thread = asanThreadRegistry().GetCurrent();
352 CHECK(curr_thread);
Kostya Serebryany3f4c3872012-05-31 14:35:53 +0000353 uptr top = curr_thread->stack_top();
354 uptr bottom = ((uptr)&local_stack - kPageSize) & ~(kPageSize-1);
Kostya Serebryanyf54b1f92012-02-08 21:33:27 +0000355 PoisonShadow(bottom, top - bottom, 0);
356}
357
Alexander Potapenko2f3f9622012-03-01 14:39:21 +0000358void NOINLINE __asan_set_death_callback(void (*callback)(void)) {
Kostya Serebryanye1fe0fd2012-02-13 21:24:29 +0000359 death_callback = callback;
360}
361
Alexander Potapenko3fe91352012-02-27 14:06:48 +0000362void NOINLINE __asan_set_error_report_callback(void (*callback)(const char*)) {
363 error_report_callback = callback;
364 if (callback) {
Alexander Potapenko8ca02782012-05-12 12:33:41 +0000365 error_message_buffer_size = 1 << 16;
Alexander Potapenko3fe91352012-02-27 14:06:48 +0000366 error_message_buffer =
Alexey Samsonova25b3462012-06-06 16:15:07 +0000367 (char*)MmapOrDie(error_message_buffer_size, __FUNCTION__);
Alexander Potapenko3fe91352012-02-27 14:06:48 +0000368 error_message_buffer_pos = 0;
369 }
370}
371
Kostya Serebryany9aead372012-05-31 14:11:07 +0000372void __asan_report_error(uptr pc, uptr bp, uptr sp,
373 uptr addr, bool is_write, uptr access_size) {
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000374 // Do not print more than one report, otherwise they will mix up.
375 static int num_calls = 0;
376 if (AtomicInc(&num_calls) > 1) return;
377
Alexey Samsonove9541012012-06-06 13:11:29 +0000378 AsanPrintf("===================================================="
379 "=============\n");
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000380 const char *bug_descr = "unknown-crash";
381 if (AddrIsInMem(addr)) {
Kostya Serebryanyee392552012-05-31 15:02:07 +0000382 u8 *shadow_addr = (u8*)MemToShadow(addr);
Kostya Serebryanyacd5c612011-12-07 21:30:20 +0000383 // If we are accessing 16 bytes, look at the second shadow byte.
384 if (*shadow_addr == 0 && access_size > SHADOW_GRANULARITY)
385 shadow_addr++;
386 // If we are in the partial right redzone, look at the next shadow byte.
387 if (*shadow_addr > 0 && *shadow_addr < 128)
388 shadow_addr++;
389 switch (*shadow_addr) {
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000390 case kAsanHeapLeftRedzoneMagic:
391 case kAsanHeapRightRedzoneMagic:
392 bug_descr = "heap-buffer-overflow";
393 break;
394 case kAsanHeapFreeMagic:
395 bug_descr = "heap-use-after-free";
396 break;
397 case kAsanStackLeftRedzoneMagic:
398 bug_descr = "stack-buffer-underflow";
399 break;
400 case kAsanStackMidRedzoneMagic:
401 case kAsanStackRightRedzoneMagic:
402 case kAsanStackPartialRedzoneMagic:
403 bug_descr = "stack-buffer-overflow";
404 break;
405 case kAsanStackAfterReturnMagic:
406 bug_descr = "stack-use-after-return";
407 break;
408 case kAsanUserPoisonedMemoryMagic:
409 bug_descr = "use-after-poison";
410 break;
411 case kAsanGlobalRedzoneMagic:
412 bug_descr = "global-buffer-overflow";
413 break;
414 }
415 }
416
Kostya Serebryanyc4b34d92011-12-09 01:49:31 +0000417 AsanThread *curr_thread = asanThreadRegistry().GetCurrent();
Kostya Serebryanye0cff0b2012-06-06 15:06:58 +0000418 u32 curr_tid = asanThreadRegistry().GetCurrentTidOrInvalid();
Kostya Serebryanyc4b34d92011-12-09 01:49:31 +0000419
420 if (curr_thread) {
421 // We started reporting an error message. Stop using the fake stack
422 // in case we will call an instrumented function from a symbolizer.
423 curr_thread->fake_stack().StopUsingFakeStack();
424 }
425
Alexey Samsonove9541012012-06-06 13:11:29 +0000426 AsanReport("ERROR: AddressSanitizer %s on address "
427 "%p at pc 0x%zx bp 0x%zx sp 0x%zx\n",
428 bug_descr, (void*)addr, pc, bp, sp);
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000429
Alexey Samsonove9541012012-06-06 13:11:29 +0000430 AsanPrintf("%s of size %zu at %p thread T%d\n",
431 access_size ? (is_write ? "WRITE" : "READ") : "ACCESS",
432 access_size, (void*)addr, curr_tid);
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000433
434 if (FLAG_debug) {
Kostya Serebryany3f4c3872012-05-31 14:35:53 +0000435 PrintBytes("PC: ", (uptr*)pc);
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000436 }
437
Evgeniy Stepanov9cfa1942012-01-19 11:34:18 +0000438 GET_STACK_TRACE_WITH_PC_AND_BP(kStackTraceMax, pc, bp);
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000439 stack.PrintStack();
440
441 CHECK(AddrIsInMem(addr));
442
443 DescribeAddress(addr, access_size);
444
Kostya Serebryany3f4c3872012-05-31 14:35:53 +0000445 uptr shadow_addr = MemToShadow(addr);
Alexey Samsonove9541012012-06-06 13:11:29 +0000446 AsanReport("ABORTING\n");
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000447 __asan_print_accumulated_stats();
Alexey Samsonove9541012012-06-06 13:11:29 +0000448 AsanPrintf("Shadow byte and word:\n");
449 AsanPrintf(" %p: %x\n", (void*)shadow_addr, *(unsigned char*)shadow_addr);
Kostya Serebryany3f4c3872012-05-31 14:35:53 +0000450 uptr aligned_shadow = shadow_addr & ~(kWordSize - 1);
451 PrintBytes(" ", (uptr*)(aligned_shadow));
Alexey Samsonove9541012012-06-06 13:11:29 +0000452 AsanPrintf("More shadow bytes:\n");
Kostya Serebryany3f4c3872012-05-31 14:35:53 +0000453 PrintBytes(" ", (uptr*)(aligned_shadow-4*kWordSize));
454 PrintBytes(" ", (uptr*)(aligned_shadow-3*kWordSize));
455 PrintBytes(" ", (uptr*)(aligned_shadow-2*kWordSize));
456 PrintBytes(" ", (uptr*)(aligned_shadow-1*kWordSize));
457 PrintBytes("=>", (uptr*)(aligned_shadow+0*kWordSize));
458 PrintBytes(" ", (uptr*)(aligned_shadow+1*kWordSize));
459 PrintBytes(" ", (uptr*)(aligned_shadow+2*kWordSize));
460 PrintBytes(" ", (uptr*)(aligned_shadow+3*kWordSize));
461 PrintBytes(" ", (uptr*)(aligned_shadow+4*kWordSize));
Alexander Potapenko3fe91352012-02-27 14:06:48 +0000462 if (error_report_callback) {
463 error_report_callback(error_message_buffer);
464 }
Alexey Samsonov47657ce2012-06-06 07:02:44 +0000465 Die();
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000466}
467
Alexander Potapenkofca72fd2012-05-25 15:37:16 +0000468static void ParseAsanOptions(const char *options) {
Alexander Potapenko62f10e72012-05-28 16:21:19 +0000469 IntFlagValue(options, "malloc_context_size=",
Kostya Serebryanyee392552012-05-31 15:02:07 +0000470 (s64*)&FLAG_malloc_context_size);
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000471 CHECK(FLAG_malloc_context_size <= kMallocContextSize);
472
Alexander Potapenko62f10e72012-05-28 16:21:19 +0000473 IntFlagValue(options, "max_malloc_fill_size=",
Kostya Serebryanyee392552012-05-31 15:02:07 +0000474 (s64*)&FLAG_max_malloc_fill_size);
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000475
Alexander Potapenko62f10e72012-05-28 16:21:19 +0000476 IntFlagValue(options, "verbosity=", &FLAG_v);
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000477
Kostya Serebryanyee392552012-05-31 15:02:07 +0000478 IntFlagValue(options, "redzone=", (s64*)&FLAG_redzone);
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000479 CHECK(FLAG_redzone >= 32);
480 CHECK((FLAG_redzone & (FLAG_redzone - 1)) == 0);
Kostya Serebryanyee392552012-05-31 15:02:07 +0000481 IntFlagValue(options, "quarantine_size=", (s64*)&FLAG_quarantine_size);
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000482
Alexander Potapenko62f10e72012-05-28 16:21:19 +0000483 IntFlagValue(options, "atexit=", &FLAG_atexit);
484 BoolFlagValue(options, "poison_shadow=", &FLAG_poison_shadow);
485 IntFlagValue(options, "report_globals=", &FLAG_report_globals);
486 BoolFlagValue(options, "handle_segv=", &FLAG_handle_segv);
487 BoolFlagValue(options, "use_sigaltstack=", &FLAG_use_sigaltstack);
488 BoolFlagValue(options, "symbolize=", &FLAG_symbolize);
489 IntFlagValue(options, "demangle=", &FLAG_demangle);
490 IntFlagValue(options, "debug=", &FLAG_debug);
491 BoolFlagValue(options, "replace_cfallocator=", &FLAG_replace_cfallocator);
492 BoolFlagValue(options, "replace_str=", &FLAG_replace_str);
493 BoolFlagValue(options, "replace_intrin=", &FLAG_replace_intrin);
494 BoolFlagValue(options, "use_fake_stack=", &FLAG_use_fake_stack);
495 IntFlagValue(options, "exitcode=", &FLAG_exitcode);
496 BoolFlagValue(options, "allow_user_poisoning=", &FLAG_allow_user_poisoning);
497 IntFlagValue(options, "sleep_before_dying=", &FLAG_sleep_before_dying);
498 BoolFlagValue(options, "abort_on_error=", &FLAG_abort_on_error);
499 BoolFlagValue(options, "unmap_shadow_on_exit=", &FLAG_unmap_shadow_on_exit);
Kostya Serebryanyf8e6fee2012-04-06 01:27:11 +0000500 // By default, disable core dumper on 64-bit --
501 // it makes little sense to dump 16T+ core.
Alexander Potapenko62f10e72012-05-28 16:21:19 +0000502 BoolFlagValue(options, "disable_core=", &FLAG_disable_core);
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000503
Alexander Potapenko37931232012-05-25 15:20:13 +0000504 // Allow the users to work around the bug in Nvidia drivers prior to 295.*.
Alexander Potapenko62f10e72012-05-28 16:21:19 +0000505 BoolFlagValue(options, "check_malloc_usable_size=",
506 &FLAG_check_malloc_usable_size);
Alexander Potapenkofca72fd2012-05-25 15:37:16 +0000507}
508
509void __asan_init() {
510 if (asan_inited) return;
Kostya Serebryanyb3cedf92012-05-29 12:18:18 +0000511 MiniLibcStub(); // FIXME: remove me once mini libc build is tested properly.
Alexander Potapenkofca72fd2012-05-25 15:37:16 +0000512 asan_init_is_running = true;
513
514 // Make sure we are not statically linked.
515 AsanDoesNotSupportStaticLinkage();
516
Alexey Samsonovff20f172012-05-29 09:39:01 +0000517#if !defined(_WIN32)
Alexander Potapenko62f10e72012-05-28 16:21:19 +0000518 if (__asan_default_options) {
Alexander Potapenko62f10e72012-05-28 16:21:19 +0000519 ParseAsanOptions(__asan_default_options);
Alexander Potapenkoe4781f02012-05-30 14:12:20 +0000520 if (FLAG_v) {
521 Report("Using the defaults from __asan_default_options: %s\n",
522 __asan_default_options);
523 }
Alexander Potapenko62f10e72012-05-28 16:21:19 +0000524 }
Alexey Samsonovff20f172012-05-29 09:39:01 +0000525#endif
Alexander Potapenkofca72fd2012-05-25 15:37:16 +0000526 // flags
527 const char *options = AsanGetEnv("ASAN_OPTIONS");
Alexander Potapenkof2981f32012-05-25 15:56:40 +0000528 ParseAsanOptions(options);
Alexander Potapenkofeb47932012-03-16 16:38:31 +0000529
Alexander Potapenko62f10e72012-05-28 16:21:19 +0000530 if (FLAG_v && options) {
Alexander Potapenkofeb47932012-03-16 16:38:31 +0000531 Report("Parsed ASAN_OPTIONS: %s\n", options);
532 }
533
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000534 if (FLAG_atexit) {
Alexey Samsonovb823e3c2012-02-22 14:07:06 +0000535 Atexit(asan_atexit);
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000536 }
537
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000538 // interceptors
539 InitializeAsanInterceptors();
540
541 ReplaceSystemMalloc();
Alexey Samsonov4d5f98d2012-04-06 08:21:08 +0000542 ReplaceOperatorsNewAndDelete();
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000543
544 if (FLAG_v) {
Alexey Samsonove4309e82012-06-06 10:54:25 +0000545 Printf("|| `[%p, %p]` || HighMem ||\n",
546 (void*)kHighMemBeg, (void*)kHighMemEnd);
547 Printf("|| `[%p, %p]` || HighShadow ||\n",
548 (void*)kHighShadowBeg, (void*)kHighShadowEnd);
549 Printf("|| `[%p, %p]` || ShadowGap ||\n",
550 (void*)kShadowGapBeg, (void*)kShadowGapEnd);
551 Printf("|| `[%p, %p]` || LowShadow ||\n",
552 (void*)kLowShadowBeg, (void*)kLowShadowEnd);
553 Printf("|| `[%p, %p]` || LowMem ||\n",
554 (void*)kLowMemBeg, (void*)kLowMemEnd);
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000555 Printf("MemToShadow(shadow): %p %p %p %p\n",
Alexey Samsonove4309e82012-06-06 10:54:25 +0000556 (void*)MEM_TO_SHADOW(kLowShadowBeg),
557 (void*)MEM_TO_SHADOW(kLowShadowEnd),
558 (void*)MEM_TO_SHADOW(kHighShadowBeg),
559 (void*)MEM_TO_SHADOW(kHighShadowEnd));
Kostya Serebryany3f4c3872012-05-31 14:35:53 +0000560 Printf("red_zone=%zu\n", (uptr)FLAG_redzone);
561 Printf("malloc_context_size=%zu\n", (uptr)FLAG_malloc_context_size);
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000562
Kostya Serebryany3f4c3872012-05-31 14:35:53 +0000563 Printf("SHADOW_SCALE: %zx\n", (uptr)SHADOW_SCALE);
564 Printf("SHADOW_GRANULARITY: %zx\n", (uptr)SHADOW_GRANULARITY);
565 Printf("SHADOW_OFFSET: %zx\n", (uptr)SHADOW_OFFSET);
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000566 CHECK(SHADOW_SCALE >= 3 && SHADOW_SCALE <= 7);
567 }
568
Kostya Serebryanyf8e6fee2012-04-06 01:27:11 +0000569 if (FLAG_disable_core) {
Kostya Serebryanyef14ff62012-01-06 02:12:25 +0000570 AsanDisableCoreDumper();
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000571 }
572
Alexander Potapenkoc50e8352012-02-13 15:11:23 +0000573 if (AsanShadowRangeIsAvailable()) {
Kostya Serebryanya7e760a2012-01-09 19:18:27 +0000574 if (kLowShadowBeg != kLowShadowEnd) {
Timur Iskhodzhanov3e81fe42012-02-09 17:20:14 +0000575 // mmap the low shadow plus at least one page.
576 ReserveShadowMemoryRange(kLowShadowBeg - kMmapGranularity, kLowShadowEnd);
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000577 }
Kostya Serebryanya7e760a2012-01-09 19:18:27 +0000578 // mmap the high shadow.
579 ReserveShadowMemoryRange(kHighShadowBeg, kHighShadowEnd);
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000580 // protect the gap
Kostya Serebryanya874fe52011-12-28 23:28:54 +0000581 void *prot = AsanMprotect(kShadowGapBeg, kShadowGapEnd - kShadowGapBeg + 1);
582 CHECK(prot == (void*)kShadowGapBeg);
Alexander Potapenkoc50e8352012-02-13 15:11:23 +0000583 } else {
584 Report("Shadow memory range interleaves with an existing memory mapping. "
585 "ASan cannot proceed correctly. ABORTING.\n");
Alexander Potapenko99d17eb2012-02-22 09:11:55 +0000586 AsanDumpProcessMap();
Alexey Samsonov47657ce2012-06-06 07:02:44 +0000587 Die();
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000588 }
589
Alexander Potapenkof03d8af2012-04-05 10:54:52 +0000590 InstallSignalHandlers();
591
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000592 // On Linux AsanThread::ThreadStart() calls malloc() that's why asan_inited
593 // should be set to 1 prior to initializing the threads.
594 asan_inited = 1;
595 asan_init_is_running = false;
596
597 asanThreadRegistry().Init();
598 asanThreadRegistry().GetMain()->ThreadStart();
Kostya Serebryany51e75c42011-12-28 00:59:39 +0000599 force_interface_symbols(); // no-op.
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000600
601 if (FLAG_v) {
Kostya Serebryanyd6567c52011-12-01 21:40:52 +0000602 Report("AddressSanitizer Init done\n");
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000603 }
604}
Evgeniy Stepanov8bcc6b92012-01-11 08:17:19 +0000605
606#if defined(ASAN_USE_PREINIT_ARRAY)
Timur Iskhodzhanov38ed7362012-02-21 16:24:23 +0000607 // On Linux, we force __asan_init to be called before anyone else
608 // by placing it into .preinit_array section.
609 // FIXME: do we have anything like this on Mac?
610 __attribute__((section(".preinit_array")))
611 typeof(__asan_init) *__asan_preinit =__asan_init;
612#elif defined(_WIN32) && defined(_DLL)
613 // On Windows, when using dynamic CRT (/MD), we can put a pointer
614 // to __asan_init into the global list of C initializers.
615 // See crt0dat.c in the CRT sources for the details.
Timur Iskhodzhanov39c22ee2012-02-22 09:28:14 +0000616 #pragma section(".CRT$XIB", long, read) // NOLINT
Timur Iskhodzhanov38ed7362012-02-21 16:24:23 +0000617 __declspec(allocate(".CRT$XIB")) void (*__asan_preinit)() = __asan_init;
Evgeniy Stepanov8bcc6b92012-01-11 08:17:19 +0000618#endif