asan_rtl.cpp 22 KB

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