blob: fff199bf352821e9c3ccf0019af00b1c5bdefcba [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);
Kostya Serebryany1e172b42011-11-30 01:07:02 +000055
Kostya Serebryany1e172b42011-11-30 01:07:02 +000056// -------------------------- Misc ---------------- {{{1
57void ShowStatsAndAbort() {
58 __asan_print_accumulated_stats();
Kostya Serebryany0ecf5eb2012-01-09 23:11:26 +000059 AsanDie();
Kostya Serebryany1e172b42011-11-30 01:07:02 +000060}
61
62static void PrintBytes(const char *before, uintptr_t *a) {
63 uint8_t *bytes = (uint8_t*)a;
64 size_t byte_num = (__WORDSIZE) / 8;
65 Printf("%s%p:", before, (uintptr_t)a);
66 for (size_t i = 0; i < byte_num; i++) {
67 Printf(" %lx%lx", bytes[i] >> 4, bytes[i] & 15);
68 }
69 Printf("\n");
70}
71
Kostya Serebryany0ecf5eb2012-01-09 23:11:26 +000072size_t ReadFileToBuffer(const char *file_name, char **buff,
Kostya Serebryanydf499b42012-01-05 00:44:33 +000073 size_t *buff_size, size_t max_len) {
Kostya Serebryanyde496f42011-12-28 22:58:01 +000074 const size_t kMinFileLen = kPageSize;
Kostya Serebryany0ecf5eb2012-01-09 23:11:26 +000075 size_t read_len = 0;
Kostya Serebryanyde496f42011-12-28 22:58:01 +000076 *buff = 0;
Kostya Serebryanydf499b42012-01-05 00:44:33 +000077 *buff_size = 0;
Kostya Serebryanyde496f42011-12-28 22:58:01 +000078 // The files we usually open are not seekable, so try different buffer sizes.
79 for (size_t size = kMinFileLen; size <= max_len; size *= 2) {
80 int fd = AsanOpenReadonly(file_name);
81 if (fd < 0) return -1;
Kostya Serebryanydf499b42012-01-05 00:44:33 +000082 AsanUnmapOrDie(*buff, *buff_size);
Kostya Serebryanyde496f42011-12-28 22:58:01 +000083 *buff = (char*)AsanMmapSomewhereOrDie(size, __FUNCTION__);
Kostya Serebryanydf499b42012-01-05 00:44:33 +000084 *buff_size = size;
Kostya Serebryany454a0642012-01-17 18:00:07 +000085 // Read up to one page at a time.
86 read_len = 0;
87 bool reached_eof = false;
88 while (read_len + kPageSize <= size) {
89 size_t just_read = AsanRead(fd, *buff + read_len, kPageSize);
90 if (just_read == 0) {
91 reached_eof = true;
92 break;
93 }
94 read_len += just_read;
95 }
Kostya Serebryanyde496f42011-12-28 22:58:01 +000096 AsanClose(fd);
Kostya Serebryany454a0642012-01-17 18:00:07 +000097 if (reached_eof) // We've read the whole file.
Kostya Serebryanyde496f42011-12-28 22:58:01 +000098 break;
99 }
100 return read_len;
101}
102
Kostya Serebryanye1fe0fd2012-02-13 21:24:29 +0000103void AsanDie() {
Kostya Serebryany5e4e91c2012-02-16 00:40:18 +0000104 static int num_calls = 0;
105 if (AtomicInc(&num_calls) > 1) return; // Don't die twice.
Kostya Serebryanye1fe0fd2012-02-13 21:24:29 +0000106 if (FLAG_sleep_before_dying) {
107 Report("Sleeping for %d second(s)\n", FLAG_sleep_before_dying);
108 SleepForSeconds(FLAG_sleep_before_dying);
109 }
110 if (death_callback)
111 death_callback();
112 Exit(FLAG_exitcode);
113}
114
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000115// ---------------------- mmap -------------------- {{{1
Kostya Serebryanyde496f42011-12-28 22:58:01 +0000116void OutOfMemoryMessageAndDie(const char *mem_type, size_t size) {
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000117 Report("ERROR: AddressSanitizer failed to allocate "
118 "0x%lx (%ld) bytes of %s\n",
119 size, size, mem_type);
Kostya Serebryanyde496f42011-12-28 22:58:01 +0000120 PRINT_CURRENT_STACK();
121 ShowStatsAndAbort();
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000122}
123
Kostya Serebryanya874fe52011-12-28 23:28:54 +0000124// Reserve memory range [beg, end].
125static void ReserveShadowMemoryRange(uintptr_t beg, uintptr_t end) {
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000126 CHECK((beg % kPageSize) == 0);
127 CHECK(((end + 1) % kPageSize) == 0);
Kostya Serebryanya874fe52011-12-28 23:28:54 +0000128 size_t size = end - beg + 1;
129 void *res = AsanMmapFixedNoReserve(beg, size);
130 CHECK(res == (void*)beg && "ReserveShadowMemoryRange failed");
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000131}
132
Kostya Serebryanyb89567c2011-12-02 21:02:20 +0000133// ---------------------- LowLevelAllocator ------------- {{{1
134void *LowLevelAllocator::Allocate(size_t size) {
135 CHECK((size & (size - 1)) == 0 && "size must be a power of two");
136 if (allocated_end_ - allocated_current_ < size) {
137 size_t size_to_allocate = Max(size, kPageSize);
Kostya Serebryanyde496f42011-12-28 22:58:01 +0000138 allocated_current_ =
139 (char*)AsanMmapSomewhereOrDie(size_to_allocate, __FUNCTION__);
Kostya Serebryanyb89567c2011-12-02 21:02:20 +0000140 allocated_end_ = allocated_current_ + size_to_allocate;
Kostya Serebryany6b30e2c2011-12-15 17:41:30 +0000141 PoisonShadow((uintptr_t)allocated_current_, size_to_allocate,
142 kAsanInternalHeapMagic);
Kostya Serebryanyb89567c2011-12-02 21:02:20 +0000143 }
144 CHECK(allocated_end_ - allocated_current_ >= size);
145 void *res = allocated_current_;
146 allocated_current_ += size;
147 return res;
148}
149
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000150// ---------------------- DescribeAddress -------------------- {{{1
151static bool DescribeStackAddress(uintptr_t addr, uintptr_t access_size) {
152 AsanThread *t = asanThreadRegistry().FindThreadByStackAddress(addr);
153 if (!t) return false;
154 const intptr_t kBufSize = 4095;
155 char buf[kBufSize];
156 uintptr_t offset = 0;
157 const char *frame_descr = t->GetFrameNameByAddr(addr, &offset);
158 // This string is created by the compiler and has the following form:
159 // "FunctioName n alloc_1 alloc_2 ... alloc_n"
160 // where alloc_i looks like "offset size len ObjectName ".
161 CHECK(frame_descr);
162 // Report the function name and the offset.
Timur Iskhodzhanov53627172012-02-14 19:33:04 +0000163 const char *name_end = internal_strchr(frame_descr, ' ');
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000164 CHECK(name_end);
165 buf[0] = 0;
Kostya Serebryanya4ccf872012-01-09 22:20:49 +0000166 internal_strncat(buf, frame_descr,
167 Min(kBufSize,
168 static_cast<intptr_t>(name_end - frame_descr)));
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000169 Printf("Address %p is located at offset %ld "
170 "in frame <%s> of T%d's stack:\n",
171 addr, offset, buf, t->tid());
172 // Report the number of stack objects.
173 char *p;
Alexey Samsonov88981022012-02-17 16:15:09 +0000174 size_t n_objects = internal_simple_strtoll(name_end, &p, 10);
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000175 CHECK(n_objects > 0);
176 Printf(" This frame has %ld object(s):\n", n_objects);
177 // Report all objects in this frame.
178 for (size_t i = 0; i < n_objects; i++) {
179 size_t beg, size;
180 intptr_t len;
Alexey Samsonov88981022012-02-17 16:15:09 +0000181 beg = internal_simple_strtoll(p, &p, 10);
182 size = internal_simple_strtoll(p, &p, 10);
183 len = internal_simple_strtoll(p, &p, 10);
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000184 if (beg <= 0 || size <= 0 || len < 0 || *p != ' ') {
185 Printf("AddressSanitizer can't parse the stack frame descriptor: |%s|\n",
186 frame_descr);
187 break;
188 }
189 p++;
190 buf[0] = 0;
Kostya Serebryanya4ccf872012-01-09 22:20:49 +0000191 internal_strncat(buf, p, Min(kBufSize, len));
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000192 p += len;
193 Printf(" [%ld, %ld) '%s'\n", beg, beg + size, buf);
194 }
195 Printf("HINT: this may be a false positive if your program uses "
196 "some custom stack unwind mechanism\n"
197 " (longjmp and C++ exceptions *are* supported)\n");
198 t->summary()->Announce();
199 return true;
200}
201
Alexey Samsonovadf2b032012-02-03 08:37:19 +0000202static NOINLINE void DescribeAddress(uintptr_t addr, uintptr_t access_size) {
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000203 // Check if this is a global.
204 if (DescribeAddrIfGlobal(addr))
205 return;
206
207 if (DescribeStackAddress(addr, access_size))
208 return;
209
210 // finally, check if this is a heap.
211 DescribeHeapAddress(addr, access_size);
212}
213
214// -------------------------- Run-time entry ------------------- {{{1
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000215// exported functions
Kostya Serebryany51e75c42011-12-28 00:59:39 +0000216#define ASAN_REPORT_ERROR(type, is_write, size) \
Alexey Samsonovadf2b032012-02-03 08:37:19 +0000217NOINLINE ASAN_INTERFACE_ATTRIBUTE \
218extern "C" void __asan_report_ ## type ## size(uintptr_t addr); \
Kostya Serebryany51e75c42011-12-28 00:59:39 +0000219extern "C" void __asan_report_ ## type ## size(uintptr_t addr) { \
220 GET_BP_PC_SP; \
221 __asan_report_error(pc, bp, sp, addr, is_write, size); \
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000222}
223
224ASAN_REPORT_ERROR(load, false, 1)
225ASAN_REPORT_ERROR(load, false, 2)
226ASAN_REPORT_ERROR(load, false, 4)
227ASAN_REPORT_ERROR(load, false, 8)
228ASAN_REPORT_ERROR(load, false, 16)
229ASAN_REPORT_ERROR(store, true, 1)
230ASAN_REPORT_ERROR(store, true, 2)
231ASAN_REPORT_ERROR(store, true, 4)
232ASAN_REPORT_ERROR(store, true, 8)
233ASAN_REPORT_ERROR(store, true, 16)
234
235// Force the linker to keep the symbols for various ASan interface functions.
236// We want to keep those in the executable in order to let the instrumented
237// dynamic libraries access the symbol even if it is not used by the executable
238// itself. This should help if the build system is removing dead code at link
239// time.
Kostya Serebryany51e75c42011-12-28 00:59:39 +0000240static void force_interface_symbols() {
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000241 volatile int fake_condition = 0; // prevent dead condition elimination.
242 if (fake_condition) {
243 __asan_report_load1(NULL);
244 __asan_report_load2(NULL);
245 __asan_report_load4(NULL);
246 __asan_report_load8(NULL);
247 __asan_report_load16(NULL);
248 __asan_report_store1(NULL);
249 __asan_report_store2(NULL);
250 __asan_report_store4(NULL);
251 __asan_report_store8(NULL);
252 __asan_report_store16(NULL);
253 __asan_register_global(0, 0, NULL);
254 __asan_register_globals(NULL, 0);
Kostya Serebryany45581682011-12-28 23:35:46 +0000255 __asan_unregister_globals(NULL, 0);
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000256 }
257}
258
259// -------------------------- Init ------------------- {{{1
260static int64_t IntFlagValue(const char *flags, const char *flag,
261 int64_t default_val) {
262 if (!flags) return default_val;
Kostya Serebryanya4ccf872012-01-09 22:20:49 +0000263 const char *str = internal_strstr(flags, flag);
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000264 if (!str) return default_val;
Alexey Samsonov88981022012-02-17 16:15:09 +0000265 return internal_atoll(str + internal_strlen(flag));
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000266}
267
268static void asan_atexit() {
269 Printf("AddressSanitizer exit stats:\n");
270 __asan_print_accumulated_stats();
271}
272
273void CheckFailed(const char *cond, const char *file, int line) {
Kostya Serebryanya7e760a2012-01-09 19:18:27 +0000274 Report("CHECK failed: %s at %s:%d\n", cond, file, line);
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000275 PRINT_CURRENT_STACK();
276 ShowStatsAndAbort();
277}
278
279} // namespace __asan
280
Kostya Serebryany4803ab92012-01-09 18:53:15 +0000281// ---------------------- Interface ---------------- {{{1
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000282using namespace __asan; // NOLINT
283
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000284int __asan_set_error_exit_code(int exit_code) {
285 int old = FLAG_exitcode;
286 FLAG_exitcode = exit_code;
287 return old;
288}
289
Kostya Serebryanyf54b1f92012-02-08 21:33:27 +0000290void __asan_handle_no_return() {
291 int local_stack;
292 AsanThread *curr_thread = asanThreadRegistry().GetCurrent();
293 CHECK(curr_thread);
294 uintptr_t top = curr_thread->stack_top();
295 uintptr_t bottom = ((uintptr_t)&local_stack - kPageSize) & ~(kPageSize-1);
296 PoisonShadow(bottom, top - bottom, 0);
297}
298
Kostya Serebryanye1fe0fd2012-02-13 21:24:29 +0000299void __asan_set_death_callback(void (*callback)(void)) {
300 death_callback = callback;
301}
302
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000303void __asan_report_error(uintptr_t pc, uintptr_t bp, uintptr_t sp,
304 uintptr_t addr, bool is_write, size_t access_size) {
305 // Do not print more than one report, otherwise they will mix up.
306 static int num_calls = 0;
307 if (AtomicInc(&num_calls) > 1) return;
308
309 Printf("=================================================================\n");
310 const char *bug_descr = "unknown-crash";
311 if (AddrIsInMem(addr)) {
312 uint8_t *shadow_addr = (uint8_t*)MemToShadow(addr);
Kostya Serebryanyacd5c612011-12-07 21:30:20 +0000313 // If we are accessing 16 bytes, look at the second shadow byte.
314 if (*shadow_addr == 0 && access_size > SHADOW_GRANULARITY)
315 shadow_addr++;
316 // If we are in the partial right redzone, look at the next shadow byte.
317 if (*shadow_addr > 0 && *shadow_addr < 128)
318 shadow_addr++;
319 switch (*shadow_addr) {
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000320 case kAsanHeapLeftRedzoneMagic:
321 case kAsanHeapRightRedzoneMagic:
322 bug_descr = "heap-buffer-overflow";
323 break;
324 case kAsanHeapFreeMagic:
325 bug_descr = "heap-use-after-free";
326 break;
327 case kAsanStackLeftRedzoneMagic:
328 bug_descr = "stack-buffer-underflow";
329 break;
330 case kAsanStackMidRedzoneMagic:
331 case kAsanStackRightRedzoneMagic:
332 case kAsanStackPartialRedzoneMagic:
333 bug_descr = "stack-buffer-overflow";
334 break;
335 case kAsanStackAfterReturnMagic:
336 bug_descr = "stack-use-after-return";
337 break;
338 case kAsanUserPoisonedMemoryMagic:
339 bug_descr = "use-after-poison";
340 break;
341 case kAsanGlobalRedzoneMagic:
342 bug_descr = "global-buffer-overflow";
343 break;
344 }
345 }
346
Kostya Serebryanyc4b34d92011-12-09 01:49:31 +0000347 AsanThread *curr_thread = asanThreadRegistry().GetCurrent();
348 int curr_tid = asanThreadRegistry().GetCurrentTidOrMinusOne();
349
350 if (curr_thread) {
351 // We started reporting an error message. Stop using the fake stack
352 // in case we will call an instrumented function from a symbolizer.
353 curr_thread->fake_stack().StopUsingFakeStack();
354 }
355
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000356 Report("ERROR: AddressSanitizer %s on address "
357 "%p at pc 0x%lx bp 0x%lx sp 0x%lx\n",
358 bug_descr, addr, pc, bp, sp);
359
360 Printf("%s of size %d at %p thread T%d\n",
361 access_size ? (is_write ? "WRITE" : "READ") : "ACCESS",
Kostya Serebryanyc4b34d92011-12-09 01:49:31 +0000362 access_size, addr, curr_tid);
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000363
364 if (FLAG_debug) {
365 PrintBytes("PC: ", (uintptr_t*)pc);
366 }
367
Evgeniy Stepanov9cfa1942012-01-19 11:34:18 +0000368 GET_STACK_TRACE_WITH_PC_AND_BP(kStackTraceMax, pc, bp);
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000369 stack.PrintStack();
370
371 CHECK(AddrIsInMem(addr));
372
373 DescribeAddress(addr, access_size);
374
375 uintptr_t shadow_addr = MemToShadow(addr);
376 Report("ABORTING\n");
377 __asan_print_accumulated_stats();
378 Printf("Shadow byte and word:\n");
379 Printf(" %p: %x\n", shadow_addr, *(unsigned char*)shadow_addr);
380 uintptr_t aligned_shadow = shadow_addr & ~(kWordSize - 1);
381 PrintBytes(" ", (uintptr_t*)(aligned_shadow));
382 Printf("More shadow bytes:\n");
383 PrintBytes(" ", (uintptr_t*)(aligned_shadow-4*kWordSize));
384 PrintBytes(" ", (uintptr_t*)(aligned_shadow-3*kWordSize));
385 PrintBytes(" ", (uintptr_t*)(aligned_shadow-2*kWordSize));
386 PrintBytes(" ", (uintptr_t*)(aligned_shadow-1*kWordSize));
387 PrintBytes("=>", (uintptr_t*)(aligned_shadow+0*kWordSize));
388 PrintBytes(" ", (uintptr_t*)(aligned_shadow+1*kWordSize));
389 PrintBytes(" ", (uintptr_t*)(aligned_shadow+2*kWordSize));
390 PrintBytes(" ", (uintptr_t*)(aligned_shadow+3*kWordSize));
391 PrintBytes(" ", (uintptr_t*)(aligned_shadow+4*kWordSize));
Kostya Serebryany0ecf5eb2012-01-09 23:11:26 +0000392 AsanDie();
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000393}
394
395void __asan_init() {
396 if (asan_inited) return;
397 asan_init_is_running = true;
398
399 // Make sure we are not statically linked.
400 AsanDoesNotSupportStaticLinkage();
401
402 // flags
Alexander Potapenko1e316d72012-01-13 12:59:48 +0000403 const char *options = AsanGetEnv("ASAN_OPTIONS");
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000404 FLAG_malloc_context_size =
405 IntFlagValue(options, "malloc_context_size=", kMallocContextSize);
406 CHECK(FLAG_malloc_context_size <= kMallocContextSize);
407
408 FLAG_max_malloc_fill_size =
409 IntFlagValue(options, "max_malloc_fill_size=", 0);
410
411 FLAG_v = IntFlagValue(options, "verbosity=", 0);
412
Evgeniy Stepanov788e1d72012-02-16 13:35:11 +0000413#if ASAN_LOW_MEMORY == 1
414 FLAG_quarantine_size =
Alexey Samsonov6a3e6fd2012-02-17 08:31:10 +0000415 IntFlagValue(options, "quarantine_size=", 1UL << 24); // 16M
Evgeniy Stepanov788e1d72012-02-16 13:35:11 +0000416 FLAG_redzone = IntFlagValue(options, "redzone=", 64);
417#else
418 FLAG_quarantine_size =
Alexey Samsonov6a3e6fd2012-02-17 08:31:10 +0000419 IntFlagValue(options, "quarantine_size=", 1UL << 28); // 256M
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000420 FLAG_redzone = IntFlagValue(options, "redzone=", 128);
Evgeniy Stepanov788e1d72012-02-16 13:35:11 +0000421#endif
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000422 CHECK(FLAG_redzone >= 32);
423 CHECK((FLAG_redzone & (FLAG_redzone - 1)) == 0);
424
425 FLAG_atexit = IntFlagValue(options, "atexit=", 0);
426 FLAG_poison_shadow = IntFlagValue(options, "poison_shadow=", 1);
427 FLAG_report_globals = IntFlagValue(options, "report_globals=", 1);
Kostya Serebryanyc6f22232011-12-08 18:30:42 +0000428 FLAG_handle_segv = IntFlagValue(options, "handle_segv=", ASAN_NEEDS_SEGV);
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000429 FLAG_symbolize = IntFlagValue(options, "symbolize=", 1);
430 FLAG_demangle = IntFlagValue(options, "demangle=", 1);
431 FLAG_debug = IntFlagValue(options, "debug=", 0);
432 FLAG_replace_cfallocator = IntFlagValue(options, "replace_cfallocator=", 1);
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000433 FLAG_replace_str = IntFlagValue(options, "replace_str=", 1);
Kostya Serebryany0ffe35c2011-12-28 19:55:30 +0000434 FLAG_replace_intrin = IntFlagValue(options, "replace_intrin=", 1);
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000435 FLAG_use_fake_stack = IntFlagValue(options, "use_fake_stack=", 1);
Alexey Samsonovb823e3c2012-02-22 14:07:06 +0000436 FLAG_exitcode = IntFlagValue(options, "exitcode=",
437 ASAN_DEFAULT_FAILURE_EXITCODE);
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000438 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) {
Alexey Samsonovb823e3c2012-02-22 14:07:06 +0000443 Atexit(asan_atexit);
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000444 }
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 Potapenko99d17eb2012-02-22 09:11:55 +0000493 AsanDumpProcessMap();
Alexander Potapenkoc50e8352012-02-13 15:11:23 +0000494 AsanDie();
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000495 }
496
497 // On Linux AsanThread::ThreadStart() calls malloc() that's why asan_inited
498 // should be set to 1 prior to initializing the threads.
499 asan_inited = 1;
500 asan_init_is_running = false;
501
502 asanThreadRegistry().Init();
503 asanThreadRegistry().GetMain()->ThreadStart();
Kostya Serebryany51e75c42011-12-28 00:59:39 +0000504 force_interface_symbols(); // no-op.
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000505
506 if (FLAG_v) {
Kostya Serebryanyd6567c52011-12-01 21:40:52 +0000507 Report("AddressSanitizer Init done\n");
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000508 }
509}
Evgeniy Stepanov8bcc6b92012-01-11 08:17:19 +0000510
511#if defined(ASAN_USE_PREINIT_ARRAY)
Timur Iskhodzhanov38ed7362012-02-21 16:24:23 +0000512 // On Linux, we force __asan_init to be called before anyone else
513 // by placing it into .preinit_array section.
514 // FIXME: do we have anything like this on Mac?
515 __attribute__((section(".preinit_array")))
516 typeof(__asan_init) *__asan_preinit =__asan_init;
517#elif defined(_WIN32) && defined(_DLL)
518 // On Windows, when using dynamic CRT (/MD), we can put a pointer
519 // to __asan_init into the global list of C initializers.
520 // See crt0dat.c in the CRT sources for the details.
Timur Iskhodzhanov39c22ee2012-02-22 09:28:14 +0000521 #pragma section(".CRT$XIB", long, read) // NOLINT
Timur Iskhodzhanov38ed7362012-02-21 16:24:23 +0000522 __declspec(allocate(".CRT$XIB")) void (*__asan_preinit)() = __asan_init;
Evgeniy Stepanov8bcc6b92012-01-11 08:17:19 +0000523#endif