hwasan.cpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599
  1. //===-- hwasan.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 HWAddressSanitizer.
  10. //
  11. // HWAddressSanitizer runtime.
  12. //===----------------------------------------------------------------------===//
  13. #include "hwasan.h"
  14. #include "hwasan_checks.h"
  15. #include "hwasan_dynamic_shadow.h"
  16. #include "hwasan_globals.h"
  17. #include "hwasan_mapping.h"
  18. #include "hwasan_poisoning.h"
  19. #include "hwasan_report.h"
  20. #include "hwasan_thread.h"
  21. #include "hwasan_thread_list.h"
  22. #include "sanitizer_common/sanitizer_atomic.h"
  23. #include "sanitizer_common/sanitizer_common.h"
  24. #include "sanitizer_common/sanitizer_flag_parser.h"
  25. #include "sanitizer_common/sanitizer_flags.h"
  26. #include "sanitizer_common/sanitizer_libc.h"
  27. #include "sanitizer_common/sanitizer_procmaps.h"
  28. #include "sanitizer_common/sanitizer_stackdepot.h"
  29. #include "sanitizer_common/sanitizer_stacktrace.h"
  30. #include "sanitizer_common/sanitizer_symbolizer.h"
  31. #include "ubsan/ubsan_flags.h"
  32. #include "ubsan/ubsan_init.h"
  33. // ACHTUNG! No system header includes in this file.
  34. using namespace __sanitizer;
  35. namespace __hwasan {
  36. static Flags hwasan_flags;
  37. Flags *flags() {
  38. return &hwasan_flags;
  39. }
  40. int hwasan_inited = 0;
  41. int hwasan_instrumentation_inited = 0;
  42. bool hwasan_init_is_running;
  43. int hwasan_report_count = 0;
  44. uptr kLowShadowStart;
  45. uptr kLowShadowEnd;
  46. uptr kHighShadowStart;
  47. uptr kHighShadowEnd;
  48. void Flags::SetDefaults() {
  49. #define HWASAN_FLAG(Type, Name, DefaultValue, Description) Name = DefaultValue;
  50. #include "hwasan_flags.inc"
  51. #undef HWASAN_FLAG
  52. }
  53. static void RegisterHwasanFlags(FlagParser *parser, Flags *f) {
  54. #define HWASAN_FLAG(Type, Name, DefaultValue, Description) \
  55. RegisterFlag(parser, #Name, Description, &f->Name);
  56. #include "hwasan_flags.inc"
  57. #undef HWASAN_FLAG
  58. }
  59. static void InitializeFlags() {
  60. SetCommonFlagsDefaults();
  61. {
  62. CommonFlags cf;
  63. cf.CopyFrom(*common_flags());
  64. cf.external_symbolizer_path = GetEnv("HWASAN_SYMBOLIZER_PATH");
  65. cf.malloc_context_size = 20;
  66. cf.handle_ioctl = true;
  67. // FIXME: test and enable.
  68. cf.check_printf = false;
  69. cf.intercept_tls_get_addr = true;
  70. cf.exitcode = 99;
  71. // 8 shadow pages ~512kB, small enough to cover common stack sizes.
  72. cf.clear_shadow_mmap_threshold = 4096 * (SANITIZER_ANDROID ? 2 : 8);
  73. // Sigtrap is used in error reporting.
  74. cf.handle_sigtrap = kHandleSignalExclusive;
  75. #if SANITIZER_ANDROID
  76. // Let platform handle other signals. It is better at reporting them then we
  77. // are.
  78. cf.handle_segv = kHandleSignalNo;
  79. cf.handle_sigbus = kHandleSignalNo;
  80. cf.handle_abort = kHandleSignalNo;
  81. cf.handle_sigill = kHandleSignalNo;
  82. cf.handle_sigfpe = kHandleSignalNo;
  83. #endif
  84. OverrideCommonFlags(cf);
  85. }
  86. Flags *f = flags();
  87. f->SetDefaults();
  88. FlagParser parser;
  89. RegisterHwasanFlags(&parser, f);
  90. RegisterCommonFlags(&parser);
  91. #if HWASAN_CONTAINS_UBSAN
  92. __ubsan::Flags *uf = __ubsan::flags();
  93. uf->SetDefaults();
  94. FlagParser ubsan_parser;
  95. __ubsan::RegisterUbsanFlags(&ubsan_parser, uf);
  96. RegisterCommonFlags(&ubsan_parser);
  97. #endif
  98. // Override from user-specified string.
  99. if (__hwasan_default_options)
  100. parser.ParseString(__hwasan_default_options());
  101. #if HWASAN_CONTAINS_UBSAN
  102. const char *ubsan_default_options = __ubsan_default_options();
  103. ubsan_parser.ParseString(ubsan_default_options);
  104. #endif
  105. parser.ParseStringFromEnv("HWASAN_OPTIONS");
  106. #if HWASAN_CONTAINS_UBSAN
  107. ubsan_parser.ParseStringFromEnv("UBSAN_OPTIONS");
  108. #endif
  109. InitializeCommonFlags();
  110. if (Verbosity()) ReportUnrecognizedFlags();
  111. if (common_flags()->help) parser.PrintFlagDescriptions();
  112. }
  113. static void CheckUnwind() {
  114. GET_FATAL_STACK_TRACE_PC_BP(StackTrace::GetCurrentPc(), GET_CURRENT_FRAME());
  115. stack.Print();
  116. }
  117. static void HwasanFormatMemoryUsage(InternalScopedString &s) {
  118. HwasanThreadList &thread_list = hwasanThreadList();
  119. auto thread_stats = thread_list.GetThreadStats();
  120. auto sds = StackDepotGetStats();
  121. AllocatorStatCounters asc;
  122. GetAllocatorStats(asc);
  123. s.append(
  124. "HWASAN pid: %d rss: %zd threads: %zd stacks: %zd"
  125. " thr_aux: %zd stack_depot: %zd uniq_stacks: %zd"
  126. " heap: %zd",
  127. internal_getpid(), GetRSS(), thread_stats.n_live_threads,
  128. thread_stats.total_stack_size,
  129. thread_stats.n_live_threads * thread_list.MemoryUsedPerThread(),
  130. sds.allocated, sds.n_uniq_ids, asc[AllocatorStatMapped]);
  131. }
  132. #if SANITIZER_ANDROID
  133. static constexpr uptr kMemoryUsageBufferSize = 4096;
  134. static char *memory_usage_buffer = nullptr;
  135. static void InitMemoryUsage() {
  136. memory_usage_buffer =
  137. (char *)MmapOrDie(kMemoryUsageBufferSize, "memory usage string");
  138. CHECK(memory_usage_buffer);
  139. memory_usage_buffer[0] = '\0';
  140. DecorateMapping((uptr)memory_usage_buffer, kMemoryUsageBufferSize,
  141. memory_usage_buffer);
  142. }
  143. void UpdateMemoryUsage() {
  144. if (!flags()->export_memory_stats)
  145. return;
  146. if (!memory_usage_buffer)
  147. InitMemoryUsage();
  148. InternalScopedString s;
  149. HwasanFormatMemoryUsage(s);
  150. internal_strncpy(memory_usage_buffer, s.data(), kMemoryUsageBufferSize - 1);
  151. memory_usage_buffer[kMemoryUsageBufferSize - 1] = '\0';
  152. }
  153. #else
  154. void UpdateMemoryUsage() {}
  155. #endif
  156. void HwasanAtExit() {
  157. if (common_flags()->print_module_map)
  158. DumpProcessMap();
  159. if (flags()->print_stats && (flags()->atexit || hwasan_report_count > 0))
  160. ReportStats();
  161. if (hwasan_report_count > 0) {
  162. // ReportAtExitStatistics();
  163. if (common_flags()->exitcode)
  164. internal__exit(common_flags()->exitcode);
  165. }
  166. }
  167. void HandleTagMismatch(AccessInfo ai, uptr pc, uptr frame, void *uc,
  168. uptr *registers_frame) {
  169. InternalMmapVector<BufferedStackTrace> stack_buffer(1);
  170. BufferedStackTrace *stack = stack_buffer.data();
  171. stack->Reset();
  172. stack->Unwind(pc, frame, uc, common_flags()->fast_unwind_on_fatal);
  173. // The second stack frame contains the failure __hwasan_check function, as
  174. // we have a stack frame for the registers saved in __hwasan_tag_mismatch that
  175. // we wish to ignore. This (currently) only occurs on AArch64, as x64
  176. // implementations use SIGTRAP to implement the failure, and thus do not go
  177. // through the stack saver.
  178. if (registers_frame && stack->trace && stack->size > 0) {
  179. stack->trace++;
  180. stack->size--;
  181. }
  182. bool fatal = flags()->halt_on_error || !ai.recover;
  183. ReportTagMismatch(stack, ai.addr, ai.size, ai.is_store, fatal,
  184. registers_frame);
  185. }
  186. void HwasanTagMismatch(uptr addr, uptr access_info, uptr *registers_frame,
  187. size_t outsize) {
  188. __hwasan::AccessInfo ai;
  189. ai.is_store = access_info & 0x10;
  190. ai.is_load = !ai.is_store;
  191. ai.recover = access_info & 0x20;
  192. ai.addr = addr;
  193. if ((access_info & 0xf) == 0xf)
  194. ai.size = outsize;
  195. else
  196. ai.size = 1 << (access_info & 0xf);
  197. HandleTagMismatch(ai, (uptr)__builtin_return_address(0),
  198. (uptr)__builtin_frame_address(0), nullptr, registers_frame);
  199. __builtin_unreachable();
  200. }
  201. Thread *GetCurrentThread() {
  202. uptr *ThreadLongPtr = GetCurrentThreadLongPtr();
  203. if (UNLIKELY(*ThreadLongPtr == 0))
  204. return nullptr;
  205. auto *R = (StackAllocationsRingBuffer *)ThreadLongPtr;
  206. return hwasanThreadList().GetThreadByBufferAddress((uptr)R->Next());
  207. }
  208. } // namespace __hwasan
  209. using namespace __hwasan;
  210. void __sanitizer::BufferedStackTrace::UnwindImpl(
  211. uptr pc, uptr bp, void *context, bool request_fast, u32 max_depth) {
  212. Thread *t = GetCurrentThread();
  213. if (!t) {
  214. // The thread is still being created, or has already been destroyed.
  215. size = 0;
  216. return;
  217. }
  218. Unwind(max_depth, pc, bp, context, t->stack_top(), t->stack_bottom(),
  219. request_fast);
  220. }
  221. static bool InitializeSingleGlobal(const hwasan_global &global) {
  222. uptr full_granule_size = RoundDownTo(global.size(), 16);
  223. TagMemoryAligned(global.addr(), full_granule_size, global.tag());
  224. if (global.size() % 16)
  225. TagMemoryAligned(global.addr() + full_granule_size, 16, global.size() % 16);
  226. return false;
  227. }
  228. static void InitLoadedGlobals() {
  229. dl_iterate_phdr(
  230. [](dl_phdr_info *info, size_t /* size */, void * /* data */) -> int {
  231. for (const hwasan_global &global : HwasanGlobalsFor(
  232. info->dlpi_addr, info->dlpi_phdr, info->dlpi_phnum))
  233. InitializeSingleGlobal(global);
  234. return 0;
  235. },
  236. nullptr);
  237. }
  238. // Prepare to run instrumented code on the main thread.
  239. static void InitInstrumentation() {
  240. if (hwasan_instrumentation_inited) return;
  241. InitializeOsSupport();
  242. if (!InitShadow()) {
  243. Printf("FATAL: HWAddressSanitizer cannot mmap the shadow memory.\n");
  244. DumpProcessMap();
  245. Die();
  246. }
  247. InitThreads();
  248. hwasan_instrumentation_inited = 1;
  249. }
  250. // Interface.
  251. uptr __hwasan_shadow_memory_dynamic_address; // Global interface symbol.
  252. // This function was used by the old frame descriptor mechanism. We keep it
  253. // around to avoid breaking ABI.
  254. void __hwasan_init_frames(uptr beg, uptr end) {}
  255. void __hwasan_init_static() {
  256. InitShadowGOT();
  257. InitInstrumentation();
  258. // In the non-static code path we call dl_iterate_phdr here. But at this point
  259. // libc might not have been initialized enough for dl_iterate_phdr to work.
  260. // Fortunately, since this is a statically linked executable we can use the
  261. // linker-defined symbol __ehdr_start to find the only relevant set of phdrs.
  262. extern ElfW(Ehdr) __ehdr_start;
  263. for (const hwasan_global &global : HwasanGlobalsFor(
  264. /* base */ 0,
  265. reinterpret_cast<const ElfW(Phdr) *>(
  266. reinterpret_cast<const char *>(&__ehdr_start) +
  267. __ehdr_start.e_phoff),
  268. __ehdr_start.e_phnum))
  269. InitializeSingleGlobal(global);
  270. }
  271. __attribute__((constructor(0))) void __hwasan_init() {
  272. CHECK(!hwasan_init_is_running);
  273. if (hwasan_inited) return;
  274. hwasan_init_is_running = 1;
  275. SanitizerToolName = "HWAddressSanitizer";
  276. InitTlsSize();
  277. CacheBinaryName();
  278. InitializeFlags();
  279. // Install tool-specific callbacks in sanitizer_common.
  280. SetCheckUnwindCallback(CheckUnwind);
  281. __sanitizer_set_report_path(common_flags()->log_path);
  282. AndroidTestTlsSlot();
  283. DisableCoreDumperIfNecessary();
  284. InitInstrumentation();
  285. InitLoadedGlobals();
  286. // Needs to be called here because flags()->random_tags might not have been
  287. // initialized when InitInstrumentation() was called.
  288. GetCurrentThread()->EnsureRandomStateInited();
  289. SetPrintfAndReportCallback(AppendToErrorMessageBuffer);
  290. // This may call libc -> needs initialized shadow.
  291. AndroidLogInit();
  292. InitializeInterceptors();
  293. InstallDeadlySignalHandlers(HwasanOnDeadlySignal);
  294. InstallAtExitHandler(); // Needs __cxa_atexit interceptor.
  295. InitializeCoverage(common_flags()->coverage, common_flags()->coverage_dir);
  296. HwasanTSDInit();
  297. HwasanTSDThreadInit();
  298. HwasanAllocatorInit();
  299. HwasanInstallAtForkHandler();
  300. #if HWASAN_CONTAINS_UBSAN
  301. __ubsan::InitAsPlugin();
  302. #endif
  303. VPrintf(1, "HWAddressSanitizer init done\n");
  304. hwasan_init_is_running = 0;
  305. hwasan_inited = 1;
  306. }
  307. void __hwasan_library_loaded(ElfW(Addr) base, const ElfW(Phdr) * phdr,
  308. ElfW(Half) phnum) {
  309. for (const hwasan_global &global : HwasanGlobalsFor(base, phdr, phnum))
  310. InitializeSingleGlobal(global);
  311. }
  312. void __hwasan_library_unloaded(ElfW(Addr) base, const ElfW(Phdr) * phdr,
  313. ElfW(Half) phnum) {
  314. for (; phnum != 0; ++phdr, --phnum)
  315. if (phdr->p_type == PT_LOAD)
  316. TagMemory(base + phdr->p_vaddr, phdr->p_memsz, 0);
  317. }
  318. void __hwasan_print_shadow(const void *p, uptr sz) {
  319. uptr ptr_raw = UntagAddr(reinterpret_cast<uptr>(p));
  320. uptr shadow_first = MemToShadow(ptr_raw);
  321. uptr shadow_last = MemToShadow(ptr_raw + sz - 1);
  322. Printf("HWASan shadow map for %zx .. %zx (pointer tag %x)\n", ptr_raw,
  323. ptr_raw + sz, GetTagFromPointer((uptr)p));
  324. for (uptr s = shadow_first; s <= shadow_last; ++s) {
  325. tag_t mem_tag = *reinterpret_cast<tag_t *>(s);
  326. uptr granule_addr = ShadowToMem(s);
  327. if (mem_tag && mem_tag < kShadowAlignment)
  328. Printf(" %zx: %02x(%02x)\n", granule_addr, mem_tag,
  329. *reinterpret_cast<tag_t *>(granule_addr + kShadowAlignment - 1));
  330. else
  331. Printf(" %zx: %02x\n", granule_addr, mem_tag);
  332. }
  333. }
  334. sptr __hwasan_test_shadow(const void *p, uptr sz) {
  335. if (sz == 0)
  336. return -1;
  337. tag_t ptr_tag = GetTagFromPointer((uptr)p);
  338. uptr ptr_raw = UntagAddr(reinterpret_cast<uptr>(p));
  339. uptr shadow_first = MemToShadow(ptr_raw);
  340. uptr shadow_last = MemToShadow(ptr_raw + sz - 1);
  341. for (uptr s = shadow_first; s <= shadow_last; ++s)
  342. if (*(tag_t *)s != ptr_tag) {
  343. sptr offset = ShadowToMem(s) - ptr_raw;
  344. return offset < 0 ? 0 : offset;
  345. }
  346. return -1;
  347. }
  348. u16 __sanitizer_unaligned_load16(const uu16 *p) {
  349. return *p;
  350. }
  351. u32 __sanitizer_unaligned_load32(const uu32 *p) {
  352. return *p;
  353. }
  354. u64 __sanitizer_unaligned_load64(const uu64 *p) {
  355. return *p;
  356. }
  357. void __sanitizer_unaligned_store16(uu16 *p, u16 x) {
  358. *p = x;
  359. }
  360. void __sanitizer_unaligned_store32(uu32 *p, u32 x) {
  361. *p = x;
  362. }
  363. void __sanitizer_unaligned_store64(uu64 *p, u64 x) {
  364. *p = x;
  365. }
  366. void __hwasan_loadN(uptr p, uptr sz) {
  367. CheckAddressSized<ErrorAction::Abort, AccessType::Load>(p, sz);
  368. }
  369. void __hwasan_load1(uptr p) {
  370. CheckAddress<ErrorAction::Abort, AccessType::Load, 0>(p);
  371. }
  372. void __hwasan_load2(uptr p) {
  373. CheckAddress<ErrorAction::Abort, AccessType::Load, 1>(p);
  374. }
  375. void __hwasan_load4(uptr p) {
  376. CheckAddress<ErrorAction::Abort, AccessType::Load, 2>(p);
  377. }
  378. void __hwasan_load8(uptr p) {
  379. CheckAddress<ErrorAction::Abort, AccessType::Load, 3>(p);
  380. }
  381. void __hwasan_load16(uptr p) {
  382. CheckAddress<ErrorAction::Abort, AccessType::Load, 4>(p);
  383. }
  384. void __hwasan_loadN_noabort(uptr p, uptr sz) {
  385. CheckAddressSized<ErrorAction::Recover, AccessType::Load>(p, sz);
  386. }
  387. void __hwasan_load1_noabort(uptr p) {
  388. CheckAddress<ErrorAction::Recover, AccessType::Load, 0>(p);
  389. }
  390. void __hwasan_load2_noabort(uptr p) {
  391. CheckAddress<ErrorAction::Recover, AccessType::Load, 1>(p);
  392. }
  393. void __hwasan_load4_noabort(uptr p) {
  394. CheckAddress<ErrorAction::Recover, AccessType::Load, 2>(p);
  395. }
  396. void __hwasan_load8_noabort(uptr p) {
  397. CheckAddress<ErrorAction::Recover, AccessType::Load, 3>(p);
  398. }
  399. void __hwasan_load16_noabort(uptr p) {
  400. CheckAddress<ErrorAction::Recover, AccessType::Load, 4>(p);
  401. }
  402. void __hwasan_storeN(uptr p, uptr sz) {
  403. CheckAddressSized<ErrorAction::Abort, AccessType::Store>(p, sz);
  404. }
  405. void __hwasan_store1(uptr p) {
  406. CheckAddress<ErrorAction::Abort, AccessType::Store, 0>(p);
  407. }
  408. void __hwasan_store2(uptr p) {
  409. CheckAddress<ErrorAction::Abort, AccessType::Store, 1>(p);
  410. }
  411. void __hwasan_store4(uptr p) {
  412. CheckAddress<ErrorAction::Abort, AccessType::Store, 2>(p);
  413. }
  414. void __hwasan_store8(uptr p) {
  415. CheckAddress<ErrorAction::Abort, AccessType::Store, 3>(p);
  416. }
  417. void __hwasan_store16(uptr p) {
  418. CheckAddress<ErrorAction::Abort, AccessType::Store, 4>(p);
  419. }
  420. void __hwasan_storeN_noabort(uptr p, uptr sz) {
  421. CheckAddressSized<ErrorAction::Recover, AccessType::Store>(p, sz);
  422. }
  423. void __hwasan_store1_noabort(uptr p) {
  424. CheckAddress<ErrorAction::Recover, AccessType::Store, 0>(p);
  425. }
  426. void __hwasan_store2_noabort(uptr p) {
  427. CheckAddress<ErrorAction::Recover, AccessType::Store, 1>(p);
  428. }
  429. void __hwasan_store4_noabort(uptr p) {
  430. CheckAddress<ErrorAction::Recover, AccessType::Store, 2>(p);
  431. }
  432. void __hwasan_store8_noabort(uptr p) {
  433. CheckAddress<ErrorAction::Recover, AccessType::Store, 3>(p);
  434. }
  435. void __hwasan_store16_noabort(uptr p) {
  436. CheckAddress<ErrorAction::Recover, AccessType::Store, 4>(p);
  437. }
  438. void __hwasan_tag_memory(uptr p, u8 tag, uptr sz) {
  439. TagMemoryAligned(p, sz, tag);
  440. }
  441. uptr __hwasan_tag_pointer(uptr p, u8 tag) {
  442. return AddTagToPointer(p, tag);
  443. }
  444. void __hwasan_handle_longjmp(const void *sp_dst) {
  445. uptr dst = (uptr)sp_dst;
  446. // HWASan does not support tagged SP.
  447. CHECK(GetTagFromPointer(dst) == 0);
  448. uptr sp = (uptr)__builtin_frame_address(0);
  449. static const uptr kMaxExpectedCleanupSize = 64 << 20; // 64M
  450. if (dst < sp || dst - sp > kMaxExpectedCleanupSize) {
  451. Report(
  452. "WARNING: HWASan is ignoring requested __hwasan_handle_longjmp: "
  453. "stack top: %p; target %p; distance: %p (%zd)\n"
  454. "False positive error reports may follow\n",
  455. (void *)sp, (void *)dst, dst - sp);
  456. return;
  457. }
  458. TagMemory(sp, dst - sp, 0);
  459. }
  460. void __hwasan_handle_vfork(const void *sp_dst) {
  461. uptr sp = (uptr)sp_dst;
  462. Thread *t = GetCurrentThread();
  463. CHECK(t);
  464. uptr top = t->stack_top();
  465. uptr bottom = t->stack_bottom();
  466. if (top == 0 || bottom == 0 || sp < bottom || sp >= top) {
  467. Report(
  468. "WARNING: HWASan is ignoring requested __hwasan_handle_vfork: "
  469. "stack top: %zx; current %zx; bottom: %zx \n"
  470. "False positive error reports may follow\n",
  471. top, sp, bottom);
  472. return;
  473. }
  474. TagMemory(bottom, sp - bottom, 0);
  475. }
  476. extern "C" void *__hwasan_extra_spill_area() {
  477. Thread *t = GetCurrentThread();
  478. return &t->vfork_spill();
  479. }
  480. void __hwasan_print_memory_usage() {
  481. InternalScopedString s;
  482. HwasanFormatMemoryUsage(s);
  483. Printf("%s\n", s.data());
  484. }
  485. static const u8 kFallbackTag = 0xBB & kTagMask;
  486. u8 __hwasan_generate_tag() {
  487. Thread *t = GetCurrentThread();
  488. if (!t) return kFallbackTag;
  489. return t->GenerateRandomTag();
  490. }
  491. #if !SANITIZER_SUPPORTS_WEAK_HOOKS
  492. extern "C" {
  493. SANITIZER_INTERFACE_ATTRIBUTE SANITIZER_WEAK_ATTRIBUTE
  494. const char* __hwasan_default_options() { return ""; }
  495. } // extern "C"
  496. #endif
  497. extern "C" {
  498. SANITIZER_INTERFACE_ATTRIBUTE
  499. void __sanitizer_print_stack_trace() {
  500. GET_FATAL_STACK_TRACE_PC_BP(StackTrace::GetCurrentPc(), GET_CURRENT_FRAME());
  501. stack.Print();
  502. }
  503. // Entry point for interoperability between __hwasan_tag_mismatch (ASM) and the
  504. // rest of the mismatch handling code (C++).
  505. void __hwasan_tag_mismatch4(uptr addr, uptr access_info, uptr *registers_frame,
  506. size_t outsize) {
  507. __hwasan::HwasanTagMismatch(addr, access_info, registers_frame, outsize);
  508. }
  509. } // extern "C"