asan_rtl.cpp 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621
  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_interface_internal.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. uptr __asan_shadow_memory_dynamic_address; // Global interface symbol.
  35. int __asan_option_detect_stack_use_after_return; // Global interface symbol.
  36. uptr *__asan_test_only_reported_buggy_pointer; // Used only for testing asan.
  37. namespace __asan {
  38. uptr AsanMappingProfile[kAsanMappingProfileSize];
  39. static void AsanDie() {
  40. static atomic_uint32_t num_calls;
  41. if (atomic_fetch_add(&num_calls, 1, memory_order_relaxed) != 0) {
  42. // Don't die twice - run a busy loop.
  43. while (1) {
  44. internal_sched_yield();
  45. }
  46. }
  47. if (common_flags()->print_module_map >= 1)
  48. DumpProcessMap();
  49. WaitForDebugger(flags()->sleep_before_dying, "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. 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. bool replace_intrin_cached;
  68. #if !ASAN_FIXED_MAPPING
  69. uptr kHighMemEnd, kMidMemBeg, kMidMemEnd;
  70. #endif
  71. // -------------------------- Misc ---------------- {{{1
  72. void ShowStatsAndAbort() {
  73. __asan_print_accumulated_stats();
  74. Die();
  75. }
  76. NOINLINE
  77. static void ReportGenericErrorWrapper(uptr addr, bool is_write, int size,
  78. int exp_arg, bool fatal) {
  79. GET_CALLER_PC_BP_SP;
  80. ReportGenericError(pc, bp, sp, addr, is_write, size, exp_arg, fatal);
  81. }
  82. // --------------- LowLevelAllocateCallbac ---------- {{{1
  83. static void OnLowLevelAllocate(uptr ptr, uptr size) {
  84. PoisonShadow(ptr, size, kAsanInternalHeapMagic);
  85. }
  86. // -------------------------- Run-time entry ------------------- {{{1
  87. // exported functions
  88. #define ASAN_REPORT_ERROR(type, is_write, size) \
  89. extern "C" NOINLINE INTERFACE_ATTRIBUTE \
  90. void __asan_report_ ## type ## size(uptr addr) { \
  91. GET_CALLER_PC_BP_SP; \
  92. ReportGenericError(pc, bp, sp, addr, is_write, size, 0, true); \
  93. } \
  94. extern "C" NOINLINE INTERFACE_ATTRIBUTE \
  95. void __asan_report_exp_ ## type ## size(uptr addr, u32 exp) { \
  96. GET_CALLER_PC_BP_SP; \
  97. ReportGenericError(pc, bp, sp, addr, is_write, size, exp, true); \
  98. } \
  99. extern "C" NOINLINE INTERFACE_ATTRIBUTE \
  100. void __asan_report_ ## type ## size ## _noabort(uptr addr) { \
  101. GET_CALLER_PC_BP_SP; \
  102. ReportGenericError(pc, bp, sp, addr, is_write, size, 0, false); \
  103. } \
  104. ASAN_REPORT_ERROR(load, false, 1)
  105. ASAN_REPORT_ERROR(load, false, 2)
  106. ASAN_REPORT_ERROR(load, false, 4)
  107. ASAN_REPORT_ERROR(load, false, 8)
  108. ASAN_REPORT_ERROR(load, false, 16)
  109. ASAN_REPORT_ERROR(store, true, 1)
  110. ASAN_REPORT_ERROR(store, true, 2)
  111. ASAN_REPORT_ERROR(store, true, 4)
  112. ASAN_REPORT_ERROR(store, true, 8)
  113. ASAN_REPORT_ERROR(store, true, 16)
  114. #define ASAN_REPORT_ERROR_N(type, is_write) \
  115. extern "C" NOINLINE INTERFACE_ATTRIBUTE \
  116. void __asan_report_ ## type ## _n(uptr addr, uptr size) { \
  117. GET_CALLER_PC_BP_SP; \
  118. ReportGenericError(pc, bp, sp, addr, is_write, size, 0, true); \
  119. } \
  120. extern "C" NOINLINE INTERFACE_ATTRIBUTE \
  121. void __asan_report_exp_ ## type ## _n(uptr addr, uptr size, u32 exp) { \
  122. GET_CALLER_PC_BP_SP; \
  123. ReportGenericError(pc, bp, sp, addr, is_write, size, exp, true); \
  124. } \
  125. extern "C" NOINLINE INTERFACE_ATTRIBUTE \
  126. void __asan_report_ ## type ## _n_noabort(uptr addr, uptr size) { \
  127. GET_CALLER_PC_BP_SP; \
  128. ReportGenericError(pc, bp, sp, addr, is_write, size, 0, false); \
  129. } \
  130. ASAN_REPORT_ERROR_N(load, false)
  131. ASAN_REPORT_ERROR_N(store, true)
  132. #define ASAN_MEMORY_ACCESS_CALLBACK_BODY(type, is_write, size, exp_arg, fatal) \
  133. uptr sp = MEM_TO_SHADOW(addr); \
  134. uptr s = size <= ASAN_SHADOW_GRANULARITY ? *reinterpret_cast<u8 *>(sp) \
  135. : *reinterpret_cast<u16 *>(sp); \
  136. if (UNLIKELY(s)) { \
  137. if (UNLIKELY(size >= ASAN_SHADOW_GRANULARITY || \
  138. ((s8)((addr & (ASAN_SHADOW_GRANULARITY - 1)) + size - 1)) >= \
  139. (s8)s)) { \
  140. ReportGenericErrorWrapper(addr, is_write, size, exp_arg, fatal); \
  141. } \
  142. }
  143. #define ASAN_MEMORY_ACCESS_CALLBACK(type, is_write, size) \
  144. extern "C" NOINLINE INTERFACE_ATTRIBUTE \
  145. void __asan_##type##size(uptr addr) { \
  146. ASAN_MEMORY_ACCESS_CALLBACK_BODY(type, is_write, size, 0, true) \
  147. } \
  148. extern "C" NOINLINE INTERFACE_ATTRIBUTE \
  149. void __asan_exp_##type##size(uptr addr, u32 exp) { \
  150. ASAN_MEMORY_ACCESS_CALLBACK_BODY(type, is_write, size, exp, true) \
  151. } \
  152. extern "C" NOINLINE INTERFACE_ATTRIBUTE \
  153. void __asan_##type##size ## _noabort(uptr addr) { \
  154. ASAN_MEMORY_ACCESS_CALLBACK_BODY(type, is_write, size, 0, false) \
  155. } \
  156. ASAN_MEMORY_ACCESS_CALLBACK(load, false, 1)
  157. ASAN_MEMORY_ACCESS_CALLBACK(load, false, 2)
  158. ASAN_MEMORY_ACCESS_CALLBACK(load, false, 4)
  159. ASAN_MEMORY_ACCESS_CALLBACK(load, false, 8)
  160. ASAN_MEMORY_ACCESS_CALLBACK(load, false, 16)
  161. ASAN_MEMORY_ACCESS_CALLBACK(store, true, 1)
  162. ASAN_MEMORY_ACCESS_CALLBACK(store, true, 2)
  163. ASAN_MEMORY_ACCESS_CALLBACK(store, true, 4)
  164. ASAN_MEMORY_ACCESS_CALLBACK(store, true, 8)
  165. ASAN_MEMORY_ACCESS_CALLBACK(store, true, 16)
  166. extern "C"
  167. NOINLINE INTERFACE_ATTRIBUTE
  168. void __asan_loadN(uptr addr, uptr size) {
  169. if ((addr = __asan_region_is_poisoned(addr, size))) {
  170. GET_CALLER_PC_BP_SP;
  171. ReportGenericError(pc, bp, sp, addr, false, size, 0, true);
  172. }
  173. }
  174. extern "C"
  175. NOINLINE INTERFACE_ATTRIBUTE
  176. void __asan_exp_loadN(uptr addr, uptr size, u32 exp) {
  177. if ((addr = __asan_region_is_poisoned(addr, size))) {
  178. GET_CALLER_PC_BP_SP;
  179. ReportGenericError(pc, bp, sp, addr, false, size, exp, true);
  180. }
  181. }
  182. extern "C"
  183. NOINLINE INTERFACE_ATTRIBUTE
  184. void __asan_loadN_noabort(uptr addr, uptr size) {
  185. if ((addr = __asan_region_is_poisoned(addr, size))) {
  186. GET_CALLER_PC_BP_SP;
  187. ReportGenericError(pc, bp, sp, addr, false, size, 0, false);
  188. }
  189. }
  190. extern "C"
  191. NOINLINE INTERFACE_ATTRIBUTE
  192. void __asan_storeN(uptr addr, uptr size) {
  193. if ((addr = __asan_region_is_poisoned(addr, size))) {
  194. GET_CALLER_PC_BP_SP;
  195. ReportGenericError(pc, bp, sp, addr, true, size, 0, true);
  196. }
  197. }
  198. extern "C"
  199. NOINLINE INTERFACE_ATTRIBUTE
  200. void __asan_exp_storeN(uptr addr, uptr size, u32 exp) {
  201. if ((addr = __asan_region_is_poisoned(addr, size))) {
  202. GET_CALLER_PC_BP_SP;
  203. ReportGenericError(pc, bp, sp, addr, true, size, exp, true);
  204. }
  205. }
  206. extern "C"
  207. NOINLINE INTERFACE_ATTRIBUTE
  208. void __asan_storeN_noabort(uptr addr, uptr size) {
  209. if ((addr = __asan_region_is_poisoned(addr, size))) {
  210. GET_CALLER_PC_BP_SP;
  211. ReportGenericError(pc, bp, sp, addr, true, size, 0, false);
  212. }
  213. }
  214. // Force the linker to keep the symbols for various ASan interface functions.
  215. // We want to keep those in the executable in order to let the instrumented
  216. // dynamic libraries access the symbol even if it is not used by the executable
  217. // itself. This should help if the build system is removing dead code at link
  218. // time.
  219. static NOINLINE void force_interface_symbols() {
  220. volatile int fake_condition = 0; // prevent dead condition elimination.
  221. // __asan_report_* functions are noreturn, so we need a switch to prevent
  222. // the compiler from removing any of them.
  223. // clang-format off
  224. switch (fake_condition) {
  225. case 1: __asan_report_load1(0); break;
  226. case 2: __asan_report_load2(0); break;
  227. case 3: __asan_report_load4(0); break;
  228. case 4: __asan_report_load8(0); break;
  229. case 5: __asan_report_load16(0); break;
  230. case 6: __asan_report_load_n(0, 0); break;
  231. case 7: __asan_report_store1(0); break;
  232. case 8: __asan_report_store2(0); break;
  233. case 9: __asan_report_store4(0); break;
  234. case 10: __asan_report_store8(0); break;
  235. case 11: __asan_report_store16(0); break;
  236. case 12: __asan_report_store_n(0, 0); break;
  237. case 13: __asan_report_exp_load1(0, 0); break;
  238. case 14: __asan_report_exp_load2(0, 0); break;
  239. case 15: __asan_report_exp_load4(0, 0); break;
  240. case 16: __asan_report_exp_load8(0, 0); break;
  241. case 17: __asan_report_exp_load16(0, 0); break;
  242. case 18: __asan_report_exp_load_n(0, 0, 0); break;
  243. case 19: __asan_report_exp_store1(0, 0); break;
  244. case 20: __asan_report_exp_store2(0, 0); break;
  245. case 21: __asan_report_exp_store4(0, 0); break;
  246. case 22: __asan_report_exp_store8(0, 0); break;
  247. case 23: __asan_report_exp_store16(0, 0); break;
  248. case 24: __asan_report_exp_store_n(0, 0, 0); break;
  249. case 25: __asan_register_globals(nullptr, 0); break;
  250. case 26: __asan_unregister_globals(nullptr, 0); break;
  251. case 27: __asan_set_death_callback(nullptr); break;
  252. case 28: __asan_set_error_report_callback(nullptr); break;
  253. case 29: __asan_handle_no_return(); break;
  254. case 30: __asan_address_is_poisoned(nullptr); break;
  255. case 31: __asan_poison_memory_region(nullptr, 0); break;
  256. case 32: __asan_unpoison_memory_region(nullptr, 0); break;
  257. case 34: __asan_before_dynamic_init(nullptr); break;
  258. case 35: __asan_after_dynamic_init(); break;
  259. case 36: __asan_poison_stack_memory(0, 0); break;
  260. case 37: __asan_unpoison_stack_memory(0, 0); break;
  261. case 38: __asan_region_is_poisoned(0, 0); break;
  262. case 39: __asan_describe_address(0); break;
  263. case 40: __asan_set_shadow_00(0, 0); break;
  264. case 41: __asan_set_shadow_01(0, 0); break;
  265. case 42: __asan_set_shadow_02(0, 0); break;
  266. case 43: __asan_set_shadow_03(0, 0); break;
  267. case 44: __asan_set_shadow_04(0, 0); break;
  268. case 45: __asan_set_shadow_05(0, 0); break;
  269. case 46: __asan_set_shadow_06(0, 0); break;
  270. case 47: __asan_set_shadow_07(0, 0); break;
  271. case 48: __asan_set_shadow_f1(0, 0); break;
  272. case 49: __asan_set_shadow_f2(0, 0); break;
  273. case 50: __asan_set_shadow_f3(0, 0); break;
  274. case 51: __asan_set_shadow_f5(0, 0); break;
  275. case 52: __asan_set_shadow_f8(0, 0); break;
  276. }
  277. // clang-format on
  278. }
  279. static void asan_atexit() {
  280. Printf("AddressSanitizer exit stats:\n");
  281. __asan_print_accumulated_stats();
  282. // Print AsanMappingProfile.
  283. for (uptr i = 0; i < kAsanMappingProfileSize; i++) {
  284. if (AsanMappingProfile[i] == 0) continue;
  285. Printf("asan_mapping.h:%zd -- %zd\n", i, AsanMappingProfile[i]);
  286. }
  287. }
  288. static void InitializeHighMemEnd() {
  289. #if !ASAN_FIXED_MAPPING
  290. kHighMemEnd = GetMaxUserVirtualAddress();
  291. // Increase kHighMemEnd to make sure it's properly
  292. // aligned together with kHighMemBeg:
  293. kHighMemEnd |= (GetMmapGranularity() << ASAN_SHADOW_SCALE) - 1;
  294. #endif // !ASAN_FIXED_MAPPING
  295. CHECK_EQ((kHighMemBeg % GetMmapGranularity()), 0);
  296. }
  297. void PrintAddressSpaceLayout() {
  298. if (kHighMemBeg) {
  299. Printf("|| `[%p, %p]` || HighMem ||\n",
  300. (void*)kHighMemBeg, (void*)kHighMemEnd);
  301. Printf("|| `[%p, %p]` || HighShadow ||\n",
  302. (void*)kHighShadowBeg, (void*)kHighShadowEnd);
  303. }
  304. if (kMidMemBeg) {
  305. Printf("|| `[%p, %p]` || ShadowGap3 ||\n",
  306. (void*)kShadowGap3Beg, (void*)kShadowGap3End);
  307. Printf("|| `[%p, %p]` || MidMem ||\n",
  308. (void*)kMidMemBeg, (void*)kMidMemEnd);
  309. Printf("|| `[%p, %p]` || ShadowGap2 ||\n",
  310. (void*)kShadowGap2Beg, (void*)kShadowGap2End);
  311. Printf("|| `[%p, %p]` || MidShadow ||\n",
  312. (void*)kMidShadowBeg, (void*)kMidShadowEnd);
  313. }
  314. Printf("|| `[%p, %p]` || ShadowGap ||\n",
  315. (void*)kShadowGapBeg, (void*)kShadowGapEnd);
  316. if (kLowShadowBeg) {
  317. Printf("|| `[%p, %p]` || LowShadow ||\n",
  318. (void*)kLowShadowBeg, (void*)kLowShadowEnd);
  319. Printf("|| `[%p, %p]` || LowMem ||\n",
  320. (void*)kLowMemBeg, (void*)kLowMemEnd);
  321. }
  322. Printf("MemToShadow(shadow): %p %p",
  323. (void*)MEM_TO_SHADOW(kLowShadowBeg),
  324. (void*)MEM_TO_SHADOW(kLowShadowEnd));
  325. if (kHighMemBeg) {
  326. Printf(" %p %p",
  327. (void*)MEM_TO_SHADOW(kHighShadowBeg),
  328. (void*)MEM_TO_SHADOW(kHighShadowEnd));
  329. }
  330. if (kMidMemBeg) {
  331. Printf(" %p %p",
  332. (void*)MEM_TO_SHADOW(kMidShadowBeg),
  333. (void*)MEM_TO_SHADOW(kMidShadowEnd));
  334. }
  335. Printf("\n");
  336. Printf("redzone=%zu\n", (uptr)flags()->redzone);
  337. Printf("max_redzone=%zu\n", (uptr)flags()->max_redzone);
  338. Printf("quarantine_size_mb=%zuM\n", (uptr)flags()->quarantine_size_mb);
  339. Printf("thread_local_quarantine_size_kb=%zuK\n",
  340. (uptr)flags()->thread_local_quarantine_size_kb);
  341. Printf("malloc_context_size=%zu\n",
  342. (uptr)common_flags()->malloc_context_size);
  343. Printf("SHADOW_SCALE: %d\n", (int)ASAN_SHADOW_SCALE);
  344. Printf("SHADOW_GRANULARITY: %d\n", (int)ASAN_SHADOW_GRANULARITY);
  345. Printf("SHADOW_OFFSET: 0x%zx\n", (uptr)ASAN_SHADOW_OFFSET);
  346. CHECK(ASAN_SHADOW_SCALE >= 3 && ASAN_SHADOW_SCALE <= 7);
  347. if (kMidMemBeg)
  348. CHECK(kMidShadowBeg > kLowShadowEnd &&
  349. kMidMemBeg > kMidShadowEnd &&
  350. kHighShadowBeg > kMidMemEnd);
  351. }
  352. static void AsanInitInternal() {
  353. if (LIKELY(asan_inited)) return;
  354. SanitizerToolName = "AddressSanitizer";
  355. CHECK(!asan_init_is_running && "ASan init calls itself!");
  356. asan_init_is_running = true;
  357. CacheBinaryName();
  358. // Initialize flags. This must be done early, because most of the
  359. // initialization steps look at flags().
  360. InitializeFlags();
  361. WaitForDebugger(flags()->sleep_before_init, "before init");
  362. // Stop performing init at this point if we are being loaded via
  363. // dlopen() and the platform supports it.
  364. if (SANITIZER_SUPPORTS_INIT_FOR_DLOPEN && UNLIKELY(HandleDlopenInit())) {
  365. asan_init_is_running = false;
  366. VReport(1, "AddressSanitizer init is being performed for dlopen().\n");
  367. return;
  368. }
  369. AsanCheckIncompatibleRT();
  370. AsanCheckDynamicRTPrereqs();
  371. AvoidCVE_2016_2143();
  372. SetCanPoisonMemory(flags()->poison_heap);
  373. SetMallocContextSize(common_flags()->malloc_context_size);
  374. InitializePlatformExceptionHandlers();
  375. InitializeHighMemEnd();
  376. // Make sure we are not statically linked.
  377. AsanDoesNotSupportStaticLinkage();
  378. // Install tool-specific callbacks in sanitizer_common.
  379. AddDieCallback(AsanDie);
  380. SetCheckUnwindCallback(CheckUnwind);
  381. SetPrintfAndReportCallback(AppendToErrorMessageBuffer);
  382. __sanitizer_set_report_path(common_flags()->log_path);
  383. __asan_option_detect_stack_use_after_return =
  384. flags()->detect_stack_use_after_return;
  385. __sanitizer::InitializePlatformEarly();
  386. // Setup internal allocator callback.
  387. SetLowLevelAllocateMinAlignment(ASAN_SHADOW_GRANULARITY);
  388. SetLowLevelAllocateCallback(OnLowLevelAllocate);
  389. InitializeAsanInterceptors();
  390. CheckASLR();
  391. // Enable system log ("adb logcat") on Android.
  392. // Doing this before interceptors are initialized crashes in:
  393. // AsanInitInternal -> android_log_write -> __interceptor_strcmp
  394. AndroidLogInit();
  395. ReplaceSystemMalloc();
  396. DisableCoreDumperIfNecessary();
  397. InitializeShadowMemory();
  398. AsanTSDInit(PlatformTSDDtor);
  399. InstallDeadlySignalHandlers(AsanOnDeadlySignal);
  400. AllocatorOptions allocator_options;
  401. allocator_options.SetFrom(flags(), common_flags());
  402. InitializeAllocator(allocator_options);
  403. if (SANITIZER_START_BACKGROUND_THREAD_IN_ASAN_INTERNAL)
  404. MaybeStartBackgroudThread();
  405. // On Linux AsanThread::ThreadStart() calls malloc() that's why asan_inited
  406. // should be set to 1 prior to initializing the threads.
  407. replace_intrin_cached = flags()->replace_intrin;
  408. asan_inited = 1;
  409. asan_init_is_running = false;
  410. if (flags()->atexit)
  411. Atexit(asan_atexit);
  412. InitializeCoverage(common_flags()->coverage, common_flags()->coverage_dir);
  413. // Now that ASan runtime is (mostly) initialized, deactivate it if
  414. // necessary, so that it can be re-activated when requested.
  415. if (flags()->start_deactivated)
  416. AsanDeactivate();
  417. // interceptors
  418. InitTlsSize();
  419. // Create main thread.
  420. AsanThread *main_thread = CreateMainThread();
  421. CHECK_EQ(0, main_thread->tid());
  422. force_interface_symbols(); // no-op.
  423. SanitizerInitializeUnwinder();
  424. if (CAN_SANITIZE_LEAKS) {
  425. __lsan::InitCommonLsan();
  426. InstallAtExitCheckLeaks();
  427. }
  428. #if CAN_SANITIZE_UB
  429. __ubsan::InitAsPlugin();
  430. #endif
  431. InitializeSuppressions();
  432. if (CAN_SANITIZE_LEAKS) {
  433. // LateInitialize() calls dlsym, which can allocate an error string buffer
  434. // in the TLS. Let's ignore the allocation to avoid reporting a leak.
  435. __lsan::ScopedInterceptorDisabler disabler;
  436. Symbolizer::LateInitialize();
  437. } else {
  438. Symbolizer::LateInitialize();
  439. }
  440. VReport(1, "AddressSanitizer Init done\n");
  441. WaitForDebugger(flags()->sleep_after_init, "after init");
  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. }