1 //===-- asan_rtl.cpp ------------------------------------------------------===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 // 9 // This file is a part of AddressSanitizer, an address sanity checker. 10 // 11 // Main file of the ASan run-time library. 12 //===----------------------------------------------------------------------===// 13 14 #include "asan_activation.h" 15 #include "asan_allocator.h" 16 #include "asan_fake_stack.h" 17 #include "asan_interceptors.h" 18 #include "asan_interface_internal.h" 19 #include "asan_internal.h" 20 #include "asan_mapping.h" 21 #include "asan_poisoning.h" 22 #include "asan_report.h" 23 #include "asan_stack.h" 24 #include "asan_stats.h" 25 #include "asan_suppressions.h" 26 #include "asan_thread.h" 27 #include "lsan/lsan_common.h" 28 #include "sanitizer_common/sanitizer_atomic.h" 29 #include "sanitizer_common/sanitizer_flags.h" 30 #include "sanitizer_common/sanitizer_libc.h" 31 #include "sanitizer_common/sanitizer_symbolizer.h" 32 #include "ubsan/ubsan_init.h" 33 #include "ubsan/ubsan_platform.h" 34 35 uptr __asan_shadow_memory_dynamic_address; // Global interface symbol. 36 int __asan_option_detect_stack_use_after_return; // Global interface symbol. 37 uptr *__asan_test_only_reported_buggy_pointer; // Used only for testing asan. 38 39 namespace __asan { 40 41 uptr AsanMappingProfile[kAsanMappingProfileSize]; 42 43 static void AsanDie() { 44 static atomic_uint32_t num_calls; 45 if (atomic_fetch_add(&num_calls, 1, memory_order_relaxed) != 0) { 46 // Don't die twice - run a busy loop. 47 while (1) { } 48 } 49 if (common_flags()->print_module_map >= 1) 50 DumpProcessMap(); 51 if (flags()->sleep_before_dying) { 52 Report("Sleeping for %d second(s)\n", flags()->sleep_before_dying); 53 SleepForSeconds(flags()->sleep_before_dying); 54 } 55 if (flags()->unmap_shadow_on_exit) { 56 if (kMidMemBeg) { 57 UnmapOrDie((void*)kLowShadowBeg, kMidMemBeg - kLowShadowBeg); 58 UnmapOrDie((void*)kMidMemEnd, kHighShadowEnd - kMidMemEnd); 59 } else { 60 if (kHighShadowEnd) 61 UnmapOrDie((void*)kLowShadowBeg, kHighShadowEnd - kLowShadowBeg); 62 } 63 } 64 } 65 66 static void CheckUnwind() { 67 GET_STACK_TRACE(kStackTraceMax, common_flags()->fast_unwind_on_check); 68 stack.Print(); 69 } 70 71 // -------------------------- Globals --------------------- {{{1 72 int asan_inited; 73 bool asan_init_is_running; 74 75 #if !ASAN_FIXED_MAPPING 76 uptr kHighMemEnd, kMidMemBeg, kMidMemEnd; 77 #endif 78 79 // -------------------------- Misc ---------------- {{{1 80 void ShowStatsAndAbort() { 81 __asan_print_accumulated_stats(); 82 Die(); 83 } 84 85 NOINLINE 86 static void ReportGenericErrorWrapper(uptr addr, bool is_write, int size, 87 int exp_arg, bool fatal) { 88 GET_CALLER_PC_BP_SP; 89 ReportGenericError(pc, bp, sp, addr, is_write, size, exp_arg, fatal); 90 } 91 92 // --------------- LowLevelAllocateCallbac ---------- {{{1 93 static void OnLowLevelAllocate(uptr ptr, uptr size) { 94 PoisonShadow(ptr, size, kAsanInternalHeapMagic); 95 } 96 97 // -------------------------- Run-time entry ------------------- {{{1 98 // exported functions 99 #define ASAN_REPORT_ERROR(type, is_write, size) \ 100 extern "C" NOINLINE INTERFACE_ATTRIBUTE \ 101 void __asan_report_ ## type ## size(uptr addr) { \ 102 GET_CALLER_PC_BP_SP; \ 103 ReportGenericError(pc, bp, sp, addr, is_write, size, 0, true); \ 104 } \ 105 extern "C" NOINLINE INTERFACE_ATTRIBUTE \ 106 void __asan_report_exp_ ## type ## size(uptr addr, u32 exp) { \ 107 GET_CALLER_PC_BP_SP; \ 108 ReportGenericError(pc, bp, sp, addr, is_write, size, exp, true); \ 109 } \ 110 extern "C" NOINLINE INTERFACE_ATTRIBUTE \ 111 void __asan_report_ ## type ## size ## _noabort(uptr addr) { \ 112 GET_CALLER_PC_BP_SP; \ 113 ReportGenericError(pc, bp, sp, addr, is_write, size, 0, false); \ 114 } \ 115 116 ASAN_REPORT_ERROR(load, false, 1) 117 ASAN_REPORT_ERROR(load, false, 2) 118 ASAN_REPORT_ERROR(load, false, 4) 119 ASAN_REPORT_ERROR(load, false, 8) 120 ASAN_REPORT_ERROR(load, false, 16) 121 ASAN_REPORT_ERROR(store, true, 1) 122 ASAN_REPORT_ERROR(store, true, 2) 123 ASAN_REPORT_ERROR(store, true, 4) 124 ASAN_REPORT_ERROR(store, true, 8) 125 ASAN_REPORT_ERROR(store, true, 16) 126 127 #define ASAN_REPORT_ERROR_N(type, is_write) \ 128 extern "C" NOINLINE INTERFACE_ATTRIBUTE \ 129 void __asan_report_ ## type ## _n(uptr addr, uptr size) { \ 130 GET_CALLER_PC_BP_SP; \ 131 ReportGenericError(pc, bp, sp, addr, is_write, size, 0, true); \ 132 } \ 133 extern "C" NOINLINE INTERFACE_ATTRIBUTE \ 134 void __asan_report_exp_ ## type ## _n(uptr addr, uptr size, u32 exp) { \ 135 GET_CALLER_PC_BP_SP; \ 136 ReportGenericError(pc, bp, sp, addr, is_write, size, exp, true); \ 137 } \ 138 extern "C" NOINLINE INTERFACE_ATTRIBUTE \ 139 void __asan_report_ ## type ## _n_noabort(uptr addr, uptr size) { \ 140 GET_CALLER_PC_BP_SP; \ 141 ReportGenericError(pc, bp, sp, addr, is_write, size, 0, false); \ 142 } \ 143 144 ASAN_REPORT_ERROR_N(load, false) 145 ASAN_REPORT_ERROR_N(store, true) 146 147 #define ASAN_MEMORY_ACCESS_CALLBACK_BODY(type, is_write, size, exp_arg, fatal) \ 148 uptr sp = MEM_TO_SHADOW(addr); \ 149 uptr s = size <= SHADOW_GRANULARITY ? *reinterpret_cast<u8 *>(sp) \ 150 : *reinterpret_cast<u16 *>(sp); \ 151 if (UNLIKELY(s)) { \ 152 if (UNLIKELY(size >= SHADOW_GRANULARITY || \ 153 ((s8)((addr & (SHADOW_GRANULARITY - 1)) + size - 1)) >= \ 154 (s8)s)) { \ 155 ReportGenericErrorWrapper(addr, is_write, size, exp_arg, fatal); \ 156 } \ 157 } 158 159 #define ASAN_MEMORY_ACCESS_CALLBACK(type, is_write, size) \ 160 extern "C" NOINLINE INTERFACE_ATTRIBUTE \ 161 void __asan_##type##size(uptr addr) { \ 162 ASAN_MEMORY_ACCESS_CALLBACK_BODY(type, is_write, size, 0, true) \ 163 } \ 164 extern "C" NOINLINE INTERFACE_ATTRIBUTE \ 165 void __asan_exp_##type##size(uptr addr, u32 exp) { \ 166 ASAN_MEMORY_ACCESS_CALLBACK_BODY(type, is_write, size, exp, true) \ 167 } \ 168 extern "C" NOINLINE INTERFACE_ATTRIBUTE \ 169 void __asan_##type##size ## _noabort(uptr addr) { \ 170 ASAN_MEMORY_ACCESS_CALLBACK_BODY(type, is_write, size, 0, false) \ 171 } \ 172 173 ASAN_MEMORY_ACCESS_CALLBACK(load, false, 1) 174 ASAN_MEMORY_ACCESS_CALLBACK(load, false, 2) 175 ASAN_MEMORY_ACCESS_CALLBACK(load, false, 4) 176 ASAN_MEMORY_ACCESS_CALLBACK(load, false, 8) 177 ASAN_MEMORY_ACCESS_CALLBACK(load, false, 16) 178 ASAN_MEMORY_ACCESS_CALLBACK(store, true, 1) 179 ASAN_MEMORY_ACCESS_CALLBACK(store, true, 2) 180 ASAN_MEMORY_ACCESS_CALLBACK(store, true, 4) 181 ASAN_MEMORY_ACCESS_CALLBACK(store, true, 8) 182 ASAN_MEMORY_ACCESS_CALLBACK(store, true, 16) 183 184 extern "C" 185 NOINLINE INTERFACE_ATTRIBUTE 186 void __asan_loadN(uptr addr, uptr size) { 187 if (__asan_region_is_poisoned(addr, size)) { 188 GET_CALLER_PC_BP_SP; 189 ReportGenericError(pc, bp, sp, addr, false, size, 0, true); 190 } 191 } 192 193 extern "C" 194 NOINLINE INTERFACE_ATTRIBUTE 195 void __asan_exp_loadN(uptr addr, uptr size, u32 exp) { 196 if (__asan_region_is_poisoned(addr, size)) { 197 GET_CALLER_PC_BP_SP; 198 ReportGenericError(pc, bp, sp, addr, false, size, exp, true); 199 } 200 } 201 202 extern "C" 203 NOINLINE INTERFACE_ATTRIBUTE 204 void __asan_loadN_noabort(uptr addr, uptr size) { 205 if (__asan_region_is_poisoned(addr, size)) { 206 GET_CALLER_PC_BP_SP; 207 ReportGenericError(pc, bp, sp, addr, false, size, 0, false); 208 } 209 } 210 211 extern "C" 212 NOINLINE INTERFACE_ATTRIBUTE 213 void __asan_storeN(uptr addr, uptr size) { 214 if (__asan_region_is_poisoned(addr, size)) { 215 GET_CALLER_PC_BP_SP; 216 ReportGenericError(pc, bp, sp, addr, true, size, 0, true); 217 } 218 } 219 220 extern "C" 221 NOINLINE INTERFACE_ATTRIBUTE 222 void __asan_exp_storeN(uptr addr, uptr size, u32 exp) { 223 if (__asan_region_is_poisoned(addr, size)) { 224 GET_CALLER_PC_BP_SP; 225 ReportGenericError(pc, bp, sp, addr, true, size, exp, true); 226 } 227 } 228 229 extern "C" 230 NOINLINE INTERFACE_ATTRIBUTE 231 void __asan_storeN_noabort(uptr addr, uptr size) { 232 if (__asan_region_is_poisoned(addr, size)) { 233 GET_CALLER_PC_BP_SP; 234 ReportGenericError(pc, bp, sp, addr, true, size, 0, false); 235 } 236 } 237 238 // Force the linker to keep the symbols for various ASan interface functions. 239 // We want to keep those in the executable in order to let the instrumented 240 // dynamic libraries access the symbol even if it is not used by the executable 241 // itself. This should help if the build system is removing dead code at link 242 // time. 243 static NOINLINE void force_interface_symbols() { 244 volatile int fake_condition = 0; // prevent dead condition elimination. 245 // __asan_report_* functions are noreturn, so we need a switch to prevent 246 // the compiler from removing any of them. 247 // clang-format off 248 switch (fake_condition) { 249 case 1: __asan_report_load1(0); break; 250 case 2: __asan_report_load2(0); break; 251 case 3: __asan_report_load4(0); break; 252 case 4: __asan_report_load8(0); break; 253 case 5: __asan_report_load16(0); break; 254 case 6: __asan_report_load_n(0, 0); break; 255 case 7: __asan_report_store1(0); break; 256 case 8: __asan_report_store2(0); break; 257 case 9: __asan_report_store4(0); break; 258 case 10: __asan_report_store8(0); break; 259 case 11: __asan_report_store16(0); break; 260 case 12: __asan_report_store_n(0, 0); break; 261 case 13: __asan_report_exp_load1(0, 0); break; 262 case 14: __asan_report_exp_load2(0, 0); break; 263 case 15: __asan_report_exp_load4(0, 0); break; 264 case 16: __asan_report_exp_load8(0, 0); break; 265 case 17: __asan_report_exp_load16(0, 0); break; 266 case 18: __asan_report_exp_load_n(0, 0, 0); break; 267 case 19: __asan_report_exp_store1(0, 0); break; 268 case 20: __asan_report_exp_store2(0, 0); break; 269 case 21: __asan_report_exp_store4(0, 0); break; 270 case 22: __asan_report_exp_store8(0, 0); break; 271 case 23: __asan_report_exp_store16(0, 0); break; 272 case 24: __asan_report_exp_store_n(0, 0, 0); break; 273 case 25: __asan_register_globals(nullptr, 0); break; 274 case 26: __asan_unregister_globals(nullptr, 0); break; 275 case 27: __asan_set_death_callback(nullptr); break; 276 case 28: __asan_set_error_report_callback(nullptr); break; 277 case 29: __asan_handle_no_return(); break; 278 case 30: __asan_address_is_poisoned(nullptr); break; 279 case 31: __asan_poison_memory_region(nullptr, 0); break; 280 case 32: __asan_unpoison_memory_region(nullptr, 0); break; 281 case 34: __asan_before_dynamic_init(nullptr); break; 282 case 35: __asan_after_dynamic_init(); break; 283 case 36: __asan_poison_stack_memory(0, 0); break; 284 case 37: __asan_unpoison_stack_memory(0, 0); break; 285 case 38: __asan_region_is_poisoned(0, 0); break; 286 case 39: __asan_describe_address(0); break; 287 case 40: __asan_set_shadow_00(0, 0); break; 288 case 41: __asan_set_shadow_f1(0, 0); break; 289 case 42: __asan_set_shadow_f2(0, 0); break; 290 case 43: __asan_set_shadow_f3(0, 0); break; 291 case 44: __asan_set_shadow_f5(0, 0); break; 292 case 45: __asan_set_shadow_f8(0, 0); break; 293 } 294 // clang-format on 295 } 296 297 static void asan_atexit() { 298 Printf("AddressSanitizer exit stats:\n"); 299 __asan_print_accumulated_stats(); 300 // Print AsanMappingProfile. 301 for (uptr i = 0; i < kAsanMappingProfileSize; i++) { 302 if (AsanMappingProfile[i] == 0) continue; 303 Printf("asan_mapping.h:%zd -- %zd\n", i, AsanMappingProfile[i]); 304 } 305 } 306 307 static void InitializeHighMemEnd() { 308 #if !ASAN_FIXED_MAPPING 309 kHighMemEnd = GetMaxUserVirtualAddress(); 310 // Increase kHighMemEnd to make sure it's properly 311 // aligned together with kHighMemBeg: 312 kHighMemEnd |= (GetMmapGranularity() << SHADOW_SCALE) - 1; 313 #endif // !ASAN_FIXED_MAPPING 314 CHECK_EQ((kHighMemBeg % GetMmapGranularity()), 0); 315 } 316 317 void PrintAddressSpaceLayout() { 318 if (kHighMemBeg) { 319 Printf("|| `[%p, %p]` || HighMem ||\n", 320 (void*)kHighMemBeg, (void*)kHighMemEnd); 321 Printf("|| `[%p, %p]` || HighShadow ||\n", 322 (void*)kHighShadowBeg, (void*)kHighShadowEnd); 323 } 324 if (kMidMemBeg) { 325 Printf("|| `[%p, %p]` || ShadowGap3 ||\n", 326 (void*)kShadowGap3Beg, (void*)kShadowGap3End); 327 Printf("|| `[%p, %p]` || MidMem ||\n", 328 (void*)kMidMemBeg, (void*)kMidMemEnd); 329 Printf("|| `[%p, %p]` || ShadowGap2 ||\n", 330 (void*)kShadowGap2Beg, (void*)kShadowGap2End); 331 Printf("|| `[%p, %p]` || MidShadow ||\n", 332 (void*)kMidShadowBeg, (void*)kMidShadowEnd); 333 } 334 Printf("|| `[%p, %p]` || ShadowGap ||\n", 335 (void*)kShadowGapBeg, (void*)kShadowGapEnd); 336 if (kLowShadowBeg) { 337 Printf("|| `[%p, %p]` || LowShadow ||\n", 338 (void*)kLowShadowBeg, (void*)kLowShadowEnd); 339 Printf("|| `[%p, %p]` || LowMem ||\n", 340 (void*)kLowMemBeg, (void*)kLowMemEnd); 341 } 342 Printf("MemToShadow(shadow): %p %p", 343 (void*)MEM_TO_SHADOW(kLowShadowBeg), 344 (void*)MEM_TO_SHADOW(kLowShadowEnd)); 345 if (kHighMemBeg) { 346 Printf(" %p %p", 347 (void*)MEM_TO_SHADOW(kHighShadowBeg), 348 (void*)MEM_TO_SHADOW(kHighShadowEnd)); 349 } 350 if (kMidMemBeg) { 351 Printf(" %p %p", 352 (void*)MEM_TO_SHADOW(kMidShadowBeg), 353 (void*)MEM_TO_SHADOW(kMidShadowEnd)); 354 } 355 Printf("\n"); 356 Printf("redzone=%zu\n", (uptr)flags()->redzone); 357 Printf("max_redzone=%zu\n", (uptr)flags()->max_redzone); 358 Printf("quarantine_size_mb=%zuM\n", (uptr)flags()->quarantine_size_mb); 359 Printf("thread_local_quarantine_size_kb=%zuK\n", 360 (uptr)flags()->thread_local_quarantine_size_kb); 361 Printf("malloc_context_size=%zu\n", 362 (uptr)common_flags()->malloc_context_size); 363 364 Printf("SHADOW_SCALE: %d\n", (int)SHADOW_SCALE); 365 Printf("SHADOW_GRANULARITY: %d\n", (int)SHADOW_GRANULARITY); 366 Printf("SHADOW_OFFSET: 0x%zx\n", (uptr)SHADOW_OFFSET); 367 CHECK(SHADOW_SCALE >= 3 && SHADOW_SCALE <= 7); 368 if (kMidMemBeg) 369 CHECK(kMidShadowBeg > kLowShadowEnd && 370 kMidMemBeg > kMidShadowEnd && 371 kHighShadowBeg > kMidMemEnd); 372 } 373 374 #if defined(__thumb__) && defined(__linux__) 375 #define START_BACKGROUND_THREAD_IN_ASAN_INTERNAL 376 #endif 377 378 #ifndef START_BACKGROUND_THREAD_IN_ASAN_INTERNAL 379 static bool UNUSED __local_asan_dyninit = [] { 380 MaybeStartBackgroudThread(); 381 SetSoftRssLimitExceededCallback(AsanSoftRssLimitExceededCallback); 382 383 return false; 384 }(); 385 #endif 386 387 static void AsanInitInternal() { 388 if (LIKELY(asan_inited)) return; 389 SanitizerToolName = "AddressSanitizer"; 390 CHECK(!asan_init_is_running && "ASan init calls itself!"); 391 asan_init_is_running = true; 392 393 CacheBinaryName(); 394 395 // Initialize flags. This must be done early, because most of the 396 // initialization steps look at flags(). 397 InitializeFlags(); 398 399 // Stop performing init at this point if we are being loaded via 400 // dlopen() and the platform supports it. 401 if (SANITIZER_SUPPORTS_INIT_FOR_DLOPEN && UNLIKELY(HandleDlopenInit())) { 402 asan_init_is_running = false; 403 VReport(1, "AddressSanitizer init is being performed for dlopen().\n"); 404 return; 405 } 406 407 AsanCheckIncompatibleRT(); 408 AsanCheckDynamicRTPrereqs(); 409 AvoidCVE_2016_2143(); 410 411 SetCanPoisonMemory(flags()->poison_heap); 412 SetMallocContextSize(common_flags()->malloc_context_size); 413 414 InitializePlatformExceptionHandlers(); 415 416 InitializeHighMemEnd(); 417 418 // Make sure we are not statically linked. 419 AsanDoesNotSupportStaticLinkage(); 420 421 // Install tool-specific callbacks in sanitizer_common. 422 AddDieCallback(AsanDie); 423 SetCheckUnwindCallback(CheckUnwind); 424 SetPrintfAndReportCallback(AppendToErrorMessageBuffer); 425 426 __sanitizer_set_report_path(common_flags()->log_path); 427 428 __asan_option_detect_stack_use_after_return = 429 flags()->detect_stack_use_after_return; 430 431 __sanitizer::InitializePlatformEarly(); 432 433 // Re-exec ourselves if we need to set additional env or command line args. 434 MaybeReexec(); 435 436 // Setup internal allocator callback. 437 SetLowLevelAllocateMinAlignment(SHADOW_GRANULARITY); 438 SetLowLevelAllocateCallback(OnLowLevelAllocate); 439 440 InitializeAsanInterceptors(); 441 CheckASLR(); 442 443 // Enable system log ("adb logcat") on Android. 444 // Doing this before interceptors are initialized crashes in: 445 // AsanInitInternal -> android_log_write -> __interceptor_strcmp 446 AndroidLogInit(); 447 448 ReplaceSystemMalloc(); 449 450 DisableCoreDumperIfNecessary(); 451 452 InitializeShadowMemory(); 453 454 AsanTSDInit(PlatformTSDDtor); 455 InstallDeadlySignalHandlers(AsanOnDeadlySignal); 456 457 AllocatorOptions allocator_options; 458 allocator_options.SetFrom(flags(), common_flags()); 459 InitializeAllocator(allocator_options); 460 461 #ifdef START_BACKGROUND_THREAD_IN_ASAN_INTERNAL 462 MaybeStartBackgroudThread(); 463 SetSoftRssLimitExceededCallback(AsanSoftRssLimitExceededCallback); 464 #endif 465 466 // On Linux AsanThread::ThreadStart() calls malloc() that's why asan_inited 467 // should be set to 1 prior to initializing the threads. 468 asan_inited = 1; 469 asan_init_is_running = false; 470 471 if (flags()->atexit) 472 Atexit(asan_atexit); 473 474 InitializeCoverage(common_flags()->coverage, common_flags()->coverage_dir); 475 476 // Now that ASan runtime is (mostly) initialized, deactivate it if 477 // necessary, so that it can be re-activated when requested. 478 if (flags()->start_deactivated) 479 AsanDeactivate(); 480 481 // interceptors 482 InitTlsSize(); 483 484 // Create main thread. 485 AsanThread *main_thread = CreateMainThread(); 486 CHECK_EQ(0, main_thread->tid()); 487 force_interface_symbols(); // no-op. 488 SanitizerInitializeUnwinder(); 489 490 if (CAN_SANITIZE_LEAKS) { 491 __lsan::InitCommonLsan(); 492 if (common_flags()->detect_leaks && common_flags()->leak_check_at_exit) { 493 if (flags()->halt_on_error) 494 Atexit(__lsan::DoLeakCheck); 495 else 496 Atexit(__lsan::DoRecoverableLeakCheckVoid); 497 } 498 } 499 500 #if CAN_SANITIZE_UB 501 __ubsan::InitAsPlugin(); 502 #endif 503 504 InitializeSuppressions(); 505 506 if (CAN_SANITIZE_LEAKS) { 507 // LateInitialize() calls dlsym, which can allocate an error string buffer 508 // in the TLS. Let's ignore the allocation to avoid reporting a leak. 509 __lsan::ScopedInterceptorDisabler disabler; 510 Symbolizer::LateInitialize(); 511 } else { 512 Symbolizer::LateInitialize(); 513 } 514 515 VReport(1, "AddressSanitizer Init done\n"); 516 517 if (flags()->sleep_after_init) { 518 Report("Sleeping for %d second(s)\n", flags()->sleep_after_init); 519 SleepForSeconds(flags()->sleep_after_init); 520 } 521 } 522 523 // Initialize as requested from some part of ASan runtime library (interceptors, 524 // allocator, etc). 525 void AsanInitFromRtl() { 526 AsanInitInternal(); 527 } 528 529 #if ASAN_DYNAMIC 530 // Initialize runtime in case it's LD_PRELOAD-ed into unsanitized executable 531 // (and thus normal initializers from .preinit_array or modules haven't run). 532 533 class AsanInitializer { 534 public: 535 AsanInitializer() { 536 AsanInitFromRtl(); 537 } 538 }; 539 540 static AsanInitializer asan_initializer; 541 #endif // ASAN_DYNAMIC 542 543 void UnpoisonStack(uptr bottom, uptr top, const char *type) { 544 static const uptr kMaxExpectedCleanupSize = 64 << 20; // 64M 545 if (top - bottom > kMaxExpectedCleanupSize) { 546 static bool reported_warning = false; 547 if (reported_warning) 548 return; 549 reported_warning = true; 550 Report( 551 "WARNING: ASan is ignoring requested __asan_handle_no_return: " 552 "stack type: %s top: %p; bottom %p; size: %p (%zd)\n" 553 "False positive error reports may follow\n" 554 "For details see " 555 "https://github.com/google/sanitizers/issues/189\n", 556 type, (void *)top, (void *)bottom, (void *)(top - bottom), 557 top - bottom); 558 return; 559 } 560 PoisonShadow(bottom, RoundUpTo(top - bottom, SHADOW_GRANULARITY), 0); 561 } 562 563 static void UnpoisonDefaultStack() { 564 uptr bottom, top; 565 566 if (AsanThread *curr_thread = GetCurrentThread()) { 567 int local_stack; 568 const uptr page_size = GetPageSizeCached(); 569 top = curr_thread->stack_top(); 570 bottom = ((uptr)&local_stack - page_size) & ~(page_size - 1); 571 } else { 572 CHECK(!SANITIZER_FUCHSIA); 573 // If we haven't seen this thread, try asking the OS for stack bounds. 574 uptr tls_addr, tls_size, stack_size; 575 GetThreadStackAndTls(/*main=*/false, &bottom, &stack_size, &tls_addr, 576 &tls_size); 577 top = bottom + stack_size; 578 } 579 580 UnpoisonStack(bottom, top, "default"); 581 } 582 583 static void UnpoisonFakeStack() { 584 AsanThread *curr_thread = GetCurrentThread(); 585 if (!curr_thread) 586 return; 587 FakeStack *stack = curr_thread->get_fake_stack(); 588 if (!stack) 589 return; 590 stack->HandleNoReturn(); 591 } 592 593 } // namespace __asan 594 595 // ---------------------- Interface ---------------- {{{1 596 using namespace __asan; 597 598 void NOINLINE __asan_handle_no_return() { 599 if (asan_init_is_running) 600 return; 601 602 if (!PlatformUnpoisonStacks()) 603 UnpoisonDefaultStack(); 604 605 UnpoisonFakeStack(); 606 } 607 608 extern "C" void *__asan_extra_spill_area() { 609 AsanThread *t = GetCurrentThread(); 610 CHECK(t); 611 return t->extra_spill_area(); 612 } 613 614 void __asan_handle_vfork(void *sp) { 615 AsanThread *t = GetCurrentThread(); 616 CHECK(t); 617 uptr bottom = t->stack_bottom(); 618 PoisonShadow(bottom, (uptr)sp - bottom, 0); 619 } 620 621 void NOINLINE __asan_set_death_callback(void (*callback)(void)) { 622 SetUserDieCallback(callback); 623 } 624 625 // Initialize as requested from instrumented application code. 626 // We use this call as a trigger to wake up ASan from deactivated state. 627 void __asan_init() { 628 AsanActivate(); 629 AsanInitInternal(); 630 } 631 632 void __asan_version_mismatch_check() { 633 // Do nothing. 634 } 635