asan_errors.cpp 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620
  1. //===-- asan_errors.cpp -----------------------------------------*- C++ -*-===//
  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. // ASan implementation for error structures.
  12. //===----------------------------------------------------------------------===//
  13. #include "asan_errors.h"
  14. #include "asan_descriptions.h"
  15. #include "asan_mapping.h"
  16. #include "asan_report.h"
  17. #include "asan_stack.h"
  18. #include "sanitizer_common/sanitizer_stackdepot.h"
  19. namespace __asan {
  20. static void OnStackUnwind(const SignalContext &sig,
  21. const void *callback_context,
  22. BufferedStackTrace *stack) {
  23. bool fast = common_flags()->fast_unwind_on_fatal;
  24. #if SANITIZER_FREEBSD || SANITIZER_NETBSD
  25. // On FreeBSD the slow unwinding that leverages _Unwind_Backtrace()
  26. // yields the call stack of the signal's handler and not of the code
  27. // that raised the signal (as it does on Linux).
  28. fast = true;
  29. #endif
  30. // Tests and maybe some users expect that scariness is going to be printed
  31. // just before the stack. As only asan has scariness score we have no
  32. // corresponding code in the sanitizer_common and we use this callback to
  33. // print it.
  34. static_cast<const ScarinessScoreBase *>(callback_context)->Print();
  35. stack->Unwind(StackTrace::GetNextInstructionPc(sig.pc), sig.bp, sig.context,
  36. fast);
  37. }
  38. void ErrorDeadlySignal::Print() {
  39. ReportDeadlySignal(signal, tid, &OnStackUnwind, &scariness);
  40. }
  41. void ErrorDoubleFree::Print() {
  42. Decorator d;
  43. Printf("%s", d.Error());
  44. Report("ERROR: AddressSanitizer: attempting %s on %p in thread %s:\n",
  45. scariness.GetDescription(), (void *)addr_description.addr,
  46. AsanThreadIdAndName(tid).c_str());
  47. Printf("%s", d.Default());
  48. scariness.Print();
  49. GET_STACK_TRACE_FATAL(second_free_stack->trace[0],
  50. second_free_stack->top_frame_bp);
  51. stack.Print();
  52. addr_description.Print();
  53. ReportErrorSummary(scariness.GetDescription(), &stack);
  54. }
  55. void ErrorNewDeleteTypeMismatch::Print() {
  56. Decorator d;
  57. Printf("%s", d.Error());
  58. Report("ERROR: AddressSanitizer: %s on %p in thread %s:\n",
  59. scariness.GetDescription(), (void *)addr_description.addr,
  60. AsanThreadIdAndName(tid).c_str());
  61. Printf("%s object passed to delete has wrong type:\n", d.Default());
  62. if (delete_size != 0) {
  63. Printf(
  64. " size of the allocated type: %zd bytes;\n"
  65. " size of the deallocated type: %zd bytes.\n",
  66. addr_description.chunk_access.chunk_size, delete_size);
  67. }
  68. const uptr user_alignment =
  69. addr_description.chunk_access.user_requested_alignment;
  70. if (delete_alignment != user_alignment) {
  71. char user_alignment_str[32];
  72. char delete_alignment_str[32];
  73. internal_snprintf(user_alignment_str, sizeof(user_alignment_str),
  74. "%zd bytes", user_alignment);
  75. internal_snprintf(delete_alignment_str, sizeof(delete_alignment_str),
  76. "%zd bytes", delete_alignment);
  77. static const char *kDefaultAlignment = "default-aligned";
  78. Printf(
  79. " alignment of the allocated type: %s;\n"
  80. " alignment of the deallocated type: %s.\n",
  81. user_alignment > 0 ? user_alignment_str : kDefaultAlignment,
  82. delete_alignment > 0 ? delete_alignment_str : kDefaultAlignment);
  83. }
  84. CHECK_GT(free_stack->size, 0);
  85. scariness.Print();
  86. GET_STACK_TRACE_FATAL(free_stack->trace[0], free_stack->top_frame_bp);
  87. stack.Print();
  88. addr_description.Print();
  89. ReportErrorSummary(scariness.GetDescription(), &stack);
  90. Report(
  91. "HINT: if you don't care about these errors you may set "
  92. "ASAN_OPTIONS=new_delete_type_mismatch=0\n");
  93. }
  94. void ErrorFreeNotMalloced::Print() {
  95. Decorator d;
  96. Printf("%s", d.Error());
  97. Report(
  98. "ERROR: AddressSanitizer: attempting free on address "
  99. "which was not malloc()-ed: %p in thread %s\n",
  100. (void *)addr_description.Address(), AsanThreadIdAndName(tid).c_str());
  101. Printf("%s", d.Default());
  102. CHECK_GT(free_stack->size, 0);
  103. scariness.Print();
  104. GET_STACK_TRACE_FATAL(free_stack->trace[0], free_stack->top_frame_bp);
  105. stack.Print();
  106. addr_description.Print();
  107. ReportErrorSummary(scariness.GetDescription(), &stack);
  108. }
  109. void ErrorAllocTypeMismatch::Print() {
  110. static const char *alloc_names[] = {"INVALID", "malloc", "operator new",
  111. "operator new []"};
  112. static const char *dealloc_names[] = {"INVALID", "free", "operator delete",
  113. "operator delete []"};
  114. CHECK_NE(alloc_type, dealloc_type);
  115. Decorator d;
  116. Printf("%s", d.Error());
  117. Report("ERROR: AddressSanitizer: %s (%s vs %s) on %p\n",
  118. scariness.GetDescription(), alloc_names[alloc_type],
  119. dealloc_names[dealloc_type], (void *)addr_description.Address());
  120. Printf("%s", d.Default());
  121. CHECK_GT(dealloc_stack->size, 0);
  122. scariness.Print();
  123. GET_STACK_TRACE_FATAL(dealloc_stack->trace[0], dealloc_stack->top_frame_bp);
  124. stack.Print();
  125. addr_description.Print();
  126. ReportErrorSummary(scariness.GetDescription(), &stack);
  127. Report(
  128. "HINT: if you don't care about these errors you may set "
  129. "ASAN_OPTIONS=alloc_dealloc_mismatch=0\n");
  130. }
  131. void ErrorMallocUsableSizeNotOwned::Print() {
  132. Decorator d;
  133. Printf("%s", d.Error());
  134. Report(
  135. "ERROR: AddressSanitizer: attempting to call malloc_usable_size() for "
  136. "pointer which is not owned: %p\n",
  137. (void *)addr_description.Address());
  138. Printf("%s", d.Default());
  139. stack->Print();
  140. addr_description.Print();
  141. ReportErrorSummary(scariness.GetDescription(), stack);
  142. }
  143. void ErrorSanitizerGetAllocatedSizeNotOwned::Print() {
  144. Decorator d;
  145. Printf("%s", d.Error());
  146. Report(
  147. "ERROR: AddressSanitizer: attempting to call "
  148. "__sanitizer_get_allocated_size() for pointer which is not owned: %p\n",
  149. (void *)addr_description.Address());
  150. Printf("%s", d.Default());
  151. stack->Print();
  152. addr_description.Print();
  153. ReportErrorSummary(scariness.GetDescription(), stack);
  154. }
  155. void ErrorCallocOverflow::Print() {
  156. Decorator d;
  157. Printf("%s", d.Error());
  158. Report(
  159. "ERROR: AddressSanitizer: calloc parameters overflow: count * size "
  160. "(%zd * %zd) cannot be represented in type size_t (thread %s)\n",
  161. count, size, AsanThreadIdAndName(tid).c_str());
  162. Printf("%s", d.Default());
  163. stack->Print();
  164. PrintHintAllocatorCannotReturnNull();
  165. ReportErrorSummary(scariness.GetDescription(), stack);
  166. }
  167. void ErrorReallocArrayOverflow::Print() {
  168. Decorator d;
  169. Printf("%s", d.Error());
  170. Report(
  171. "ERROR: AddressSanitizer: reallocarray parameters overflow: count * size "
  172. "(%zd * %zd) cannot be represented in type size_t (thread %s)\n",
  173. count, size, AsanThreadIdAndName(tid).c_str());
  174. Printf("%s", d.Default());
  175. stack->Print();
  176. PrintHintAllocatorCannotReturnNull();
  177. ReportErrorSummary(scariness.GetDescription(), stack);
  178. }
  179. void ErrorPvallocOverflow::Print() {
  180. Decorator d;
  181. Printf("%s", d.Error());
  182. Report(
  183. "ERROR: AddressSanitizer: pvalloc parameters overflow: size 0x%zx "
  184. "rounded up to system page size 0x%zx cannot be represented in type "
  185. "size_t (thread %s)\n",
  186. size, GetPageSizeCached(), AsanThreadIdAndName(tid).c_str());
  187. Printf("%s", d.Default());
  188. stack->Print();
  189. PrintHintAllocatorCannotReturnNull();
  190. ReportErrorSummary(scariness.GetDescription(), stack);
  191. }
  192. void ErrorInvalidAllocationAlignment::Print() {
  193. Decorator d;
  194. Printf("%s", d.Error());
  195. Report(
  196. "ERROR: AddressSanitizer: invalid allocation alignment: %zd, "
  197. "alignment must be a power of two (thread %s)\n",
  198. alignment, AsanThreadIdAndName(tid).c_str());
  199. Printf("%s", d.Default());
  200. stack->Print();
  201. PrintHintAllocatorCannotReturnNull();
  202. ReportErrorSummary(scariness.GetDescription(), stack);
  203. }
  204. void ErrorInvalidAlignedAllocAlignment::Print() {
  205. Decorator d;
  206. Printf("%s", d.Error());
  207. #if SANITIZER_POSIX
  208. Report("ERROR: AddressSanitizer: invalid alignment requested in "
  209. "aligned_alloc: %zd, alignment must be a power of two and the "
  210. "requested size 0x%zx must be a multiple of alignment "
  211. "(thread %s)\n", alignment, size, AsanThreadIdAndName(tid).c_str());
  212. #else
  213. Report("ERROR: AddressSanitizer: invalid alignment requested in "
  214. "aligned_alloc: %zd, the requested size 0x%zx must be a multiple of "
  215. "alignment (thread %s)\n", alignment, size,
  216. AsanThreadIdAndName(tid).c_str());
  217. #endif
  218. Printf("%s", d.Default());
  219. stack->Print();
  220. PrintHintAllocatorCannotReturnNull();
  221. ReportErrorSummary(scariness.GetDescription(), stack);
  222. }
  223. void ErrorInvalidPosixMemalignAlignment::Print() {
  224. Decorator d;
  225. Printf("%s", d.Error());
  226. Report(
  227. "ERROR: AddressSanitizer: invalid alignment requested in posix_memalign: "
  228. "%zd, alignment must be a power of two and a multiple of sizeof(void*) "
  229. "== %zd (thread %s)\n",
  230. alignment, sizeof(void *), AsanThreadIdAndName(tid).c_str());
  231. Printf("%s", d.Default());
  232. stack->Print();
  233. PrintHintAllocatorCannotReturnNull();
  234. ReportErrorSummary(scariness.GetDescription(), stack);
  235. }
  236. void ErrorAllocationSizeTooBig::Print() {
  237. Decorator d;
  238. Printf("%s", d.Error());
  239. Report(
  240. "ERROR: AddressSanitizer: requested allocation size 0x%zx (0x%zx after "
  241. "adjustments for alignment, red zones etc.) exceeds maximum supported "
  242. "size of 0x%zx (thread %s)\n",
  243. user_size, total_size, max_size, AsanThreadIdAndName(tid).c_str());
  244. Printf("%s", d.Default());
  245. stack->Print();
  246. PrintHintAllocatorCannotReturnNull();
  247. ReportErrorSummary(scariness.GetDescription(), stack);
  248. }
  249. void ErrorRssLimitExceeded::Print() {
  250. Decorator d;
  251. Printf("%s", d.Error());
  252. Report(
  253. "ERROR: AddressSanitizer: specified RSS limit exceeded, currently set to "
  254. "soft_rss_limit_mb=%zd\n", common_flags()->soft_rss_limit_mb);
  255. Printf("%s", d.Default());
  256. stack->Print();
  257. PrintHintAllocatorCannotReturnNull();
  258. ReportErrorSummary(scariness.GetDescription(), stack);
  259. }
  260. void ErrorOutOfMemory::Print() {
  261. Decorator d;
  262. Printf("%s", d.Error());
  263. ERROR_OOM("allocator is trying to allocate 0x%zx bytes\n", requested_size);
  264. Printf("%s", d.Default());
  265. stack->Print();
  266. PrintHintAllocatorCannotReturnNull();
  267. ReportErrorSummary(scariness.GetDescription(), stack);
  268. }
  269. void ErrorStringFunctionMemoryRangesOverlap::Print() {
  270. Decorator d;
  271. char bug_type[100];
  272. internal_snprintf(bug_type, sizeof(bug_type), "%s-param-overlap", function);
  273. Printf("%s", d.Error());
  274. Report(
  275. "ERROR: AddressSanitizer: %s: memory ranges [%p,%p) and [%p, %p) "
  276. "overlap\n",
  277. bug_type, (void *)addr1_description.Address(),
  278. (void *)(addr1_description.Address() + length1),
  279. (void *)addr2_description.Address(),
  280. (void *)(addr2_description.Address() + length2));
  281. Printf("%s", d.Default());
  282. scariness.Print();
  283. stack->Print();
  284. addr1_description.Print();
  285. addr2_description.Print();
  286. ReportErrorSummary(bug_type, stack);
  287. }
  288. void ErrorStringFunctionSizeOverflow::Print() {
  289. Decorator d;
  290. Printf("%s", d.Error());
  291. Report("ERROR: AddressSanitizer: %s: (size=%zd)\n",
  292. scariness.GetDescription(), size);
  293. Printf("%s", d.Default());
  294. scariness.Print();
  295. stack->Print();
  296. addr_description.Print();
  297. ReportErrorSummary(scariness.GetDescription(), stack);
  298. }
  299. void ErrorBadParamsToAnnotateContiguousContainer::Print() {
  300. Report(
  301. "ERROR: AddressSanitizer: bad parameters to "
  302. "__sanitizer_annotate_contiguous_container:\n"
  303. " beg : %p\n"
  304. " end : %p\n"
  305. " old_mid : %p\n"
  306. " new_mid : %p\n",
  307. (void *)beg, (void *)end, (void *)old_mid, (void *)new_mid);
  308. uptr granularity = ASAN_SHADOW_GRANULARITY;
  309. if (!IsAligned(beg, granularity))
  310. Report("ERROR: beg is not aligned by %zu\n", granularity);
  311. stack->Print();
  312. ReportErrorSummary(scariness.GetDescription(), stack);
  313. }
  314. void ErrorBadParamsToAnnotateDoubleEndedContiguousContainer::Print() {
  315. Report(
  316. "ERROR: AddressSanitizer: bad parameters to "
  317. "__sanitizer_annotate_double_ended_contiguous_container:\n"
  318. " storage_beg : %p\n"
  319. " storage_end : %p\n"
  320. " old_container_beg : %p\n"
  321. " old_container_end : %p\n"
  322. " new_container_beg : %p\n"
  323. " new_container_end : %p\n",
  324. (void *)storage_beg, (void *)storage_end, (void *)old_container_beg,
  325. (void *)old_container_end, (void *)new_container_beg,
  326. (void *)new_container_end);
  327. uptr granularity = ASAN_SHADOW_GRANULARITY;
  328. if (!IsAligned(storage_beg, granularity))
  329. Report("ERROR: storage_beg is not aligned by %zu\n", granularity);
  330. stack->Print();
  331. ReportErrorSummary(scariness.GetDescription(), stack);
  332. }
  333. void ErrorODRViolation::Print() {
  334. Decorator d;
  335. Printf("%s", d.Error());
  336. Report("ERROR: AddressSanitizer: %s (%p):\n", scariness.GetDescription(),
  337. (void *)global1.beg);
  338. Printf("%s", d.Default());
  339. InternalScopedString g1_loc;
  340. InternalScopedString g2_loc;
  341. PrintGlobalLocation(&g1_loc, global1);
  342. PrintGlobalLocation(&g2_loc, global2);
  343. Printf(" [1] size=%zd '%s' %s\n", global1.size,
  344. MaybeDemangleGlobalName(global1.name), g1_loc.data());
  345. Printf(" [2] size=%zd '%s' %s\n", global2.size,
  346. MaybeDemangleGlobalName(global2.name), g2_loc.data());
  347. if (stack_id1 && stack_id2) {
  348. Printf("These globals were registered at these points:\n");
  349. Printf(" [1]:\n");
  350. StackDepotGet(stack_id1).Print();
  351. Printf(" [2]:\n");
  352. StackDepotGet(stack_id2).Print();
  353. }
  354. Report(
  355. "HINT: if you don't care about these errors you may set "
  356. "ASAN_OPTIONS=detect_odr_violation=0\n");
  357. InternalScopedString error_msg;
  358. error_msg.append("%s: global '%s' at %s", scariness.GetDescription(),
  359. MaybeDemangleGlobalName(global1.name), g1_loc.data());
  360. ReportErrorSummary(error_msg.data());
  361. }
  362. void ErrorInvalidPointerPair::Print() {
  363. Decorator d;
  364. Printf("%s", d.Error());
  365. Report("ERROR: AddressSanitizer: %s: %p %p\n", scariness.GetDescription(),
  366. (void *)addr1_description.Address(),
  367. (void *)addr2_description.Address());
  368. Printf("%s", d.Default());
  369. GET_STACK_TRACE_FATAL(pc, bp);
  370. stack.Print();
  371. addr1_description.Print();
  372. addr2_description.Print();
  373. ReportErrorSummary(scariness.GetDescription(), &stack);
  374. }
  375. static bool AdjacentShadowValuesAreFullyPoisoned(u8 *s) {
  376. return s[-1] > 127 && s[1] > 127;
  377. }
  378. ErrorGeneric::ErrorGeneric(u32 tid, uptr pc_, uptr bp_, uptr sp_, uptr addr,
  379. bool is_write_, uptr access_size_)
  380. : ErrorBase(tid),
  381. addr_description(addr, access_size_, /*shouldLockThreadRegistry=*/false),
  382. pc(pc_),
  383. bp(bp_),
  384. sp(sp_),
  385. access_size(access_size_),
  386. is_write(is_write_),
  387. shadow_val(0) {
  388. scariness.Clear();
  389. if (access_size) {
  390. if (access_size <= 9) {
  391. char desr[] = "?-byte";
  392. desr[0] = '0' + access_size;
  393. scariness.Scare(access_size + access_size / 2, desr);
  394. } else if (access_size >= 10) {
  395. scariness.Scare(15, "multi-byte");
  396. }
  397. is_write ? scariness.Scare(20, "write") : scariness.Scare(1, "read");
  398. // Determine the error type.
  399. bug_descr = "unknown-crash";
  400. if (AddrIsInMem(addr)) {
  401. u8 *shadow_addr = (u8 *)MemToShadow(addr);
  402. // If we are accessing 16 bytes, look at the second shadow byte.
  403. if (*shadow_addr == 0 && access_size > ASAN_SHADOW_GRANULARITY)
  404. shadow_addr++;
  405. // If we are in the partial right redzone, look at the next shadow byte.
  406. if (*shadow_addr > 0 && *shadow_addr < 128) shadow_addr++;
  407. bool far_from_bounds = false;
  408. shadow_val = *shadow_addr;
  409. int bug_type_score = 0;
  410. // For use-after-frees reads are almost as bad as writes.
  411. int read_after_free_bonus = 0;
  412. switch (shadow_val) {
  413. case kAsanHeapLeftRedzoneMagic:
  414. case kAsanArrayCookieMagic:
  415. bug_descr = "heap-buffer-overflow";
  416. bug_type_score = 10;
  417. far_from_bounds = AdjacentShadowValuesAreFullyPoisoned(shadow_addr);
  418. break;
  419. case kAsanHeapFreeMagic:
  420. bug_descr = "heap-use-after-free";
  421. bug_type_score = 20;
  422. if (!is_write) read_after_free_bonus = 18;
  423. break;
  424. case kAsanStackLeftRedzoneMagic:
  425. bug_descr = "stack-buffer-underflow";
  426. bug_type_score = 25;
  427. far_from_bounds = AdjacentShadowValuesAreFullyPoisoned(shadow_addr);
  428. break;
  429. case kAsanInitializationOrderMagic:
  430. bug_descr = "initialization-order-fiasco";
  431. bug_type_score = 1;
  432. break;
  433. case kAsanStackMidRedzoneMagic:
  434. case kAsanStackRightRedzoneMagic:
  435. bug_descr = "stack-buffer-overflow";
  436. bug_type_score = 25;
  437. far_from_bounds = AdjacentShadowValuesAreFullyPoisoned(shadow_addr);
  438. break;
  439. case kAsanStackAfterReturnMagic:
  440. bug_descr = "stack-use-after-return";
  441. bug_type_score = 30;
  442. if (!is_write) read_after_free_bonus = 18;
  443. break;
  444. case kAsanUserPoisonedMemoryMagic:
  445. bug_descr = "use-after-poison";
  446. bug_type_score = 20;
  447. break;
  448. case kAsanContiguousContainerOOBMagic:
  449. bug_descr = "container-overflow";
  450. bug_type_score = 10;
  451. break;
  452. case kAsanStackUseAfterScopeMagic:
  453. bug_descr = "stack-use-after-scope";
  454. bug_type_score = 10;
  455. break;
  456. case kAsanGlobalRedzoneMagic:
  457. bug_descr = "global-buffer-overflow";
  458. bug_type_score = 10;
  459. far_from_bounds = AdjacentShadowValuesAreFullyPoisoned(shadow_addr);
  460. break;
  461. case kAsanIntraObjectRedzone:
  462. bug_descr = "intra-object-overflow";
  463. bug_type_score = 10;
  464. break;
  465. case kAsanAllocaLeftMagic:
  466. case kAsanAllocaRightMagic:
  467. bug_descr = "dynamic-stack-buffer-overflow";
  468. bug_type_score = 25;
  469. far_from_bounds = AdjacentShadowValuesAreFullyPoisoned(shadow_addr);
  470. break;
  471. }
  472. scariness.Scare(bug_type_score + read_after_free_bonus, bug_descr);
  473. if (far_from_bounds) scariness.Scare(10, "far-from-bounds");
  474. }
  475. }
  476. }
  477. static void PrintContainerOverflowHint() {
  478. Printf("HINT: if you don't care about these errors you may set "
  479. "ASAN_OPTIONS=detect_container_overflow=0.\n"
  480. "If you suspect a false positive see also: "
  481. "https://github.com/google/sanitizers/wiki/"
  482. "AddressSanitizerContainerOverflow.\n");
  483. }
  484. static void PrintShadowByte(InternalScopedString *str, const char *before,
  485. u8 byte, const char *after = "\n") {
  486. PrintMemoryByte(str, before, byte, /*in_shadow*/true, after);
  487. }
  488. static void PrintLegend(InternalScopedString *str) {
  489. str->append(
  490. "Shadow byte legend (one shadow byte represents %d "
  491. "application bytes):\n",
  492. (int)ASAN_SHADOW_GRANULARITY);
  493. PrintShadowByte(str, " Addressable: ", 0);
  494. str->append(" Partially addressable: ");
  495. for (u8 i = 1; i < ASAN_SHADOW_GRANULARITY; i++)
  496. PrintShadowByte(str, "", i, " ");
  497. str->append("\n");
  498. PrintShadowByte(str, " Heap left redzone: ",
  499. kAsanHeapLeftRedzoneMagic);
  500. PrintShadowByte(str, " Freed heap region: ", kAsanHeapFreeMagic);
  501. PrintShadowByte(str, " Stack left redzone: ",
  502. kAsanStackLeftRedzoneMagic);
  503. PrintShadowByte(str, " Stack mid redzone: ",
  504. kAsanStackMidRedzoneMagic);
  505. PrintShadowByte(str, " Stack right redzone: ",
  506. kAsanStackRightRedzoneMagic);
  507. PrintShadowByte(str, " Stack after return: ",
  508. kAsanStackAfterReturnMagic);
  509. PrintShadowByte(str, " Stack use after scope: ",
  510. kAsanStackUseAfterScopeMagic);
  511. PrintShadowByte(str, " Global redzone: ", kAsanGlobalRedzoneMagic);
  512. PrintShadowByte(str, " Global init order: ",
  513. kAsanInitializationOrderMagic);
  514. PrintShadowByte(str, " Poisoned by user: ",
  515. kAsanUserPoisonedMemoryMagic);
  516. PrintShadowByte(str, " Container overflow: ",
  517. kAsanContiguousContainerOOBMagic);
  518. PrintShadowByte(str, " Array cookie: ",
  519. kAsanArrayCookieMagic);
  520. PrintShadowByte(str, " Intra object redzone: ",
  521. kAsanIntraObjectRedzone);
  522. PrintShadowByte(str, " ASan internal: ", kAsanInternalHeapMagic);
  523. PrintShadowByte(str, " Left alloca redzone: ", kAsanAllocaLeftMagic);
  524. PrintShadowByte(str, " Right alloca redzone: ", kAsanAllocaRightMagic);
  525. }
  526. static void PrintShadowBytes(InternalScopedString *str, const char *before,
  527. u8 *bytes, u8 *guilty, uptr n) {
  528. Decorator d;
  529. if (before)
  530. str->append("%s%p:", before,
  531. (void *)ShadowToMem(reinterpret_cast<uptr>(bytes)));
  532. for (uptr i = 0; i < n; i++) {
  533. u8 *p = bytes + i;
  534. const char *before =
  535. p == guilty ? "[" : (p - 1 == guilty && i != 0) ? "" : " ";
  536. const char *after = p == guilty ? "]" : "";
  537. PrintShadowByte(str, before, *p, after);
  538. }
  539. str->append("\n");
  540. }
  541. static void PrintShadowMemoryForAddress(uptr addr) {
  542. if (!AddrIsInMem(addr)) return;
  543. uptr shadow_addr = MemToShadow(addr);
  544. const uptr n_bytes_per_row = 16;
  545. uptr aligned_shadow = shadow_addr & ~(n_bytes_per_row - 1);
  546. InternalScopedString str;
  547. str.append("Shadow bytes around the buggy address:\n");
  548. for (int i = -5; i <= 5; i++) {
  549. uptr row_shadow_addr = aligned_shadow + i * n_bytes_per_row;
  550. // Skip rows that would be outside the shadow range. This can happen when
  551. // the user address is near the bottom, top, or shadow gap of the address
  552. // space.
  553. if (!AddrIsInShadow(row_shadow_addr)) continue;
  554. const char *prefix = (i == 0) ? "=>" : " ";
  555. PrintShadowBytes(&str, prefix, (u8 *)row_shadow_addr, (u8 *)shadow_addr,
  556. n_bytes_per_row);
  557. }
  558. if (flags()->print_legend) PrintLegend(&str);
  559. Printf("%s", str.data());
  560. }
  561. void ErrorGeneric::Print() {
  562. Decorator d;
  563. Printf("%s", d.Error());
  564. uptr addr = addr_description.Address();
  565. Report("ERROR: AddressSanitizer: %s on address %p at pc %p bp %p sp %p\n",
  566. bug_descr, (void *)addr, (void *)pc, (void *)bp, (void *)sp);
  567. Printf("%s", d.Default());
  568. Printf("%s%s of size %zu at %p thread %s%s\n", d.Access(),
  569. access_size ? (is_write ? "WRITE" : "READ") : "ACCESS", access_size,
  570. (void *)addr, AsanThreadIdAndName(tid).c_str(), d.Default());
  571. scariness.Print();
  572. GET_STACK_TRACE_FATAL(pc, bp);
  573. stack.Print();
  574. // Pass bug_descr because we have a special case for
  575. // initialization-order-fiasco
  576. addr_description.Print(bug_descr);
  577. if (shadow_val == kAsanContiguousContainerOOBMagic)
  578. PrintContainerOverflowHint();
  579. ReportErrorSummary(bug_descr, &stack);
  580. PrintShadowMemoryForAddress(addr);
  581. }
  582. } // namespace __asan