blob: 339ac7e9dc0e4cf6f6bce5cc9fc25345ad296aad [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"
20#include "asan_stack.h"
21#include "asan_stats.h"
22#include "asan_thread.h"
23#include "asan_thread_registry.h"
24
Kostya Serebryany1e172b42011-11-30 01:07:02 +000025namespace __asan {
26
27// -------------------------- Flags ------------------------- {{{1
28static const size_t kMallocContextSize = 30;
29static int FLAG_atexit;
Kostya Serebryany1e172b42011-11-30 01:07:02 +000030
31size_t FLAG_redzone; // power of two, >= 32
Kostya Serebryany1e172b42011-11-30 01:07:02 +000032size_t FLAG_quarantine_size;
33int FLAG_demangle;
34bool FLAG_symbolize;
35int FLAG_v;
36int FLAG_debug;
37bool FLAG_poison_shadow;
38int FLAG_report_globals;
39size_t FLAG_malloc_context_size = kMallocContextSize;
40uintptr_t FLAG_large_malloc;
Kostya Serebryany1e172b42011-11-30 01:07:02 +000041bool FLAG_handle_segv;
Kostya Serebryany1e172b42011-11-30 01:07:02 +000042bool FLAG_replace_str;
43bool FLAG_replace_intrin;
44bool FLAG_replace_cfallocator; // Used on Mac only.
Kostya Serebryany1e172b42011-11-30 01:07:02 +000045size_t FLAG_max_malloc_fill_size = 0;
46bool FLAG_use_fake_stack;
Alexey Samsonovb823e3c2012-02-22 14:07:06 +000047int FLAG_exitcode = ASAN_DEFAULT_FAILURE_EXITCODE;
Kostya Serebryany1e172b42011-11-30 01:07:02 +000048bool FLAG_allow_user_poisoning;
Kostya Serebryanycb00d132012-01-31 00:52:18 +000049int FLAG_sleep_before_dying;
Kostya Serebryany1e172b42011-11-30 01:07:02 +000050
51// -------------------------- Globals --------------------- {{{1
52int asan_inited;
53bool asan_init_is_running;
Kostya Serebryanye1fe0fd2012-02-13 21:24:29 +000054static void (*death_callback)(void);
Alexander Potapenko3fe91352012-02-27 14:06:48 +000055static void (*error_report_callback)(const char*);
56char *error_message_buffer = NULL;
57size_t error_message_buffer_pos = 0;
58size_t error_message_buffer_size = 0;
Kostya Serebryany1e172b42011-11-30 01:07:02 +000059
Kostya Serebryany1e172b42011-11-30 01:07:02 +000060// -------------------------- Misc ---------------- {{{1
61void ShowStatsAndAbort() {
62 __asan_print_accumulated_stats();
Kostya Serebryany0ecf5eb2012-01-09 23:11:26 +000063 AsanDie();
Kostya Serebryany1e172b42011-11-30 01:07:02 +000064}
65
66static void PrintBytes(const char *before, uintptr_t *a) {
67 uint8_t *bytes = (uint8_t*)a;
68 size_t byte_num = (__WORDSIZE) / 8;
69 Printf("%s%p:", before, (uintptr_t)a);
70 for (size_t i = 0; i < byte_num; i++) {
71 Printf(" %lx%lx", bytes[i] >> 4, bytes[i] & 15);
72 }
73 Printf("\n");
74}
75
Kostya Serebryany0ecf5eb2012-01-09 23:11:26 +000076size_t ReadFileToBuffer(const char *file_name, char **buff,
Kostya Serebryanydf499b42012-01-05 00:44:33 +000077 size_t *buff_size, size_t max_len) {
Kostya Serebryanyde496f42011-12-28 22:58:01 +000078 const size_t kMinFileLen = kPageSize;
Kostya Serebryany0ecf5eb2012-01-09 23:11:26 +000079 size_t read_len = 0;
Kostya Serebryanyde496f42011-12-28 22:58:01 +000080 *buff = 0;
Kostya Serebryanydf499b42012-01-05 00:44:33 +000081 *buff_size = 0;
Kostya Serebryanyde496f42011-12-28 22:58:01 +000082 // The files we usually open are not seekable, so try different buffer sizes.
83 for (size_t size = kMinFileLen; size <= max_len; size *= 2) {
84 int fd = AsanOpenReadonly(file_name);
85 if (fd < 0) return -1;
Kostya Serebryanydf499b42012-01-05 00:44:33 +000086 AsanUnmapOrDie(*buff, *buff_size);
Kostya Serebryanyde496f42011-12-28 22:58:01 +000087 *buff = (char*)AsanMmapSomewhereOrDie(size, __FUNCTION__);
Kostya Serebryanydf499b42012-01-05 00:44:33 +000088 *buff_size = size;
Kostya Serebryany454a0642012-01-17 18:00:07 +000089 // Read up to one page at a time.
90 read_len = 0;
91 bool reached_eof = false;
92 while (read_len + kPageSize <= size) {
93 size_t just_read = AsanRead(fd, *buff + read_len, kPageSize);
94 if (just_read == 0) {
95 reached_eof = true;
96 break;
97 }
98 read_len += just_read;
99 }
Kostya Serebryanyde496f42011-12-28 22:58:01 +0000100 AsanClose(fd);
Kostya Serebryany454a0642012-01-17 18:00:07 +0000101 if (reached_eof) // We've read the whole file.
Kostya Serebryanyde496f42011-12-28 22:58:01 +0000102 break;
103 }
104 return read_len;
105}
106
Kostya Serebryanye1fe0fd2012-02-13 21:24:29 +0000107void AsanDie() {
Kostya Serebryany5e4e91c2012-02-16 00:40:18 +0000108 static int num_calls = 0;
109 if (AtomicInc(&num_calls) > 1) return; // Don't die twice.
Kostya Serebryanye1fe0fd2012-02-13 21:24:29 +0000110 if (FLAG_sleep_before_dying) {
111 Report("Sleeping for %d second(s)\n", FLAG_sleep_before_dying);
112 SleepForSeconds(FLAG_sleep_before_dying);
113 }
114 if (death_callback)
115 death_callback();
116 Exit(FLAG_exitcode);
117}
118
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000119// ---------------------- mmap -------------------- {{{1
Kostya Serebryanyde496f42011-12-28 22:58:01 +0000120void OutOfMemoryMessageAndDie(const char *mem_type, size_t size) {
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000121 Report("ERROR: AddressSanitizer failed to allocate "
122 "0x%lx (%ld) bytes of %s\n",
123 size, size, mem_type);
Kostya Serebryanyde496f42011-12-28 22:58:01 +0000124 PRINT_CURRENT_STACK();
125 ShowStatsAndAbort();
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000126}
127
Kostya Serebryanya874fe52011-12-28 23:28:54 +0000128// Reserve memory range [beg, end].
129static void ReserveShadowMemoryRange(uintptr_t beg, uintptr_t end) {
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000130 CHECK((beg % kPageSize) == 0);
131 CHECK(((end + 1) % kPageSize) == 0);
Kostya Serebryanya874fe52011-12-28 23:28:54 +0000132 size_t size = end - beg + 1;
133 void *res = AsanMmapFixedNoReserve(beg, size);
134 CHECK(res == (void*)beg && "ReserveShadowMemoryRange failed");
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000135}
136
Kostya Serebryanyb89567c2011-12-02 21:02:20 +0000137// ---------------------- LowLevelAllocator ------------- {{{1
138void *LowLevelAllocator::Allocate(size_t size) {
139 CHECK((size & (size - 1)) == 0 && "size must be a power of two");
140 if (allocated_end_ - allocated_current_ < size) {
141 size_t size_to_allocate = Max(size, kPageSize);
Kostya Serebryanyde496f42011-12-28 22:58:01 +0000142 allocated_current_ =
143 (char*)AsanMmapSomewhereOrDie(size_to_allocate, __FUNCTION__);
Kostya Serebryanyb89567c2011-12-02 21:02:20 +0000144 allocated_end_ = allocated_current_ + size_to_allocate;
Kostya Serebryany6b30e2c2011-12-15 17:41:30 +0000145 PoisonShadow((uintptr_t)allocated_current_, size_to_allocate,
146 kAsanInternalHeapMagic);
Kostya Serebryanyb89567c2011-12-02 21:02:20 +0000147 }
148 CHECK(allocated_end_ - allocated_current_ >= size);
149 void *res = allocated_current_;
150 allocated_current_ += size;
151 return res;
152}
153
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000154// ---------------------- DescribeAddress -------------------- {{{1
155static bool DescribeStackAddress(uintptr_t addr, uintptr_t access_size) {
156 AsanThread *t = asanThreadRegistry().FindThreadByStackAddress(addr);
157 if (!t) return false;
158 const intptr_t kBufSize = 4095;
159 char buf[kBufSize];
160 uintptr_t offset = 0;
161 const char *frame_descr = t->GetFrameNameByAddr(addr, &offset);
162 // This string is created by the compiler and has the following form:
163 // "FunctioName n alloc_1 alloc_2 ... alloc_n"
164 // where alloc_i looks like "offset size len ObjectName ".
165 CHECK(frame_descr);
166 // Report the function name and the offset.
Timur Iskhodzhanov53627172012-02-14 19:33:04 +0000167 const char *name_end = internal_strchr(frame_descr, ' ');
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000168 CHECK(name_end);
169 buf[0] = 0;
Kostya Serebryanya4ccf872012-01-09 22:20:49 +0000170 internal_strncat(buf, frame_descr,
171 Min(kBufSize,
172 static_cast<intptr_t>(name_end - frame_descr)));
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000173 Printf("Address %p is located at offset %ld "
174 "in frame <%s> of T%d's stack:\n",
175 addr, offset, buf, t->tid());
176 // Report the number of stack objects.
177 char *p;
Alexey Samsonov88981022012-02-17 16:15:09 +0000178 size_t n_objects = internal_simple_strtoll(name_end, &p, 10);
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000179 CHECK(n_objects > 0);
180 Printf(" This frame has %ld object(s):\n", n_objects);
181 // Report all objects in this frame.
182 for (size_t i = 0; i < n_objects; i++) {
183 size_t beg, size;
184 intptr_t len;
Alexey Samsonov88981022012-02-17 16:15:09 +0000185 beg = internal_simple_strtoll(p, &p, 10);
186 size = internal_simple_strtoll(p, &p, 10);
187 len = internal_simple_strtoll(p, &p, 10);
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000188 if (beg <= 0 || size <= 0 || len < 0 || *p != ' ') {
189 Printf("AddressSanitizer can't parse the stack frame descriptor: |%s|\n",
190 frame_descr);
191 break;
192 }
193 p++;
194 buf[0] = 0;
Kostya Serebryanya4ccf872012-01-09 22:20:49 +0000195 internal_strncat(buf, p, Min(kBufSize, len));
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000196 p += len;
197 Printf(" [%ld, %ld) '%s'\n", beg, beg + size, buf);
198 }
199 Printf("HINT: this may be a false positive if your program uses "
200 "some custom stack unwind mechanism\n"
201 " (longjmp and C++ exceptions *are* supported)\n");
202 t->summary()->Announce();
203 return true;
204}
205
Alexey Samsonovadf2b032012-02-03 08:37:19 +0000206static NOINLINE void DescribeAddress(uintptr_t addr, uintptr_t access_size) {
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000207 // Check if this is a global.
208 if (DescribeAddrIfGlobal(addr))
209 return;
210
211 if (DescribeStackAddress(addr, access_size))
212 return;
213
214 // finally, check if this is a heap.
215 DescribeHeapAddress(addr, access_size);
216}
217
218// -------------------------- Run-time entry ------------------- {{{1
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000219// exported functions
Kostya Serebryany51e75c42011-12-28 00:59:39 +0000220#define ASAN_REPORT_ERROR(type, is_write, size) \
Alexey Samsonovadf2b032012-02-03 08:37:19 +0000221NOINLINE ASAN_INTERFACE_ATTRIBUTE \
222extern "C" void __asan_report_ ## type ## size(uintptr_t addr); \
Kostya Serebryany51e75c42011-12-28 00:59:39 +0000223extern "C" void __asan_report_ ## type ## size(uintptr_t addr) { \
224 GET_BP_PC_SP; \
225 __asan_report_error(pc, bp, sp, addr, is_write, size); \
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000226}
227
228ASAN_REPORT_ERROR(load, false, 1)
229ASAN_REPORT_ERROR(load, false, 2)
230ASAN_REPORT_ERROR(load, false, 4)
231ASAN_REPORT_ERROR(load, false, 8)
232ASAN_REPORT_ERROR(load, false, 16)
233ASAN_REPORT_ERROR(store, true, 1)
234ASAN_REPORT_ERROR(store, true, 2)
235ASAN_REPORT_ERROR(store, true, 4)
236ASAN_REPORT_ERROR(store, true, 8)
237ASAN_REPORT_ERROR(store, true, 16)
238
239// Force the linker to keep the symbols for various ASan interface functions.
240// We want to keep those in the executable in order to let the instrumented
241// dynamic libraries access the symbol even if it is not used by the executable
242// itself. This should help if the build system is removing dead code at link
243// time.
Alexander Potapenko3fe91352012-02-27 14:06:48 +0000244static NOINLINE void force_interface_symbols() {
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000245 volatile int fake_condition = 0; // prevent dead condition elimination.
246 if (fake_condition) {
247 __asan_report_load1(NULL);
248 __asan_report_load2(NULL);
249 __asan_report_load4(NULL);
250 __asan_report_load8(NULL);
251 __asan_report_load16(NULL);
252 __asan_report_store1(NULL);
253 __asan_report_store2(NULL);
254 __asan_report_store4(NULL);
255 __asan_report_store8(NULL);
256 __asan_report_store16(NULL);
257 __asan_register_global(0, 0, NULL);
258 __asan_register_globals(NULL, 0);
Kostya Serebryany45581682011-12-28 23:35:46 +0000259 __asan_unregister_globals(NULL, 0);
Alexander Potapenko2f3f9622012-03-01 14:39:21 +0000260 __asan_set_death_callback(NULL);
Alexander Potapenko3fe91352012-02-27 14:06:48 +0000261 __asan_set_error_report_callback(NULL);
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000262 }
263}
264
265// -------------------------- Init ------------------- {{{1
266static int64_t IntFlagValue(const char *flags, const char *flag,
267 int64_t default_val) {
268 if (!flags) return default_val;
Kostya Serebryanya4ccf872012-01-09 22:20:49 +0000269 const char *str = internal_strstr(flags, flag);
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000270 if (!str) return default_val;
Alexey Samsonov88981022012-02-17 16:15:09 +0000271 return internal_atoll(str + internal_strlen(flag));
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000272}
273
274static void asan_atexit() {
275 Printf("AddressSanitizer exit stats:\n");
276 __asan_print_accumulated_stats();
277}
278
279void CheckFailed(const char *cond, const char *file, int line) {
Kostya Serebryanya7e760a2012-01-09 19:18:27 +0000280 Report("CHECK failed: %s at %s:%d\n", cond, file, line);
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000281 PRINT_CURRENT_STACK();
282 ShowStatsAndAbort();
283}
284
285} // namespace __asan
286
Kostya Serebryany4803ab92012-01-09 18:53:15 +0000287// ---------------------- Interface ---------------- {{{1
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000288using namespace __asan; // NOLINT
289
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000290int __asan_set_error_exit_code(int exit_code) {
291 int old = FLAG_exitcode;
292 FLAG_exitcode = exit_code;
293 return old;
294}
295
Kostya Serebryanyf54b1f92012-02-08 21:33:27 +0000296void __asan_handle_no_return() {
297 int local_stack;
298 AsanThread *curr_thread = asanThreadRegistry().GetCurrent();
299 CHECK(curr_thread);
300 uintptr_t top = curr_thread->stack_top();
301 uintptr_t bottom = ((uintptr_t)&local_stack - kPageSize) & ~(kPageSize-1);
302 PoisonShadow(bottom, top - bottom, 0);
303}
304
Alexander Potapenko2f3f9622012-03-01 14:39:21 +0000305void NOINLINE __asan_set_death_callback(void (*callback)(void)) {
Kostya Serebryanye1fe0fd2012-02-13 21:24:29 +0000306 death_callback = callback;
307}
308
Alexander Potapenko3fe91352012-02-27 14:06:48 +0000309void NOINLINE __asan_set_error_report_callback(void (*callback)(const char*)) {
310 error_report_callback = callback;
311 if (callback) {
312 error_message_buffer_size = 1 << 14;
313 error_message_buffer =
314 (char*)AsanMmapSomewhereOrDie(error_message_buffer_size, __FUNCTION__);
315 error_message_buffer_pos = 0;
316 }
317}
318
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000319void __asan_report_error(uintptr_t pc, uintptr_t bp, uintptr_t sp,
320 uintptr_t addr, bool is_write, size_t access_size) {
321 // Do not print more than one report, otherwise they will mix up.
322 static int num_calls = 0;
323 if (AtomicInc(&num_calls) > 1) return;
324
325 Printf("=================================================================\n");
326 const char *bug_descr = "unknown-crash";
327 if (AddrIsInMem(addr)) {
328 uint8_t *shadow_addr = (uint8_t*)MemToShadow(addr);
Kostya Serebryanyacd5c612011-12-07 21:30:20 +0000329 // If we are accessing 16 bytes, look at the second shadow byte.
330 if (*shadow_addr == 0 && access_size > SHADOW_GRANULARITY)
331 shadow_addr++;
332 // If we are in the partial right redzone, look at the next shadow byte.
333 if (*shadow_addr > 0 && *shadow_addr < 128)
334 shadow_addr++;
335 switch (*shadow_addr) {
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000336 case kAsanHeapLeftRedzoneMagic:
337 case kAsanHeapRightRedzoneMagic:
338 bug_descr = "heap-buffer-overflow";
339 break;
340 case kAsanHeapFreeMagic:
341 bug_descr = "heap-use-after-free";
342 break;
343 case kAsanStackLeftRedzoneMagic:
344 bug_descr = "stack-buffer-underflow";
345 break;
346 case kAsanStackMidRedzoneMagic:
347 case kAsanStackRightRedzoneMagic:
348 case kAsanStackPartialRedzoneMagic:
349 bug_descr = "stack-buffer-overflow";
350 break;
351 case kAsanStackAfterReturnMagic:
352 bug_descr = "stack-use-after-return";
353 break;
354 case kAsanUserPoisonedMemoryMagic:
355 bug_descr = "use-after-poison";
356 break;
357 case kAsanGlobalRedzoneMagic:
358 bug_descr = "global-buffer-overflow";
359 break;
360 }
361 }
362
Kostya Serebryanyc4b34d92011-12-09 01:49:31 +0000363 AsanThread *curr_thread = asanThreadRegistry().GetCurrent();
364 int curr_tid = asanThreadRegistry().GetCurrentTidOrMinusOne();
365
366 if (curr_thread) {
367 // We started reporting an error message. Stop using the fake stack
368 // in case we will call an instrumented function from a symbolizer.
369 curr_thread->fake_stack().StopUsingFakeStack();
370 }
371
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000372 Report("ERROR: AddressSanitizer %s on address "
373 "%p at pc 0x%lx bp 0x%lx sp 0x%lx\n",
374 bug_descr, addr, pc, bp, sp);
375
376 Printf("%s of size %d at %p thread T%d\n",
377 access_size ? (is_write ? "WRITE" : "READ") : "ACCESS",
Kostya Serebryanyc4b34d92011-12-09 01:49:31 +0000378 access_size, addr, curr_tid);
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000379
380 if (FLAG_debug) {
381 PrintBytes("PC: ", (uintptr_t*)pc);
382 }
383
Evgeniy Stepanov9cfa1942012-01-19 11:34:18 +0000384 GET_STACK_TRACE_WITH_PC_AND_BP(kStackTraceMax, pc, bp);
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000385 stack.PrintStack();
386
387 CHECK(AddrIsInMem(addr));
388
389 DescribeAddress(addr, access_size);
390
391 uintptr_t shadow_addr = MemToShadow(addr);
392 Report("ABORTING\n");
393 __asan_print_accumulated_stats();
394 Printf("Shadow byte and word:\n");
395 Printf(" %p: %x\n", shadow_addr, *(unsigned char*)shadow_addr);
396 uintptr_t aligned_shadow = shadow_addr & ~(kWordSize - 1);
397 PrintBytes(" ", (uintptr_t*)(aligned_shadow));
398 Printf("More shadow bytes:\n");
399 PrintBytes(" ", (uintptr_t*)(aligned_shadow-4*kWordSize));
400 PrintBytes(" ", (uintptr_t*)(aligned_shadow-3*kWordSize));
401 PrintBytes(" ", (uintptr_t*)(aligned_shadow-2*kWordSize));
402 PrintBytes(" ", (uintptr_t*)(aligned_shadow-1*kWordSize));
403 PrintBytes("=>", (uintptr_t*)(aligned_shadow+0*kWordSize));
404 PrintBytes(" ", (uintptr_t*)(aligned_shadow+1*kWordSize));
405 PrintBytes(" ", (uintptr_t*)(aligned_shadow+2*kWordSize));
406 PrintBytes(" ", (uintptr_t*)(aligned_shadow+3*kWordSize));
407 PrintBytes(" ", (uintptr_t*)(aligned_shadow+4*kWordSize));
Alexander Potapenko3fe91352012-02-27 14:06:48 +0000408 if (error_report_callback) {
409 error_report_callback(error_message_buffer);
410 }
Kostya Serebryany0ecf5eb2012-01-09 23:11:26 +0000411 AsanDie();
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000412}
413
414void __asan_init() {
415 if (asan_inited) return;
416 asan_init_is_running = true;
417
418 // Make sure we are not statically linked.
419 AsanDoesNotSupportStaticLinkage();
420
421 // flags
Alexander Potapenko1e316d72012-01-13 12:59:48 +0000422 const char *options = AsanGetEnv("ASAN_OPTIONS");
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000423 FLAG_malloc_context_size =
424 IntFlagValue(options, "malloc_context_size=", kMallocContextSize);
425 CHECK(FLAG_malloc_context_size <= kMallocContextSize);
426
427 FLAG_max_malloc_fill_size =
428 IntFlagValue(options, "max_malloc_fill_size=", 0);
429
430 FLAG_v = IntFlagValue(options, "verbosity=", 0);
431
Evgeniy Stepanov8ae44ac2012-02-27 13:07:29 +0000432 FLAG_redzone = IntFlagValue(options, "redzone=",
433 (ASAN_LOW_MEMORY) ? 64 : 128);
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000434 CHECK(FLAG_redzone >= 32);
435 CHECK((FLAG_redzone & (FLAG_redzone - 1)) == 0);
436
437 FLAG_atexit = IntFlagValue(options, "atexit=", 0);
438 FLAG_poison_shadow = IntFlagValue(options, "poison_shadow=", 1);
439 FLAG_report_globals = IntFlagValue(options, "report_globals=", 1);
Kostya Serebryanyc6f22232011-12-08 18:30:42 +0000440 FLAG_handle_segv = IntFlagValue(options, "handle_segv=", ASAN_NEEDS_SEGV);
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000441 FLAG_symbolize = IntFlagValue(options, "symbolize=", 1);
442 FLAG_demangle = IntFlagValue(options, "demangle=", 1);
443 FLAG_debug = IntFlagValue(options, "debug=", 0);
444 FLAG_replace_cfallocator = IntFlagValue(options, "replace_cfallocator=", 1);
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000445 FLAG_replace_str = IntFlagValue(options, "replace_str=", 1);
Kostya Serebryany0ffe35c2011-12-28 19:55:30 +0000446 FLAG_replace_intrin = IntFlagValue(options, "replace_intrin=", 1);
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000447 FLAG_use_fake_stack = IntFlagValue(options, "use_fake_stack=", 1);
Alexey Samsonovb823e3c2012-02-22 14:07:06 +0000448 FLAG_exitcode = IntFlagValue(options, "exitcode=",
449 ASAN_DEFAULT_FAILURE_EXITCODE);
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000450 FLAG_allow_user_poisoning = IntFlagValue(options,
451 "allow_user_poisoning=", 1);
Kostya Serebryanycb00d132012-01-31 00:52:18 +0000452 FLAG_sleep_before_dying = IntFlagValue(options, "sleep_before_dying=", 0);
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000453
454 if (FLAG_atexit) {
Alexey Samsonovb823e3c2012-02-22 14:07:06 +0000455 Atexit(asan_atexit);
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000456 }
457
Evgeniy Stepanov8ae44ac2012-02-27 13:07:29 +0000458 FLAG_quarantine_size = IntFlagValue(options, "quarantine_size=",
459 (ASAN_LOW_MEMORY) ? 1UL << 24 : 1UL << 28);
460
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000461 // interceptors
462 InitializeAsanInterceptors();
463
464 ReplaceSystemMalloc();
Kostya Serebryanya7e760a2012-01-09 19:18:27 +0000465 InstallSignalHandlers();
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000466
467 if (FLAG_v) {
468 Printf("|| `[%p, %p]` || HighMem ||\n", kHighMemBeg, kHighMemEnd);
469 Printf("|| `[%p, %p]` || HighShadow ||\n",
470 kHighShadowBeg, kHighShadowEnd);
471 Printf("|| `[%p, %p]` || ShadowGap ||\n",
472 kShadowGapBeg, kShadowGapEnd);
473 Printf("|| `[%p, %p]` || LowShadow ||\n",
474 kLowShadowBeg, kLowShadowEnd);
475 Printf("|| `[%p, %p]` || LowMem ||\n", kLowMemBeg, kLowMemEnd);
476 Printf("MemToShadow(shadow): %p %p %p %p\n",
477 MEM_TO_SHADOW(kLowShadowBeg),
478 MEM_TO_SHADOW(kLowShadowEnd),
479 MEM_TO_SHADOW(kHighShadowBeg),
480 MEM_TO_SHADOW(kHighShadowEnd));
481 Printf("red_zone=%ld\n", FLAG_redzone);
482 Printf("malloc_context_size=%ld\n", (int)FLAG_malloc_context_size);
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000483
484 Printf("SHADOW_SCALE: %lx\n", SHADOW_SCALE);
485 Printf("SHADOW_GRANULARITY: %lx\n", SHADOW_GRANULARITY);
486 Printf("SHADOW_OFFSET: %lx\n", SHADOW_OFFSET);
487 CHECK(SHADOW_SCALE >= 3 && SHADOW_SCALE <= 7);
488 }
489
490 if (__WORDSIZE == 64) {
491 // Disable core dumper -- it makes little sense to dump 16T+ core.
Kostya Serebryanyef14ff62012-01-06 02:12:25 +0000492 AsanDisableCoreDumper();
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000493 }
494
Alexander Potapenkoc50e8352012-02-13 15:11:23 +0000495 if (AsanShadowRangeIsAvailable()) {
Kostya Serebryanya7e760a2012-01-09 19:18:27 +0000496 if (kLowShadowBeg != kLowShadowEnd) {
Timur Iskhodzhanov3e81fe42012-02-09 17:20:14 +0000497 // mmap the low shadow plus at least one page.
498 ReserveShadowMemoryRange(kLowShadowBeg - kMmapGranularity, kLowShadowEnd);
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000499 }
Kostya Serebryanya7e760a2012-01-09 19:18:27 +0000500 // mmap the high shadow.
501 ReserveShadowMemoryRange(kHighShadowBeg, kHighShadowEnd);
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000502 // protect the gap
Kostya Serebryanya874fe52011-12-28 23:28:54 +0000503 void *prot = AsanMprotect(kShadowGapBeg, kShadowGapEnd - kShadowGapBeg + 1);
504 CHECK(prot == (void*)kShadowGapBeg);
Alexander Potapenkoc50e8352012-02-13 15:11:23 +0000505 } else {
506 Report("Shadow memory range interleaves with an existing memory mapping. "
507 "ASan cannot proceed correctly. ABORTING.\n");
Alexander Potapenko99d17eb2012-02-22 09:11:55 +0000508 AsanDumpProcessMap();
Alexander Potapenkoc50e8352012-02-13 15:11:23 +0000509 AsanDie();
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000510 }
511
512 // On Linux AsanThread::ThreadStart() calls malloc() that's why asan_inited
513 // should be set to 1 prior to initializing the threads.
514 asan_inited = 1;
515 asan_init_is_running = false;
516
517 asanThreadRegistry().Init();
518 asanThreadRegistry().GetMain()->ThreadStart();
Kostya Serebryany51e75c42011-12-28 00:59:39 +0000519 force_interface_symbols(); // no-op.
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000520
521 if (FLAG_v) {
Kostya Serebryanyd6567c52011-12-01 21:40:52 +0000522 Report("AddressSanitizer Init done\n");
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000523 }
524}
Evgeniy Stepanov8bcc6b92012-01-11 08:17:19 +0000525
526#if defined(ASAN_USE_PREINIT_ARRAY)
Timur Iskhodzhanov38ed7362012-02-21 16:24:23 +0000527 // On Linux, we force __asan_init to be called before anyone else
528 // by placing it into .preinit_array section.
529 // FIXME: do we have anything like this on Mac?
530 __attribute__((section(".preinit_array")))
531 typeof(__asan_init) *__asan_preinit =__asan_init;
532#elif defined(_WIN32) && defined(_DLL)
533 // On Windows, when using dynamic CRT (/MD), we can put a pointer
534 // to __asan_init into the global list of C initializers.
535 // See crt0dat.c in the CRT sources for the details.
Timur Iskhodzhanov39c22ee2012-02-22 09:28:14 +0000536 #pragma section(".CRT$XIB", long, read) // NOLINT
Timur Iskhodzhanov38ed7362012-02-21 16:24:23 +0000537 __declspec(allocate(".CRT$XIB")) void (*__asan_preinit)() = __asan_init;
Evgeniy Stepanov8bcc6b92012-01-11 08:17:19 +0000538#endif