asan_errors.cpp 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601
  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. Report(
  264. "ERROR: AddressSanitizer: allocator is out of memory trying to allocate "
  265. "0x%zx bytes\n", requested_size);
  266. Printf("%s", d.Default());
  267. stack->Print();
  268. PrintHintAllocatorCannotReturnNull();
  269. ReportErrorSummary(scariness.GetDescription(), stack);
  270. }
  271. void ErrorStringFunctionMemoryRangesOverlap::Print() {
  272. Decorator d;
  273. char bug_type[100];
  274. internal_snprintf(bug_type, sizeof(bug_type), "%s-param-overlap", function);
  275. Printf("%s", d.Error());
  276. Report(
  277. "ERROR: AddressSanitizer: %s: memory ranges [%p,%p) and [%p, %p) "
  278. "overlap\n",
  279. bug_type, (void *)addr1_description.Address(),
  280. (void *)(addr1_description.Address() + length1),
  281. (void *)addr2_description.Address(),
  282. (void *)(addr2_description.Address() + length2));
  283. Printf("%s", d.Default());
  284. scariness.Print();
  285. stack->Print();
  286. addr1_description.Print();
  287. addr2_description.Print();
  288. ReportErrorSummary(bug_type, stack);
  289. }
  290. void ErrorStringFunctionSizeOverflow::Print() {
  291. Decorator d;
  292. Printf("%s", d.Error());
  293. Report("ERROR: AddressSanitizer: %s: (size=%zd)\n",
  294. scariness.GetDescription(), size);
  295. Printf("%s", d.Default());
  296. scariness.Print();
  297. stack->Print();
  298. addr_description.Print();
  299. ReportErrorSummary(scariness.GetDescription(), stack);
  300. }
  301. void ErrorBadParamsToAnnotateContiguousContainer::Print() {
  302. Report(
  303. "ERROR: AddressSanitizer: bad parameters to "
  304. "__sanitizer_annotate_contiguous_container:\n"
  305. " beg : %p\n"
  306. " end : %p\n"
  307. " old_mid : %p\n"
  308. " new_mid : %p\n",
  309. (void *)beg, (void *)end, (void *)old_mid, (void *)new_mid);
  310. uptr granularity = ASAN_SHADOW_GRANULARITY;
  311. if (!IsAligned(beg, granularity))
  312. Report("ERROR: beg is not aligned by %zu\n", granularity);
  313. stack->Print();
  314. ReportErrorSummary(scariness.GetDescription(), stack);
  315. }
  316. void ErrorODRViolation::Print() {
  317. Decorator d;
  318. Printf("%s", d.Error());
  319. Report("ERROR: AddressSanitizer: %s (%p):\n", scariness.GetDescription(),
  320. (void *)global1.beg);
  321. Printf("%s", d.Default());
  322. InternalScopedString g1_loc;
  323. InternalScopedString g2_loc;
  324. PrintGlobalLocation(&g1_loc, global1);
  325. PrintGlobalLocation(&g2_loc, global2);
  326. Printf(" [1] size=%zd '%s' %s\n", global1.size,
  327. MaybeDemangleGlobalName(global1.name), g1_loc.data());
  328. Printf(" [2] size=%zd '%s' %s\n", global2.size,
  329. MaybeDemangleGlobalName(global2.name), g2_loc.data());
  330. if (stack_id1 && stack_id2) {
  331. Printf("These globals were registered at these points:\n");
  332. Printf(" [1]:\n");
  333. StackDepotGet(stack_id1).Print();
  334. Printf(" [2]:\n");
  335. StackDepotGet(stack_id2).Print();
  336. }
  337. Report(
  338. "HINT: if you don't care about these errors you may set "
  339. "ASAN_OPTIONS=detect_odr_violation=0\n");
  340. InternalScopedString error_msg;
  341. error_msg.append("%s: global '%s' at %s", scariness.GetDescription(),
  342. MaybeDemangleGlobalName(global1.name), g1_loc.data());
  343. ReportErrorSummary(error_msg.data());
  344. }
  345. void ErrorInvalidPointerPair::Print() {
  346. Decorator d;
  347. Printf("%s", d.Error());
  348. Report("ERROR: AddressSanitizer: %s: %p %p\n", scariness.GetDescription(),
  349. (void *)addr1_description.Address(),
  350. (void *)addr2_description.Address());
  351. Printf("%s", d.Default());
  352. GET_STACK_TRACE_FATAL(pc, bp);
  353. stack.Print();
  354. addr1_description.Print();
  355. addr2_description.Print();
  356. ReportErrorSummary(scariness.GetDescription(), &stack);
  357. }
  358. static bool AdjacentShadowValuesAreFullyPoisoned(u8 *s) {
  359. return s[-1] > 127 && s[1] > 127;
  360. }
  361. ErrorGeneric::ErrorGeneric(u32 tid, uptr pc_, uptr bp_, uptr sp_, uptr addr,
  362. bool is_write_, uptr access_size_)
  363. : ErrorBase(tid),
  364. addr_description(addr, access_size_, /*shouldLockThreadRegistry=*/false),
  365. pc(pc_),
  366. bp(bp_),
  367. sp(sp_),
  368. access_size(access_size_),
  369. is_write(is_write_),
  370. shadow_val(0) {
  371. scariness.Clear();
  372. if (access_size) {
  373. if (access_size <= 9) {
  374. char desr[] = "?-byte";
  375. desr[0] = '0' + access_size;
  376. scariness.Scare(access_size + access_size / 2, desr);
  377. } else if (access_size >= 10) {
  378. scariness.Scare(15, "multi-byte");
  379. }
  380. is_write ? scariness.Scare(20, "write") : scariness.Scare(1, "read");
  381. // Determine the error type.
  382. bug_descr = "unknown-crash";
  383. if (AddrIsInMem(addr)) {
  384. u8 *shadow_addr = (u8 *)MemToShadow(addr);
  385. // If we are accessing 16 bytes, look at the second shadow byte.
  386. if (*shadow_addr == 0 && access_size > ASAN_SHADOW_GRANULARITY)
  387. shadow_addr++;
  388. // If we are in the partial right redzone, look at the next shadow byte.
  389. if (*shadow_addr > 0 && *shadow_addr < 128) shadow_addr++;
  390. bool far_from_bounds = false;
  391. shadow_val = *shadow_addr;
  392. int bug_type_score = 0;
  393. // For use-after-frees reads are almost as bad as writes.
  394. int read_after_free_bonus = 0;
  395. switch (shadow_val) {
  396. case kAsanHeapLeftRedzoneMagic:
  397. case kAsanArrayCookieMagic:
  398. bug_descr = "heap-buffer-overflow";
  399. bug_type_score = 10;
  400. far_from_bounds = AdjacentShadowValuesAreFullyPoisoned(shadow_addr);
  401. break;
  402. case kAsanHeapFreeMagic:
  403. bug_descr = "heap-use-after-free";
  404. bug_type_score = 20;
  405. if (!is_write) read_after_free_bonus = 18;
  406. break;
  407. case kAsanStackLeftRedzoneMagic:
  408. bug_descr = "stack-buffer-underflow";
  409. bug_type_score = 25;
  410. far_from_bounds = AdjacentShadowValuesAreFullyPoisoned(shadow_addr);
  411. break;
  412. case kAsanInitializationOrderMagic:
  413. bug_descr = "initialization-order-fiasco";
  414. bug_type_score = 1;
  415. break;
  416. case kAsanStackMidRedzoneMagic:
  417. case kAsanStackRightRedzoneMagic:
  418. bug_descr = "stack-buffer-overflow";
  419. bug_type_score = 25;
  420. far_from_bounds = AdjacentShadowValuesAreFullyPoisoned(shadow_addr);
  421. break;
  422. case kAsanStackAfterReturnMagic:
  423. bug_descr = "stack-use-after-return";
  424. bug_type_score = 30;
  425. if (!is_write) read_after_free_bonus = 18;
  426. break;
  427. case kAsanUserPoisonedMemoryMagic:
  428. bug_descr = "use-after-poison";
  429. bug_type_score = 20;
  430. break;
  431. case kAsanContiguousContainerOOBMagic:
  432. bug_descr = "container-overflow";
  433. bug_type_score = 10;
  434. break;
  435. case kAsanStackUseAfterScopeMagic:
  436. bug_descr = "stack-use-after-scope";
  437. bug_type_score = 10;
  438. break;
  439. case kAsanGlobalRedzoneMagic:
  440. bug_descr = "global-buffer-overflow";
  441. bug_type_score = 10;
  442. far_from_bounds = AdjacentShadowValuesAreFullyPoisoned(shadow_addr);
  443. break;
  444. case kAsanIntraObjectRedzone:
  445. bug_descr = "intra-object-overflow";
  446. bug_type_score = 10;
  447. break;
  448. case kAsanAllocaLeftMagic:
  449. case kAsanAllocaRightMagic:
  450. bug_descr = "dynamic-stack-buffer-overflow";
  451. bug_type_score = 25;
  452. far_from_bounds = AdjacentShadowValuesAreFullyPoisoned(shadow_addr);
  453. break;
  454. }
  455. scariness.Scare(bug_type_score + read_after_free_bonus, bug_descr);
  456. if (far_from_bounds) scariness.Scare(10, "far-from-bounds");
  457. }
  458. }
  459. }
  460. static void PrintContainerOverflowHint() {
  461. Printf("HINT: if you don't care about these errors you may set "
  462. "ASAN_OPTIONS=detect_container_overflow=0.\n"
  463. "If you suspect a false positive see also: "
  464. "https://github.com/google/sanitizers/wiki/"
  465. "AddressSanitizerContainerOverflow.\n");
  466. }
  467. static void PrintShadowByte(InternalScopedString *str, const char *before,
  468. u8 byte, const char *after = "\n") {
  469. PrintMemoryByte(str, before, byte, /*in_shadow*/true, after);
  470. }
  471. static void PrintLegend(InternalScopedString *str) {
  472. str->append(
  473. "Shadow byte legend (one shadow byte represents %d "
  474. "application bytes):\n",
  475. (int)ASAN_SHADOW_GRANULARITY);
  476. PrintShadowByte(str, " Addressable: ", 0);
  477. str->append(" Partially addressable: ");
  478. for (u8 i = 1; i < ASAN_SHADOW_GRANULARITY; i++)
  479. PrintShadowByte(str, "", i, " ");
  480. str->append("\n");
  481. PrintShadowByte(str, " Heap left redzone: ",
  482. kAsanHeapLeftRedzoneMagic);
  483. PrintShadowByte(str, " Freed heap region: ", kAsanHeapFreeMagic);
  484. PrintShadowByte(str, " Stack left redzone: ",
  485. kAsanStackLeftRedzoneMagic);
  486. PrintShadowByte(str, " Stack mid redzone: ",
  487. kAsanStackMidRedzoneMagic);
  488. PrintShadowByte(str, " Stack right redzone: ",
  489. kAsanStackRightRedzoneMagic);
  490. PrintShadowByte(str, " Stack after return: ",
  491. kAsanStackAfterReturnMagic);
  492. PrintShadowByte(str, " Stack use after scope: ",
  493. kAsanStackUseAfterScopeMagic);
  494. PrintShadowByte(str, " Global redzone: ", kAsanGlobalRedzoneMagic);
  495. PrintShadowByte(str, " Global init order: ",
  496. kAsanInitializationOrderMagic);
  497. PrintShadowByte(str, " Poisoned by user: ",
  498. kAsanUserPoisonedMemoryMagic);
  499. PrintShadowByte(str, " Container overflow: ",
  500. kAsanContiguousContainerOOBMagic);
  501. PrintShadowByte(str, " Array cookie: ",
  502. kAsanArrayCookieMagic);
  503. PrintShadowByte(str, " Intra object redzone: ",
  504. kAsanIntraObjectRedzone);
  505. PrintShadowByte(str, " ASan internal: ", kAsanInternalHeapMagic);
  506. PrintShadowByte(str, " Left alloca redzone: ", kAsanAllocaLeftMagic);
  507. PrintShadowByte(str, " Right alloca redzone: ", kAsanAllocaRightMagic);
  508. }
  509. static void PrintShadowBytes(InternalScopedString *str, const char *before,
  510. u8 *bytes, u8 *guilty, uptr n) {
  511. Decorator d;
  512. if (before)
  513. str->append("%s%p:", before, (void *)bytes);
  514. for (uptr i = 0; i < n; i++) {
  515. u8 *p = bytes + i;
  516. const char *before =
  517. p == guilty ? "[" : (p - 1 == guilty && i != 0) ? "" : " ";
  518. const char *after = p == guilty ? "]" : "";
  519. PrintShadowByte(str, before, *p, after);
  520. }
  521. str->append("\n");
  522. }
  523. static void PrintShadowMemoryForAddress(uptr addr) {
  524. if (!AddrIsInMem(addr)) return;
  525. uptr shadow_addr = MemToShadow(addr);
  526. const uptr n_bytes_per_row = 16;
  527. uptr aligned_shadow = shadow_addr & ~(n_bytes_per_row - 1);
  528. InternalScopedString str;
  529. str.append("Shadow bytes around the buggy address:\n");
  530. for (int i = -5; i <= 5; i++) {
  531. uptr row_shadow_addr = aligned_shadow + i * n_bytes_per_row;
  532. // Skip rows that would be outside the shadow range. This can happen when
  533. // the user address is near the bottom, top, or shadow gap of the address
  534. // space.
  535. if (!AddrIsInShadow(row_shadow_addr)) continue;
  536. const char *prefix = (i == 0) ? "=>" : " ";
  537. PrintShadowBytes(&str, prefix, (u8 *)row_shadow_addr, (u8 *)shadow_addr,
  538. n_bytes_per_row);
  539. }
  540. if (flags()->print_legend) PrintLegend(&str);
  541. Printf("%s", str.data());
  542. }
  543. void ErrorGeneric::Print() {
  544. Decorator d;
  545. Printf("%s", d.Error());
  546. uptr addr = addr_description.Address();
  547. Report("ERROR: AddressSanitizer: %s on address %p at pc %p bp %p sp %p\n",
  548. bug_descr, (void *)addr, (void *)pc, (void *)bp, (void *)sp);
  549. Printf("%s", d.Default());
  550. Printf("%s%s of size %zu at %p thread %s%s\n", d.Access(),
  551. access_size ? (is_write ? "WRITE" : "READ") : "ACCESS", access_size,
  552. (void *)addr, AsanThreadIdAndName(tid).c_str(), d.Default());
  553. scariness.Print();
  554. GET_STACK_TRACE_FATAL(pc, bp);
  555. stack.Print();
  556. // Pass bug_descr because we have a special case for
  557. // initialization-order-fiasco
  558. addr_description.Print(bug_descr);
  559. if (shadow_val == kAsanContiguousContainerOOBMagic)
  560. PrintContainerOverflowHint();
  561. ReportErrorSummary(bug_descr, &stack);
  562. PrintShadowMemoryForAddress(addr);
  563. }
  564. } // namespace __asan