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