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