blob: 89f46b1ef86045b5ce1a1c0a3cca45a4923602c9 [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"
Dmitry Vyukovfce5bd42012-06-29 16:58:33 +000024#include "sanitizer_common/sanitizer_atomic.h"
Alexey Samsonovcb8c4dc2012-07-09 14:36:04 +000025#include "sanitizer_common/sanitizer_flags.h"
Alexey Samsonov9552db72012-06-05 07:25:47 +000026#include "sanitizer_common/sanitizer_libc.h"
Kostya Serebryany1e172b42011-11-30 01:07:02 +000027
Alexey Samsonov47657ce2012-06-06 07:02:44 +000028namespace __sanitizer {
29using namespace __asan;
30
31void Die() {
Dmitry Vyukovfce5bd42012-06-29 16:58:33 +000032 static atomic_uint32_t num_calls;
33 if (atomic_fetch_add(&num_calls, 1, memory_order_relaxed) != 0) {
Alexey Samsonov47657ce2012-06-06 07:02:44 +000034 // Don't die twice - run a busy loop.
35 while (1) { }
36 }
Alexey Samsonovcb8c4dc2012-07-09 14:36:04 +000037 if (flags()->sleep_before_dying) {
38 Report("Sleeping for %zd second(s)\n", flags()->sleep_before_dying);
39 SleepForSeconds(flags()->sleep_before_dying);
Alexey Samsonov47657ce2012-06-06 07:02:44 +000040 }
Alexey Samsonovcb8c4dc2012-07-09 14:36:04 +000041 if (flags()->unmap_shadow_on_exit)
Alexey Samsonova25b3462012-06-06 16:15:07 +000042 UnmapOrDie((void*)kLowShadowBeg, kHighShadowEnd - kLowShadowBeg);
Alexey Samsonov47657ce2012-06-06 07:02:44 +000043 if (death_callback)
44 death_callback();
Alexey Samsonovcb8c4dc2012-07-09 14:36:04 +000045 if (flags()->abort_on_error)
Alexey Samsonov47657ce2012-06-06 07:02:44 +000046 Abort();
Alexey Samsonovcb8c4dc2012-07-09 14:36:04 +000047 Exit(flags()->exitcode);
Alexey Samsonov47657ce2012-06-06 07:02:44 +000048}
49
Alexey Samsonov15a77612012-06-06 15:22:20 +000050void CheckFailed(const char *file, int line, const char *cond, u64 v1, u64 v2) {
51 AsanReport("AddressSanitizer CHECK failed: %s:%d \"%s\" (%zx, %zx)\n",
52 file, line, cond, (uptr)v1, (uptr)v2);
53 PRINT_CURRENT_STACK();
54 ShowStatsAndAbort();
55}
56
Alexey Samsonov47657ce2012-06-06 07:02:44 +000057} // namespace __sanitizer
58
Kostya Serebryany1e172b42011-11-30 01:07:02 +000059namespace __asan {
60
61// -------------------------- Flags ------------------------- {{{1
Kostya Serebryany3f4c3872012-05-31 14:35:53 +000062static const uptr kMallocContextSize = 30;
Kostya Serebryany1e172b42011-11-30 01:07:02 +000063
Alexey Samsonovcb8c4dc2012-07-09 14:36:04 +000064static Flags asan_flags;
65
66Flags *flags() {
67 return &asan_flags;
68}
69
70// Can be overriden in frontend.
71void WEAK OverrideFlags(Flags *f) {
72 (void)f;
73}
74
75static void ParseFlagsFromString(Flags *f, const char *str) {
76 ParseFlag(str, &f->quarantine_size, "quarantine_size");
77 ParseFlag(str, &f->symbolize, "symbolize");
78 ParseFlag(str, &f->verbosity, "verbosity");
79 ParseFlag(str, &f->redzone, "redzone");
80 CHECK(f->redzone >= 16);
81 CHECK(IsPowerOfTwo(f->redzone));
82
83 ParseFlag(str, &f->debug, "debug");
84 ParseFlag(str, &f->poison_shadow, "poison_shadow");
85 ParseFlag(str, &f->report_globals, "report_globals");
86 ParseFlag(str, &f->malloc_context_size, "malloc_context_size");
87 CHECK(f->malloc_context_size <= kMallocContextSize);
88
89 ParseFlag(str, &f->replace_str, "replace_str");
90 ParseFlag(str, &f->replace_intrin, "replace_intrin");
91 ParseFlag(str, &f->replace_cfallocator, "replace_cfallocator");
92 ParseFlag(str, &f->mac_ignore_invalid_free, "mac_ignore_invalid_free");
93 ParseFlag(str, &f->use_fake_stack, "use_fake_stack");
94 ParseFlag(str, &f->max_malloc_fill_size, "max_malloc_fill_size");
95 ParseFlag(str, &f->exitcode, "exitcode");
96 ParseFlag(str, &f->allow_user_poisoning, "allow_user_poisoning");
97 ParseFlag(str, &f->sleep_before_dying, "sleep_before_dying");
98 ParseFlag(str, &f->handle_segv, "handle_segv");
99 ParseFlag(str, &f->use_sigaltstack, "use_sigaltstack");
100 // Allow the users to work around the bug in Nvidia drivers prior to 295.*.
101 ParseFlag(str, &f->check_malloc_usable_size, "check_malloc_usable_size");
102 ParseFlag(str, &f->unmap_shadow_on_exit, "unmap_shadow_on_exit");
103 ParseFlag(str, &f->abort_on_error, "abort_on_error");
104 ParseFlag(str, &f->atexit, "atexit");
105 // By default, disable core dumper on 64-bit --
106 // it makes little sense to dump 16T+ core.
107 ParseFlag(str, &f->disable_core, "disable_core");
108}
109
110void InitializeFlags(Flags *f, const char *env) {
111 internal_memset(f, 0, sizeof(*f));
112
113 f->quarantine_size = (ASAN_LOW_MEMORY) ? 1UL << 24 : 1UL << 28;
114 f->symbolize = false;
115 f->verbosity = 0;
116 f->redzone = (ASAN_LOW_MEMORY) ? 64 : 128; // power of two, >= 32.
117 f->debug = 0;
118 f->poison_shadow = true;
119 f->report_globals = 1;
120 f->malloc_context_size = kMallocContextSize;
121 f->replace_str = true;
122 f->replace_intrin = true;
123 f->replace_cfallocator = true; // Used on Mac only.
124 f->mac_ignore_invalid_free = false; // Used on Mac only.
125 f->use_fake_stack = true;
126 f->max_malloc_fill_size = 0;
127 f->exitcode = ASAN_DEFAULT_FAILURE_EXITCODE;
128 f->allow_user_poisoning = true;
129 f->sleep_before_dying = 0;
130 f->handle_segv = ASAN_NEEDS_SEGV;
131 f->use_sigaltstack = false;
132 f->check_malloc_usable_size = true;
133 f->unmap_shadow_on_exit = false;
134 f->abort_on_error = false;
135 f->atexit = false;
136 f->disable_core = (__WORDSIZE == 64);
137
138 // Let a frontend override.
139 OverrideFlags(f);
140
141 // Override from user-specified string.
142#if !defined(_WIN32)
143 if (__asan_default_options) {
144 ParseFlagsFromString(f, __asan_default_options);
145 if (flags()->verbosity) {
146 Report("Using the defaults from __asan_default_options: %s\n",
147 __asan_default_options);
148 }
149 }
150#endif
151
152 // Override from command line.
153 ParseFlagsFromString(f, env);
154}
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000155
156// -------------------------- Globals --------------------- {{{1
157int asan_inited;
158bool asan_init_is_running;
Alexey Samsonov47657ce2012-06-06 07:02:44 +0000159void (*death_callback)(void);
Alexander Potapenko3fe91352012-02-27 14:06:48 +0000160static void (*error_report_callback)(const char*);
Kostya Serebryany3f4c3872012-05-31 14:35:53 +0000161char *error_message_buffer = 0;
162uptr error_message_buffer_pos = 0;
163uptr error_message_buffer_size = 0;
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000164
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000165// -------------------------- Misc ---------------- {{{1
166void ShowStatsAndAbort() {
167 __asan_print_accumulated_stats();
Alexey Samsonov47657ce2012-06-06 07:02:44 +0000168 Die();
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000169}
170
Kostya Serebryany3f4c3872012-05-31 14:35:53 +0000171static void PrintBytes(const char *before, uptr *a) {
Kostya Serebryanyee392552012-05-31 15:02:07 +0000172 u8 *bytes = (u8*)a;
Kostya Serebryany3f4c3872012-05-31 14:35:53 +0000173 uptr byte_num = (__WORDSIZE) / 8;
Alexey Samsonove9541012012-06-06 13:11:29 +0000174 AsanPrintf("%s%p:", before, (void*)a);
Kostya Serebryany3f4c3872012-05-31 14:35:53 +0000175 for (uptr i = 0; i < byte_num; i++) {
Alexey Samsonove9541012012-06-06 13:11:29 +0000176 AsanPrintf(" %x%x", bytes[i] >> 4, bytes[i] & 15);
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000177 }
Alexey Samsonove9541012012-06-06 13:11:29 +0000178 AsanPrintf("\n");
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000179}
180
Alexey Samsonove9541012012-06-06 13:11:29 +0000181void AppendToErrorMessageBuffer(const char *buffer) {
182 if (error_message_buffer) {
Kostya Serebryany94c54f12012-06-23 16:30:48 +0000183 uptr length = internal_strlen(buffer);
184 CHECK_GE(error_message_buffer_size, error_message_buffer_pos);
185 uptr remaining = error_message_buffer_size - error_message_buffer_pos;
Alexey Samsonove9541012012-06-06 13:11:29 +0000186 internal_strncpy(error_message_buffer + error_message_buffer_pos,
187 buffer, remaining);
188 error_message_buffer[error_message_buffer_size - 1] = '\0';
189 // FIXME: reallocate the buffer instead of truncating the message.
190 error_message_buffer_pos += remaining > length ? length : remaining;
191 }
192}
193
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000194// ---------------------- mmap -------------------- {{{1
Kostya Serebryanya874fe52011-12-28 23:28:54 +0000195// Reserve memory range [beg, end].
Kostya Serebryany3f4c3872012-05-31 14:35:53 +0000196static void ReserveShadowMemoryRange(uptr beg, uptr end) {
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000197 CHECK((beg % kPageSize) == 0);
198 CHECK(((end + 1) % kPageSize) == 0);
Kostya Serebryany3f4c3872012-05-31 14:35:53 +0000199 uptr size = end - beg + 1;
Alexey Samsonovf607fc12012-06-14 14:42:58 +0000200 void *res = MmapFixedNoReserve(beg, size);
Kostya Serebryanya874fe52011-12-28 23:28:54 +0000201 CHECK(res == (void*)beg && "ReserveShadowMemoryRange failed");
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000202}
203
Kostya Serebryanyb89567c2011-12-02 21:02:20 +0000204// ---------------------- LowLevelAllocator ------------- {{{1
Kostya Serebryany3f4c3872012-05-31 14:35:53 +0000205void *LowLevelAllocator::Allocate(uptr size) {
Kostya Serebryanyb89567c2011-12-02 21:02:20 +0000206 CHECK((size & (size - 1)) == 0 && "size must be a power of two");
Kostya Serebryany94c54f12012-06-23 16:30:48 +0000207 if (allocated_end_ - allocated_current_ < (sptr)size) {
Kostya Serebryany3f4c3872012-05-31 14:35:53 +0000208 uptr size_to_allocate = Max(size, kPageSize);
Kostya Serebryanyde496f42011-12-28 22:58:01 +0000209 allocated_current_ =
Alexey Samsonova25b3462012-06-06 16:15:07 +0000210 (char*)MmapOrDie(size_to_allocate, __FUNCTION__);
Kostya Serebryanyb89567c2011-12-02 21:02:20 +0000211 allocated_end_ = allocated_current_ + size_to_allocate;
Kostya Serebryany3f4c3872012-05-31 14:35:53 +0000212 PoisonShadow((uptr)allocated_current_, size_to_allocate,
Kostya Serebryany6b30e2c2011-12-15 17:41:30 +0000213 kAsanInternalHeapMagic);
Kostya Serebryanyb89567c2011-12-02 21:02:20 +0000214 }
Kostya Serebryany94c54f12012-06-23 16:30:48 +0000215 CHECK(allocated_end_ - allocated_current_ >= (sptr)size);
Kostya Serebryanyb89567c2011-12-02 21:02:20 +0000216 void *res = allocated_current_;
217 allocated_current_ += size;
218 return res;
219}
220
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000221// ---------------------- DescribeAddress -------------------- {{{1
Kostya Serebryany3f4c3872012-05-31 14:35:53 +0000222static bool DescribeStackAddress(uptr addr, uptr access_size) {
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000223 AsanThread *t = asanThreadRegistry().FindThreadByStackAddress(addr);
224 if (!t) return false;
Kostya Serebryanyee392552012-05-31 15:02:07 +0000225 const sptr kBufSize = 4095;
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000226 char buf[kBufSize];
Kostya Serebryany3f4c3872012-05-31 14:35:53 +0000227 uptr offset = 0;
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000228 const char *frame_descr = t->GetFrameNameByAddr(addr, &offset);
229 // This string is created by the compiler and has the following form:
230 // "FunctioName n alloc_1 alloc_2 ... alloc_n"
231 // where alloc_i looks like "offset size len ObjectName ".
232 CHECK(frame_descr);
233 // Report the function name and the offset.
Timur Iskhodzhanov53627172012-02-14 19:33:04 +0000234 const char *name_end = internal_strchr(frame_descr, ' ');
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000235 CHECK(name_end);
236 buf[0] = 0;
Kostya Serebryanya4ccf872012-01-09 22:20:49 +0000237 internal_strncat(buf, frame_descr,
238 Min(kBufSize,
Kostya Serebryanyee392552012-05-31 15:02:07 +0000239 static_cast<sptr>(name_end - frame_descr)));
Alexey Samsonove9541012012-06-06 13:11:29 +0000240 AsanPrintf("Address %p is located at offset %zu "
241 "in frame <%s> of T%d's stack:\n",
242 (void*)addr, offset, buf, t->tid());
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000243 // Report the number of stack objects.
244 char *p;
Kostya Serebryany3f4c3872012-05-31 14:35:53 +0000245 uptr n_objects = internal_simple_strtoll(name_end, &p, 10);
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000246 CHECK(n_objects > 0);
Alexey Samsonove9541012012-06-06 13:11:29 +0000247 AsanPrintf(" This frame has %zu object(s):\n", n_objects);
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000248 // Report all objects in this frame.
Kostya Serebryany3f4c3872012-05-31 14:35:53 +0000249 for (uptr i = 0; i < n_objects; i++) {
250 uptr beg, size;
Kostya Serebryanyee392552012-05-31 15:02:07 +0000251 sptr len;
Alexey Samsonov88981022012-02-17 16:15:09 +0000252 beg = internal_simple_strtoll(p, &p, 10);
253 size = internal_simple_strtoll(p, &p, 10);
254 len = internal_simple_strtoll(p, &p, 10);
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000255 if (beg <= 0 || size <= 0 || len < 0 || *p != ' ') {
Alexey Samsonove9541012012-06-06 13:11:29 +0000256 AsanPrintf("AddressSanitizer can't parse the stack frame "
257 "descriptor: |%s|\n", frame_descr);
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000258 break;
259 }
260 p++;
261 buf[0] = 0;
Kostya Serebryanya4ccf872012-01-09 22:20:49 +0000262 internal_strncat(buf, p, Min(kBufSize, len));
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000263 p += len;
Alexey Samsonove9541012012-06-06 13:11:29 +0000264 AsanPrintf(" [%zu, %zu) '%s'\n", beg, beg + size, buf);
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000265 }
Alexey Samsonove9541012012-06-06 13:11:29 +0000266 AsanPrintf("HINT: this may be a false positive if your program uses "
267 "some custom stack unwind mechanism\n"
268 " (longjmp and C++ exceptions *are* supported)\n");
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000269 t->summary()->Announce();
270 return true;
271}
272
Kostya Serebryany3f4c3872012-05-31 14:35:53 +0000273static NOINLINE void DescribeAddress(uptr addr, uptr access_size) {
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000274 // Check if this is a global.
275 if (DescribeAddrIfGlobal(addr))
276 return;
277
278 if (DescribeStackAddress(addr, access_size))
279 return;
280
281 // finally, check if this is a heap.
282 DescribeHeapAddress(addr, access_size);
283}
284
285// -------------------------- Run-time entry ------------------- {{{1
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000286// exported functions
Kostya Serebryany51e75c42011-12-28 00:59:39 +0000287#define ASAN_REPORT_ERROR(type, is_write, size) \
Alexey Samsonov0a4c9062012-06-05 13:50:57 +0000288extern "C" NOINLINE INTERFACE_ATTRIBUTE \
Kostya Serebryany3f4c3872012-05-31 14:35:53 +0000289void __asan_report_ ## type ## size(uptr addr); \
290void __asan_report_ ## type ## size(uptr addr) { \
Kostya Serebryany9f311bb2012-03-15 01:36:00 +0000291 GET_CALLER_PC_BP_SP; \
Kostya Serebryany51e75c42011-12-28 00:59:39 +0000292 __asan_report_error(pc, bp, sp, addr, is_write, size); \
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000293}
294
295ASAN_REPORT_ERROR(load, false, 1)
296ASAN_REPORT_ERROR(load, false, 2)
297ASAN_REPORT_ERROR(load, false, 4)
298ASAN_REPORT_ERROR(load, false, 8)
299ASAN_REPORT_ERROR(load, false, 16)
300ASAN_REPORT_ERROR(store, true, 1)
301ASAN_REPORT_ERROR(store, true, 2)
302ASAN_REPORT_ERROR(store, true, 4)
303ASAN_REPORT_ERROR(store, true, 8)
304ASAN_REPORT_ERROR(store, true, 16)
305
306// Force the linker to keep the symbols for various ASan interface functions.
307// We want to keep those in the executable in order to let the instrumented
308// dynamic libraries access the symbol even if it is not used by the executable
309// itself. This should help if the build system is removing dead code at link
310// time.
Alexander Potapenko3fe91352012-02-27 14:06:48 +0000311static NOINLINE void force_interface_symbols() {
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000312 volatile int fake_condition = 0; // prevent dead condition elimination.
313 if (fake_condition) {
Kostya Serebryanyf0977db2012-03-14 22:48:09 +0000314 __asan_report_load1(0);
315 __asan_report_load2(0);
316 __asan_report_load4(0);
317 __asan_report_load8(0);
318 __asan_report_load16(0);
319 __asan_report_store1(0);
320 __asan_report_store2(0);
321 __asan_report_store4(0);
322 __asan_report_store8(0);
323 __asan_report_store16(0);
Kostya Serebryany3f4c3872012-05-31 14:35:53 +0000324 __asan_register_global(0, 0, 0);
325 __asan_register_globals(0, 0);
326 __asan_unregister_globals(0, 0);
327 __asan_set_death_callback(0);
328 __asan_set_error_report_callback(0);
Alexander Potapenkodadc45d2012-03-06 11:45:59 +0000329 __asan_handle_no_return();
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000330 }
331}
332
333// -------------------------- Init ------------------- {{{1
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000334static void asan_atexit() {
Alexey Samsonove9541012012-06-06 13:11:29 +0000335 AsanPrintf("AddressSanitizer exit stats:\n");
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000336 __asan_print_accumulated_stats();
337}
338
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000339} // namespace __asan
340
Kostya Serebryany4803ab92012-01-09 18:53:15 +0000341// ---------------------- Interface ---------------- {{{1
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000342using namespace __asan; // NOLINT
343
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000344int __asan_set_error_exit_code(int exit_code) {
Alexey Samsonovcb8c4dc2012-07-09 14:36:04 +0000345 int old = flags()->exitcode;
346 flags()->exitcode = exit_code;
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000347 return old;
348}
349
Alexander Potapenkodadc45d2012-03-06 11:45:59 +0000350void NOINLINE __asan_handle_no_return() {
Kostya Serebryanyf54b1f92012-02-08 21:33:27 +0000351 int local_stack;
352 AsanThread *curr_thread = asanThreadRegistry().GetCurrent();
353 CHECK(curr_thread);
Kostya Serebryany3f4c3872012-05-31 14:35:53 +0000354 uptr top = curr_thread->stack_top();
355 uptr bottom = ((uptr)&local_stack - kPageSize) & ~(kPageSize-1);
Kostya Serebryanyf54b1f92012-02-08 21:33:27 +0000356 PoisonShadow(bottom, top - bottom, 0);
357}
358
Alexander Potapenko2f3f9622012-03-01 14:39:21 +0000359void NOINLINE __asan_set_death_callback(void (*callback)(void)) {
Kostya Serebryanye1fe0fd2012-02-13 21:24:29 +0000360 death_callback = callback;
361}
362
Alexander Potapenko3fe91352012-02-27 14:06:48 +0000363void NOINLINE __asan_set_error_report_callback(void (*callback)(const char*)) {
364 error_report_callback = callback;
365 if (callback) {
Alexander Potapenko8ca02782012-05-12 12:33:41 +0000366 error_message_buffer_size = 1 << 16;
Alexander Potapenko3fe91352012-02-27 14:06:48 +0000367 error_message_buffer =
Alexey Samsonova25b3462012-06-06 16:15:07 +0000368 (char*)MmapOrDie(error_message_buffer_size, __FUNCTION__);
Alexander Potapenko3fe91352012-02-27 14:06:48 +0000369 error_message_buffer_pos = 0;
370 }
371}
372
Kostya Serebryany9aead372012-05-31 14:11:07 +0000373void __asan_report_error(uptr pc, uptr bp, uptr sp,
374 uptr addr, bool is_write, uptr access_size) {
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000375 // Do not print more than one report, otherwise they will mix up.
Dmitry Vyukovfce5bd42012-06-29 16:58:33 +0000376 static atomic_uint32_t num_calls;
377 if (atomic_fetch_add(&num_calls, 1, memory_order_relaxed) != 0) return;
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000378
Alexey Samsonove9541012012-06-06 13:11:29 +0000379 AsanPrintf("===================================================="
380 "=============\n");
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000381 const char *bug_descr = "unknown-crash";
382 if (AddrIsInMem(addr)) {
Kostya Serebryanyee392552012-05-31 15:02:07 +0000383 u8 *shadow_addr = (u8*)MemToShadow(addr);
Kostya Serebryanyacd5c612011-12-07 21:30:20 +0000384 // If we are accessing 16 bytes, look at the second shadow byte.
385 if (*shadow_addr == 0 && access_size > SHADOW_GRANULARITY)
386 shadow_addr++;
387 // If we are in the partial right redzone, look at the next shadow byte.
388 if (*shadow_addr > 0 && *shadow_addr < 128)
389 shadow_addr++;
390 switch (*shadow_addr) {
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000391 case kAsanHeapLeftRedzoneMagic:
392 case kAsanHeapRightRedzoneMagic:
393 bug_descr = "heap-buffer-overflow";
394 break;
395 case kAsanHeapFreeMagic:
396 bug_descr = "heap-use-after-free";
397 break;
398 case kAsanStackLeftRedzoneMagic:
399 bug_descr = "stack-buffer-underflow";
400 break;
401 case kAsanStackMidRedzoneMagic:
402 case kAsanStackRightRedzoneMagic:
403 case kAsanStackPartialRedzoneMagic:
404 bug_descr = "stack-buffer-overflow";
405 break;
406 case kAsanStackAfterReturnMagic:
407 bug_descr = "stack-use-after-return";
408 break;
409 case kAsanUserPoisonedMemoryMagic:
410 bug_descr = "use-after-poison";
411 break;
412 case kAsanGlobalRedzoneMagic:
413 bug_descr = "global-buffer-overflow";
414 break;
415 }
416 }
417
Kostya Serebryanyc4b34d92011-12-09 01:49:31 +0000418 AsanThread *curr_thread = asanThreadRegistry().GetCurrent();
Kostya Serebryanye0cff0b2012-06-06 15:06:58 +0000419 u32 curr_tid = asanThreadRegistry().GetCurrentTidOrInvalid();
Kostya Serebryanyc4b34d92011-12-09 01:49:31 +0000420
421 if (curr_thread) {
422 // We started reporting an error message. Stop using the fake stack
423 // in case we will call an instrumented function from a symbolizer.
424 curr_thread->fake_stack().StopUsingFakeStack();
425 }
426
Alexey Samsonove9541012012-06-06 13:11:29 +0000427 AsanReport("ERROR: AddressSanitizer %s on address "
428 "%p at pc 0x%zx bp 0x%zx sp 0x%zx\n",
429 bug_descr, (void*)addr, pc, bp, sp);
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000430
Alexey Samsonove9541012012-06-06 13:11:29 +0000431 AsanPrintf("%s of size %zu at %p thread T%d\n",
432 access_size ? (is_write ? "WRITE" : "READ") : "ACCESS",
433 access_size, (void*)addr, curr_tid);
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000434
Alexey Samsonovcb8c4dc2012-07-09 14:36:04 +0000435 if (flags()->debug) {
Kostya Serebryany3f4c3872012-05-31 14:35:53 +0000436 PrintBytes("PC: ", (uptr*)pc);
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000437 }
438
Evgeniy Stepanov9cfa1942012-01-19 11:34:18 +0000439 GET_STACK_TRACE_WITH_PC_AND_BP(kStackTraceMax, pc, bp);
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000440 stack.PrintStack();
441
442 CHECK(AddrIsInMem(addr));
443
444 DescribeAddress(addr, access_size);
445
Kostya Serebryany3f4c3872012-05-31 14:35:53 +0000446 uptr shadow_addr = MemToShadow(addr);
Alexey Samsonove9541012012-06-06 13:11:29 +0000447 AsanReport("ABORTING\n");
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000448 __asan_print_accumulated_stats();
Alexey Samsonove9541012012-06-06 13:11:29 +0000449 AsanPrintf("Shadow byte and word:\n");
450 AsanPrintf(" %p: %x\n", (void*)shadow_addr, *(unsigned char*)shadow_addr);
Kostya Serebryany3f4c3872012-05-31 14:35:53 +0000451 uptr aligned_shadow = shadow_addr & ~(kWordSize - 1);
452 PrintBytes(" ", (uptr*)(aligned_shadow));
Alexey Samsonove9541012012-06-06 13:11:29 +0000453 AsanPrintf("More shadow bytes:\n");
Kostya Serebryany3f4c3872012-05-31 14:35:53 +0000454 PrintBytes(" ", (uptr*)(aligned_shadow-4*kWordSize));
455 PrintBytes(" ", (uptr*)(aligned_shadow-3*kWordSize));
456 PrintBytes(" ", (uptr*)(aligned_shadow-2*kWordSize));
457 PrintBytes(" ", (uptr*)(aligned_shadow-1*kWordSize));
458 PrintBytes("=>", (uptr*)(aligned_shadow+0*kWordSize));
459 PrintBytes(" ", (uptr*)(aligned_shadow+1*kWordSize));
460 PrintBytes(" ", (uptr*)(aligned_shadow+2*kWordSize));
461 PrintBytes(" ", (uptr*)(aligned_shadow+3*kWordSize));
462 PrintBytes(" ", (uptr*)(aligned_shadow+4*kWordSize));
Alexander Potapenko3fe91352012-02-27 14:06:48 +0000463 if (error_report_callback) {
464 error_report_callback(error_message_buffer);
465 }
Alexey Samsonov47657ce2012-06-06 07:02:44 +0000466 Die();
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000467}
468
Alexander Potapenkofca72fd2012-05-25 15:37:16 +0000469
470void __asan_init() {
471 if (asan_inited) return;
472 asan_init_is_running = true;
473
474 // Make sure we are not statically linked.
475 AsanDoesNotSupportStaticLinkage();
476
Alexey Samsonovcb8c4dc2012-07-09 14:36:04 +0000477 // Initialize flags.
Alexey Samsonov3dbeabb2012-06-14 14:07:21 +0000478 const char *options = GetEnv("ASAN_OPTIONS");
Alexey Samsonovcb8c4dc2012-07-09 14:36:04 +0000479 InitializeFlags(flags(), options);
Alexander Potapenkofeb47932012-03-16 16:38:31 +0000480
Alexey Samsonovcb8c4dc2012-07-09 14:36:04 +0000481 if (flags()->verbosity && options) {
Alexander Potapenkofeb47932012-03-16 16:38:31 +0000482 Report("Parsed ASAN_OPTIONS: %s\n", options);
483 }
484
Alexey Samsonovcb8c4dc2012-07-09 14:36:04 +0000485 if (flags()->atexit) {
Alexey Samsonovb823e3c2012-02-22 14:07:06 +0000486 Atexit(asan_atexit);
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000487 }
488
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000489 // interceptors
490 InitializeAsanInterceptors();
491
492 ReplaceSystemMalloc();
Alexey Samsonov4d5f98d2012-04-06 08:21:08 +0000493 ReplaceOperatorsNewAndDelete();
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000494
Alexey Samsonovcb8c4dc2012-07-09 14:36:04 +0000495 if (flags()->verbosity) {
Alexey Samsonove4309e82012-06-06 10:54:25 +0000496 Printf("|| `[%p, %p]` || HighMem ||\n",
497 (void*)kHighMemBeg, (void*)kHighMemEnd);
498 Printf("|| `[%p, %p]` || HighShadow ||\n",
499 (void*)kHighShadowBeg, (void*)kHighShadowEnd);
500 Printf("|| `[%p, %p]` || ShadowGap ||\n",
501 (void*)kShadowGapBeg, (void*)kShadowGapEnd);
502 Printf("|| `[%p, %p]` || LowShadow ||\n",
503 (void*)kLowShadowBeg, (void*)kLowShadowEnd);
504 Printf("|| `[%p, %p]` || LowMem ||\n",
505 (void*)kLowMemBeg, (void*)kLowMemEnd);
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000506 Printf("MemToShadow(shadow): %p %p %p %p\n",
Alexey Samsonove4309e82012-06-06 10:54:25 +0000507 (void*)MEM_TO_SHADOW(kLowShadowBeg),
508 (void*)MEM_TO_SHADOW(kLowShadowEnd),
509 (void*)MEM_TO_SHADOW(kHighShadowBeg),
510 (void*)MEM_TO_SHADOW(kHighShadowEnd));
Alexey Samsonovcb8c4dc2012-07-09 14:36:04 +0000511 Printf("red_zone=%zu\n", (uptr)flags()->redzone);
512 Printf("malloc_context_size=%zu\n", (uptr)flags()->malloc_context_size);
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000513
Kostya Serebryany3f4c3872012-05-31 14:35:53 +0000514 Printf("SHADOW_SCALE: %zx\n", (uptr)SHADOW_SCALE);
515 Printf("SHADOW_GRANULARITY: %zx\n", (uptr)SHADOW_GRANULARITY);
516 Printf("SHADOW_OFFSET: %zx\n", (uptr)SHADOW_OFFSET);
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000517 CHECK(SHADOW_SCALE >= 3 && SHADOW_SCALE <= 7);
518 }
519
Alexey Samsonovcb8c4dc2012-07-09 14:36:04 +0000520 if (flags()->disable_core) {
Alexey Samsonovbe7420c2012-06-15 06:08:19 +0000521 DisableCoreDumper();
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000522 }
523
Alexey Samsonovdd3a9112012-06-15 07:29:14 +0000524 uptr shadow_start = kLowShadowBeg;
525 if (kLowShadowBeg > 0) shadow_start -= kMmapGranularity;
526 uptr shadow_end = kHighShadowEnd;
527 if (MemoryRangeIsAvailable(shadow_start, shadow_end)) {
Kostya Serebryanya7e760a2012-01-09 19:18:27 +0000528 if (kLowShadowBeg != kLowShadowEnd) {
Timur Iskhodzhanov3e81fe42012-02-09 17:20:14 +0000529 // mmap the low shadow plus at least one page.
530 ReserveShadowMemoryRange(kLowShadowBeg - kMmapGranularity, kLowShadowEnd);
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000531 }
Kostya Serebryanya7e760a2012-01-09 19:18:27 +0000532 // mmap the high shadow.
533 ReserveShadowMemoryRange(kHighShadowBeg, kHighShadowEnd);
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000534 // protect the gap
Alexey Samsonovf607fc12012-06-14 14:42:58 +0000535 void *prot = Mprotect(kShadowGapBeg, kShadowGapEnd - kShadowGapBeg + 1);
Kostya Serebryanya874fe52011-12-28 23:28:54 +0000536 CHECK(prot == (void*)kShadowGapBeg);
Alexander Potapenkoc50e8352012-02-13 15:11:23 +0000537 } else {
538 Report("Shadow memory range interleaves with an existing memory mapping. "
539 "ASan cannot proceed correctly. ABORTING.\n");
Alexey Samsonovbe7420c2012-06-15 06:08:19 +0000540 DumpProcessMap();
Alexey Samsonov47657ce2012-06-06 07:02:44 +0000541 Die();
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000542 }
543
Alexander Potapenkof03d8af2012-04-05 10:54:52 +0000544 InstallSignalHandlers();
545
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000546 // On Linux AsanThread::ThreadStart() calls malloc() that's why asan_inited
547 // should be set to 1 prior to initializing the threads.
548 asan_inited = 1;
549 asan_init_is_running = false;
550
551 asanThreadRegistry().Init();
552 asanThreadRegistry().GetMain()->ThreadStart();
Kostya Serebryany51e75c42011-12-28 00:59:39 +0000553 force_interface_symbols(); // no-op.
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000554
Alexey Samsonovcb8c4dc2012-07-09 14:36:04 +0000555 if (flags()->verbosity) {
Kostya Serebryanyd6567c52011-12-01 21:40:52 +0000556 Report("AddressSanitizer Init done\n");
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000557 }
558}
Evgeniy Stepanov8bcc6b92012-01-11 08:17:19 +0000559
560#if defined(ASAN_USE_PREINIT_ARRAY)
Timur Iskhodzhanov38ed7362012-02-21 16:24:23 +0000561 // On Linux, we force __asan_init to be called before anyone else
562 // by placing it into .preinit_array section.
563 // FIXME: do we have anything like this on Mac?
564 __attribute__((section(".preinit_array")))
565 typeof(__asan_init) *__asan_preinit =__asan_init;
566#elif defined(_WIN32) && defined(_DLL)
567 // On Windows, when using dynamic CRT (/MD), we can put a pointer
568 // to __asan_init into the global list of C initializers.
569 // See crt0dat.c in the CRT sources for the details.
Timur Iskhodzhanov39c22ee2012-02-22 09:28:14 +0000570 #pragma section(".CRT$XIB", long, read) // NOLINT
Timur Iskhodzhanov38ed7362012-02-21 16:24:23 +0000571 __declspec(allocate(".CRT$XIB")) void (*__asan_preinit)() = __asan_init;
Evgeniy Stepanov8bcc6b92012-01-11 08:17:19 +0000572#endif