2016-09-25 François Dumont <fdumont@gcc.gnu.org>
[official-gcc.git] / libsanitizer / asan / asan_rtl.cc
blob50b02453a2b0d72af5144c4263126e68ac896150
1 //===-- asan_rtl.cc -------------------------------------------------------===//
2 //
3 // This file is distributed under the University of Illinois Open Source
4 // License. See LICENSE.TXT for details.
5 //
6 //===----------------------------------------------------------------------===//
7 //
8 // This file is a part of AddressSanitizer, an address sanity checker.
9 //
10 // Main file of the ASan run-time library.
11 //===----------------------------------------------------------------------===//
13 #include "asan_activation.h"
14 #include "asan_allocator.h"
15 #include "asan_interceptors.h"
16 #include "asan_interface_internal.h"
17 #include "asan_internal.h"
18 #include "asan_mapping.h"
19 #include "asan_poisoning.h"
20 #include "asan_report.h"
21 #include "asan_stack.h"
22 #include "asan_stats.h"
23 #include "asan_suppressions.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"
30 #include "ubsan/ubsan_init.h"
31 #include "ubsan/ubsan_platform.h"
33 int __asan_option_detect_stack_use_after_return; // Global interface symbol.
34 uptr *__asan_test_only_reported_buggy_pointer; // Used only for testing asan.
36 namespace __asan {
38 uptr AsanMappingProfile[kAsanMappingProfileSize];
40 static void AsanDie() {
41 static atomic_uint32_t num_calls;
42 if (atomic_fetch_add(&num_calls, 1, memory_order_relaxed) != 0) {
43 // Don't die twice - run a busy loop.
44 while (1) { }
46 if (flags()->sleep_before_dying) {
47 Report("Sleeping for %d second(s)\n", flags()->sleep_before_dying);
48 SleepForSeconds(flags()->sleep_before_dying);
50 if (flags()->unmap_shadow_on_exit) {
51 if (kMidMemBeg) {
52 UnmapOrDie((void*)kLowShadowBeg, kMidMemBeg - kLowShadowBeg);
53 UnmapOrDie((void*)kMidMemEnd, kHighShadowEnd - kMidMemEnd);
54 } else {
55 UnmapOrDie((void*)kLowShadowBeg, kHighShadowEnd - kLowShadowBeg);
60 static void AsanCheckFailed(const char *file, int line, const char *cond,
61 u64 v1, u64 v2) {
62 Report("AddressSanitizer CHECK failed: %s:%d \"%s\" (0x%zx, 0x%zx)\n", file,
63 line, cond, (uptr)v1, (uptr)v2);
64 // FIXME: check for infinite recursion without a thread-local counter here.
65 PRINT_CURRENT_STACK_CHECK();
66 Die();
69 // -------------------------- Globals --------------------- {{{1
70 int asan_inited;
71 bool asan_init_is_running;
73 #if !ASAN_FIXED_MAPPING
74 uptr kHighMemEnd, kMidMemBeg, kMidMemEnd;
75 #endif
77 // -------------------------- Misc ---------------- {{{1
78 void ShowStatsAndAbort() {
79 __asan_print_accumulated_stats();
80 Die();
83 // ---------------------- mmap -------------------- {{{1
84 // Reserve memory range [beg, end].
85 // We need to use inclusive range because end+1 may not be representable.
86 void ReserveShadowMemoryRange(uptr beg, uptr end, const char *name) {
87 CHECK_EQ((beg % GetPageSizeCached()), 0);
88 CHECK_EQ(((end + 1) % GetPageSizeCached()), 0);
89 uptr size = end - beg + 1;
90 DecreaseTotalMmap(size); // Don't count the shadow against mmap_limit_mb.
91 void *res = MmapFixedNoReserve(beg, size, name);
92 if (res != (void*)beg) {
93 Report("ReserveShadowMemoryRange failed while trying to map 0x%zx bytes. "
94 "Perhaps you're using ulimit -v\n", size);
95 Abort();
97 if (common_flags()->no_huge_pages_for_shadow)
98 NoHugePagesInRegion(beg, size);
99 if (common_flags()->use_madv_dontdump)
100 DontDumpShadowMemory(beg, size);
103 // --------------- LowLevelAllocateCallbac ---------- {{{1
104 static void OnLowLevelAllocate(uptr ptr, uptr size) {
105 PoisonShadow(ptr, size, kAsanInternalHeapMagic);
108 // -------------------------- Run-time entry ------------------- {{{1
109 // exported functions
110 #define ASAN_REPORT_ERROR(type, is_write, size) \
111 extern "C" NOINLINE INTERFACE_ATTRIBUTE \
112 void __asan_report_ ## type ## size(uptr addr) { \
113 GET_CALLER_PC_BP_SP; \
114 ReportGenericError(pc, bp, sp, addr, is_write, size, 0, true); \
116 extern "C" NOINLINE INTERFACE_ATTRIBUTE \
117 void __asan_report_exp_ ## type ## size(uptr addr, u32 exp) { \
118 GET_CALLER_PC_BP_SP; \
119 ReportGenericError(pc, bp, sp, addr, is_write, size, exp, true); \
121 extern "C" NOINLINE INTERFACE_ATTRIBUTE \
122 void __asan_report_ ## type ## size ## _noabort(uptr addr) { \
123 GET_CALLER_PC_BP_SP; \
124 ReportGenericError(pc, bp, sp, addr, is_write, size, 0, false); \
127 ASAN_REPORT_ERROR(load, false, 1)
128 ASAN_REPORT_ERROR(load, false, 2)
129 ASAN_REPORT_ERROR(load, false, 4)
130 ASAN_REPORT_ERROR(load, false, 8)
131 ASAN_REPORT_ERROR(load, false, 16)
132 ASAN_REPORT_ERROR(store, true, 1)
133 ASAN_REPORT_ERROR(store, true, 2)
134 ASAN_REPORT_ERROR(store, true, 4)
135 ASAN_REPORT_ERROR(store, true, 8)
136 ASAN_REPORT_ERROR(store, true, 16)
138 #define ASAN_REPORT_ERROR_N(type, is_write) \
139 extern "C" NOINLINE INTERFACE_ATTRIBUTE \
140 void __asan_report_ ## type ## _n(uptr addr, uptr size) { \
141 GET_CALLER_PC_BP_SP; \
142 ReportGenericError(pc, bp, sp, addr, is_write, size, 0, true); \
144 extern "C" NOINLINE INTERFACE_ATTRIBUTE \
145 void __asan_report_exp_ ## type ## _n(uptr addr, uptr size, u32 exp) { \
146 GET_CALLER_PC_BP_SP; \
147 ReportGenericError(pc, bp, sp, addr, is_write, size, exp, true); \
149 extern "C" NOINLINE INTERFACE_ATTRIBUTE \
150 void __asan_report_ ## type ## _n_noabort(uptr addr, uptr size) { \
151 GET_CALLER_PC_BP_SP; \
152 ReportGenericError(pc, bp, sp, addr, is_write, size, 0, false); \
155 ASAN_REPORT_ERROR_N(load, false)
156 ASAN_REPORT_ERROR_N(store, true)
158 #define ASAN_MEMORY_ACCESS_CALLBACK_BODY(type, is_write, size, exp_arg, fatal) \
159 uptr sp = MEM_TO_SHADOW(addr); \
160 uptr s = size <= SHADOW_GRANULARITY ? *reinterpret_cast<u8 *>(sp) \
161 : *reinterpret_cast<u16 *>(sp); \
162 if (UNLIKELY(s)) { \
163 if (UNLIKELY(size >= SHADOW_GRANULARITY || \
164 ((s8)((addr & (SHADOW_GRANULARITY - 1)) + size - 1)) >= \
165 (s8)s)) { \
166 if (__asan_test_only_reported_buggy_pointer) { \
167 *__asan_test_only_reported_buggy_pointer = addr; \
168 } else { \
169 GET_CALLER_PC_BP_SP; \
170 ReportGenericError(pc, bp, sp, addr, is_write, size, exp_arg, \
171 fatal); \
176 #define ASAN_MEMORY_ACCESS_CALLBACK(type, is_write, size) \
177 extern "C" NOINLINE INTERFACE_ATTRIBUTE \
178 void __asan_##type##size(uptr addr) { \
179 ASAN_MEMORY_ACCESS_CALLBACK_BODY(type, is_write, size, 0, true) \
181 extern "C" NOINLINE INTERFACE_ATTRIBUTE \
182 void __asan_exp_##type##size(uptr addr, u32 exp) { \
183 ASAN_MEMORY_ACCESS_CALLBACK_BODY(type, is_write, size, exp, true) \
185 extern "C" NOINLINE INTERFACE_ATTRIBUTE \
186 void __asan_##type##size ## _noabort(uptr addr) { \
187 ASAN_MEMORY_ACCESS_CALLBACK_BODY(type, is_write, size, 0, false) \
190 ASAN_MEMORY_ACCESS_CALLBACK(load, false, 1)
191 ASAN_MEMORY_ACCESS_CALLBACK(load, false, 2)
192 ASAN_MEMORY_ACCESS_CALLBACK(load, false, 4)
193 ASAN_MEMORY_ACCESS_CALLBACK(load, false, 8)
194 ASAN_MEMORY_ACCESS_CALLBACK(load, false, 16)
195 ASAN_MEMORY_ACCESS_CALLBACK(store, true, 1)
196 ASAN_MEMORY_ACCESS_CALLBACK(store, true, 2)
197 ASAN_MEMORY_ACCESS_CALLBACK(store, true, 4)
198 ASAN_MEMORY_ACCESS_CALLBACK(store, true, 8)
199 ASAN_MEMORY_ACCESS_CALLBACK(store, true, 16)
201 extern "C"
202 NOINLINE INTERFACE_ATTRIBUTE
203 void __asan_loadN(uptr addr, uptr size) {
204 if (__asan_region_is_poisoned(addr, size)) {
205 GET_CALLER_PC_BP_SP;
206 ReportGenericError(pc, bp, sp, addr, false, size, 0, true);
210 extern "C"
211 NOINLINE INTERFACE_ATTRIBUTE
212 void __asan_exp_loadN(uptr addr, uptr size, u32 exp) {
213 if (__asan_region_is_poisoned(addr, size)) {
214 GET_CALLER_PC_BP_SP;
215 ReportGenericError(pc, bp, sp, addr, false, size, exp, true);
219 extern "C"
220 NOINLINE INTERFACE_ATTRIBUTE
221 void __asan_loadN_noabort(uptr addr, uptr size) {
222 if (__asan_region_is_poisoned(addr, size)) {
223 GET_CALLER_PC_BP_SP;
224 ReportGenericError(pc, bp, sp, addr, false, size, 0, false);
228 extern "C"
229 NOINLINE INTERFACE_ATTRIBUTE
230 void __asan_storeN(uptr addr, uptr size) {
231 if (__asan_region_is_poisoned(addr, size)) {
232 GET_CALLER_PC_BP_SP;
233 ReportGenericError(pc, bp, sp, addr, true, size, 0, true);
237 extern "C"
238 NOINLINE INTERFACE_ATTRIBUTE
239 void __asan_exp_storeN(uptr addr, uptr size, u32 exp) {
240 if (__asan_region_is_poisoned(addr, size)) {
241 GET_CALLER_PC_BP_SP;
242 ReportGenericError(pc, bp, sp, addr, true, size, exp, true);
246 extern "C"
247 NOINLINE INTERFACE_ATTRIBUTE
248 void __asan_storeN_noabort(uptr addr, uptr size) {
249 if (__asan_region_is_poisoned(addr, size)) {
250 GET_CALLER_PC_BP_SP;
251 ReportGenericError(pc, bp, sp, addr, true, size, 0, false);
255 // Force the linker to keep the symbols for various ASan interface functions.
256 // We want to keep those in the executable in order to let the instrumented
257 // dynamic libraries access the symbol even if it is not used by the executable
258 // itself. This should help if the build system is removing dead code at link
259 // time.
260 static NOINLINE void force_interface_symbols() {
261 volatile int fake_condition = 0; // prevent dead condition elimination.
262 // __asan_report_* functions are noreturn, so we need a switch to prevent
263 // the compiler from removing any of them.
264 switch (fake_condition) {
265 case 1: __asan_report_load1(0); break;
266 case 2: __asan_report_load2(0); break;
267 case 3: __asan_report_load4(0); break;
268 case 4: __asan_report_load8(0); break;
269 case 5: __asan_report_load16(0); break;
270 case 6: __asan_report_load_n(0, 0); break;
271 case 7: __asan_report_store1(0); break;
272 case 8: __asan_report_store2(0); break;
273 case 9: __asan_report_store4(0); break;
274 case 10: __asan_report_store8(0); break;
275 case 11: __asan_report_store16(0); break;
276 case 12: __asan_report_store_n(0, 0); break;
277 case 13: __asan_report_exp_load1(0, 0); break;
278 case 14: __asan_report_exp_load2(0, 0); break;
279 case 15: __asan_report_exp_load4(0, 0); break;
280 case 16: __asan_report_exp_load8(0, 0); break;
281 case 17: __asan_report_exp_load16(0, 0); break;
282 case 18: __asan_report_exp_load_n(0, 0, 0); break;
283 case 19: __asan_report_exp_store1(0, 0); break;
284 case 20: __asan_report_exp_store2(0, 0); break;
285 case 21: __asan_report_exp_store4(0, 0); break;
286 case 22: __asan_report_exp_store8(0, 0); break;
287 case 23: __asan_report_exp_store16(0, 0); break;
288 case 24: __asan_report_exp_store_n(0, 0, 0); break;
289 case 25: __asan_register_globals(nullptr, 0); break;
290 case 26: __asan_unregister_globals(nullptr, 0); break;
291 case 27: __asan_set_death_callback(nullptr); break;
292 case 28: __asan_set_error_report_callback(nullptr); break;
293 case 29: __asan_handle_no_return(); break;
294 case 30: __asan_address_is_poisoned(nullptr); break;
295 case 31: __asan_poison_memory_region(nullptr, 0); break;
296 case 32: __asan_unpoison_memory_region(nullptr, 0); break;
297 case 34: __asan_before_dynamic_init(nullptr); break;
298 case 35: __asan_after_dynamic_init(); break;
299 case 36: __asan_poison_stack_memory(0, 0); break;
300 case 37: __asan_unpoison_stack_memory(0, 0); break;
301 case 38: __asan_region_is_poisoned(0, 0); break;
302 case 39: __asan_describe_address(0); break;
306 static void asan_atexit() {
307 Printf("AddressSanitizer exit stats:\n");
308 __asan_print_accumulated_stats();
309 // Print AsanMappingProfile.
310 for (uptr i = 0; i < kAsanMappingProfileSize; i++) {
311 if (AsanMappingProfile[i] == 0) continue;
312 Printf("asan_mapping.h:%zd -- %zd\n", i, AsanMappingProfile[i]);
316 static void InitializeHighMemEnd() {
317 #if !ASAN_FIXED_MAPPING
318 kHighMemEnd = GetMaxVirtualAddress();
319 // Increase kHighMemEnd to make sure it's properly
320 // aligned together with kHighMemBeg:
321 kHighMemEnd |= SHADOW_GRANULARITY * GetPageSizeCached() - 1;
322 #endif // !ASAN_FIXED_MAPPING
323 CHECK_EQ((kHighMemBeg % GetPageSizeCached()), 0);
326 static void ProtectGap(uptr addr, uptr size) {
327 if (!flags()->protect_shadow_gap)
328 return;
329 void *res = MmapNoAccess(addr, size, "shadow gap");
330 if (addr == (uptr)res)
331 return;
332 // A few pages at the start of the address space can not be protected.
333 // But we really want to protect as much as possible, to prevent this memory
334 // being returned as a result of a non-FIXED mmap().
335 if (addr == kZeroBaseShadowStart) {
336 uptr step = GetPageSizeCached();
337 while (size > step && addr < kZeroBaseMaxShadowStart) {
338 addr += step;
339 size -= step;
340 void *res = MmapNoAccess(addr, size, "shadow gap");
341 if (addr == (uptr)res)
342 return;
346 Report("ERROR: Failed to protect the shadow gap. "
347 "ASan cannot proceed correctly. ABORTING.\n");
348 DumpProcessMap();
349 Die();
352 static void PrintAddressSpaceLayout() {
353 Printf("|| `[%p, %p]` || HighMem ||\n",
354 (void*)kHighMemBeg, (void*)kHighMemEnd);
355 Printf("|| `[%p, %p]` || HighShadow ||\n",
356 (void*)kHighShadowBeg, (void*)kHighShadowEnd);
357 if (kMidMemBeg) {
358 Printf("|| `[%p, %p]` || ShadowGap3 ||\n",
359 (void*)kShadowGap3Beg, (void*)kShadowGap3End);
360 Printf("|| `[%p, %p]` || MidMem ||\n",
361 (void*)kMidMemBeg, (void*)kMidMemEnd);
362 Printf("|| `[%p, %p]` || ShadowGap2 ||\n",
363 (void*)kShadowGap2Beg, (void*)kShadowGap2End);
364 Printf("|| `[%p, %p]` || MidShadow ||\n",
365 (void*)kMidShadowBeg, (void*)kMidShadowEnd);
367 Printf("|| `[%p, %p]` || ShadowGap ||\n",
368 (void*)kShadowGapBeg, (void*)kShadowGapEnd);
369 if (kLowShadowBeg) {
370 Printf("|| `[%p, %p]` || LowShadow ||\n",
371 (void*)kLowShadowBeg, (void*)kLowShadowEnd);
372 Printf("|| `[%p, %p]` || LowMem ||\n",
373 (void*)kLowMemBeg, (void*)kLowMemEnd);
375 Printf("MemToShadow(shadow): %p %p %p %p",
376 (void*)MEM_TO_SHADOW(kLowShadowBeg),
377 (void*)MEM_TO_SHADOW(kLowShadowEnd),
378 (void*)MEM_TO_SHADOW(kHighShadowBeg),
379 (void*)MEM_TO_SHADOW(kHighShadowEnd));
380 if (kMidMemBeg) {
381 Printf(" %p %p",
382 (void*)MEM_TO_SHADOW(kMidShadowBeg),
383 (void*)MEM_TO_SHADOW(kMidShadowEnd));
385 Printf("\n");
386 Printf("redzone=%zu\n", (uptr)flags()->redzone);
387 Printf("max_redzone=%zu\n", (uptr)flags()->max_redzone);
388 Printf("quarantine_size_mb=%zuM\n", (uptr)flags()->quarantine_size_mb);
389 Printf("malloc_context_size=%zu\n",
390 (uptr)common_flags()->malloc_context_size);
392 Printf("SHADOW_SCALE: %d\n", (int)SHADOW_SCALE);
393 Printf("SHADOW_GRANULARITY: %d\n", (int)SHADOW_GRANULARITY);
394 Printf("SHADOW_OFFSET: 0x%zx\n", (uptr)SHADOW_OFFSET);
395 CHECK(SHADOW_SCALE >= 3 && SHADOW_SCALE <= 7);
396 if (kMidMemBeg)
397 CHECK(kMidShadowBeg > kLowShadowEnd &&
398 kMidMemBeg > kMidShadowEnd &&
399 kHighShadowBeg > kMidMemEnd);
402 static void AsanInitInternal() {
403 if (LIKELY(asan_inited)) return;
404 SanitizerToolName = "AddressSanitizer";
405 CHECK(!asan_init_is_running && "ASan init calls itself!");
406 asan_init_is_running = true;
408 CacheBinaryName();
410 // Initialize flags. This must be done early, because most of the
411 // initialization steps look at flags().
412 InitializeFlags();
414 AsanCheckIncompatibleRT();
415 AsanCheckDynamicRTPrereqs();
417 SetCanPoisonMemory(flags()->poison_heap);
418 SetMallocContextSize(common_flags()->malloc_context_size);
420 InitializeHighMemEnd();
422 // Make sure we are not statically linked.
423 AsanDoesNotSupportStaticLinkage();
425 // Install tool-specific callbacks in sanitizer_common.
426 AddDieCallback(AsanDie);
427 SetCheckFailedCallback(AsanCheckFailed);
428 SetPrintfAndReportCallback(AppendToErrorMessageBuffer);
430 __sanitizer_set_report_path(common_flags()->log_path);
432 // Enable UAR detection, if required.
433 __asan_option_detect_stack_use_after_return =
434 flags()->detect_stack_use_after_return;
436 // Re-exec ourselves if we need to set additional env or command line args.
437 MaybeReexec();
439 // Setup internal allocator callback.
440 SetLowLevelAllocateCallback(OnLowLevelAllocate);
442 InitializeAsanInterceptors();
444 // Enable system log ("adb logcat") on Android.
445 // Doing this before interceptors are initialized crashes in:
446 // AsanInitInternal -> android_log_write -> __interceptor_strcmp
447 AndroidLogInit();
449 ReplaceSystemMalloc();
451 uptr shadow_start = kLowShadowBeg;
452 if (kLowShadowBeg)
453 shadow_start -= GetMmapGranularity();
454 bool full_shadow_is_available =
455 MemoryRangeIsAvailable(shadow_start, kHighShadowEnd);
457 #if SANITIZER_LINUX && defined(__x86_64__) && defined(_LP64) && \
458 !ASAN_FIXED_MAPPING
459 if (!full_shadow_is_available) {
460 kMidMemBeg = kLowMemEnd < 0x3000000000ULL ? 0x3000000000ULL : 0;
461 kMidMemEnd = kLowMemEnd < 0x3000000000ULL ? 0x4fffffffffULL : 0;
463 #endif
465 if (Verbosity()) PrintAddressSpaceLayout();
467 DisableCoreDumperIfNecessary();
469 if (full_shadow_is_available) {
470 // mmap the low shadow plus at least one page at the left.
471 if (kLowShadowBeg)
472 ReserveShadowMemoryRange(shadow_start, kLowShadowEnd, "low shadow");
473 // mmap the high shadow.
474 ReserveShadowMemoryRange(kHighShadowBeg, kHighShadowEnd, "high shadow");
475 // protect the gap.
476 ProtectGap(kShadowGapBeg, kShadowGapEnd - kShadowGapBeg + 1);
477 CHECK_EQ(kShadowGapEnd, kHighShadowBeg - 1);
478 } else if (kMidMemBeg &&
479 MemoryRangeIsAvailable(shadow_start, kMidMemBeg - 1) &&
480 MemoryRangeIsAvailable(kMidMemEnd + 1, kHighShadowEnd)) {
481 CHECK(kLowShadowBeg != kLowShadowEnd);
482 // mmap the low shadow plus at least one page at the left.
483 ReserveShadowMemoryRange(shadow_start, kLowShadowEnd, "low shadow");
484 // mmap the mid shadow.
485 ReserveShadowMemoryRange(kMidShadowBeg, kMidShadowEnd, "mid shadow");
486 // mmap the high shadow.
487 ReserveShadowMemoryRange(kHighShadowBeg, kHighShadowEnd, "high shadow");
488 // protect the gaps.
489 ProtectGap(kShadowGapBeg, kShadowGapEnd - kShadowGapBeg + 1);
490 ProtectGap(kShadowGap2Beg, kShadowGap2End - kShadowGap2Beg + 1);
491 ProtectGap(kShadowGap3Beg, kShadowGap3End - kShadowGap3Beg + 1);
492 } else {
493 Report("Shadow memory range interleaves with an existing memory mapping. "
494 "ASan cannot proceed correctly. ABORTING.\n");
495 Report("ASan shadow was supposed to be located in the [%p-%p] range.\n",
496 shadow_start, kHighShadowEnd);
497 DumpProcessMap();
498 Die();
501 AsanTSDInit(PlatformTSDDtor);
502 InstallDeadlySignalHandlers(AsanOnDeadlySignal);
504 AllocatorOptions allocator_options;
505 allocator_options.SetFrom(flags(), common_flags());
506 InitializeAllocator(allocator_options);
508 MaybeStartBackgroudThread();
509 SetSoftRssLimitExceededCallback(AsanSoftRssLimitExceededCallback);
511 // On Linux AsanThread::ThreadStart() calls malloc() that's why asan_inited
512 // should be set to 1 prior to initializing the threads.
513 asan_inited = 1;
514 asan_init_is_running = false;
516 if (flags()->atexit)
517 Atexit(asan_atexit);
519 InitializeCoverage(common_flags()->coverage, common_flags()->coverage_dir);
521 // Now that ASan runtime is (mostly) initialized, deactivate it if
522 // necessary, so that it can be re-activated when requested.
523 if (flags()->start_deactivated)
524 AsanDeactivate();
526 // interceptors
527 InitTlsSize();
529 // Create main thread.
530 AsanThread *main_thread = AsanThread::Create(
531 /* start_routine */ nullptr, /* arg */ nullptr, /* parent_tid */ 0,
532 /* stack */ nullptr, /* detached */ true);
533 CHECK_EQ(0, main_thread->tid());
534 SetCurrentThread(main_thread);
535 main_thread->ThreadStart(internal_getpid(),
536 /* signal_thread_is_registered */ nullptr);
537 force_interface_symbols(); // no-op.
538 SanitizerInitializeUnwinder();
540 #if CAN_SANITIZE_LEAKS
541 __lsan::InitCommonLsan();
542 if (common_flags()->detect_leaks && common_flags()->leak_check_at_exit) {
543 Atexit(__lsan::DoLeakCheck);
545 #endif // CAN_SANITIZE_LEAKS
547 #if CAN_SANITIZE_UB
548 __ubsan::InitAsPlugin();
549 #endif
551 InitializeSuppressions();
553 VReport(1, "AddressSanitizer Init done\n");
556 // Initialize as requested from some part of ASan runtime library (interceptors,
557 // allocator, etc).
558 void AsanInitFromRtl() {
559 AsanInitInternal();
562 #if ASAN_DYNAMIC
563 // Initialize runtime in case it's LD_PRELOAD-ed into unsanitized executable
564 // (and thus normal initializers from .preinit_array or modules haven't run).
566 class AsanInitializer {
567 public: // NOLINT
568 AsanInitializer() {
569 AsanInitFromRtl();
573 static AsanInitializer asan_initializer;
574 #endif // ASAN_DYNAMIC
576 } // namespace __asan
578 // ---------------------- Interface ---------------- {{{1
579 using namespace __asan; // NOLINT
581 void NOINLINE __asan_handle_no_return() {
582 int local_stack;
583 AsanThread *curr_thread = GetCurrentThread();
584 uptr PageSize = GetPageSizeCached();
585 uptr top, bottom;
586 if (curr_thread) {
587 top = curr_thread->stack_top();
588 bottom = ((uptr)&local_stack - PageSize) & ~(PageSize - 1);
589 } else {
590 // If we haven't seen this thread, try asking the OS for stack bounds.
591 uptr tls_addr, tls_size, stack_size;
592 GetThreadStackAndTls(/*main=*/false, &bottom, &stack_size, &tls_addr,
593 &tls_size);
594 top = bottom + stack_size;
596 static const uptr kMaxExpectedCleanupSize = 64 << 20; // 64M
597 if (top - bottom > kMaxExpectedCleanupSize) {
598 static bool reported_warning = false;
599 if (reported_warning)
600 return;
601 reported_warning = true;
602 Report("WARNING: ASan is ignoring requested __asan_handle_no_return: "
603 "stack top: %p; bottom %p; size: %p (%zd)\n"
604 "False positive error reports may follow\n"
605 "For details see "
606 "http://code.google.com/p/address-sanitizer/issues/detail?id=189\n",
607 top, bottom, top - bottom, top - bottom);
608 return;
610 PoisonShadow(bottom, top - bottom, 0);
611 if (curr_thread && curr_thread->has_fake_stack())
612 curr_thread->fake_stack()->HandleNoReturn();
615 void NOINLINE __asan_set_death_callback(void (*callback)(void)) {
616 SetUserDieCallback(callback);
619 // Initialize as requested from instrumented application code.
620 // We use this call as a trigger to wake up ASan from deactivated state.
621 void __asan_init() {
622 AsanActivate();
623 AsanInitInternal();
626 void __asan_version_mismatch_check() {
627 // Do nothing.