blob: d75128b2038c3a18d665b4a11140cc59a1a1194d [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) {
Kostya Serebryanyf0977db2012-03-14 22:48:09 +0000247 __asan_report_load1(0);
248 __asan_report_load2(0);
249 __asan_report_load4(0);
250 __asan_report_load8(0);
251 __asan_report_load16(0);
252 __asan_report_store1(0);
253 __asan_report_store2(0);
254 __asan_report_store4(0);
255 __asan_report_store8(0);
256 __asan_report_store16(0);
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000257 __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);
Alexander Potapenkodadc45d2012-03-06 11:45:59 +0000262 __asan_handle_no_return();
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000263 }
264}
265
266// -------------------------- Init ------------------- {{{1
267static int64_t IntFlagValue(const char *flags, const char *flag,
268 int64_t default_val) {
269 if (!flags) return default_val;
Kostya Serebryanya4ccf872012-01-09 22:20:49 +0000270 const char *str = internal_strstr(flags, flag);
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000271 if (!str) return default_val;
Alexey Samsonov88981022012-02-17 16:15:09 +0000272 return internal_atoll(str + internal_strlen(flag));
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000273}
274
275static void asan_atexit() {
276 Printf("AddressSanitizer exit stats:\n");
277 __asan_print_accumulated_stats();
278}
279
280void CheckFailed(const char *cond, const char *file, int line) {
Kostya Serebryanya7e760a2012-01-09 19:18:27 +0000281 Report("CHECK failed: %s at %s:%d\n", cond, file, line);
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000282 PRINT_CURRENT_STACK();
283 ShowStatsAndAbort();
284}
285
286} // namespace __asan
287
Kostya Serebryany4803ab92012-01-09 18:53:15 +0000288// ---------------------- Interface ---------------- {{{1
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000289using namespace __asan; // NOLINT
290
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000291int __asan_set_error_exit_code(int exit_code) {
292 int old = FLAG_exitcode;
293 FLAG_exitcode = exit_code;
294 return old;
295}
296
Alexander Potapenkodadc45d2012-03-06 11:45:59 +0000297void NOINLINE __asan_handle_no_return() {
Kostya Serebryanyf54b1f92012-02-08 21:33:27 +0000298 int local_stack;
299 AsanThread *curr_thread = asanThreadRegistry().GetCurrent();
300 CHECK(curr_thread);
301 uintptr_t top = curr_thread->stack_top();
302 uintptr_t bottom = ((uintptr_t)&local_stack - kPageSize) & ~(kPageSize-1);
303 PoisonShadow(bottom, top - bottom, 0);
304}
305
Alexander Potapenko2f3f9622012-03-01 14:39:21 +0000306void NOINLINE __asan_set_death_callback(void (*callback)(void)) {
Kostya Serebryanye1fe0fd2012-02-13 21:24:29 +0000307 death_callback = callback;
308}
309
Alexander Potapenko3fe91352012-02-27 14:06:48 +0000310void NOINLINE __asan_set_error_report_callback(void (*callback)(const char*)) {
311 error_report_callback = callback;
312 if (callback) {
313 error_message_buffer_size = 1 << 14;
314 error_message_buffer =
315 (char*)AsanMmapSomewhereOrDie(error_message_buffer_size, __FUNCTION__);
316 error_message_buffer_pos = 0;
317 }
318}
319
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000320void __asan_report_error(uintptr_t pc, uintptr_t bp, uintptr_t sp,
321 uintptr_t addr, bool is_write, size_t access_size) {
322 // Do not print more than one report, otherwise they will mix up.
323 static int num_calls = 0;
324 if (AtomicInc(&num_calls) > 1) return;
325
326 Printf("=================================================================\n");
327 const char *bug_descr = "unknown-crash";
328 if (AddrIsInMem(addr)) {
329 uint8_t *shadow_addr = (uint8_t*)MemToShadow(addr);
Kostya Serebryanyacd5c612011-12-07 21:30:20 +0000330 // If we are accessing 16 bytes, look at the second shadow byte.
331 if (*shadow_addr == 0 && access_size > SHADOW_GRANULARITY)
332 shadow_addr++;
333 // If we are in the partial right redzone, look at the next shadow byte.
334 if (*shadow_addr > 0 && *shadow_addr < 128)
335 shadow_addr++;
336 switch (*shadow_addr) {
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000337 case kAsanHeapLeftRedzoneMagic:
338 case kAsanHeapRightRedzoneMagic:
339 bug_descr = "heap-buffer-overflow";
340 break;
341 case kAsanHeapFreeMagic:
342 bug_descr = "heap-use-after-free";
343 break;
344 case kAsanStackLeftRedzoneMagic:
345 bug_descr = "stack-buffer-underflow";
346 break;
347 case kAsanStackMidRedzoneMagic:
348 case kAsanStackRightRedzoneMagic:
349 case kAsanStackPartialRedzoneMagic:
350 bug_descr = "stack-buffer-overflow";
351 break;
352 case kAsanStackAfterReturnMagic:
353 bug_descr = "stack-use-after-return";
354 break;
355 case kAsanUserPoisonedMemoryMagic:
356 bug_descr = "use-after-poison";
357 break;
358 case kAsanGlobalRedzoneMagic:
359 bug_descr = "global-buffer-overflow";
360 break;
361 }
362 }
363
Kostya Serebryanyc4b34d92011-12-09 01:49:31 +0000364 AsanThread *curr_thread = asanThreadRegistry().GetCurrent();
365 int curr_tid = asanThreadRegistry().GetCurrentTidOrMinusOne();
366
367 if (curr_thread) {
368 // We started reporting an error message. Stop using the fake stack
369 // in case we will call an instrumented function from a symbolizer.
370 curr_thread->fake_stack().StopUsingFakeStack();
371 }
372
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000373 Report("ERROR: AddressSanitizer %s on address "
374 "%p at pc 0x%lx bp 0x%lx sp 0x%lx\n",
375 bug_descr, addr, pc, bp, sp);
376
377 Printf("%s of size %d at %p thread T%d\n",
378 access_size ? (is_write ? "WRITE" : "READ") : "ACCESS",
Kostya Serebryanyc4b34d92011-12-09 01:49:31 +0000379 access_size, addr, curr_tid);
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000380
381 if (FLAG_debug) {
382 PrintBytes("PC: ", (uintptr_t*)pc);
383 }
384
Evgeniy Stepanov9cfa1942012-01-19 11:34:18 +0000385 GET_STACK_TRACE_WITH_PC_AND_BP(kStackTraceMax, pc, bp);
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000386 stack.PrintStack();
387
388 CHECK(AddrIsInMem(addr));
389
390 DescribeAddress(addr, access_size);
391
392 uintptr_t shadow_addr = MemToShadow(addr);
393 Report("ABORTING\n");
394 __asan_print_accumulated_stats();
395 Printf("Shadow byte and word:\n");
396 Printf(" %p: %x\n", shadow_addr, *(unsigned char*)shadow_addr);
397 uintptr_t aligned_shadow = shadow_addr & ~(kWordSize - 1);
398 PrintBytes(" ", (uintptr_t*)(aligned_shadow));
399 Printf("More shadow bytes:\n");
400 PrintBytes(" ", (uintptr_t*)(aligned_shadow-4*kWordSize));
401 PrintBytes(" ", (uintptr_t*)(aligned_shadow-3*kWordSize));
402 PrintBytes(" ", (uintptr_t*)(aligned_shadow-2*kWordSize));
403 PrintBytes(" ", (uintptr_t*)(aligned_shadow-1*kWordSize));
404 PrintBytes("=>", (uintptr_t*)(aligned_shadow+0*kWordSize));
405 PrintBytes(" ", (uintptr_t*)(aligned_shadow+1*kWordSize));
406 PrintBytes(" ", (uintptr_t*)(aligned_shadow+2*kWordSize));
407 PrintBytes(" ", (uintptr_t*)(aligned_shadow+3*kWordSize));
408 PrintBytes(" ", (uintptr_t*)(aligned_shadow+4*kWordSize));
Alexander Potapenko3fe91352012-02-27 14:06:48 +0000409 if (error_report_callback) {
410 error_report_callback(error_message_buffer);
411 }
Kostya Serebryany0ecf5eb2012-01-09 23:11:26 +0000412 AsanDie();
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000413}
414
415void __asan_init() {
416 if (asan_inited) return;
417 asan_init_is_running = true;
418
419 // Make sure we are not statically linked.
420 AsanDoesNotSupportStaticLinkage();
421
422 // flags
Alexander Potapenko1e316d72012-01-13 12:59:48 +0000423 const char *options = AsanGetEnv("ASAN_OPTIONS");
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000424 FLAG_malloc_context_size =
425 IntFlagValue(options, "malloc_context_size=", kMallocContextSize);
426 CHECK(FLAG_malloc_context_size <= kMallocContextSize);
427
428 FLAG_max_malloc_fill_size =
429 IntFlagValue(options, "max_malloc_fill_size=", 0);
430
431 FLAG_v = IntFlagValue(options, "verbosity=", 0);
432
Evgeniy Stepanov8ae44ac2012-02-27 13:07:29 +0000433 FLAG_redzone = IntFlagValue(options, "redzone=",
434 (ASAN_LOW_MEMORY) ? 64 : 128);
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000435 CHECK(FLAG_redzone >= 32);
436 CHECK((FLAG_redzone & (FLAG_redzone - 1)) == 0);
437
438 FLAG_atexit = IntFlagValue(options, "atexit=", 0);
439 FLAG_poison_shadow = IntFlagValue(options, "poison_shadow=", 1);
440 FLAG_report_globals = IntFlagValue(options, "report_globals=", 1);
Kostya Serebryanyc6f22232011-12-08 18:30:42 +0000441 FLAG_handle_segv = IntFlagValue(options, "handle_segv=", ASAN_NEEDS_SEGV);
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000442 FLAG_symbolize = IntFlagValue(options, "symbolize=", 1);
443 FLAG_demangle = IntFlagValue(options, "demangle=", 1);
444 FLAG_debug = IntFlagValue(options, "debug=", 0);
445 FLAG_replace_cfallocator = IntFlagValue(options, "replace_cfallocator=", 1);
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000446 FLAG_replace_str = IntFlagValue(options, "replace_str=", 1);
Kostya Serebryany0ffe35c2011-12-28 19:55:30 +0000447 FLAG_replace_intrin = IntFlagValue(options, "replace_intrin=", 1);
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000448 FLAG_use_fake_stack = IntFlagValue(options, "use_fake_stack=", 1);
Alexey Samsonovb823e3c2012-02-22 14:07:06 +0000449 FLAG_exitcode = IntFlagValue(options, "exitcode=",
450 ASAN_DEFAULT_FAILURE_EXITCODE);
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000451 FLAG_allow_user_poisoning = IntFlagValue(options,
452 "allow_user_poisoning=", 1);
Kostya Serebryanycb00d132012-01-31 00:52:18 +0000453 FLAG_sleep_before_dying = IntFlagValue(options, "sleep_before_dying=", 0);
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000454
455 if (FLAG_atexit) {
Alexey Samsonovb823e3c2012-02-22 14:07:06 +0000456 Atexit(asan_atexit);
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000457 }
458
Evgeniy Stepanov8ae44ac2012-02-27 13:07:29 +0000459 FLAG_quarantine_size = IntFlagValue(options, "quarantine_size=",
460 (ASAN_LOW_MEMORY) ? 1UL << 24 : 1UL << 28);
461
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000462 // interceptors
463 InitializeAsanInterceptors();
464
465 ReplaceSystemMalloc();
Kostya Serebryanya7e760a2012-01-09 19:18:27 +0000466 InstallSignalHandlers();
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000467
468 if (FLAG_v) {
469 Printf("|| `[%p, %p]` || HighMem ||\n", kHighMemBeg, kHighMemEnd);
470 Printf("|| `[%p, %p]` || HighShadow ||\n",
471 kHighShadowBeg, kHighShadowEnd);
472 Printf("|| `[%p, %p]` || ShadowGap ||\n",
473 kShadowGapBeg, kShadowGapEnd);
474 Printf("|| `[%p, %p]` || LowShadow ||\n",
475 kLowShadowBeg, kLowShadowEnd);
476 Printf("|| `[%p, %p]` || LowMem ||\n", kLowMemBeg, kLowMemEnd);
477 Printf("MemToShadow(shadow): %p %p %p %p\n",
478 MEM_TO_SHADOW(kLowShadowBeg),
479 MEM_TO_SHADOW(kLowShadowEnd),
480 MEM_TO_SHADOW(kHighShadowBeg),
481 MEM_TO_SHADOW(kHighShadowEnd));
482 Printf("red_zone=%ld\n", FLAG_redzone);
483 Printf("malloc_context_size=%ld\n", (int)FLAG_malloc_context_size);
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000484
485 Printf("SHADOW_SCALE: %lx\n", SHADOW_SCALE);
486 Printf("SHADOW_GRANULARITY: %lx\n", SHADOW_GRANULARITY);
487 Printf("SHADOW_OFFSET: %lx\n", SHADOW_OFFSET);
488 CHECK(SHADOW_SCALE >= 3 && SHADOW_SCALE <= 7);
489 }
490
491 if (__WORDSIZE == 64) {
492 // Disable core dumper -- it makes little sense to dump 16T+ core.
Kostya Serebryanyef14ff62012-01-06 02:12:25 +0000493 AsanDisableCoreDumper();
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000494 }
495
Alexander Potapenkoc50e8352012-02-13 15:11:23 +0000496 if (AsanShadowRangeIsAvailable()) {
Kostya Serebryanya7e760a2012-01-09 19:18:27 +0000497 if (kLowShadowBeg != kLowShadowEnd) {
Timur Iskhodzhanov3e81fe42012-02-09 17:20:14 +0000498 // mmap the low shadow plus at least one page.
499 ReserveShadowMemoryRange(kLowShadowBeg - kMmapGranularity, kLowShadowEnd);
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000500 }
Kostya Serebryanya7e760a2012-01-09 19:18:27 +0000501 // mmap the high shadow.
502 ReserveShadowMemoryRange(kHighShadowBeg, kHighShadowEnd);
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000503 // protect the gap
Kostya Serebryanya874fe52011-12-28 23:28:54 +0000504 void *prot = AsanMprotect(kShadowGapBeg, kShadowGapEnd - kShadowGapBeg + 1);
505 CHECK(prot == (void*)kShadowGapBeg);
Alexander Potapenkoc50e8352012-02-13 15:11:23 +0000506 } else {
507 Report("Shadow memory range interleaves with an existing memory mapping. "
508 "ASan cannot proceed correctly. ABORTING.\n");
Alexander Potapenko99d17eb2012-02-22 09:11:55 +0000509 AsanDumpProcessMap();
Alexander Potapenkoc50e8352012-02-13 15:11:23 +0000510 AsanDie();
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000511 }
512
513 // On Linux AsanThread::ThreadStart() calls malloc() that's why asan_inited
514 // should be set to 1 prior to initializing the threads.
515 asan_inited = 1;
516 asan_init_is_running = false;
517
518 asanThreadRegistry().Init();
519 asanThreadRegistry().GetMain()->ThreadStart();
Kostya Serebryany51e75c42011-12-28 00:59:39 +0000520 force_interface_symbols(); // no-op.
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000521
522 if (FLAG_v) {
Kostya Serebryanyd6567c52011-12-01 21:40:52 +0000523 Report("AddressSanitizer Init done\n");
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000524 }
525}
Evgeniy Stepanov8bcc6b92012-01-11 08:17:19 +0000526
527#if defined(ASAN_USE_PREINIT_ARRAY)
Timur Iskhodzhanov38ed7362012-02-21 16:24:23 +0000528 // On Linux, we force __asan_init to be called before anyone else
529 // by placing it into .preinit_array section.
530 // FIXME: do we have anything like this on Mac?
531 __attribute__((section(".preinit_array")))
532 typeof(__asan_init) *__asan_preinit =__asan_init;
533#elif defined(_WIN32) && defined(_DLL)
534 // On Windows, when using dynamic CRT (/MD), we can put a pointer
535 // to __asan_init into the global list of C initializers.
536 // See crt0dat.c in the CRT sources for the details.
Timur Iskhodzhanov39c22ee2012-02-22 09:28:14 +0000537 #pragma section(".CRT$XIB", long, read) // NOLINT
Timur Iskhodzhanov38ed7362012-02-21 16:24:23 +0000538 __declspec(allocate(".CRT$XIB")) void (*__asan_preinit)() = __asan_init;
Evgeniy Stepanov8bcc6b92012-01-11 08:17:19 +0000539#endif