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