hwasan.cpp 19 KB

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