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 
21 void __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 
35 namespace {
36 size_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 
56 size_t Backtrace(uintptr_t *TraceBuffer, size_t Size) {
57   return BacktraceCommon(TraceBuffer, Size, nullptr);
58 }
59 
60 size_t SegvBacktrace(uintptr_t *TraceBuffer, size_t Size, void *Context) {
61   return BacktraceCommon(TraceBuffer, Size, Context);
62 }
63 
64 static void PrintBacktrace(uintptr_t *Trace, size_t TraceLength,
65                            gwp_asan::crash_handler::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   StackTrace.Print();
76 }
77 } // anonymous namespace
78 
79 namespace gwp_asan {
80 namespace options {
81 // This function is thread-compatible. It must be synchronised in respect to any
82 // other calls to getBacktraceFunction(), calls to getPrintBacktraceFunction(),
83 // and calls to either of the functions that they return. Furthermore, this may
84 // require synchronisation with any calls to sanitizer_common that use flags.
85 // Generally, this function will be called during the initialisation of the
86 // allocator, which is done in a thread-compatible manner.
87 Backtrace_t getBacktraceFunction() {
88   // The unwinder requires the default flags to be set.
89   __sanitizer::SetCommonFlagsDefaults();
90   __sanitizer::InitializeCommonFlags();
91   return Backtrace;
92 }
93 crash_handler::PrintBacktrace_t getPrintBacktraceFunction() {
94   return PrintBacktrace;
95 }
96 } // namespace options
97 
98 namespace crash_handler {
99 SegvBacktrace_t getSegvBacktraceFunction() { return SegvBacktrace; }
100 } // namespace crash_handler
101 } // namespace gwp_asan
102