[CMake] Rename add_compiler_rt_static_runtime to add_compiler_rt_runtime.
[blocksruntime.git] / lib / asan / asan_rtl.cc
blob631b5c9558ddd4b4495a3e87996589b4372d0ef1
1 //===-- asan_rtl.cc -------------------------------------------------------===//
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.
12 // Main file of the ASan run-time library.
13 //===----------------------------------------------------------------------===//
14 #include "asan_activation.h"
15 #include "asan_allocator.h"
16 #include "asan_interceptors.h"
17 #include "asan_interface_internal.h"
18 #include "asan_internal.h"
19 #include "asan_mapping.h"
20 #include "asan_poisoning.h"
21 #include "asan_report.h"
22 #include "asan_stack.h"
23 #include "asan_stats.h"
24 #include "asan_thread.h"
25 #include "sanitizer_common/sanitizer_atomic.h"
26 #include "sanitizer_common/sanitizer_flags.h"
27 #include "sanitizer_common/sanitizer_libc.h"
28 #include "sanitizer_common/sanitizer_symbolizer.h"
29 #include "lsan/lsan_common.h"
31 int __asan_option_detect_stack_use_after_return; // Global interface symbol.
33 namespace __asan {
35 uptr AsanMappingProfile[kAsanMappingProfileSize];
37 static void AsanDie() {
38 static atomic_uint32_t num_calls;
39 if (atomic_fetch_add(&num_calls, 1, memory_order_relaxed) != 0) {
40 // Don't die twice - run a busy loop.
41 while (1) { }
43 if (flags()->sleep_before_dying) {
44 Report("Sleeping for %d second(s)\n", flags()->sleep_before_dying);
45 SleepForSeconds(flags()->sleep_before_dying);
47 if (flags()->unmap_shadow_on_exit) {
48 if (kMidMemBeg) {
49 UnmapOrDie((void*)kLowShadowBeg, kMidMemBeg - kLowShadowBeg);
50 UnmapOrDie((void*)kMidMemEnd, kHighShadowEnd - kMidMemEnd);
51 } else {
52 UnmapOrDie((void*)kLowShadowBeg, kHighShadowEnd - kLowShadowBeg);
55 if (flags()->coverage)
56 __sanitizer_cov_dump();
57 if (death_callback)
58 death_callback();
59 if (flags()->abort_on_error)
60 Abort();
61 internal__exit(flags()->exitcode);
64 static void AsanCheckFailed(const char *file, int line, const char *cond,
65 u64 v1, u64 v2) {
66 Report("AddressSanitizer CHECK failed: %s:%d \"%s\" (0x%zx, 0x%zx)\n", file,
67 line, cond, (uptr)v1, (uptr)v2);
68 // FIXME: check for infinite recursion without a thread-local counter here.
69 PRINT_CURRENT_STACK();
70 Die();
73 // -------------------------- Flags ------------------------- {{{1
74 static const int kDefaultMallocContextSize = 30;
76 Flags asan_flags_dont_use_directly; // use via flags().
78 static const char *MaybeCallAsanDefaultOptions() {
79 return (&__asan_default_options) ? __asan_default_options() : "";
82 static const char *MaybeUseAsanDefaultOptionsCompileDefiniton() {
83 #ifdef ASAN_DEFAULT_OPTIONS
84 // Stringize the macro value.
85 # define ASAN_STRINGIZE(x) #x
86 # define ASAN_STRINGIZE_OPTIONS(options) ASAN_STRINGIZE(options)
87 return ASAN_STRINGIZE_OPTIONS(ASAN_DEFAULT_OPTIONS);
88 #else
89 return "";
90 #endif
93 static void ParseFlagsFromString(Flags *f, const char *str) {
94 CommonFlags *cf = common_flags();
95 ParseCommonFlagsFromString(cf, str);
96 CHECK((uptr)cf->malloc_context_size <= kStackTraceMax);
97 // Please write meaningful flag descriptions when adding new flags.
98 ParseFlag(str, &f->quarantine_size, "quarantine_size",
99 "Size (in bytes) of quarantine used to detect use-after-free "
100 "errors. Lower value may reduce memory usage but increase the "
101 "chance of false negatives.");
102 ParseFlag(str, &f->redzone, "redzone",
103 "Minimal size (in bytes) of redzones around heap objects. "
104 "Requirement: redzone >= 16, is a power of two.");
105 ParseFlag(str, &f->max_redzone, "max_redzone",
106 "Maximal size (in bytes) of redzones around heap objects.");
107 CHECK_GE(f->redzone, 16);
108 CHECK_GE(f->max_redzone, f->redzone);
109 CHECK_LE(f->max_redzone, 2048);
110 CHECK(IsPowerOfTwo(f->redzone));
111 CHECK(IsPowerOfTwo(f->max_redzone));
113 ParseFlag(str, &f->debug, "debug",
114 "If set, prints some debugging information and does additional checks.");
115 ParseFlag(str, &f->report_globals, "report_globals",
116 "Controls the way to handle globals (0 - don't detect buffer overflow on "
117 "globals, 1 - detect buffer overflow, 2 - print data about registered "
118 "globals).");
120 ParseFlag(str, &f->check_initialization_order,
121 "check_initialization_order",
122 "If set, attempts to catch initialization order issues.");
124 ParseFlag(str, &f->replace_str, "replace_str",
125 "If set, uses custom wrappers and replacements for libc string functions "
126 "to find more errors.");
128 ParseFlag(str, &f->replace_intrin, "replace_intrin",
129 "If set, uses custom wrappers for memset/memcpy/memmove intinsics.");
130 ParseFlag(str, &f->mac_ignore_invalid_free, "mac_ignore_invalid_free",
131 "Ignore invalid free() calls to work around some bugs. Used on OS X "
132 "only.");
133 ParseFlag(str, &f->detect_stack_use_after_return,
134 "detect_stack_use_after_return",
135 "Enables stack-use-after-return checking at run-time.");
136 ParseFlag(str, &f->min_uar_stack_size_log, "min_uar_stack_size_log",
137 "Minimum fake stack size log.");
138 ParseFlag(str, &f->max_uar_stack_size_log, "max_uar_stack_size_log",
139 "Maximum fake stack size log.");
140 ParseFlag(str, &f->uar_noreserve, "uar_noreserve",
141 "Use mmap with 'norserve' flag to allocate fake stack.");
142 ParseFlag(str, &f->max_malloc_fill_size, "max_malloc_fill_size",
143 "ASan allocator flag. max_malloc_fill_size is the maximal amount of "
144 "bytes that will be filled with malloc_fill_byte on malloc.");
145 ParseFlag(str, &f->malloc_fill_byte, "malloc_fill_byte",
146 "Value used to fill the newly allocated memory.");
147 ParseFlag(str, &f->exitcode, "exitcode",
148 "Override the program exit status if the tool found an error.");
149 ParseFlag(str, &f->allow_user_poisoning, "allow_user_poisoning",
150 "If set, user may manually mark memory regions as poisoned or "
151 "unpoisoned.");
152 ParseFlag(str, &f->sleep_before_dying, "sleep_before_dying",
153 "Number of seconds to sleep between printing an error report and "
154 "terminating the program. Useful for debugging purposes (e.g. when one "
155 "needs to attach gdb).");
157 ParseFlag(str, &f->check_malloc_usable_size, "check_malloc_usable_size",
158 "Allows the users to work around the bug in Nvidia drivers prior to "
159 "295.*.");
161 ParseFlag(str, &f->unmap_shadow_on_exit, "unmap_shadow_on_exit",
162 "If set, explicitly unmaps the (huge) shadow at exit.");
163 ParseFlag(str, &f->abort_on_error, "abort_on_error",
164 "If set, the tool calls abort() instead of _exit() after printing the "
165 "error report.");
166 ParseFlag(str, &f->print_stats, "print_stats",
167 "Print various statistics after printing an error message or if "
168 "atexit=1.");
169 ParseFlag(str, &f->print_legend, "print_legend",
170 "Print the legend for the shadow bytes.");
171 ParseFlag(str, &f->atexit, "atexit",
172 "If set, prints ASan exit stats even after program terminates "
173 "successfully.");
174 ParseFlag(str, &f->coverage, "coverage",
175 "If set, coverage information will be dumped at program shutdown (if the "
176 "coverage instrumentation was enabled at compile time).");
178 ParseFlag(str, &f->disable_core, "disable_core",
179 "Disable core dumping. By default, disable_core=1 on 64-bit to avoid "
180 "dumping a 16T+ core file.");
182 ParseFlag(str, &f->allow_reexec, "allow_reexec",
183 "Allow the tool to re-exec the program. This may interfere badly with "
184 "the debugger.");
186 ParseFlag(str, &f->print_full_thread_history,
187 "print_full_thread_history",
188 "If set, prints thread creation stacks for the threads involved in the "
189 "report and their ancestors up to the main thread.");
191 ParseFlag(str, &f->poison_heap, "poison_heap",
192 "Poison (or not) the heap memory on [de]allocation. Zero value is useful "
193 "for benchmarking the allocator or instrumentator.");
195 ParseFlag(str, &f->poison_partial, "poison_partial",
196 "If true, poison partially addressable 8-byte aligned words "
197 "(default=true). This flag affects heap and global buffers, but not "
198 "stack buffers.");
200 ParseFlag(str, &f->alloc_dealloc_mismatch, "alloc_dealloc_mismatch",
201 "Report errors on malloc/delete, new/free, new/delete[], etc.");
202 ParseFlag(str, &f->strict_memcmp, "strict_memcmp",
203 "If true, assume that memcmp(p1, p2, n) always reads n bytes before "
204 "comparing p1 and p2.");
206 ParseFlag(str, &f->strict_init_order, "strict_init_order",
207 "If true, assume that dynamic initializers can never access globals from "
208 "other modules, even if the latter are already initialized.");
210 ParseFlag(str, &f->start_deactivated, "start_deactivated",
211 "If true, ASan tweaks a bunch of other flags (quarantine, redzone, heap "
212 "poisoning) to reduce memory consumption as much as possible, and "
213 "restores them to original values when the first instrumented module is "
214 "loaded into the process. This is mainly intended to be used on "
215 "Android. ");
217 ParseFlag(str, &f->detect_invalid_pointer_pairs,
218 "detect_invalid_pointer_pairs",
219 "If non-zero, try to detect operations like <, <=, >, >= and - on "
220 "invalid pointer pairs (e.g. when pointers belong to different objects). "
221 "The bigger the value the harder we try.");
224 void InitializeFlags(Flags *f, const char *env) {
225 CommonFlags *cf = common_flags();
226 SetCommonFlagsDefaults(cf);
227 cf->external_symbolizer_path = GetEnv("ASAN_SYMBOLIZER_PATH");
228 cf->malloc_context_size = kDefaultMallocContextSize;
230 internal_memset(f, 0, sizeof(*f));
231 f->quarantine_size = (ASAN_LOW_MEMORY) ? 1UL << 26 : 1UL << 28;
232 f->redzone = 16;
233 f->max_redzone = 2048;
234 f->debug = false;
235 f->report_globals = 1;
236 f->check_initialization_order = false;
237 f->replace_str = true;
238 f->replace_intrin = true;
239 f->mac_ignore_invalid_free = false;
240 f->detect_stack_use_after_return = false; // Also needs the compiler flag.
241 f->min_uar_stack_size_log = 16; // We can't do smaller anyway.
242 f->max_uar_stack_size_log = 20; // 1Mb per size class, i.e. ~11Mb per thread.
243 f->uar_noreserve = false;
244 f->max_malloc_fill_size = 0x1000; // By default, fill only the first 4K.
245 f->malloc_fill_byte = 0xbe;
246 f->exitcode = ASAN_DEFAULT_FAILURE_EXITCODE;
247 f->allow_user_poisoning = true;
248 f->sleep_before_dying = 0;
249 f->check_malloc_usable_size = true;
250 f->unmap_shadow_on_exit = false;
251 f->abort_on_error = false;
252 f->print_stats = false;
253 f->print_legend = true;
254 f->atexit = false;
255 f->coverage = false;
256 f->disable_core = (SANITIZER_WORDSIZE == 64);
257 f->allow_reexec = true;
258 f->print_full_thread_history = true;
259 f->poison_heap = true;
260 f->poison_partial = true;
261 // Turn off alloc/dealloc mismatch checker on Mac and Windows for now.
262 // TODO(glider,timurrrr): Fix known issues and enable this back.
263 f->alloc_dealloc_mismatch = (SANITIZER_MAC == 0) && (SANITIZER_WINDOWS == 0);
264 f->strict_memcmp = true;
265 f->strict_init_order = false;
266 f->start_deactivated = false;
267 f->detect_invalid_pointer_pairs = 0;
269 // Override from compile definition.
270 ParseFlagsFromString(f, MaybeUseAsanDefaultOptionsCompileDefiniton());
272 // Override from user-specified string.
273 ParseFlagsFromString(f, MaybeCallAsanDefaultOptions());
274 VReport(1, "Using the defaults from __asan_default_options: %s\n",
275 MaybeCallAsanDefaultOptions());
277 // Override from command line.
278 ParseFlagsFromString(f, env);
279 if (common_flags()->help) {
280 PrintFlagDescriptions();
283 #if !CAN_SANITIZE_LEAKS
284 if (cf->detect_leaks) {
285 Report("%s: detect_leaks is not supported on this platform.\n",
286 SanitizerToolName);
287 cf->detect_leaks = false;
289 #endif
291 // Make "strict_init_order" imply "check_initialization_order".
292 // TODO(samsonov): Use a single runtime flag for an init-order checker.
293 if (f->strict_init_order) {
294 f->check_initialization_order = true;
298 // Parse flags that may change between startup and activation.
299 // On Android they come from a system property.
300 // On other platforms this is no-op.
301 void ParseExtraActivationFlags() {
302 char buf[100];
303 GetExtraActivationFlags(buf, sizeof(buf));
304 ParseFlagsFromString(flags(), buf);
305 if (buf[0] != '\0')
306 VReport(1, "Extra activation flags: %s\n", buf);
309 // -------------------------- Globals --------------------- {{{1
310 int asan_inited;
311 bool asan_init_is_running;
312 void (*death_callback)(void);
314 #if !ASAN_FIXED_MAPPING
315 uptr kHighMemEnd, kMidMemBeg, kMidMemEnd;
316 #endif
318 // -------------------------- Misc ---------------- {{{1
319 void ShowStatsAndAbort() {
320 __asan_print_accumulated_stats();
321 Die();
324 // ---------------------- mmap -------------------- {{{1
325 // Reserve memory range [beg, end].
326 static void ReserveShadowMemoryRange(uptr beg, uptr end) {
327 CHECK_EQ((beg % GetPageSizeCached()), 0);
328 CHECK_EQ(((end + 1) % GetPageSizeCached()), 0);
329 uptr size = end - beg + 1;
330 void *res = MmapFixedNoReserve(beg, size);
331 if (res != (void*)beg) {
332 Report("ReserveShadowMemoryRange failed while trying to map 0x%zx bytes. "
333 "Perhaps you're using ulimit -v\n", size);
334 Abort();
338 // --------------- LowLevelAllocateCallbac ---------- {{{1
339 static void OnLowLevelAllocate(uptr ptr, uptr size) {
340 PoisonShadow(ptr, size, kAsanInternalHeapMagic);
343 // -------------------------- Run-time entry ------------------- {{{1
344 // exported functions
345 #define ASAN_REPORT_ERROR(type, is_write, size) \
346 extern "C" NOINLINE INTERFACE_ATTRIBUTE \
347 void __asan_report_ ## type ## size(uptr addr); \
348 void __asan_report_ ## type ## size(uptr addr) { \
349 GET_CALLER_PC_BP_SP; \
350 __asan_report_error(pc, bp, sp, addr, is_write, size); \
353 ASAN_REPORT_ERROR(load, false, 1)
354 ASAN_REPORT_ERROR(load, false, 2)
355 ASAN_REPORT_ERROR(load, false, 4)
356 ASAN_REPORT_ERROR(load, false, 8)
357 ASAN_REPORT_ERROR(load, false, 16)
358 ASAN_REPORT_ERROR(store, true, 1)
359 ASAN_REPORT_ERROR(store, true, 2)
360 ASAN_REPORT_ERROR(store, true, 4)
361 ASAN_REPORT_ERROR(store, true, 8)
362 ASAN_REPORT_ERROR(store, true, 16)
364 #define ASAN_REPORT_ERROR_N(type, is_write) \
365 extern "C" NOINLINE INTERFACE_ATTRIBUTE \
366 void __asan_report_ ## type ## _n(uptr addr, uptr size); \
367 void __asan_report_ ## type ## _n(uptr addr, uptr size) { \
368 GET_CALLER_PC_BP_SP; \
369 __asan_report_error(pc, bp, sp, addr, is_write, size); \
372 ASAN_REPORT_ERROR_N(load, false)
373 ASAN_REPORT_ERROR_N(store, true)
375 // Force the linker to keep the symbols for various ASan interface functions.
376 // We want to keep those in the executable in order to let the instrumented
377 // dynamic libraries access the symbol even if it is not used by the executable
378 // itself. This should help if the build system is removing dead code at link
379 // time.
380 static NOINLINE void force_interface_symbols() {
381 volatile int fake_condition = 0; // prevent dead condition elimination.
382 // __asan_report_* functions are noreturn, so we need a switch to prevent
383 // the compiler from removing any of them.
384 switch (fake_condition) {
385 case 1: __asan_report_load1(0); break;
386 case 2: __asan_report_load2(0); break;
387 case 3: __asan_report_load4(0); break;
388 case 4: __asan_report_load8(0); break;
389 case 5: __asan_report_load16(0); break;
390 case 6: __asan_report_store1(0); break;
391 case 7: __asan_report_store2(0); break;
392 case 8: __asan_report_store4(0); break;
393 case 9: __asan_report_store8(0); break;
394 case 10: __asan_report_store16(0); break;
395 case 12: __asan_register_globals(0, 0); break;
396 case 13: __asan_unregister_globals(0, 0); break;
397 case 14: __asan_set_death_callback(0); break;
398 case 15: __asan_set_error_report_callback(0); break;
399 case 16: __asan_handle_no_return(); break;
400 case 17: __asan_address_is_poisoned(0); break;
401 case 18: __asan_get_allocated_size(0); break;
402 case 19: __asan_get_current_allocated_bytes(); break;
403 case 20: __asan_get_estimated_allocated_size(0); break;
404 case 21: __asan_get_free_bytes(); break;
405 case 22: __asan_get_heap_size(); break;
406 case 23: __asan_get_ownership(0); break;
407 case 24: __asan_get_unmapped_bytes(); break;
408 case 25: __asan_poison_memory_region(0, 0); break;
409 case 26: __asan_unpoison_memory_region(0, 0); break;
410 case 27: __asan_set_error_exit_code(0); break;
411 case 30: __asan_before_dynamic_init(0); break;
412 case 31: __asan_after_dynamic_init(); break;
413 case 32: __asan_poison_stack_memory(0, 0); break;
414 case 33: __asan_unpoison_stack_memory(0, 0); break;
415 case 34: __asan_region_is_poisoned(0, 0); break;
416 case 35: __asan_describe_address(0); break;
420 static void asan_atexit() {
421 Printf("AddressSanitizer exit stats:\n");
422 __asan_print_accumulated_stats();
423 // Print AsanMappingProfile.
424 for (uptr i = 0; i < kAsanMappingProfileSize; i++) {
425 if (AsanMappingProfile[i] == 0) continue;
426 Printf("asan_mapping.h:%zd -- %zd\n", i, AsanMappingProfile[i]);
430 static void InitializeHighMemEnd() {
431 #if !ASAN_FIXED_MAPPING
432 kHighMemEnd = GetMaxVirtualAddress();
433 // Increase kHighMemEnd to make sure it's properly
434 // aligned together with kHighMemBeg:
435 kHighMemEnd |= SHADOW_GRANULARITY * GetPageSizeCached() - 1;
436 #endif // !ASAN_FIXED_MAPPING
437 CHECK_EQ((kHighMemBeg % GetPageSizeCached()), 0);
440 static void ProtectGap(uptr a, uptr size) {
441 CHECK_EQ(a, (uptr)Mprotect(a, size));
444 static void PrintAddressSpaceLayout() {
445 Printf("|| `[%p, %p]` || HighMem ||\n",
446 (void*)kHighMemBeg, (void*)kHighMemEnd);
447 Printf("|| `[%p, %p]` || HighShadow ||\n",
448 (void*)kHighShadowBeg, (void*)kHighShadowEnd);
449 if (kMidMemBeg) {
450 Printf("|| `[%p, %p]` || ShadowGap3 ||\n",
451 (void*)kShadowGap3Beg, (void*)kShadowGap3End);
452 Printf("|| `[%p, %p]` || MidMem ||\n",
453 (void*)kMidMemBeg, (void*)kMidMemEnd);
454 Printf("|| `[%p, %p]` || ShadowGap2 ||\n",
455 (void*)kShadowGap2Beg, (void*)kShadowGap2End);
456 Printf("|| `[%p, %p]` || MidShadow ||\n",
457 (void*)kMidShadowBeg, (void*)kMidShadowEnd);
459 Printf("|| `[%p, %p]` || ShadowGap ||\n",
460 (void*)kShadowGapBeg, (void*)kShadowGapEnd);
461 if (kLowShadowBeg) {
462 Printf("|| `[%p, %p]` || LowShadow ||\n",
463 (void*)kLowShadowBeg, (void*)kLowShadowEnd);
464 Printf("|| `[%p, %p]` || LowMem ||\n",
465 (void*)kLowMemBeg, (void*)kLowMemEnd);
467 Printf("MemToShadow(shadow): %p %p %p %p",
468 (void*)MEM_TO_SHADOW(kLowShadowBeg),
469 (void*)MEM_TO_SHADOW(kLowShadowEnd),
470 (void*)MEM_TO_SHADOW(kHighShadowBeg),
471 (void*)MEM_TO_SHADOW(kHighShadowEnd));
472 if (kMidMemBeg) {
473 Printf(" %p %p",
474 (void*)MEM_TO_SHADOW(kMidShadowBeg),
475 (void*)MEM_TO_SHADOW(kMidShadowEnd));
477 Printf("\n");
478 Printf("redzone=%zu\n", (uptr)flags()->redzone);
479 Printf("max_redzone=%zu\n", (uptr)flags()->max_redzone);
480 Printf("quarantine_size=%zuM\n", (uptr)flags()->quarantine_size >> 20);
481 Printf("malloc_context_size=%zu\n",
482 (uptr)common_flags()->malloc_context_size);
484 Printf("SHADOW_SCALE: %zx\n", (uptr)SHADOW_SCALE);
485 Printf("SHADOW_GRANULARITY: %zx\n", (uptr)SHADOW_GRANULARITY);
486 Printf("SHADOW_OFFSET: %zx\n", (uptr)SHADOW_OFFSET);
487 CHECK(SHADOW_SCALE >= 3 && SHADOW_SCALE <= 7);
488 if (kMidMemBeg)
489 CHECK(kMidShadowBeg > kLowShadowEnd &&
490 kMidMemBeg > kMidShadowEnd &&
491 kHighShadowBeg > kMidMemEnd);
494 static void AsanInitInternal() {
495 if (asan_inited) return;
496 SanitizerToolName = "AddressSanitizer";
497 CHECK(!asan_init_is_running && "ASan init calls itself!");
498 asan_init_is_running = true;
499 InitializeHighMemEnd();
501 // Make sure we are not statically linked.
502 AsanDoesNotSupportStaticLinkage();
504 // Install tool-specific callbacks in sanitizer_common.
505 SetDieCallback(AsanDie);
506 SetCheckFailedCallback(AsanCheckFailed);
507 SetPrintfAndReportCallback(AppendToErrorMessageBuffer);
509 // Initialize flags. This must be done early, because most of the
510 // initialization steps look at flags().
511 const char *options = GetEnv("ASAN_OPTIONS");
512 InitializeFlags(flags(), options);
514 if (!flags()->start_deactivated)
515 ParseExtraActivationFlags();
517 __sanitizer_set_report_path(common_flags()->log_path);
518 __asan_option_detect_stack_use_after_return =
519 flags()->detect_stack_use_after_return;
520 CHECK_LE(flags()->min_uar_stack_size_log, flags()->max_uar_stack_size_log);
522 if (options) {
523 VReport(1, "Parsed ASAN_OPTIONS: %s\n", options);
526 if (flags()->start_deactivated)
527 AsanStartDeactivated();
529 // Re-exec ourselves if we need to set additional env or command line args.
530 MaybeReexec();
532 // Setup internal allocator callback.
533 SetLowLevelAllocateCallback(OnLowLevelAllocate);
535 InitializeAsanInterceptors();
537 ReplaceSystemMalloc();
538 ReplaceOperatorsNewAndDelete();
540 uptr shadow_start = kLowShadowBeg;
541 if (kLowShadowBeg)
542 shadow_start -= GetMmapGranularity();
543 bool full_shadow_is_available =
544 MemoryRangeIsAvailable(shadow_start, kHighShadowEnd);
546 #if SANITIZER_LINUX && defined(__x86_64__) && !ASAN_FIXED_MAPPING
547 if (!full_shadow_is_available) {
548 kMidMemBeg = kLowMemEnd < 0x3000000000ULL ? 0x3000000000ULL : 0;
549 kMidMemEnd = kLowMemEnd < 0x3000000000ULL ? 0x4fffffffffULL : 0;
551 #endif
553 if (common_flags()->verbosity)
554 PrintAddressSpaceLayout();
556 if (flags()->disable_core) {
557 DisableCoreDumper();
560 if (full_shadow_is_available) {
561 // mmap the low shadow plus at least one page at the left.
562 if (kLowShadowBeg)
563 ReserveShadowMemoryRange(shadow_start, kLowShadowEnd);
564 // mmap the high shadow.
565 ReserveShadowMemoryRange(kHighShadowBeg, kHighShadowEnd);
566 // protect the gap.
567 ProtectGap(kShadowGapBeg, kShadowGapEnd - kShadowGapBeg + 1);
568 CHECK_EQ(kShadowGapEnd, kHighShadowBeg - 1);
569 } else if (kMidMemBeg &&
570 MemoryRangeIsAvailable(shadow_start, kMidMemBeg - 1) &&
571 MemoryRangeIsAvailable(kMidMemEnd + 1, kHighShadowEnd)) {
572 CHECK(kLowShadowBeg != kLowShadowEnd);
573 // mmap the low shadow plus at least one page at the left.
574 ReserveShadowMemoryRange(shadow_start, kLowShadowEnd);
575 // mmap the mid shadow.
576 ReserveShadowMemoryRange(kMidShadowBeg, kMidShadowEnd);
577 // mmap the high shadow.
578 ReserveShadowMemoryRange(kHighShadowBeg, kHighShadowEnd);
579 // protect the gaps.
580 ProtectGap(kShadowGapBeg, kShadowGapEnd - kShadowGapBeg + 1);
581 ProtectGap(kShadowGap2Beg, kShadowGap2End - kShadowGap2Beg + 1);
582 ProtectGap(kShadowGap3Beg, kShadowGap3End - kShadowGap3Beg + 1);
583 } else {
584 Report("Shadow memory range interleaves with an existing memory mapping. "
585 "ASan cannot proceed correctly. ABORTING.\n");
586 DumpProcessMap();
587 Die();
590 AsanTSDInit(PlatformTSDDtor);
591 InstallDeadlySignalHandlers(AsanOnSIGSEGV);
593 // Allocator should be initialized before starting external symbolizer, as
594 // fork() on Mac locks the allocator.
595 InitializeAllocator();
597 Symbolizer::Init(common_flags()->external_symbolizer_path);
599 // On Linux AsanThread::ThreadStart() calls malloc() that's why asan_inited
600 // should be set to 1 prior to initializing the threads.
601 asan_inited = 1;
602 asan_init_is_running = false;
604 if (flags()->atexit)
605 Atexit(asan_atexit);
607 if (flags()->coverage)
608 Atexit(__sanitizer_cov_dump);
610 // interceptors
611 InitTlsSize();
613 // Create main thread.
614 AsanThread *main_thread = AsanThread::Create(0, 0);
615 CreateThreadContextArgs create_main_args = { main_thread, 0 };
616 u32 main_tid = asanThreadRegistry().CreateThread(
617 0, true, 0, &create_main_args);
618 CHECK_EQ(0, main_tid);
619 SetCurrentThread(main_thread);
620 main_thread->ThreadStart(internal_getpid());
621 force_interface_symbols(); // no-op.
622 SanitizerInitializeUnwinder();
624 #if CAN_SANITIZE_LEAKS
625 __lsan::InitCommonLsan();
626 if (common_flags()->detect_leaks && common_flags()->leak_check_at_exit) {
627 Atexit(__lsan::DoLeakCheck);
629 #endif // CAN_SANITIZE_LEAKS
631 VReport(1, "AddressSanitizer Init done\n");
634 // Initialize as requested from some part of ASan runtime library (interceptors,
635 // allocator, etc).
636 void AsanInitFromRtl() {
637 AsanInitInternal();
640 } // namespace __asan
642 // ---------------------- Interface ---------------- {{{1
643 using namespace __asan; // NOLINT
645 #if !SANITIZER_SUPPORTS_WEAK_HOOKS
646 extern "C" {
647 SANITIZER_INTERFACE_ATTRIBUTE SANITIZER_WEAK_ATTRIBUTE
648 const char* __asan_default_options() { return ""; }
649 } // extern "C"
650 #endif
652 int NOINLINE __asan_set_error_exit_code(int exit_code) {
653 int old = flags()->exitcode;
654 flags()->exitcode = exit_code;
655 return old;
658 void NOINLINE __asan_handle_no_return() {
659 int local_stack;
660 AsanThread *curr_thread = GetCurrentThread();
661 CHECK(curr_thread);
662 uptr PageSize = GetPageSizeCached();
663 uptr top = curr_thread->stack_top();
664 uptr bottom = ((uptr)&local_stack - PageSize) & ~(PageSize-1);
665 static const uptr kMaxExpectedCleanupSize = 64 << 20; // 64M
666 if (top - bottom > kMaxExpectedCleanupSize) {
667 static bool reported_warning = false;
668 if (reported_warning)
669 return;
670 reported_warning = true;
671 Report("WARNING: ASan is ignoring requested __asan_handle_no_return: "
672 "stack top: %p; bottom %p; size: %p (%zd)\n"
673 "False positive error reports may follow\n"
674 "For details see "
675 "http://code.google.com/p/address-sanitizer/issues/detail?id=189\n",
676 top, bottom, top - bottom, top - bottom);
677 return;
679 PoisonShadow(bottom, top - bottom, 0);
680 if (curr_thread->has_fake_stack())
681 curr_thread->fake_stack()->HandleNoReturn();
684 void NOINLINE __asan_set_death_callback(void (*callback)(void)) {
685 death_callback = callback;
688 // Initialize as requested from instrumented application code.
689 // We use this call as a trigger to wake up ASan from deactivated state.
690 void __asan_init() {
691 AsanActivate();
692 AsanInitInternal();