13cab2bb3Spatrick //===-- sanitizer_symbolizer_markup.cpp -----------------------------------===//
23cab2bb3Spatrick //
33cab2bb3Spatrick // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
43cab2bb3Spatrick // See https://llvm.org/LICENSE.txt for license information.
53cab2bb3Spatrick // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
63cab2bb3Spatrick //
73cab2bb3Spatrick //===----------------------------------------------------------------------===//
83cab2bb3Spatrick //
93cab2bb3Spatrick // This file is shared between various sanitizers' runtime libraries.
103cab2bb3Spatrick //
113cab2bb3Spatrick // Implementation of offline markup symbolizer.
123cab2bb3Spatrick //===----------------------------------------------------------------------===//
133cab2bb3Spatrick
143cab2bb3Spatrick #include "sanitizer_platform.h"
153cab2bb3Spatrick #if SANITIZER_SYMBOLIZER_MARKUP
163cab2bb3Spatrick
173cab2bb3Spatrick #if SANITIZER_FUCHSIA
183cab2bb3Spatrick #include "sanitizer_symbolizer_fuchsia.h"
193cab2bb3Spatrick # endif
203cab2bb3Spatrick
213cab2bb3Spatrick # include <limits.h>
223cab2bb3Spatrick # include <unwind.h>
233cab2bb3Spatrick
24d89ec533Spatrick # include "sanitizer_stacktrace.h"
25d89ec533Spatrick # include "sanitizer_symbolizer.h"
26d89ec533Spatrick
273cab2bb3Spatrick namespace __sanitizer {
283cab2bb3Spatrick
293cab2bb3Spatrick // This generic support for offline symbolizing is based on the
303cab2bb3Spatrick // Fuchsia port. We don't do any actual symbolization per se.
313cab2bb3Spatrick // Instead, we emit text containing raw addresses and raw linkage
323cab2bb3Spatrick // symbol names, embedded in Fuchsia's symbolization markup format.
333cab2bb3Spatrick // Fuchsia's logging infrastructure emits enough information about
343cab2bb3Spatrick // process memory layout that a post-processing filter can do the
353cab2bb3Spatrick // symbolization and pretty-print the markup. See the spec at:
363cab2bb3Spatrick // https://fuchsia.googlesource.com/zircon/+/master/docs/symbolizer_markup.md
373cab2bb3Spatrick
383cab2bb3Spatrick // This is used by UBSan for type names, and by ASan for global variable names.
393cab2bb3Spatrick // It's expected to return a static buffer that will be reused on each call.
Demangle(const char * name)403cab2bb3Spatrick const char *Symbolizer::Demangle(const char *name) {
413cab2bb3Spatrick static char buffer[kFormatDemangleMax];
423cab2bb3Spatrick internal_snprintf(buffer, sizeof(buffer), kFormatDemangle, name);
433cab2bb3Spatrick return buffer;
443cab2bb3Spatrick }
453cab2bb3Spatrick
463cab2bb3Spatrick // This is used mostly for suppression matching. Making it work
473cab2bb3Spatrick // would enable "interceptor_via_lib" suppressions. It's also used
483cab2bb3Spatrick // once in UBSan to say "in module ..." in a message that also
493cab2bb3Spatrick // includes an address in the module, so post-processing can already
503cab2bb3Spatrick // pretty-print that so as to indicate the module.
GetModuleNameAndOffsetForPC(uptr pc,const char ** module_name,uptr * module_address)513cab2bb3Spatrick bool Symbolizer::GetModuleNameAndOffsetForPC(uptr pc, const char **module_name,
523cab2bb3Spatrick uptr *module_address) {
533cab2bb3Spatrick return false;
543cab2bb3Spatrick }
553cab2bb3Spatrick
56d89ec533Spatrick // This is mainly used by hwasan for online symbolization. This isn't needed
57d89ec533Spatrick // since hwasan can always just dump stack frames for offline symbolization.
SymbolizeFrame(uptr addr,FrameInfo * info)58d89ec533Spatrick bool Symbolizer::SymbolizeFrame(uptr addr, FrameInfo *info) { return false; }
59d89ec533Spatrick
603cab2bb3Spatrick // This is used in some places for suppression checking, which we
613cab2bb3Spatrick // don't really support for Fuchsia. It's also used in UBSan to
623cab2bb3Spatrick // identify a PC location to a function name, so we always fill in
633cab2bb3Spatrick // the function member with a string containing markup around the PC
643cab2bb3Spatrick // value.
653cab2bb3Spatrick // TODO(mcgrathr): Under SANITIZER_GO, it's currently used by TSan
663cab2bb3Spatrick // to render stack frames, but that should be changed to use
673cab2bb3Spatrick // RenderStackFrame.
SymbolizePC(uptr addr)683cab2bb3Spatrick SymbolizedStack *Symbolizer::SymbolizePC(uptr addr) {
693cab2bb3Spatrick SymbolizedStack *s = SymbolizedStack::New(addr);
703cab2bb3Spatrick char buffer[kFormatFunctionMax];
713cab2bb3Spatrick internal_snprintf(buffer, sizeof(buffer), kFormatFunction, addr);
723cab2bb3Spatrick s->info.function = internal_strdup(buffer);
733cab2bb3Spatrick return s;
743cab2bb3Spatrick }
753cab2bb3Spatrick
763cab2bb3Spatrick // Always claim we succeeded, so that RenderDataInfo will be called.
SymbolizeData(uptr addr,DataInfo * info)773cab2bb3Spatrick bool Symbolizer::SymbolizeData(uptr addr, DataInfo *info) {
783cab2bb3Spatrick info->Clear();
793cab2bb3Spatrick info->start = addr;
803cab2bb3Spatrick return true;
813cab2bb3Spatrick }
823cab2bb3Spatrick
833cab2bb3Spatrick // We ignore the format argument to __sanitizer_symbolize_global.
RenderData(InternalScopedString * buffer,const char * format,const DataInfo * DI,const char * strip_path_prefix)843cab2bb3Spatrick void RenderData(InternalScopedString *buffer, const char *format,
853cab2bb3Spatrick const DataInfo *DI, const char *strip_path_prefix) {
863cab2bb3Spatrick buffer->append(kFormatData, DI->start);
873cab2bb3Spatrick }
883cab2bb3Spatrick
RenderNeedsSymbolization(const char * format)89d89ec533Spatrick bool RenderNeedsSymbolization(const char *format) { return false; }
90d89ec533Spatrick
913cab2bb3Spatrick // We don't support the stack_trace_format flag at all.
RenderFrame(InternalScopedString * buffer,const char * format,int frame_no,uptr address,const AddressInfo * info,bool vs_style,const char * strip_path_prefix,const char * strip_func_prefix)923cab2bb3Spatrick void RenderFrame(InternalScopedString *buffer, const char *format, int frame_no,
93d89ec533Spatrick uptr address, const AddressInfo *info, bool vs_style,
943cab2bb3Spatrick const char *strip_path_prefix, const char *strip_func_prefix) {
95d89ec533Spatrick CHECK(!RenderNeedsSymbolization(format));
96d89ec533Spatrick buffer->append(kFormatFrame, frame_no, address);
973cab2bb3Spatrick }
983cab2bb3Spatrick
PlatformInit()993cab2bb3Spatrick Symbolizer *Symbolizer::PlatformInit() {
1003cab2bb3Spatrick return new (symbolizer_allocator_) Symbolizer({});
1013cab2bb3Spatrick }
1023cab2bb3Spatrick
LateInitialize()103*810390e3Srobert void Symbolizer::LateInitialize() { Symbolizer::GetOrInit(); }
1043cab2bb3Spatrick
StartReportDeadlySignal()1053cab2bb3Spatrick void StartReportDeadlySignal() {}
ReportDeadlySignal(const SignalContext & sig,u32 tid,UnwindSignalStackCallbackType unwind,const void * unwind_context)1063cab2bb3Spatrick void ReportDeadlySignal(const SignalContext &sig, u32 tid,
1073cab2bb3Spatrick UnwindSignalStackCallbackType unwind,
1083cab2bb3Spatrick const void *unwind_context) {}
1093cab2bb3Spatrick
1103cab2bb3Spatrick #if SANITIZER_CAN_SLOW_UNWIND
1113cab2bb3Spatrick struct UnwindTraceArg {
1123cab2bb3Spatrick BufferedStackTrace *stack;
1133cab2bb3Spatrick u32 max_depth;
1143cab2bb3Spatrick };
1153cab2bb3Spatrick
Unwind_Trace(struct _Unwind_Context * ctx,void * param)1163cab2bb3Spatrick _Unwind_Reason_Code Unwind_Trace(struct _Unwind_Context *ctx, void *param) {
1173cab2bb3Spatrick UnwindTraceArg *arg = static_cast<UnwindTraceArg *>(param);
1183cab2bb3Spatrick CHECK_LT(arg->stack->size, arg->max_depth);
1193cab2bb3Spatrick uptr pc = _Unwind_GetIP(ctx);
1203cab2bb3Spatrick if (pc < PAGE_SIZE) return _URC_NORMAL_STOP;
1213cab2bb3Spatrick arg->stack->trace_buffer[arg->stack->size++] = pc;
1223cab2bb3Spatrick return (arg->stack->size == arg->max_depth ? _URC_NORMAL_STOP
1233cab2bb3Spatrick : _URC_NO_REASON);
1243cab2bb3Spatrick }
1253cab2bb3Spatrick
UnwindSlow(uptr pc,u32 max_depth)1263cab2bb3Spatrick void BufferedStackTrace::UnwindSlow(uptr pc, u32 max_depth) {
1273cab2bb3Spatrick CHECK_GE(max_depth, 2);
1283cab2bb3Spatrick size = 0;
1293cab2bb3Spatrick UnwindTraceArg arg = {this, Min(max_depth + 1, kStackTraceMax)};
1303cab2bb3Spatrick _Unwind_Backtrace(Unwind_Trace, &arg);
1313cab2bb3Spatrick CHECK_GT(size, 0);
1323cab2bb3Spatrick // We need to pop a few frames so that pc is on top.
1333cab2bb3Spatrick uptr to_pop = LocatePcInTrace(pc);
1343cab2bb3Spatrick // trace_buffer[0] belongs to the current function so we always pop it,
1353cab2bb3Spatrick // unless there is only 1 frame in the stack trace (1 frame is always better
1363cab2bb3Spatrick // than 0!).
1373cab2bb3Spatrick PopStackFrames(Min(to_pop, static_cast<uptr>(1)));
1383cab2bb3Spatrick trace_buffer[0] = pc;
1393cab2bb3Spatrick }
1403cab2bb3Spatrick
UnwindSlow(uptr pc,void * context,u32 max_depth)1413cab2bb3Spatrick void BufferedStackTrace::UnwindSlow(uptr pc, void *context, u32 max_depth) {
1423cab2bb3Spatrick CHECK(context);
1433cab2bb3Spatrick CHECK_GE(max_depth, 2);
1443cab2bb3Spatrick UNREACHABLE("signal context doesn't exist");
1453cab2bb3Spatrick }
1463cab2bb3Spatrick #endif // SANITIZER_CAN_SLOW_UNWIND
1473cab2bb3Spatrick
1483cab2bb3Spatrick } // namespace __sanitizer
1493cab2bb3Spatrick
1503cab2bb3Spatrick #endif // SANITIZER_SYMBOLIZER_MARKUP
151