sanitizer_symbolizer_markup.cpp 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. //===-- sanitizer_symbolizer_markup.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 various sanitizers' runtime libraries.
  10. //
  11. // Implementation of offline markup symbolizer.
  12. //===----------------------------------------------------------------------===//
  13. #include "sanitizer_platform.h"
  14. #if SANITIZER_SYMBOLIZER_MARKUP
  15. #if SANITIZER_FUCHSIA
  16. #include "sanitizer_symbolizer_fuchsia.h"
  17. # endif
  18. # include <limits.h>
  19. # include <unwind.h>
  20. # include "sanitizer_stacktrace.h"
  21. # include "sanitizer_symbolizer.h"
  22. namespace __sanitizer {
  23. // This generic support for offline symbolizing is based on the
  24. // Fuchsia port. We don't do any actual symbolization per se.
  25. // Instead, we emit text containing raw addresses and raw linkage
  26. // symbol names, embedded in Fuchsia's symbolization markup format.
  27. // Fuchsia's logging infrastructure emits enough information about
  28. // process memory layout that a post-processing filter can do the
  29. // symbolization and pretty-print the markup. See the spec at:
  30. // https://fuchsia.googlesource.com/zircon/+/master/docs/symbolizer_markup.md
  31. // This is used by UBSan for type names, and by ASan for global variable names.
  32. // It's expected to return a static buffer that will be reused on each call.
  33. const char *Symbolizer::Demangle(const char *name) {
  34. static char buffer[kFormatDemangleMax];
  35. internal_snprintf(buffer, sizeof(buffer), kFormatDemangle, name);
  36. return buffer;
  37. }
  38. // This is used mostly for suppression matching. Making it work
  39. // would enable "interceptor_via_lib" suppressions. It's also used
  40. // once in UBSan to say "in module ..." in a message that also
  41. // includes an address in the module, so post-processing can already
  42. // pretty-print that so as to indicate the module.
  43. bool Symbolizer::GetModuleNameAndOffsetForPC(uptr pc, const char **module_name,
  44. uptr *module_address) {
  45. return false;
  46. }
  47. // This is mainly used by hwasan for online symbolization. This isn't needed
  48. // since hwasan can always just dump stack frames for offline symbolization.
  49. bool Symbolizer::SymbolizeFrame(uptr addr, FrameInfo *info) { return false; }
  50. // This is used in some places for suppression checking, which we
  51. // don't really support for Fuchsia. It's also used in UBSan to
  52. // identify a PC location to a function name, so we always fill in
  53. // the function member with a string containing markup around the PC
  54. // value.
  55. // TODO(mcgrathr): Under SANITIZER_GO, it's currently used by TSan
  56. // to render stack frames, but that should be changed to use
  57. // RenderStackFrame.
  58. SymbolizedStack *Symbolizer::SymbolizePC(uptr addr) {
  59. SymbolizedStack *s = SymbolizedStack::New(addr);
  60. char buffer[kFormatFunctionMax];
  61. internal_snprintf(buffer, sizeof(buffer), kFormatFunction, addr);
  62. s->info.function = internal_strdup(buffer);
  63. return s;
  64. }
  65. // Always claim we succeeded, so that RenderDataInfo will be called.
  66. bool Symbolizer::SymbolizeData(uptr addr, DataInfo *info) {
  67. info->Clear();
  68. info->start = addr;
  69. return true;
  70. }
  71. // We ignore the format argument to __sanitizer_symbolize_global.
  72. void RenderData(InternalScopedString *buffer, const char *format,
  73. const DataInfo *DI, const char *strip_path_prefix) {
  74. buffer->append(kFormatData, DI->start);
  75. }
  76. bool RenderNeedsSymbolization(const char *format) { return false; }
  77. // We don't support the stack_trace_format flag at all.
  78. void RenderFrame(InternalScopedString *buffer, const char *format, int frame_no,
  79. uptr address, const AddressInfo *info, bool vs_style,
  80. const char *strip_path_prefix, const char *strip_func_prefix) {
  81. CHECK(!RenderNeedsSymbolization(format));
  82. buffer->append(kFormatFrame, frame_no, address);
  83. }
  84. Symbolizer *Symbolizer::PlatformInit() {
  85. return new (symbolizer_allocator_) Symbolizer({});
  86. }
  87. void Symbolizer::LateInitialize() { Symbolizer::GetOrInit(); }
  88. void StartReportDeadlySignal() {}
  89. void ReportDeadlySignal(const SignalContext &sig, u32 tid,
  90. UnwindSignalStackCallbackType unwind,
  91. const void *unwind_context) {}
  92. #if SANITIZER_CAN_SLOW_UNWIND
  93. struct UnwindTraceArg {
  94. BufferedStackTrace *stack;
  95. u32 max_depth;
  96. };
  97. _Unwind_Reason_Code Unwind_Trace(struct _Unwind_Context *ctx, void *param) {
  98. UnwindTraceArg *arg = static_cast<UnwindTraceArg *>(param);
  99. CHECK_LT(arg->stack->size, arg->max_depth);
  100. uptr pc = _Unwind_GetIP(ctx);
  101. if (pc < PAGE_SIZE) return _URC_NORMAL_STOP;
  102. arg->stack->trace_buffer[arg->stack->size++] = pc;
  103. return (arg->stack->size == arg->max_depth ? _URC_NORMAL_STOP
  104. : _URC_NO_REASON);
  105. }
  106. void BufferedStackTrace::UnwindSlow(uptr pc, u32 max_depth) {
  107. CHECK_GE(max_depth, 2);
  108. size = 0;
  109. UnwindTraceArg arg = {this, Min(max_depth + 1, kStackTraceMax)};
  110. _Unwind_Backtrace(Unwind_Trace, &arg);
  111. CHECK_GT(size, 0);
  112. // We need to pop a few frames so that pc is on top.
  113. uptr to_pop = LocatePcInTrace(pc);
  114. // trace_buffer[0] belongs to the current function so we always pop it,
  115. // unless there is only 1 frame in the stack trace (1 frame is always better
  116. // than 0!).
  117. PopStackFrames(Min(to_pop, static_cast<uptr>(1)));
  118. trace_buffer[0] = pc;
  119. }
  120. void BufferedStackTrace::UnwindSlow(uptr pc, void *context, u32 max_depth) {
  121. CHECK(context);
  122. CHECK_GE(max_depth, 2);
  123. UNREACHABLE("signal context doesn't exist");
  124. }
  125. #endif // SANITIZER_CAN_SLOW_UNWIND
  126. } // namespace __sanitizer
  127. #endif // SANITIZER_SYMBOLIZER_MARKUP