blob: 84129466e1aa0e96ab6d7136cedb2c0cd0481768 [file] [log] [blame]
Kostya Serebryany1e172b42011-11-30 01:07:02 +00001//===-- asan_rtl.cc ---------------------------------------------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file is a part of AddressSanitizer, an address sanity checker.
11//
12// Main file of the ASan run-time library.
13//===----------------------------------------------------------------------===//
14#include "asan_allocator.h"
15#include "asan_interceptors.h"
16#include "asan_interface.h"
17#include "asan_internal.h"
18#include "asan_lock.h"
Kostya Serebryany1e172b42011-11-30 01:07:02 +000019#include "asan_mapping.h"
Kostya Serebryanydf499b42012-01-05 00:44:33 +000020#include "asan_procmaps.h"
Kostya Serebryany1e172b42011-11-30 01:07:02 +000021#include "asan_stack.h"
22#include "asan_stats.h"
23#include "asan_thread.h"
24#include "asan_thread_registry.h"
25
Kostya Serebryany1e172b42011-11-30 01:07:02 +000026namespace __asan {
27
28// -------------------------- Flags ------------------------- {{{1
29static const size_t kMallocContextSize = 30;
30static int FLAG_atexit;
Kostya Serebryany1e172b42011-11-30 01:07:02 +000031
32size_t FLAG_redzone; // power of two, >= 32
Kostya Serebryany1e172b42011-11-30 01:07:02 +000033size_t FLAG_quarantine_size;
34int FLAG_demangle;
35bool FLAG_symbolize;
36int FLAG_v;
37int FLAG_debug;
38bool FLAG_poison_shadow;
39int FLAG_report_globals;
40size_t FLAG_malloc_context_size = kMallocContextSize;
41uintptr_t FLAG_large_malloc;
Kostya Serebryany1e172b42011-11-30 01:07:02 +000042bool FLAG_handle_segv;
Kostya Serebryany1e172b42011-11-30 01:07:02 +000043bool FLAG_replace_str;
44bool FLAG_replace_intrin;
45bool FLAG_replace_cfallocator; // Used on Mac only.
Kostya Serebryany1e172b42011-11-30 01:07:02 +000046size_t FLAG_max_malloc_fill_size = 0;
47bool FLAG_use_fake_stack;
48int FLAG_exitcode = EXIT_FAILURE;
49bool FLAG_allow_user_poisoning;
Kostya Serebryanycb00d132012-01-31 00:52:18 +000050int FLAG_sleep_before_dying;
Kostya Serebryany1e172b42011-11-30 01:07:02 +000051
52// -------------------------- Globals --------------------- {{{1
53int asan_inited;
54bool asan_init_is_running;
Kostya Serebryanye1fe0fd2012-02-13 21:24:29 +000055static void (*death_callback)(void);
Kostya Serebryany1e172b42011-11-30 01:07:02 +000056
Kostya Serebryany1e172b42011-11-30 01:07:02 +000057// -------------------------- Misc ---------------- {{{1
58void ShowStatsAndAbort() {
59 __asan_print_accumulated_stats();
Kostya Serebryany0ecf5eb2012-01-09 23:11:26 +000060 AsanDie();
Kostya Serebryany1e172b42011-11-30 01:07:02 +000061}
62
63static void PrintBytes(const char *before, uintptr_t *a) {
64 uint8_t *bytes = (uint8_t*)a;
65 size_t byte_num = (__WORDSIZE) / 8;
66 Printf("%s%p:", before, (uintptr_t)a);
67 for (size_t i = 0; i < byte_num; i++) {
68 Printf(" %lx%lx", bytes[i] >> 4, bytes[i] & 15);
69 }
70 Printf("\n");
71}
72
Kostya Serebryany0ecf5eb2012-01-09 23:11:26 +000073size_t ReadFileToBuffer(const char *file_name, char **buff,
Kostya Serebryanydf499b42012-01-05 00:44:33 +000074 size_t *buff_size, size_t max_len) {
Kostya Serebryanyde496f42011-12-28 22:58:01 +000075 const size_t kMinFileLen = kPageSize;
Kostya Serebryany0ecf5eb2012-01-09 23:11:26 +000076 size_t read_len = 0;
Kostya Serebryanyde496f42011-12-28 22:58:01 +000077 *buff = 0;
Kostya Serebryanydf499b42012-01-05 00:44:33 +000078 *buff_size = 0;
Kostya Serebryanyde496f42011-12-28 22:58:01 +000079 // The files we usually open are not seekable, so try different buffer sizes.
80 for (size_t size = kMinFileLen; size <= max_len; size *= 2) {
81 int fd = AsanOpenReadonly(file_name);
82 if (fd < 0) return -1;
Kostya Serebryanydf499b42012-01-05 00:44:33 +000083 AsanUnmapOrDie(*buff, *buff_size);
Kostya Serebryanyde496f42011-12-28 22:58:01 +000084 *buff = (char*)AsanMmapSomewhereOrDie(size, __FUNCTION__);
Kostya Serebryanydf499b42012-01-05 00:44:33 +000085 *buff_size = size;
Kostya Serebryany454a0642012-01-17 18:00:07 +000086 // Read up to one page at a time.
87 read_len = 0;
88 bool reached_eof = false;
89 while (read_len + kPageSize <= size) {
90 size_t just_read = AsanRead(fd, *buff + read_len, kPageSize);
91 if (just_read == 0) {
92 reached_eof = true;
93 break;
94 }
95 read_len += just_read;
96 }
Kostya Serebryanyde496f42011-12-28 22:58:01 +000097 AsanClose(fd);
Kostya Serebryany454a0642012-01-17 18:00:07 +000098 if (reached_eof) // We've read the whole file.
Kostya Serebryanyde496f42011-12-28 22:58:01 +000099 break;
100 }
101 return read_len;
102}
103
Kostya Serebryanye1fe0fd2012-02-13 21:24:29 +0000104void AsanDie() {
Kostya Serebryany5e4e91c2012-02-16 00:40:18 +0000105 static int num_calls = 0;
106 if (AtomicInc(&num_calls) > 1) return; // Don't die twice.
Kostya Serebryanye1fe0fd2012-02-13 21:24:29 +0000107 if (FLAG_sleep_before_dying) {
108 Report("Sleeping for %d second(s)\n", FLAG_sleep_before_dying);
109 SleepForSeconds(FLAG_sleep_before_dying);
110 }
111 if (death_callback)
112 death_callback();
113 Exit(FLAG_exitcode);
114}
115
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000116// ---------------------- mmap -------------------- {{{1
Kostya Serebryanyde496f42011-12-28 22:58:01 +0000117void OutOfMemoryMessageAndDie(const char *mem_type, size_t size) {
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000118 Report("ERROR: AddressSanitizer failed to allocate "
119 "0x%lx (%ld) bytes of %s\n",
120 size, size, mem_type);
Kostya Serebryanyde496f42011-12-28 22:58:01 +0000121 PRINT_CURRENT_STACK();
122 ShowStatsAndAbort();
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000123}
124
Kostya Serebryanya874fe52011-12-28 23:28:54 +0000125// Reserve memory range [beg, end].
126static void ReserveShadowMemoryRange(uintptr_t beg, uintptr_t end) {
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000127 CHECK((beg % kPageSize) == 0);
128 CHECK(((end + 1) % kPageSize) == 0);
Kostya Serebryanya874fe52011-12-28 23:28:54 +0000129 size_t size = end - beg + 1;
130 void *res = AsanMmapFixedNoReserve(beg, size);
131 CHECK(res == (void*)beg && "ReserveShadowMemoryRange failed");
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000132}
133
Kostya Serebryanyb89567c2011-12-02 21:02:20 +0000134// ---------------------- LowLevelAllocator ------------- {{{1
135void *LowLevelAllocator::Allocate(size_t size) {
136 CHECK((size & (size - 1)) == 0 && "size must be a power of two");
137 if (allocated_end_ - allocated_current_ < size) {
138 size_t size_to_allocate = Max(size, kPageSize);
Kostya Serebryanyde496f42011-12-28 22:58:01 +0000139 allocated_current_ =
140 (char*)AsanMmapSomewhereOrDie(size_to_allocate, __FUNCTION__);
Kostya Serebryanyb89567c2011-12-02 21:02:20 +0000141 allocated_end_ = allocated_current_ + size_to_allocate;
Kostya Serebryany6b30e2c2011-12-15 17:41:30 +0000142 PoisonShadow((uintptr_t)allocated_current_, size_to_allocate,
143 kAsanInternalHeapMagic);
Kostya Serebryanyb89567c2011-12-02 21:02:20 +0000144 }
145 CHECK(allocated_end_ - allocated_current_ >= size);
146 void *res = allocated_current_;
147 allocated_current_ += size;
148 return res;
149}
150
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000151// ---------------------- DescribeAddress -------------------- {{{1
152static bool DescribeStackAddress(uintptr_t addr, uintptr_t access_size) {
153 AsanThread *t = asanThreadRegistry().FindThreadByStackAddress(addr);
154 if (!t) return false;
155 const intptr_t kBufSize = 4095;
156 char buf[kBufSize];
157 uintptr_t offset = 0;
158 const char *frame_descr = t->GetFrameNameByAddr(addr, &offset);
159 // This string is created by the compiler and has the following form:
160 // "FunctioName n alloc_1 alloc_2 ... alloc_n"
161 // where alloc_i looks like "offset size len ObjectName ".
162 CHECK(frame_descr);
163 // Report the function name and the offset.
Timur Iskhodzhanov53627172012-02-14 19:33:04 +0000164 const char *name_end = internal_strchr(frame_descr, ' ');
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000165 CHECK(name_end);
166 buf[0] = 0;
Kostya Serebryanya4ccf872012-01-09 22:20:49 +0000167 internal_strncat(buf, frame_descr,
168 Min(kBufSize,
169 static_cast<intptr_t>(name_end - frame_descr)));
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000170 Printf("Address %p is located at offset %ld "
171 "in frame <%s> of T%d's stack:\n",
172 addr, offset, buf, t->tid());
173 // Report the number of stack objects.
174 char *p;
Alexey Samsonov88981022012-02-17 16:15:09 +0000175 size_t n_objects = internal_simple_strtoll(name_end, &p, 10);
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000176 CHECK(n_objects > 0);
177 Printf(" This frame has %ld object(s):\n", n_objects);
178 // Report all objects in this frame.
179 for (size_t i = 0; i < n_objects; i++) {
180 size_t beg, size;
181 intptr_t len;
Alexey Samsonov88981022012-02-17 16:15:09 +0000182 beg = internal_simple_strtoll(p, &p, 10);
183 size = internal_simple_strtoll(p, &p, 10);
184 len = internal_simple_strtoll(p, &p, 10);
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000185 if (beg <= 0 || size <= 0 || len < 0 || *p != ' ') {
186 Printf("AddressSanitizer can't parse the stack frame descriptor: |%s|\n",
187 frame_descr);
188 break;
189 }
190 p++;
191 buf[0] = 0;
Kostya Serebryanya4ccf872012-01-09 22:20:49 +0000192 internal_strncat(buf, p, Min(kBufSize, len));
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000193 p += len;
194 Printf(" [%ld, %ld) '%s'\n", beg, beg + size, buf);
195 }
196 Printf("HINT: this may be a false positive if your program uses "
197 "some custom stack unwind mechanism\n"
198 " (longjmp and C++ exceptions *are* supported)\n");
199 t->summary()->Announce();
200 return true;
201}
202
Alexey Samsonovadf2b032012-02-03 08:37:19 +0000203static NOINLINE void DescribeAddress(uintptr_t addr, uintptr_t access_size) {
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000204 // Check if this is a global.
205 if (DescribeAddrIfGlobal(addr))
206 return;
207
208 if (DescribeStackAddress(addr, access_size))
209 return;
210
211 // finally, check if this is a heap.
212 DescribeHeapAddress(addr, access_size);
213}
214
215// -------------------------- Run-time entry ------------------- {{{1
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000216// exported functions
Kostya Serebryany51e75c42011-12-28 00:59:39 +0000217#define ASAN_REPORT_ERROR(type, is_write, size) \
Alexey Samsonovadf2b032012-02-03 08:37:19 +0000218NOINLINE ASAN_INTERFACE_ATTRIBUTE \
219extern "C" void __asan_report_ ## type ## size(uintptr_t addr); \
Kostya Serebryany51e75c42011-12-28 00:59:39 +0000220extern "C" void __asan_report_ ## type ## size(uintptr_t addr) { \
221 GET_BP_PC_SP; \
222 __asan_report_error(pc, bp, sp, addr, is_write, size); \
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000223}
224
225ASAN_REPORT_ERROR(load, false, 1)
226ASAN_REPORT_ERROR(load, false, 2)
227ASAN_REPORT_ERROR(load, false, 4)
228ASAN_REPORT_ERROR(load, false, 8)
229ASAN_REPORT_ERROR(load, false, 16)
230ASAN_REPORT_ERROR(store, true, 1)
231ASAN_REPORT_ERROR(store, true, 2)
232ASAN_REPORT_ERROR(store, true, 4)
233ASAN_REPORT_ERROR(store, true, 8)
234ASAN_REPORT_ERROR(store, true, 16)
235
236// Force the linker to keep the symbols for various ASan interface functions.
237// We want to keep those in the executable in order to let the instrumented
238// dynamic libraries access the symbol even if it is not used by the executable
239// itself. This should help if the build system is removing dead code at link
240// time.
Kostya Serebryany51e75c42011-12-28 00:59:39 +0000241static void force_interface_symbols() {
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000242 volatile int fake_condition = 0; // prevent dead condition elimination.
243 if (fake_condition) {
244 __asan_report_load1(NULL);
245 __asan_report_load2(NULL);
246 __asan_report_load4(NULL);
247 __asan_report_load8(NULL);
248 __asan_report_load16(NULL);
249 __asan_report_store1(NULL);
250 __asan_report_store2(NULL);
251 __asan_report_store4(NULL);
252 __asan_report_store8(NULL);
253 __asan_report_store16(NULL);
254 __asan_register_global(0, 0, NULL);
255 __asan_register_globals(NULL, 0);
Kostya Serebryany45581682011-12-28 23:35:46 +0000256 __asan_unregister_globals(NULL, 0);
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000257 }
258}
259
260// -------------------------- Init ------------------- {{{1
261static int64_t IntFlagValue(const char *flags, const char *flag,
262 int64_t default_val) {
263 if (!flags) return default_val;
Kostya Serebryanya4ccf872012-01-09 22:20:49 +0000264 const char *str = internal_strstr(flags, flag);
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000265 if (!str) return default_val;
Alexey Samsonov88981022012-02-17 16:15:09 +0000266 return internal_atoll(str + internal_strlen(flag));
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000267}
268
269static void asan_atexit() {
270 Printf("AddressSanitizer exit stats:\n");
271 __asan_print_accumulated_stats();
272}
273
274void CheckFailed(const char *cond, const char *file, int line) {
Kostya Serebryanya7e760a2012-01-09 19:18:27 +0000275 Report("CHECK failed: %s at %s:%d\n", cond, file, line);
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000276 PRINT_CURRENT_STACK();
277 ShowStatsAndAbort();
278}
279
280} // namespace __asan
281
Kostya Serebryany4803ab92012-01-09 18:53:15 +0000282// ---------------------- Interface ---------------- {{{1
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000283using namespace __asan; // NOLINT
284
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000285int __asan_set_error_exit_code(int exit_code) {
286 int old = FLAG_exitcode;
287 FLAG_exitcode = exit_code;
288 return old;
289}
290
Kostya Serebryanyf54b1f92012-02-08 21:33:27 +0000291void __asan_handle_no_return() {
292 int local_stack;
293 AsanThread *curr_thread = asanThreadRegistry().GetCurrent();
294 CHECK(curr_thread);
295 uintptr_t top = curr_thread->stack_top();
296 uintptr_t bottom = ((uintptr_t)&local_stack - kPageSize) & ~(kPageSize-1);
297 PoisonShadow(bottom, top - bottom, 0);
298}
299
Kostya Serebryanye1fe0fd2012-02-13 21:24:29 +0000300void __asan_set_death_callback(void (*callback)(void)) {
301 death_callback = callback;
302}
303
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000304void __asan_report_error(uintptr_t pc, uintptr_t bp, uintptr_t sp,
305 uintptr_t addr, bool is_write, size_t access_size) {
306 // Do not print more than one report, otherwise they will mix up.
307 static int num_calls = 0;
308 if (AtomicInc(&num_calls) > 1) return;
309
310 Printf("=================================================================\n");
311 const char *bug_descr = "unknown-crash";
312 if (AddrIsInMem(addr)) {
313 uint8_t *shadow_addr = (uint8_t*)MemToShadow(addr);
Kostya Serebryanyacd5c612011-12-07 21:30:20 +0000314 // If we are accessing 16 bytes, look at the second shadow byte.
315 if (*shadow_addr == 0 && access_size > SHADOW_GRANULARITY)
316 shadow_addr++;
317 // If we are in the partial right redzone, look at the next shadow byte.
318 if (*shadow_addr > 0 && *shadow_addr < 128)
319 shadow_addr++;
320 switch (*shadow_addr) {
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000321 case kAsanHeapLeftRedzoneMagic:
322 case kAsanHeapRightRedzoneMagic:
323 bug_descr = "heap-buffer-overflow";
324 break;
325 case kAsanHeapFreeMagic:
326 bug_descr = "heap-use-after-free";
327 break;
328 case kAsanStackLeftRedzoneMagic:
329 bug_descr = "stack-buffer-underflow";
330 break;
331 case kAsanStackMidRedzoneMagic:
332 case kAsanStackRightRedzoneMagic:
333 case kAsanStackPartialRedzoneMagic:
334 bug_descr = "stack-buffer-overflow";
335 break;
336 case kAsanStackAfterReturnMagic:
337 bug_descr = "stack-use-after-return";
338 break;
339 case kAsanUserPoisonedMemoryMagic:
340 bug_descr = "use-after-poison";
341 break;
342 case kAsanGlobalRedzoneMagic:
343 bug_descr = "global-buffer-overflow";
344 break;
345 }
346 }
347
Kostya Serebryanyc4b34d92011-12-09 01:49:31 +0000348 AsanThread *curr_thread = asanThreadRegistry().GetCurrent();
349 int curr_tid = asanThreadRegistry().GetCurrentTidOrMinusOne();
350
351 if (curr_thread) {
352 // We started reporting an error message. Stop using the fake stack
353 // in case we will call an instrumented function from a symbolizer.
354 curr_thread->fake_stack().StopUsingFakeStack();
355 }
356
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000357 Report("ERROR: AddressSanitizer %s on address "
358 "%p at pc 0x%lx bp 0x%lx sp 0x%lx\n",
359 bug_descr, addr, pc, bp, sp);
360
361 Printf("%s of size %d at %p thread T%d\n",
362 access_size ? (is_write ? "WRITE" : "READ") : "ACCESS",
Kostya Serebryanyc4b34d92011-12-09 01:49:31 +0000363 access_size, addr, curr_tid);
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000364
365 if (FLAG_debug) {
366 PrintBytes("PC: ", (uintptr_t*)pc);
367 }
368
Evgeniy Stepanov9cfa1942012-01-19 11:34:18 +0000369 GET_STACK_TRACE_WITH_PC_AND_BP(kStackTraceMax, pc, bp);
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000370 stack.PrintStack();
371
372 CHECK(AddrIsInMem(addr));
373
374 DescribeAddress(addr, access_size);
375
376 uintptr_t shadow_addr = MemToShadow(addr);
377 Report("ABORTING\n");
378 __asan_print_accumulated_stats();
379 Printf("Shadow byte and word:\n");
380 Printf(" %p: %x\n", shadow_addr, *(unsigned char*)shadow_addr);
381 uintptr_t aligned_shadow = shadow_addr & ~(kWordSize - 1);
382 PrintBytes(" ", (uintptr_t*)(aligned_shadow));
383 Printf("More shadow bytes:\n");
384 PrintBytes(" ", (uintptr_t*)(aligned_shadow-4*kWordSize));
385 PrintBytes(" ", (uintptr_t*)(aligned_shadow-3*kWordSize));
386 PrintBytes(" ", (uintptr_t*)(aligned_shadow-2*kWordSize));
387 PrintBytes(" ", (uintptr_t*)(aligned_shadow-1*kWordSize));
388 PrintBytes("=>", (uintptr_t*)(aligned_shadow+0*kWordSize));
389 PrintBytes(" ", (uintptr_t*)(aligned_shadow+1*kWordSize));
390 PrintBytes(" ", (uintptr_t*)(aligned_shadow+2*kWordSize));
391 PrintBytes(" ", (uintptr_t*)(aligned_shadow+3*kWordSize));
392 PrintBytes(" ", (uintptr_t*)(aligned_shadow+4*kWordSize));
Kostya Serebryany0ecf5eb2012-01-09 23:11:26 +0000393 AsanDie();
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000394}
395
396void __asan_init() {
397 if (asan_inited) return;
398 asan_init_is_running = true;
399
400 // Make sure we are not statically linked.
401 AsanDoesNotSupportStaticLinkage();
402
403 // flags
Alexander Potapenko1e316d72012-01-13 12:59:48 +0000404 const char *options = AsanGetEnv("ASAN_OPTIONS");
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000405 FLAG_malloc_context_size =
406 IntFlagValue(options, "malloc_context_size=", kMallocContextSize);
407 CHECK(FLAG_malloc_context_size <= kMallocContextSize);
408
409 FLAG_max_malloc_fill_size =
410 IntFlagValue(options, "max_malloc_fill_size=", 0);
411
412 FLAG_v = IntFlagValue(options, "verbosity=", 0);
413
Evgeniy Stepanov788e1d72012-02-16 13:35:11 +0000414#if ASAN_LOW_MEMORY == 1
415 FLAG_quarantine_size =
Alexey Samsonov6a3e6fd2012-02-17 08:31:10 +0000416 IntFlagValue(options, "quarantine_size=", 1UL << 24); // 16M
Evgeniy Stepanov788e1d72012-02-16 13:35:11 +0000417 FLAG_redzone = IntFlagValue(options, "redzone=", 64);
418#else
419 FLAG_quarantine_size =
Alexey Samsonov6a3e6fd2012-02-17 08:31:10 +0000420 IntFlagValue(options, "quarantine_size=", 1UL << 28); // 256M
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000421 FLAG_redzone = IntFlagValue(options, "redzone=", 128);
Evgeniy Stepanov788e1d72012-02-16 13:35:11 +0000422#endif
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000423 CHECK(FLAG_redzone >= 32);
424 CHECK((FLAG_redzone & (FLAG_redzone - 1)) == 0);
425
426 FLAG_atexit = IntFlagValue(options, "atexit=", 0);
427 FLAG_poison_shadow = IntFlagValue(options, "poison_shadow=", 1);
428 FLAG_report_globals = IntFlagValue(options, "report_globals=", 1);
Kostya Serebryanyc6f22232011-12-08 18:30:42 +0000429 FLAG_handle_segv = IntFlagValue(options, "handle_segv=", ASAN_NEEDS_SEGV);
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000430 FLAG_symbolize = IntFlagValue(options, "symbolize=", 1);
431 FLAG_demangle = IntFlagValue(options, "demangle=", 1);
432 FLAG_debug = IntFlagValue(options, "debug=", 0);
433 FLAG_replace_cfallocator = IntFlagValue(options, "replace_cfallocator=", 1);
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000434 FLAG_replace_str = IntFlagValue(options, "replace_str=", 1);
Kostya Serebryany0ffe35c2011-12-28 19:55:30 +0000435 FLAG_replace_intrin = IntFlagValue(options, "replace_intrin=", 1);
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000436 FLAG_use_fake_stack = IntFlagValue(options, "use_fake_stack=", 1);
437 FLAG_exitcode = IntFlagValue(options, "exitcode=", EXIT_FAILURE);
438 FLAG_allow_user_poisoning = IntFlagValue(options,
439 "allow_user_poisoning=", 1);
Kostya Serebryanycb00d132012-01-31 00:52:18 +0000440 FLAG_sleep_before_dying = IntFlagValue(options, "sleep_before_dying=", 0);
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000441
442 if (FLAG_atexit) {
443 atexit(asan_atexit);
444 }
445
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000446 // interceptors
447 InitializeAsanInterceptors();
448
449 ReplaceSystemMalloc();
Kostya Serebryanya7e760a2012-01-09 19:18:27 +0000450 InstallSignalHandlers();
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000451
452 if (FLAG_v) {
453 Printf("|| `[%p, %p]` || HighMem ||\n", kHighMemBeg, kHighMemEnd);
454 Printf("|| `[%p, %p]` || HighShadow ||\n",
455 kHighShadowBeg, kHighShadowEnd);
456 Printf("|| `[%p, %p]` || ShadowGap ||\n",
457 kShadowGapBeg, kShadowGapEnd);
458 Printf("|| `[%p, %p]` || LowShadow ||\n",
459 kLowShadowBeg, kLowShadowEnd);
460 Printf("|| `[%p, %p]` || LowMem ||\n", kLowMemBeg, kLowMemEnd);
461 Printf("MemToShadow(shadow): %p %p %p %p\n",
462 MEM_TO_SHADOW(kLowShadowBeg),
463 MEM_TO_SHADOW(kLowShadowEnd),
464 MEM_TO_SHADOW(kHighShadowBeg),
465 MEM_TO_SHADOW(kHighShadowEnd));
466 Printf("red_zone=%ld\n", FLAG_redzone);
467 Printf("malloc_context_size=%ld\n", (int)FLAG_malloc_context_size);
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000468
469 Printf("SHADOW_SCALE: %lx\n", SHADOW_SCALE);
470 Printf("SHADOW_GRANULARITY: %lx\n", SHADOW_GRANULARITY);
471 Printf("SHADOW_OFFSET: %lx\n", SHADOW_OFFSET);
472 CHECK(SHADOW_SCALE >= 3 && SHADOW_SCALE <= 7);
473 }
474
475 if (__WORDSIZE == 64) {
476 // Disable core dumper -- it makes little sense to dump 16T+ core.
Kostya Serebryanyef14ff62012-01-06 02:12:25 +0000477 AsanDisableCoreDumper();
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000478 }
479
Alexander Potapenkoc50e8352012-02-13 15:11:23 +0000480 if (AsanShadowRangeIsAvailable()) {
Kostya Serebryanya7e760a2012-01-09 19:18:27 +0000481 if (kLowShadowBeg != kLowShadowEnd) {
Timur Iskhodzhanov3e81fe42012-02-09 17:20:14 +0000482 // mmap the low shadow plus at least one page.
483 ReserveShadowMemoryRange(kLowShadowBeg - kMmapGranularity, kLowShadowEnd);
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000484 }
Kostya Serebryanya7e760a2012-01-09 19:18:27 +0000485 // mmap the high shadow.
486 ReserveShadowMemoryRange(kHighShadowBeg, kHighShadowEnd);
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000487 // protect the gap
Kostya Serebryanya874fe52011-12-28 23:28:54 +0000488 void *prot = AsanMprotect(kShadowGapBeg, kShadowGapEnd - kShadowGapBeg + 1);
489 CHECK(prot == (void*)kShadowGapBeg);
Alexander Potapenkoc50e8352012-02-13 15:11:23 +0000490 } else {
491 Report("Shadow memory range interleaves with an existing memory mapping. "
492 "ASan cannot proceed correctly. ABORTING.\n");
Alexander Potapenkoddaf7392012-02-22 08:27:32 +0000493 AsanProcMaps proc_maps;
494 proc_maps.Dump();
Alexander Potapenkoc50e8352012-02-13 15:11:23 +0000495 AsanDie();
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000496 }
497
498 // On Linux AsanThread::ThreadStart() calls malloc() that's why asan_inited
499 // should be set to 1 prior to initializing the threads.
500 asan_inited = 1;
501 asan_init_is_running = false;
502
503 asanThreadRegistry().Init();
504 asanThreadRegistry().GetMain()->ThreadStart();
Kostya Serebryany51e75c42011-12-28 00:59:39 +0000505 force_interface_symbols(); // no-op.
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000506
507 if (FLAG_v) {
Kostya Serebryanyd6567c52011-12-01 21:40:52 +0000508 Report("AddressSanitizer Init done\n");
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000509 }
510}
Evgeniy Stepanov8bcc6b92012-01-11 08:17:19 +0000511
512#if defined(ASAN_USE_PREINIT_ARRAY)
Timur Iskhodzhanov38ed7362012-02-21 16:24:23 +0000513 // On Linux, we force __asan_init to be called before anyone else
514 // by placing it into .preinit_array section.
515 // FIXME: do we have anything like this on Mac?
516 __attribute__((section(".preinit_array")))
517 typeof(__asan_init) *__asan_preinit =__asan_init;
518#elif defined(_WIN32) && defined(_DLL)
519 // On Windows, when using dynamic CRT (/MD), we can put a pointer
520 // to __asan_init into the global list of C initializers.
521 // See crt0dat.c in the CRT sources for the details.
522 #pragma section(".CRT$XIB",long,read)
523 __declspec(allocate(".CRT$XIB")) void (*__asan_preinit)() = __asan_init;
Evgeniy Stepanov8bcc6b92012-01-11 08:17:19 +0000524#endif