blob: d0dff1da04ef0816932eff2b92cc9eb8f4dfa60a [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) {
36 Report("Sleeping for %d second(s)\n", FLAG_sleep_before_dying);
37 SleepForSeconds(FLAG_sleep_before_dying);
38 }
39 if (FLAG_unmap_shadow_on_exit)
40 AsanUnmapOrDie((void*)kLowShadowBeg, kHighShadowEnd - kLowShadowBeg);
41 if (death_callback)
42 death_callback();
43 if (FLAG_abort_on_error)
44 Abort();
45 Exit(FLAG_exitcode);
46}
47
48} // namespace __sanitizer
49
Kostya Serebryany1e172b42011-11-30 01:07:02 +000050namespace __asan {
51
52// -------------------------- Flags ------------------------- {{{1
Kostya Serebryany3f4c3872012-05-31 14:35:53 +000053static const uptr kMallocContextSize = 30;
Kostya Serebryany1e172b42011-11-30 01:07:02 +000054
Kostya Serebryany3f4c3872012-05-31 14:35:53 +000055uptr FLAG_malloc_context_size = kMallocContextSize;
56uptr FLAG_max_malloc_fill_size = 0;
Kostya Serebryanyee392552012-05-31 15:02:07 +000057s64 FLAG_v = 0;
Kostya Serebryany3f4c3872012-05-31 14:35:53 +000058uptr FLAG_redzone = (ASAN_LOW_MEMORY) ? 64 : 128; // power of two, >= 32
59uptr FLAG_quarantine_size = (ASAN_LOW_MEMORY) ? 1UL << 24 : 1UL << 28;
Kostya Serebryanyee392552012-05-31 15:02:07 +000060static s64 FLAG_atexit = 0;
Alexander Potapenko62f10e72012-05-28 16:21:19 +000061bool FLAG_poison_shadow = 1;
Kostya Serebryanyee392552012-05-31 15:02:07 +000062s64 FLAG_report_globals = 1;
Alexander Potapenko62f10e72012-05-28 16:21:19 +000063bool FLAG_handle_segv = ASAN_NEEDS_SEGV;
64bool FLAG_use_sigaltstack = 0;
Alexey Samsonov5f2fe372012-06-04 11:20:17 +000065bool FLAG_symbolize = 0;
Kostya Serebryanyee392552012-05-31 15:02:07 +000066s64 FLAG_demangle = 1;
67s64 FLAG_debug = 0;
Alexander Potapenko62f10e72012-05-28 16:21:19 +000068bool FLAG_replace_cfallocator = 1; // Used on Mac only.
69bool FLAG_replace_str = 1;
70bool FLAG_replace_intrin = 1;
71bool FLAG_use_fake_stack = 1;
Kostya Serebryanyee392552012-05-31 15:02:07 +000072s64 FLAG_exitcode = ASAN_DEFAULT_FAILURE_EXITCODE;
Alexander Potapenko62f10e72012-05-28 16:21:19 +000073bool FLAG_allow_user_poisoning = 1;
Kostya Serebryanyee392552012-05-31 15:02:07 +000074s64 FLAG_sleep_before_dying = 0;
Alexander Potapenko62f10e72012-05-28 16:21:19 +000075bool FLAG_abort_on_error = 0;
76bool FLAG_unmap_shadow_on_exit = 0;
77bool FLAG_disable_core = __WORDSIZE == 64;
78bool FLAG_check_malloc_usable_size = 1;
Kostya Serebryany1e172b42011-11-30 01:07:02 +000079
80// -------------------------- Globals --------------------- {{{1
81int asan_inited;
82bool asan_init_is_running;
Alexey Samsonov47657ce2012-06-06 07:02:44 +000083void (*death_callback)(void);
Alexander Potapenko3fe91352012-02-27 14:06:48 +000084static void (*error_report_callback)(const char*);
Kostya Serebryany3f4c3872012-05-31 14:35:53 +000085char *error_message_buffer = 0;
86uptr error_message_buffer_pos = 0;
87uptr error_message_buffer_size = 0;
Kostya Serebryany1e172b42011-11-30 01:07:02 +000088
Kostya Serebryany1e172b42011-11-30 01:07:02 +000089// -------------------------- Misc ---------------- {{{1
90void ShowStatsAndAbort() {
91 __asan_print_accumulated_stats();
Alexey Samsonov47657ce2012-06-06 07:02:44 +000092 Die();
Kostya Serebryany1e172b42011-11-30 01:07:02 +000093}
94
Kostya Serebryany3f4c3872012-05-31 14:35:53 +000095static void PrintBytes(const char *before, uptr *a) {
Kostya Serebryanyee392552012-05-31 15:02:07 +000096 u8 *bytes = (u8*)a;
Kostya Serebryany3f4c3872012-05-31 14:35:53 +000097 uptr byte_num = (__WORDSIZE) / 8;
Evgeniy Stepanov739eb792012-03-21 11:32:46 +000098 Printf("%s%p:", before, (void*)a);
Kostya Serebryany3f4c3872012-05-31 14:35:53 +000099 for (uptr i = 0; i < byte_num; i++) {
Evgeniy Stepanov739eb792012-03-21 11:32:46 +0000100 Printf(" %x%x", bytes[i] >> 4, bytes[i] & 15);
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000101 }
102 Printf("\n");
103}
104
Kostya Serebryany3f4c3872012-05-31 14:35:53 +0000105uptr ReadFileToBuffer(const char *file_name, char **buff,
106 uptr *buff_size, uptr max_len) {
107 const uptr kMinFileLen = kPageSize;
108 uptr read_len = 0;
Kostya Serebryanyde496f42011-12-28 22:58:01 +0000109 *buff = 0;
Kostya Serebryanydf499b42012-01-05 00:44:33 +0000110 *buff_size = 0;
Kostya Serebryanyde496f42011-12-28 22:58:01 +0000111 // The files we usually open are not seekable, so try different buffer sizes.
Kostya Serebryany3f4c3872012-05-31 14:35:53 +0000112 for (uptr size = kMinFileLen; size <= max_len; size *= 2) {
Alexey Samsonov9552db72012-06-05 07:25:47 +0000113 fd_t fd = internal_open(file_name, /*write*/ false);
Alexander Potapenkof2b1df72012-05-10 12:03:09 +0000114 if (fd < 0) return 0;
Kostya Serebryanydf499b42012-01-05 00:44:33 +0000115 AsanUnmapOrDie(*buff, *buff_size);
Kostya Serebryanyde496f42011-12-28 22:58:01 +0000116 *buff = (char*)AsanMmapSomewhereOrDie(size, __FUNCTION__);
Kostya Serebryanydf499b42012-01-05 00:44:33 +0000117 *buff_size = size;
Kostya Serebryany454a0642012-01-17 18:00:07 +0000118 // Read up to one page at a time.
119 read_len = 0;
120 bool reached_eof = false;
121 while (read_len + kPageSize <= size) {
Alexey Samsonov2221f552012-06-05 08:48:10 +0000122 uptr just_read = internal_read(fd, *buff + read_len, kPageSize);
Kostya Serebryany454a0642012-01-17 18:00:07 +0000123 if (just_read == 0) {
124 reached_eof = true;
125 break;
126 }
127 read_len += just_read;
128 }
Alexey Samsonov2221f552012-06-05 08:48:10 +0000129 internal_close(fd);
Kostya Serebryany454a0642012-01-17 18:00:07 +0000130 if (reached_eof) // We've read the whole file.
Kostya Serebryanyde496f42011-12-28 22:58:01 +0000131 break;
132 }
133 return read_len;
134}
135
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000136// ---------------------- mmap -------------------- {{{1
Kostya Serebryany3f4c3872012-05-31 14:35:53 +0000137void OutOfMemoryMessageAndDie(const char *mem_type, uptr size) {
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000138 Report("ERROR: AddressSanitizer failed to allocate "
Evgeniy Stepanov739eb792012-03-21 11:32:46 +0000139 "0x%zx (%zd) bytes of %s\n",
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000140 size, size, mem_type);
Kostya Serebryanyde496f42011-12-28 22:58:01 +0000141 PRINT_CURRENT_STACK();
142 ShowStatsAndAbort();
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000143}
144
Kostya Serebryanya874fe52011-12-28 23:28:54 +0000145// Reserve memory range [beg, end].
Kostya Serebryany3f4c3872012-05-31 14:35:53 +0000146static void ReserveShadowMemoryRange(uptr beg, uptr end) {
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000147 CHECK((beg % kPageSize) == 0);
148 CHECK(((end + 1) % kPageSize) == 0);
Kostya Serebryany3f4c3872012-05-31 14:35:53 +0000149 uptr size = end - beg + 1;
Kostya Serebryanya874fe52011-12-28 23:28:54 +0000150 void *res = AsanMmapFixedNoReserve(beg, size);
151 CHECK(res == (void*)beg && "ReserveShadowMemoryRange failed");
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000152}
153
Kostya Serebryanyb89567c2011-12-02 21:02:20 +0000154// ---------------------- LowLevelAllocator ------------- {{{1
Kostya Serebryany3f4c3872012-05-31 14:35:53 +0000155void *LowLevelAllocator::Allocate(uptr size) {
Kostya Serebryanyb89567c2011-12-02 21:02:20 +0000156 CHECK((size & (size - 1)) == 0 && "size must be a power of two");
157 if (allocated_end_ - allocated_current_ < size) {
Kostya Serebryany3f4c3872012-05-31 14:35:53 +0000158 uptr size_to_allocate = Max(size, kPageSize);
Kostya Serebryanyde496f42011-12-28 22:58:01 +0000159 allocated_current_ =
160 (char*)AsanMmapSomewhereOrDie(size_to_allocate, __FUNCTION__);
Kostya Serebryanyb89567c2011-12-02 21:02:20 +0000161 allocated_end_ = allocated_current_ + size_to_allocate;
Kostya Serebryany3f4c3872012-05-31 14:35:53 +0000162 PoisonShadow((uptr)allocated_current_, size_to_allocate,
Kostya Serebryany6b30e2c2011-12-15 17:41:30 +0000163 kAsanInternalHeapMagic);
Kostya Serebryanyb89567c2011-12-02 21:02:20 +0000164 }
165 CHECK(allocated_end_ - allocated_current_ >= size);
166 void *res = allocated_current_;
167 allocated_current_ += size;
168 return res;
169}
170
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000171// ---------------------- DescribeAddress -------------------- {{{1
Kostya Serebryany3f4c3872012-05-31 14:35:53 +0000172static bool DescribeStackAddress(uptr addr, uptr access_size) {
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000173 AsanThread *t = asanThreadRegistry().FindThreadByStackAddress(addr);
174 if (!t) return false;
Kostya Serebryanyee392552012-05-31 15:02:07 +0000175 const sptr kBufSize = 4095;
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000176 char buf[kBufSize];
Kostya Serebryany3f4c3872012-05-31 14:35:53 +0000177 uptr offset = 0;
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000178 const char *frame_descr = t->GetFrameNameByAddr(addr, &offset);
179 // This string is created by the compiler and has the following form:
180 // "FunctioName n alloc_1 alloc_2 ... alloc_n"
181 // where alloc_i looks like "offset size len ObjectName ".
182 CHECK(frame_descr);
183 // Report the function name and the offset.
Timur Iskhodzhanov53627172012-02-14 19:33:04 +0000184 const char *name_end = internal_strchr(frame_descr, ' ');
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000185 CHECK(name_end);
186 buf[0] = 0;
Kostya Serebryanya4ccf872012-01-09 22:20:49 +0000187 internal_strncat(buf, frame_descr,
188 Min(kBufSize,
Kostya Serebryanyee392552012-05-31 15:02:07 +0000189 static_cast<sptr>(name_end - frame_descr)));
Evgeniy Stepanov739eb792012-03-21 11:32:46 +0000190 Printf("Address %p is located at offset %zu "
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000191 "in frame <%s> of T%d's stack:\n",
192 addr, offset, buf, t->tid());
193 // Report the number of stack objects.
194 char *p;
Kostya Serebryany3f4c3872012-05-31 14:35:53 +0000195 uptr n_objects = internal_simple_strtoll(name_end, &p, 10);
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000196 CHECK(n_objects > 0);
Evgeniy Stepanov739eb792012-03-21 11:32:46 +0000197 Printf(" This frame has %zu object(s):\n", n_objects);
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000198 // Report all objects in this frame.
Kostya Serebryany3f4c3872012-05-31 14:35:53 +0000199 for (uptr i = 0; i < n_objects; i++) {
200 uptr beg, size;
Kostya Serebryanyee392552012-05-31 15:02:07 +0000201 sptr len;
Alexey Samsonov88981022012-02-17 16:15:09 +0000202 beg = internal_simple_strtoll(p, &p, 10);
203 size = internal_simple_strtoll(p, &p, 10);
204 len = internal_simple_strtoll(p, &p, 10);
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000205 if (beg <= 0 || size <= 0 || len < 0 || *p != ' ') {
206 Printf("AddressSanitizer can't parse the stack frame descriptor: |%s|\n",
207 frame_descr);
208 break;
209 }
210 p++;
211 buf[0] = 0;
Kostya Serebryanya4ccf872012-01-09 22:20:49 +0000212 internal_strncat(buf, p, Min(kBufSize, len));
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000213 p += len;
Evgeniy Stepanov739eb792012-03-21 11:32:46 +0000214 Printf(" [%zu, %zu) '%s'\n", beg, beg + size, buf);
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000215 }
216 Printf("HINT: this may be a false positive if your program uses "
217 "some custom stack unwind mechanism\n"
218 " (longjmp and C++ exceptions *are* supported)\n");
219 t->summary()->Announce();
220 return true;
221}
222
Kostya Serebryany3f4c3872012-05-31 14:35:53 +0000223static NOINLINE void DescribeAddress(uptr addr, uptr access_size) {
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000224 // Check if this is a global.
225 if (DescribeAddrIfGlobal(addr))
226 return;
227
228 if (DescribeStackAddress(addr, access_size))
229 return;
230
231 // finally, check if this is a heap.
232 DescribeHeapAddress(addr, access_size);
233}
234
235// -------------------------- Run-time entry ------------------- {{{1
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000236// exported functions
Kostya Serebryany51e75c42011-12-28 00:59:39 +0000237#define ASAN_REPORT_ERROR(type, is_write, size) \
Alexey Samsonov0a4c9062012-06-05 13:50:57 +0000238extern "C" NOINLINE INTERFACE_ATTRIBUTE \
Kostya Serebryany3f4c3872012-05-31 14:35:53 +0000239void __asan_report_ ## type ## size(uptr addr); \
240void __asan_report_ ## type ## size(uptr addr) { \
Kostya Serebryany9f311bb2012-03-15 01:36:00 +0000241 GET_CALLER_PC_BP_SP; \
Kostya Serebryany51e75c42011-12-28 00:59:39 +0000242 __asan_report_error(pc, bp, sp, addr, is_write, size); \
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000243}
244
245ASAN_REPORT_ERROR(load, false, 1)
246ASAN_REPORT_ERROR(load, false, 2)
247ASAN_REPORT_ERROR(load, false, 4)
248ASAN_REPORT_ERROR(load, false, 8)
249ASAN_REPORT_ERROR(load, false, 16)
250ASAN_REPORT_ERROR(store, true, 1)
251ASAN_REPORT_ERROR(store, true, 2)
252ASAN_REPORT_ERROR(store, true, 4)
253ASAN_REPORT_ERROR(store, true, 8)
254ASAN_REPORT_ERROR(store, true, 16)
255
256// Force the linker to keep the symbols for various ASan interface functions.
257// We want to keep those in the executable in order to let the instrumented
258// dynamic libraries access the symbol even if it is not used by the executable
259// itself. This should help if the build system is removing dead code at link
260// time.
Alexander Potapenko3fe91352012-02-27 14:06:48 +0000261static NOINLINE void force_interface_symbols() {
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000262 volatile int fake_condition = 0; // prevent dead condition elimination.
263 if (fake_condition) {
Kostya Serebryanyf0977db2012-03-14 22:48:09 +0000264 __asan_report_load1(0);
265 __asan_report_load2(0);
266 __asan_report_load4(0);
267 __asan_report_load8(0);
268 __asan_report_load16(0);
269 __asan_report_store1(0);
270 __asan_report_store2(0);
271 __asan_report_store4(0);
272 __asan_report_store8(0);
273 __asan_report_store16(0);
Kostya Serebryany3f4c3872012-05-31 14:35:53 +0000274 __asan_register_global(0, 0, 0);
275 __asan_register_globals(0, 0);
276 __asan_unregister_globals(0, 0);
277 __asan_set_death_callback(0);
278 __asan_set_error_report_callback(0);
Alexander Potapenkodadc45d2012-03-06 11:45:59 +0000279 __asan_handle_no_return();
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000280 }
281}
282
283// -------------------------- Init ------------------- {{{1
Alexander Potapenko62f10e72012-05-28 16:21:19 +0000284static void IntFlagValue(const char *flags, const char *flag,
Kostya Serebryanyee392552012-05-31 15:02:07 +0000285 s64 *out_val) {
Alexander Potapenko62f10e72012-05-28 16:21:19 +0000286 if (!flags) return;
Kostya Serebryanya4ccf872012-01-09 22:20:49 +0000287 const char *str = internal_strstr(flags, flag);
Alexander Potapenko62f10e72012-05-28 16:21:19 +0000288 if (!str) return;
289 *out_val = internal_atoll(str + internal_strlen(flag));
290}
291
292static void BoolFlagValue(const char *flags, const char *flag,
293 bool *out_val) {
294 if (!flags) return;
295 const char *str = internal_strstr(flags, flag);
296 if (!str) return;
Alexander Potapenko6b6dc572012-05-30 15:28:45 +0000297 const char *suffix = str + internal_strlen(flag);
Alexander Potapenko62f10e72012-05-28 16:21:19 +0000298 if (!internal_atoll(str + internal_strlen(flag))) {
Alexander Potapenko6b6dc572012-05-30 15:28:45 +0000299 if (suffix[0] == '0') {
Alexander Potapenko62f10e72012-05-28 16:21:19 +0000300 *out_val = false;
301 return;
302 }
303 } else {
304 *out_val = true;
305 return;
306 }
Alexander Potapenko6b6dc572012-05-30 15:28:45 +0000307 switch (suffix[0]) {
Alexander Potapenko62f10e72012-05-28 16:21:19 +0000308 case 'y':
309 case 't': {
310 *out_val = true;
311 break;
312 }
313 case 'n':
314 case 'f': {
315 *out_val = false;
316 break;
317 }
318 default: return;
319 }
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000320}
321
322static void asan_atexit() {
323 Printf("AddressSanitizer exit stats:\n");
324 __asan_print_accumulated_stats();
325}
326
327void CheckFailed(const char *cond, const char *file, int line) {
Kostya Serebryanya7e760a2012-01-09 19:18:27 +0000328 Report("CHECK failed: %s at %s:%d\n", cond, file, line);
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000329 PRINT_CURRENT_STACK();
330 ShowStatsAndAbort();
331}
332
333} // namespace __asan
334
Kostya Serebryany4803ab92012-01-09 18:53:15 +0000335// ---------------------- Interface ---------------- {{{1
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000336using namespace __asan; // NOLINT
337
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000338int __asan_set_error_exit_code(int exit_code) {
339 int old = FLAG_exitcode;
340 FLAG_exitcode = exit_code;
341 return old;
342}
343
Alexander Potapenkodadc45d2012-03-06 11:45:59 +0000344void NOINLINE __asan_handle_no_return() {
Kostya Serebryanyf54b1f92012-02-08 21:33:27 +0000345 int local_stack;
346 AsanThread *curr_thread = asanThreadRegistry().GetCurrent();
347 CHECK(curr_thread);
Kostya Serebryany3f4c3872012-05-31 14:35:53 +0000348 uptr top = curr_thread->stack_top();
349 uptr bottom = ((uptr)&local_stack - kPageSize) & ~(kPageSize-1);
Kostya Serebryanyf54b1f92012-02-08 21:33:27 +0000350 PoisonShadow(bottom, top - bottom, 0);
351}
352
Alexander Potapenko2f3f9622012-03-01 14:39:21 +0000353void NOINLINE __asan_set_death_callback(void (*callback)(void)) {
Kostya Serebryanye1fe0fd2012-02-13 21:24:29 +0000354 death_callback = callback;
355}
356
Alexander Potapenko3fe91352012-02-27 14:06:48 +0000357void NOINLINE __asan_set_error_report_callback(void (*callback)(const char*)) {
358 error_report_callback = callback;
359 if (callback) {
Alexander Potapenko8ca02782012-05-12 12:33:41 +0000360 error_message_buffer_size = 1 << 16;
Alexander Potapenko3fe91352012-02-27 14:06:48 +0000361 error_message_buffer =
362 (char*)AsanMmapSomewhereOrDie(error_message_buffer_size, __FUNCTION__);
363 error_message_buffer_pos = 0;
364 }
365}
366
Kostya Serebryany9aead372012-05-31 14:11:07 +0000367void __asan_report_error(uptr pc, uptr bp, uptr sp,
368 uptr addr, bool is_write, uptr access_size) {
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000369 // Do not print more than one report, otherwise they will mix up.
370 static int num_calls = 0;
371 if (AtomicInc(&num_calls) > 1) return;
372
373 Printf("=================================================================\n");
374 const char *bug_descr = "unknown-crash";
375 if (AddrIsInMem(addr)) {
Kostya Serebryanyee392552012-05-31 15:02:07 +0000376 u8 *shadow_addr = (u8*)MemToShadow(addr);
Kostya Serebryanyacd5c612011-12-07 21:30:20 +0000377 // If we are accessing 16 bytes, look at the second shadow byte.
378 if (*shadow_addr == 0 && access_size > SHADOW_GRANULARITY)
379 shadow_addr++;
380 // If we are in the partial right redzone, look at the next shadow byte.
381 if (*shadow_addr > 0 && *shadow_addr < 128)
382 shadow_addr++;
383 switch (*shadow_addr) {
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000384 case kAsanHeapLeftRedzoneMagic:
385 case kAsanHeapRightRedzoneMagic:
386 bug_descr = "heap-buffer-overflow";
387 break;
388 case kAsanHeapFreeMagic:
389 bug_descr = "heap-use-after-free";
390 break;
391 case kAsanStackLeftRedzoneMagic:
392 bug_descr = "stack-buffer-underflow";
393 break;
394 case kAsanStackMidRedzoneMagic:
395 case kAsanStackRightRedzoneMagic:
396 case kAsanStackPartialRedzoneMagic:
397 bug_descr = "stack-buffer-overflow";
398 break;
399 case kAsanStackAfterReturnMagic:
400 bug_descr = "stack-use-after-return";
401 break;
402 case kAsanUserPoisonedMemoryMagic:
403 bug_descr = "use-after-poison";
404 break;
405 case kAsanGlobalRedzoneMagic:
406 bug_descr = "global-buffer-overflow";
407 break;
408 }
409 }
410
Kostya Serebryanyc4b34d92011-12-09 01:49:31 +0000411 AsanThread *curr_thread = asanThreadRegistry().GetCurrent();
412 int curr_tid = asanThreadRegistry().GetCurrentTidOrMinusOne();
413
414 if (curr_thread) {
415 // We started reporting an error message. Stop using the fake stack
416 // in case we will call an instrumented function from a symbolizer.
417 curr_thread->fake_stack().StopUsingFakeStack();
418 }
419
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000420 Report("ERROR: AddressSanitizer %s on address "
Evgeniy Stepanov739eb792012-03-21 11:32:46 +0000421 "%p at pc 0x%zx bp 0x%zx sp 0x%zx\n",
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000422 bug_descr, addr, pc, bp, sp);
423
Evgeniy Stepanov739eb792012-03-21 11:32:46 +0000424 Printf("%s of size %zu at %p thread T%d\n",
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000425 access_size ? (is_write ? "WRITE" : "READ") : "ACCESS",
Kostya Serebryanyc4b34d92011-12-09 01:49:31 +0000426 access_size, addr, curr_tid);
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000427
428 if (FLAG_debug) {
Kostya Serebryany3f4c3872012-05-31 14:35:53 +0000429 PrintBytes("PC: ", (uptr*)pc);
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000430 }
431
Evgeniy Stepanov9cfa1942012-01-19 11:34:18 +0000432 GET_STACK_TRACE_WITH_PC_AND_BP(kStackTraceMax, pc, bp);
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000433 stack.PrintStack();
434
435 CHECK(AddrIsInMem(addr));
436
437 DescribeAddress(addr, access_size);
438
Kostya Serebryany3f4c3872012-05-31 14:35:53 +0000439 uptr shadow_addr = MemToShadow(addr);
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000440 Report("ABORTING\n");
441 __asan_print_accumulated_stats();
442 Printf("Shadow byte and word:\n");
443 Printf(" %p: %x\n", shadow_addr, *(unsigned char*)shadow_addr);
Kostya Serebryany3f4c3872012-05-31 14:35:53 +0000444 uptr aligned_shadow = shadow_addr & ~(kWordSize - 1);
445 PrintBytes(" ", (uptr*)(aligned_shadow));
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000446 Printf("More shadow bytes:\n");
Kostya Serebryany3f4c3872012-05-31 14:35:53 +0000447 PrintBytes(" ", (uptr*)(aligned_shadow-4*kWordSize));
448 PrintBytes(" ", (uptr*)(aligned_shadow-3*kWordSize));
449 PrintBytes(" ", (uptr*)(aligned_shadow-2*kWordSize));
450 PrintBytes(" ", (uptr*)(aligned_shadow-1*kWordSize));
451 PrintBytes("=>", (uptr*)(aligned_shadow+0*kWordSize));
452 PrintBytes(" ", (uptr*)(aligned_shadow+1*kWordSize));
453 PrintBytes(" ", (uptr*)(aligned_shadow+2*kWordSize));
454 PrintBytes(" ", (uptr*)(aligned_shadow+3*kWordSize));
455 PrintBytes(" ", (uptr*)(aligned_shadow+4*kWordSize));
Alexander Potapenko3fe91352012-02-27 14:06:48 +0000456 if (error_report_callback) {
457 error_report_callback(error_message_buffer);
458 }
Alexey Samsonov47657ce2012-06-06 07:02:44 +0000459 Die();
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000460}
461
Alexander Potapenkofca72fd2012-05-25 15:37:16 +0000462static void ParseAsanOptions(const char *options) {
Alexander Potapenko62f10e72012-05-28 16:21:19 +0000463 IntFlagValue(options, "malloc_context_size=",
Kostya Serebryanyee392552012-05-31 15:02:07 +0000464 (s64*)&FLAG_malloc_context_size);
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000465 CHECK(FLAG_malloc_context_size <= kMallocContextSize);
466
Alexander Potapenko62f10e72012-05-28 16:21:19 +0000467 IntFlagValue(options, "max_malloc_fill_size=",
Kostya Serebryanyee392552012-05-31 15:02:07 +0000468 (s64*)&FLAG_max_malloc_fill_size);
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000469
Alexander Potapenko62f10e72012-05-28 16:21:19 +0000470 IntFlagValue(options, "verbosity=", &FLAG_v);
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000471
Kostya Serebryanyee392552012-05-31 15:02:07 +0000472 IntFlagValue(options, "redzone=", (s64*)&FLAG_redzone);
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000473 CHECK(FLAG_redzone >= 32);
474 CHECK((FLAG_redzone & (FLAG_redzone - 1)) == 0);
Kostya Serebryanyee392552012-05-31 15:02:07 +0000475 IntFlagValue(options, "quarantine_size=", (s64*)&FLAG_quarantine_size);
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000476
Alexander Potapenko62f10e72012-05-28 16:21:19 +0000477 IntFlagValue(options, "atexit=", &FLAG_atexit);
478 BoolFlagValue(options, "poison_shadow=", &FLAG_poison_shadow);
479 IntFlagValue(options, "report_globals=", &FLAG_report_globals);
480 BoolFlagValue(options, "handle_segv=", &FLAG_handle_segv);
481 BoolFlagValue(options, "use_sigaltstack=", &FLAG_use_sigaltstack);
482 BoolFlagValue(options, "symbolize=", &FLAG_symbolize);
483 IntFlagValue(options, "demangle=", &FLAG_demangle);
484 IntFlagValue(options, "debug=", &FLAG_debug);
485 BoolFlagValue(options, "replace_cfallocator=", &FLAG_replace_cfallocator);
486 BoolFlagValue(options, "replace_str=", &FLAG_replace_str);
487 BoolFlagValue(options, "replace_intrin=", &FLAG_replace_intrin);
488 BoolFlagValue(options, "use_fake_stack=", &FLAG_use_fake_stack);
489 IntFlagValue(options, "exitcode=", &FLAG_exitcode);
490 BoolFlagValue(options, "allow_user_poisoning=", &FLAG_allow_user_poisoning);
491 IntFlagValue(options, "sleep_before_dying=", &FLAG_sleep_before_dying);
492 BoolFlagValue(options, "abort_on_error=", &FLAG_abort_on_error);
493 BoolFlagValue(options, "unmap_shadow_on_exit=", &FLAG_unmap_shadow_on_exit);
Kostya Serebryanyf8e6fee2012-04-06 01:27:11 +0000494 // By default, disable core dumper on 64-bit --
495 // it makes little sense to dump 16T+ core.
Alexander Potapenko62f10e72012-05-28 16:21:19 +0000496 BoolFlagValue(options, "disable_core=", &FLAG_disable_core);
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000497
Alexander Potapenko37931232012-05-25 15:20:13 +0000498 // Allow the users to work around the bug in Nvidia drivers prior to 295.*.
Alexander Potapenko62f10e72012-05-28 16:21:19 +0000499 BoolFlagValue(options, "check_malloc_usable_size=",
500 &FLAG_check_malloc_usable_size);
Alexander Potapenkofca72fd2012-05-25 15:37:16 +0000501}
502
503void __asan_init() {
504 if (asan_inited) return;
Kostya Serebryanyb3cedf92012-05-29 12:18:18 +0000505 MiniLibcStub(); // FIXME: remove me once mini libc build is tested properly.
Alexander Potapenkofca72fd2012-05-25 15:37:16 +0000506 asan_init_is_running = true;
507
508 // Make sure we are not statically linked.
509 AsanDoesNotSupportStaticLinkage();
510
Alexey Samsonovff20f172012-05-29 09:39:01 +0000511#if !defined(_WIN32)
Alexander Potapenko62f10e72012-05-28 16:21:19 +0000512 if (__asan_default_options) {
Alexander Potapenko62f10e72012-05-28 16:21:19 +0000513 ParseAsanOptions(__asan_default_options);
Alexander Potapenkoe4781f02012-05-30 14:12:20 +0000514 if (FLAG_v) {
515 Report("Using the defaults from __asan_default_options: %s\n",
516 __asan_default_options);
517 }
Alexander Potapenko62f10e72012-05-28 16:21:19 +0000518 }
Alexey Samsonovff20f172012-05-29 09:39:01 +0000519#endif
Alexander Potapenkofca72fd2012-05-25 15:37:16 +0000520 // flags
521 const char *options = AsanGetEnv("ASAN_OPTIONS");
Alexander Potapenkof2981f32012-05-25 15:56:40 +0000522 ParseAsanOptions(options);
Alexander Potapenkofeb47932012-03-16 16:38:31 +0000523
Alexander Potapenko62f10e72012-05-28 16:21:19 +0000524 if (FLAG_v && options) {
Alexander Potapenkofeb47932012-03-16 16:38:31 +0000525 Report("Parsed ASAN_OPTIONS: %s\n", options);
526 }
527
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000528 if (FLAG_atexit) {
Alexey Samsonovb823e3c2012-02-22 14:07:06 +0000529 Atexit(asan_atexit);
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000530 }
531
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000532 // interceptors
533 InitializeAsanInterceptors();
534
535 ReplaceSystemMalloc();
Alexey Samsonov4d5f98d2012-04-06 08:21:08 +0000536 ReplaceOperatorsNewAndDelete();
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000537
538 if (FLAG_v) {
539 Printf("|| `[%p, %p]` || HighMem ||\n", kHighMemBeg, kHighMemEnd);
540 Printf("|| `[%p, %p]` || HighShadow ||\n",
541 kHighShadowBeg, kHighShadowEnd);
542 Printf("|| `[%p, %p]` || ShadowGap ||\n",
543 kShadowGapBeg, kShadowGapEnd);
544 Printf("|| `[%p, %p]` || LowShadow ||\n",
545 kLowShadowBeg, kLowShadowEnd);
546 Printf("|| `[%p, %p]` || LowMem ||\n", kLowMemBeg, kLowMemEnd);
547 Printf("MemToShadow(shadow): %p %p %p %p\n",
548 MEM_TO_SHADOW(kLowShadowBeg),
549 MEM_TO_SHADOW(kLowShadowEnd),
550 MEM_TO_SHADOW(kHighShadowBeg),
551 MEM_TO_SHADOW(kHighShadowEnd));
Kostya Serebryany3f4c3872012-05-31 14:35:53 +0000552 Printf("red_zone=%zu\n", (uptr)FLAG_redzone);
553 Printf("malloc_context_size=%zu\n", (uptr)FLAG_malloc_context_size);
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000554
Kostya Serebryany3f4c3872012-05-31 14:35:53 +0000555 Printf("SHADOW_SCALE: %zx\n", (uptr)SHADOW_SCALE);
556 Printf("SHADOW_GRANULARITY: %zx\n", (uptr)SHADOW_GRANULARITY);
557 Printf("SHADOW_OFFSET: %zx\n", (uptr)SHADOW_OFFSET);
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000558 CHECK(SHADOW_SCALE >= 3 && SHADOW_SCALE <= 7);
559 }
560
Kostya Serebryanyf8e6fee2012-04-06 01:27:11 +0000561 if (FLAG_disable_core) {
Kostya Serebryanyef14ff62012-01-06 02:12:25 +0000562 AsanDisableCoreDumper();
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000563 }
564
Alexander Potapenkoc50e8352012-02-13 15:11:23 +0000565 if (AsanShadowRangeIsAvailable()) {
Kostya Serebryanya7e760a2012-01-09 19:18:27 +0000566 if (kLowShadowBeg != kLowShadowEnd) {
Timur Iskhodzhanov3e81fe42012-02-09 17:20:14 +0000567 // mmap the low shadow plus at least one page.
568 ReserveShadowMemoryRange(kLowShadowBeg - kMmapGranularity, kLowShadowEnd);
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000569 }
Kostya Serebryanya7e760a2012-01-09 19:18:27 +0000570 // mmap the high shadow.
571 ReserveShadowMemoryRange(kHighShadowBeg, kHighShadowEnd);
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000572 // protect the gap
Kostya Serebryanya874fe52011-12-28 23:28:54 +0000573 void *prot = AsanMprotect(kShadowGapBeg, kShadowGapEnd - kShadowGapBeg + 1);
574 CHECK(prot == (void*)kShadowGapBeg);
Alexander Potapenkoc50e8352012-02-13 15:11:23 +0000575 } else {
576 Report("Shadow memory range interleaves with an existing memory mapping. "
577 "ASan cannot proceed correctly. ABORTING.\n");
Alexander Potapenko99d17eb2012-02-22 09:11:55 +0000578 AsanDumpProcessMap();
Alexey Samsonov47657ce2012-06-06 07:02:44 +0000579 Die();
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000580 }
581
Alexander Potapenkof03d8af2012-04-05 10:54:52 +0000582 InstallSignalHandlers();
583
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000584 // On Linux AsanThread::ThreadStart() calls malloc() that's why asan_inited
585 // should be set to 1 prior to initializing the threads.
586 asan_inited = 1;
587 asan_init_is_running = false;
588
589 asanThreadRegistry().Init();
590 asanThreadRegistry().GetMain()->ThreadStart();
Kostya Serebryany51e75c42011-12-28 00:59:39 +0000591 force_interface_symbols(); // no-op.
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000592
593 if (FLAG_v) {
Kostya Serebryanyd6567c52011-12-01 21:40:52 +0000594 Report("AddressSanitizer Init done\n");
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000595 }
596}
Evgeniy Stepanov8bcc6b92012-01-11 08:17:19 +0000597
598#if defined(ASAN_USE_PREINIT_ARRAY)
Timur Iskhodzhanov38ed7362012-02-21 16:24:23 +0000599 // On Linux, we force __asan_init to be called before anyone else
600 // by placing it into .preinit_array section.
601 // FIXME: do we have anything like this on Mac?
602 __attribute__((section(".preinit_array")))
603 typeof(__asan_init) *__asan_preinit =__asan_init;
604#elif defined(_WIN32) && defined(_DLL)
605 // On Windows, when using dynamic CRT (/MD), we can put a pointer
606 // to __asan_init into the global list of C initializers.
607 // See crt0dat.c in the CRT sources for the details.
Timur Iskhodzhanov39c22ee2012-02-22 09:28:14 +0000608 #pragma section(".CRT$XIB", long, read) // NOLINT
Timur Iskhodzhanov38ed7362012-02-21 16:24:23 +0000609 __declspec(allocate(".CRT$XIB")) void (*__asan_preinit)() = __asan_init;
Evgeniy Stepanov8bcc6b92012-01-11 08:17:19 +0000610#endif