1//===-- backtrace_sanitizer_common.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#include <assert.h>
10#include <stddef.h>
11#include <stdint.h>
12#include <string.h>
13
14#include "gwp_asan/optional/backtrace.h"
15#include "gwp_asan/options.h"
16#include "sanitizer_common/sanitizer_common.h"
17#include "sanitizer_common/sanitizer_flag_parser.h"
18#include "sanitizer_common/sanitizer_flags.h"
19#include "sanitizer_common/sanitizer_stacktrace.h"
20
21void __sanitizer::BufferedStackTrace::UnwindImpl(uptr pc, uptr bp,
22                                                 void *context,
23                                                 bool request_fast,
24                                                 u32 max_depth) {
25  if (!StackTrace::WillUseFastUnwind(request_fast))
26    return Unwind(max_depth, pc, 0, context, 0, 0, false);
27
28  uptr top = 0;
29  uptr bottom = 0;
30  GetThreadStackTopAndBottom(/*at_initialization*/ false, &top, &bottom);
31
32  return Unwind(max_depth, pc, bp, context, top, bottom, request_fast);
33}
34
35namespace {
36size_t BacktraceCommon(uintptr_t *TraceBuffer, size_t Size, void *Context) {
37  // Use the slow sanitizer unwinder in the segv handler. Fast frame pointer
38  // unwinders can end up dropping frames because the kernel sigreturn() frame's
39  // return address is the return address at time of fault. This has the result
40  // of never actually capturing the PC where the signal was raised.
41  bool UseFastUnwind = (Context == nullptr);
42
43  __sanitizer::BufferedStackTrace Trace;
44  Trace.Reset();
45  if (Size > __sanitizer::kStackTraceMax)
46    Size = __sanitizer::kStackTraceMax;
47
48  Trace.Unwind((__sanitizer::uptr)__builtin_return_address(0),
49               (__sanitizer::uptr)__builtin_frame_address(0), Context,
50               UseFastUnwind, Size - 1);
51
52  memcpy(TraceBuffer, Trace.trace, Trace.size * sizeof(uintptr_t));
53  return Trace.size;
54}
55
56size_t Backtrace(uintptr_t *TraceBuffer, size_t Size) {
57  return BacktraceCommon(TraceBuffer, Size, nullptr);
58}
59
60size_t SegvBacktrace(uintptr_t *TraceBuffer, size_t Size, void *Context) {
61  return BacktraceCommon(TraceBuffer, Size, Context);
62}
63
64static void PrintBacktrace(uintptr_t *Trace, size_t TraceLength,
65                           gwp_asan::Printf_t Printf) {
66  __sanitizer::StackTrace StackTrace;
67  StackTrace.trace = reinterpret_cast<__sanitizer::uptr *>(Trace);
68  StackTrace.size = TraceLength;
69
70  if (StackTrace.size == 0) {
71    Printf("  <unknown (does your allocator support backtracing?)>\n\n");
72    return;
73  }
74
75  __sanitizer::InternalScopedString buffer;
76  StackTrace.PrintTo(&buffer);
77  Printf("%s\n", buffer.data());
78}
79} // anonymous namespace
80
81namespace gwp_asan {
82namespace backtrace {
83
84// This function is thread-compatible. It must be synchronised in respect to any
85// other calls to getBacktraceFunction(), calls to getPrintBacktraceFunction(),
86// and calls to either of the functions that they return. Furthermore, this may
87// require synchronisation with any calls to sanitizer_common that use flags.
88// Generally, this function will be called during the initialisation of the
89// allocator, which is done in a thread-compatible manner.
90options::Backtrace_t getBacktraceFunction() {
91  // The unwinder requires the default flags to be set.
92  __sanitizer::SetCommonFlagsDefaults();
93  __sanitizer::InitializeCommonFlags();
94  return Backtrace;
95}
96
97PrintBacktrace_t getPrintBacktraceFunction() { return PrintBacktrace; }
98SegvBacktrace_t getSegvBacktraceFunction() { return SegvBacktrace; }
99
100} // namespace backtrace
101} // namespace gwp_asan
102