sanitizer_symbolizer_report.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  1. //===-- sanitizer_symbolizer_report.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 shared between AddressSanitizer and other sanitizer run-time
  10. /// libraries and implements symbolized reports related functions.
  11. ///
  12. //===----------------------------------------------------------------------===//
  13. #include "sanitizer_common.h"
  14. #include "sanitizer_file.h"
  15. #include "sanitizer_flags.h"
  16. #include "sanitizer_procmaps.h"
  17. #include "sanitizer_report_decorator.h"
  18. #include "sanitizer_stacktrace.h"
  19. #include "sanitizer_stacktrace_printer.h"
  20. #include "sanitizer_symbolizer.h"
  21. #if SANITIZER_POSIX
  22. # include "sanitizer_posix.h"
  23. # include <sys/mman.h>
  24. #endif
  25. namespace __sanitizer {
  26. #if !SANITIZER_GO
  27. void ReportErrorSummary(const char *error_type, const AddressInfo &info,
  28. const char *alt_tool_name) {
  29. if (!common_flags()->print_summary) return;
  30. InternalScopedString buff;
  31. buff.append("%s ", error_type);
  32. RenderFrame(&buff, "%L %F", 0, info.address, &info,
  33. common_flags()->symbolize_vs_style,
  34. common_flags()->strip_path_prefix);
  35. ReportErrorSummary(buff.data(), alt_tool_name);
  36. }
  37. #endif
  38. #if !SANITIZER_FUCHSIA
  39. bool ReportFile::SupportsColors() {
  40. SpinMutexLock l(mu);
  41. ReopenIfNecessary();
  42. return SupportsColoredOutput(fd);
  43. }
  44. static inline bool ReportSupportsColors() {
  45. return report_file.SupportsColors();
  46. }
  47. #else // SANITIZER_FUCHSIA
  48. // Fuchsia's logs always go through post-processing that handles colorization.
  49. static inline bool ReportSupportsColors() { return true; }
  50. #endif // !SANITIZER_FUCHSIA
  51. bool ColorizeReports() {
  52. // FIXME: Add proper Windows support to AnsiColorDecorator and re-enable color
  53. // printing on Windows.
  54. if (SANITIZER_WINDOWS)
  55. return false;
  56. const char *flag = common_flags()->color;
  57. return internal_strcmp(flag, "always") == 0 ||
  58. (internal_strcmp(flag, "auto") == 0 && ReportSupportsColors());
  59. }
  60. void ReportErrorSummary(const char *error_type, const StackTrace *stack,
  61. const char *alt_tool_name) {
  62. #if !SANITIZER_GO
  63. if (!common_flags()->print_summary)
  64. return;
  65. if (stack->size == 0) {
  66. ReportErrorSummary(error_type);
  67. return;
  68. }
  69. // Currently, we include the first stack frame into the report summary.
  70. // Maybe sometimes we need to choose another frame (e.g. skip memcpy/etc).
  71. uptr pc = StackTrace::GetPreviousInstructionPc(stack->trace[0]);
  72. SymbolizedStack *frame = Symbolizer::GetOrInit()->SymbolizePC(pc);
  73. ReportErrorSummary(error_type, frame->info, alt_tool_name);
  74. frame->ClearAll();
  75. #endif
  76. }
  77. void ReportMmapWriteExec(int prot, int flags) {
  78. #if SANITIZER_POSIX && (!SANITIZER_GO && !SANITIZER_ANDROID)
  79. int pflags = (PROT_WRITE | PROT_EXEC);
  80. if ((prot & pflags) != pflags)
  81. return;
  82. # if SANITIZER_MAC && defined(MAP_JIT)
  83. if ((flags & MAP_JIT) == MAP_JIT)
  84. return;
  85. # endif
  86. ScopedErrorReportLock l;
  87. SanitizerCommonDecorator d;
  88. InternalMmapVector<BufferedStackTrace> stack_buffer(1);
  89. BufferedStackTrace *stack = stack_buffer.data();
  90. stack->Reset();
  91. uptr top = 0;
  92. uptr bottom = 0;
  93. GET_CALLER_PC_BP_SP;
  94. (void)sp;
  95. bool fast = common_flags()->fast_unwind_on_fatal;
  96. if (StackTrace::WillUseFastUnwind(fast)) {
  97. GetThreadStackTopAndBottom(false, &top, &bottom);
  98. stack->Unwind(kStackTraceMax, pc, bp, nullptr, top, bottom, true);
  99. } else {
  100. stack->Unwind(kStackTraceMax, pc, 0, nullptr, 0, 0, false);
  101. }
  102. Printf("%s", d.Warning());
  103. Report("WARNING: %s: writable-executable page usage\n", SanitizerToolName);
  104. Printf("%s", d.Default());
  105. stack->Print();
  106. ReportErrorSummary("w-and-x-usage", stack);
  107. #endif
  108. }
  109. #if !SANITIZER_FUCHSIA && !SANITIZER_GO
  110. void StartReportDeadlySignal() {
  111. // Write the first message using fd=2, just in case.
  112. // It may actually fail to write in case stderr is closed.
  113. CatastrophicErrorWrite(SanitizerToolName, internal_strlen(SanitizerToolName));
  114. static const char kDeadlySignal[] = ":DEADLYSIGNAL\n";
  115. CatastrophicErrorWrite(kDeadlySignal, sizeof(kDeadlySignal) - 1);
  116. }
  117. static void MaybeReportNonExecRegion(uptr pc) {
  118. #if SANITIZER_FREEBSD || SANITIZER_LINUX || SANITIZER_NETBSD
  119. MemoryMappingLayout proc_maps(/*cache_enabled*/ true);
  120. MemoryMappedSegment segment;
  121. while (proc_maps.Next(&segment)) {
  122. if (pc >= segment.start && pc < segment.end && !segment.IsExecutable())
  123. Report("Hint: PC is at a non-executable region. Maybe a wild jump?\n");
  124. }
  125. #endif
  126. }
  127. static void PrintMemoryByte(InternalScopedString *str, const char *before,
  128. u8 byte) {
  129. SanitizerCommonDecorator d;
  130. str->append("%s%s%x%x%s ", before, d.MemoryByte(), byte >> 4, byte & 15,
  131. d.Default());
  132. }
  133. static void MaybeDumpInstructionBytes(uptr pc) {
  134. if (!common_flags()->dump_instruction_bytes || (pc < GetPageSizeCached()))
  135. return;
  136. InternalScopedString str;
  137. str.append("First 16 instruction bytes at pc: ");
  138. if (IsAccessibleMemoryRange(pc, 16)) {
  139. for (int i = 0; i < 16; ++i) {
  140. PrintMemoryByte(&str, "", ((u8 *)pc)[i]);
  141. }
  142. str.append("\n");
  143. } else {
  144. str.append("unaccessible\n");
  145. }
  146. Report("%s", str.data());
  147. }
  148. static void MaybeDumpRegisters(void *context) {
  149. if (!common_flags()->dump_registers) return;
  150. SignalContext::DumpAllRegisters(context);
  151. }
  152. static void ReportStackOverflowImpl(const SignalContext &sig, u32 tid,
  153. UnwindSignalStackCallbackType unwind,
  154. const void *unwind_context) {
  155. SanitizerCommonDecorator d;
  156. Printf("%s", d.Warning());
  157. static const char kDescription[] = "stack-overflow";
  158. Report("ERROR: %s: %s on address %p (pc %p bp %p sp %p T%d)\n",
  159. SanitizerToolName, kDescription, (void *)sig.addr, (void *)sig.pc,
  160. (void *)sig.bp, (void *)sig.sp, tid);
  161. Printf("%s", d.Default());
  162. InternalMmapVector<BufferedStackTrace> stack_buffer(1);
  163. BufferedStackTrace *stack = stack_buffer.data();
  164. stack->Reset();
  165. unwind(sig, unwind_context, stack);
  166. stack->Print();
  167. ReportErrorSummary(kDescription, stack);
  168. }
  169. static void ReportDeadlySignalImpl(const SignalContext &sig, u32 tid,
  170. UnwindSignalStackCallbackType unwind,
  171. const void *unwind_context) {
  172. SanitizerCommonDecorator d;
  173. Printf("%s", d.Warning());
  174. const char *description = sig.Describe();
  175. if (sig.is_memory_access && !sig.is_true_faulting_addr)
  176. Report("ERROR: %s: %s on unknown address (pc %p bp %p sp %p T%d)\n",
  177. SanitizerToolName, description, (void *)sig.pc, (void *)sig.bp,
  178. (void *)sig.sp, tid);
  179. else
  180. Report("ERROR: %s: %s on unknown address %p (pc %p bp %p sp %p T%d)\n",
  181. SanitizerToolName, description, (void *)sig.addr, (void *)sig.pc,
  182. (void *)sig.bp, (void *)sig.sp, tid);
  183. Printf("%s", d.Default());
  184. if (sig.pc < GetPageSizeCached())
  185. Report("Hint: pc points to the zero page.\n");
  186. if (sig.is_memory_access) {
  187. const char *access_type =
  188. sig.write_flag == SignalContext::Write
  189. ? "WRITE"
  190. : (sig.write_flag == SignalContext::Read ? "READ" : "UNKNOWN");
  191. Report("The signal is caused by a %s memory access.\n", access_type);
  192. if (!sig.is_true_faulting_addr)
  193. Report("Hint: this fault was caused by a dereference of a high value "
  194. "address (see register values below). Disassemble the provided "
  195. "pc to learn which register was used.\n");
  196. else if (sig.addr < GetPageSizeCached())
  197. Report("Hint: address points to the zero page.\n");
  198. }
  199. MaybeReportNonExecRegion(sig.pc);
  200. InternalMmapVector<BufferedStackTrace> stack_buffer(1);
  201. BufferedStackTrace *stack = stack_buffer.data();
  202. stack->Reset();
  203. unwind(sig, unwind_context, stack);
  204. stack->Print();
  205. MaybeDumpInstructionBytes(sig.pc);
  206. MaybeDumpRegisters(sig.context);
  207. Printf("%s can not provide additional info.\n", SanitizerToolName);
  208. ReportErrorSummary(description, stack);
  209. }
  210. void ReportDeadlySignal(const SignalContext &sig, u32 tid,
  211. UnwindSignalStackCallbackType unwind,
  212. const void *unwind_context) {
  213. if (sig.IsStackOverflow())
  214. ReportStackOverflowImpl(sig, tid, unwind, unwind_context);
  215. else
  216. ReportDeadlySignalImpl(sig, tid, unwind, unwind_context);
  217. }
  218. void HandleDeadlySignal(void *siginfo, void *context, u32 tid,
  219. UnwindSignalStackCallbackType unwind,
  220. const void *unwind_context) {
  221. StartReportDeadlySignal();
  222. ScopedErrorReportLock rl;
  223. SignalContext sig(siginfo, context);
  224. ReportDeadlySignal(sig, tid, unwind, unwind_context);
  225. Report("ABORTING\n");
  226. Die();
  227. }
  228. #endif // !SANITIZER_FUCHSIA && !SANITIZER_GO
  229. atomic_uintptr_t ScopedErrorReportLock::reporting_thread_ = {0};
  230. StaticSpinMutex ScopedErrorReportLock::mutex_;
  231. void ScopedErrorReportLock::Lock() {
  232. uptr current = GetThreadSelf();
  233. for (;;) {
  234. uptr expected = 0;
  235. if (atomic_compare_exchange_strong(&reporting_thread_, &expected, current,
  236. memory_order_relaxed)) {
  237. // We've claimed reporting_thread so proceed.
  238. mutex_.Lock();
  239. return;
  240. }
  241. if (expected == current) {
  242. // This is either asynch signal or nested error during error reporting.
  243. // Fail simple to avoid deadlocks in Report().
  244. // Can't use Report() here because of potential deadlocks in nested
  245. // signal handlers.
  246. CatastrophicErrorWrite(SanitizerToolName,
  247. internal_strlen(SanitizerToolName));
  248. static const char msg[] = ": nested bug in the same thread, aborting.\n";
  249. CatastrophicErrorWrite(msg, sizeof(msg) - 1);
  250. internal__exit(common_flags()->exitcode);
  251. }
  252. internal_sched_yield();
  253. }
  254. }
  255. void ScopedErrorReportLock::Unlock() {
  256. mutex_.Unlock();
  257. atomic_store_relaxed(&reporting_thread_, 0);
  258. }
  259. void ScopedErrorReportLock::CheckLocked() { mutex_.CheckLocked(); }
  260. } // namespace __sanitizer