xref: /llvm-project/compiler-rt/lib/gwp_asan/optional/backtrace_sanitizer_common.cpp (revision f529a9f324a8eb4a0f2c5e662b6c57a6a775d62f)
17339ca27SMitch Phillips //===-- backtrace_sanitizer_common.cpp --------------------------*- C++ -*-===//
27339ca27SMitch Phillips //
37339ca27SMitch Phillips // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
47339ca27SMitch Phillips // See https://llvm.org/LICENSE.txt for license information.
57339ca27SMitch Phillips // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
67339ca27SMitch Phillips //
77339ca27SMitch Phillips //===----------------------------------------------------------------------===//
87339ca27SMitch Phillips 
97339ca27SMitch Phillips #include <assert.h>
107339ca27SMitch Phillips #include <stddef.h>
117339ca27SMitch Phillips #include <stdint.h>
127339ca27SMitch Phillips #include <string.h>
137339ca27SMitch Phillips 
147339ca27SMitch Phillips #include "gwp_asan/optional/backtrace.h"
157339ca27SMitch Phillips #include "gwp_asan/options.h"
1627d69b2fSMitch Phillips #include "sanitizer_common/sanitizer_common.h"
1727d69b2fSMitch Phillips #include "sanitizer_common/sanitizer_flag_parser.h"
1827d69b2fSMitch Phillips #include "sanitizer_common/sanitizer_flags.h"
197339ca27SMitch Phillips #include "sanitizer_common/sanitizer_stacktrace.h"
207339ca27SMitch Phillips 
UnwindImpl(uptr pc,uptr bp,void * context,bool request_fast,u32 max_depth)217339ca27SMitch Phillips void __sanitizer::BufferedStackTrace::UnwindImpl(uptr pc, uptr bp,
227339ca27SMitch Phillips                                                  void *context,
237339ca27SMitch Phillips                                                  bool request_fast,
247339ca27SMitch Phillips                                                  u32 max_depth) {
254f029d1bSMitch Phillips   if (!StackTrace::WillUseFastUnwind(request_fast))
264f029d1bSMitch Phillips     return Unwind(max_depth, pc, 0, context, 0, 0, false);
274f029d1bSMitch Phillips 
284f029d1bSMitch Phillips   uptr top = 0;
294f029d1bSMitch Phillips   uptr bottom = 0;
304f029d1bSMitch Phillips   GetThreadStackTopAndBottom(/*at_initialization*/ false, &top, &bottom);
314f029d1bSMitch Phillips 
324f029d1bSMitch Phillips   return Unwind(max_depth, pc, bp, context, top, bottom, request_fast);
337339ca27SMitch Phillips }
347339ca27SMitch Phillips 
357339ca27SMitch Phillips namespace {
BacktraceCommon(uintptr_t * TraceBuffer,size_t Size,void * Context)364f029d1bSMitch Phillips size_t BacktraceCommon(uintptr_t *TraceBuffer, size_t Size, void *Context) {
374f029d1bSMitch Phillips   // Use the slow sanitizer unwinder in the segv handler. Fast frame pointer
384f029d1bSMitch Phillips   // unwinders can end up dropping frames because the kernel sigreturn() frame's
394f029d1bSMitch Phillips   // return address is the return address at time of fault. This has the result
404f029d1bSMitch Phillips   // of never actually capturing the PC where the signal was raised.
414f029d1bSMitch Phillips   bool UseFastUnwind = (Context == nullptr);
424f029d1bSMitch Phillips 
437339ca27SMitch Phillips   __sanitizer::BufferedStackTrace Trace;
447339ca27SMitch Phillips   Trace.Reset();
457339ca27SMitch Phillips   if (Size > __sanitizer::kStackTraceMax)
467339ca27SMitch Phillips     Size = __sanitizer::kStackTraceMax;
477339ca27SMitch Phillips 
487339ca27SMitch Phillips   Trace.Unwind((__sanitizer::uptr)__builtin_return_address(0),
494f029d1bSMitch Phillips                (__sanitizer::uptr)__builtin_frame_address(0), Context,
504f029d1bSMitch Phillips                UseFastUnwind, Size - 1);
517339ca27SMitch Phillips 
527339ca27SMitch Phillips   memcpy(TraceBuffer, Trace.trace, Trace.size * sizeof(uintptr_t));
53352d1b59SMitch Phillips   return Trace.size;
547339ca27SMitch Phillips }
557339ca27SMitch Phillips 
Backtrace(uintptr_t * TraceBuffer,size_t Size)564f029d1bSMitch Phillips size_t Backtrace(uintptr_t *TraceBuffer, size_t Size) {
574f029d1bSMitch Phillips   return BacktraceCommon(TraceBuffer, Size, nullptr);
584f029d1bSMitch Phillips }
594f029d1bSMitch Phillips 
SegvBacktrace(uintptr_t * TraceBuffer,size_t Size,void * Context)604f029d1bSMitch Phillips size_t SegvBacktrace(uintptr_t *TraceBuffer, size_t Size, void *Context) {
614f029d1bSMitch Phillips   return BacktraceCommon(TraceBuffer, Size, Context);
624f029d1bSMitch Phillips }
634f029d1bSMitch Phillips 
PrintBacktrace(uintptr_t * Trace,size_t TraceLength,gwp_asan::Printf_t Printf)64352d1b59SMitch Phillips static void PrintBacktrace(uintptr_t *Trace, size_t TraceLength,
65a8520f69SMitch Phillips                            gwp_asan::Printf_t Printf) {
667339ca27SMitch Phillips   __sanitizer::StackTrace StackTrace;
677339ca27SMitch Phillips   StackTrace.trace = reinterpret_cast<__sanitizer::uptr *>(Trace);
68352d1b59SMitch Phillips   StackTrace.size = TraceLength;
697339ca27SMitch Phillips 
707339ca27SMitch Phillips   if (StackTrace.size == 0) {
717339ca27SMitch Phillips     Printf("  <unknown (does your allocator support backtracing?)>\n\n");
727339ca27SMitch Phillips     return;
737339ca27SMitch Phillips   }
747339ca27SMitch Phillips 
75*f529a9f3SMitch Phillips   __sanitizer::InternalScopedString buffer;
76*f529a9f3SMitch Phillips   StackTrace.PrintTo(&buffer);
77*f529a9f3SMitch Phillips   Printf("%s\n", buffer.data());
787339ca27SMitch Phillips }
797339ca27SMitch Phillips } // anonymous namespace
807339ca27SMitch Phillips 
817339ca27SMitch Phillips namespace gwp_asan {
82a8520f69SMitch Phillips namespace backtrace {
83a8520f69SMitch Phillips 
8427d69b2fSMitch Phillips // This function is thread-compatible. It must be synchronised in respect to any
8527d69b2fSMitch Phillips // other calls to getBacktraceFunction(), calls to getPrintBacktraceFunction(),
8627d69b2fSMitch Phillips // and calls to either of the functions that they return. Furthermore, this may
8727d69b2fSMitch Phillips // require synchronisation with any calls to sanitizer_common that use flags.
8827d69b2fSMitch Phillips // Generally, this function will be called during the initialisation of the
8927d69b2fSMitch Phillips // allocator, which is done in a thread-compatible manner.
getBacktraceFunction()90a8520f69SMitch Phillips options::Backtrace_t getBacktraceFunction() {
9127d69b2fSMitch Phillips   // The unwinder requires the default flags to be set.
9227d69b2fSMitch Phillips   __sanitizer::SetCommonFlagsDefaults();
9327d69b2fSMitch Phillips   __sanitizer::InitializeCommonFlags();
9427d69b2fSMitch Phillips   return Backtrace;
9527d69b2fSMitch Phillips }
964f029d1bSMitch Phillips 
getPrintBacktraceFunction()97a8520f69SMitch Phillips PrintBacktrace_t getPrintBacktraceFunction() { return PrintBacktrace; }
getSegvBacktraceFunction()984f029d1bSMitch Phillips SegvBacktrace_t getSegvBacktraceFunction() { return SegvBacktrace; }
99a8520f69SMitch Phillips 
100a8520f69SMitch Phillips } // namespace backtrace
1017339ca27SMitch Phillips } // namespace gwp_asan
102