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_stacktrace.h"
17 
18 void __sanitizer::BufferedStackTrace::UnwindImpl(uptr pc, uptr bp,
19                                                  void *context,
20                                                  bool request_fast,
21                                                  u32 max_depth) {
22   if (!StackTrace::WillUseFastUnwind(request_fast)) {
23     return Unwind(max_depth, pc, bp, context, 0, 0, request_fast);
24   }
25   Unwind(max_depth, pc, 0, context, 0, 0, false);
26 }
27 
28 namespace {
29 size_t Backtrace(uintptr_t *TraceBuffer, size_t Size) {
30   __sanitizer::BufferedStackTrace Trace;
31   Trace.Reset();
32   if (Size > __sanitizer::kStackTraceMax)
33     Size = __sanitizer::kStackTraceMax;
34 
35   Trace.Unwind((__sanitizer::uptr)__builtin_return_address(0),
36                (__sanitizer::uptr)__builtin_frame_address(0),
37                /* ucontext */ nullptr,
38                /* fast unwind */ true, Size - 1);
39 
40   memcpy(TraceBuffer, Trace.trace, Trace.size * sizeof(uintptr_t));
41   return Trace.size;
42 }
43 
44 static void PrintBacktrace(uintptr_t *Trace, size_t TraceLength,
45                            gwp_asan::options::Printf_t Printf) {
46   __sanitizer::StackTrace StackTrace;
47   StackTrace.trace = reinterpret_cast<__sanitizer::uptr *>(Trace);
48   StackTrace.size = TraceLength;
49 
50   if (StackTrace.size == 0) {
51     Printf("  <unknown (does your allocator support backtracing?)>\n\n");
52     return;
53   }
54 
55   StackTrace.Print();
56 }
57 } // anonymous namespace
58 
59 namespace gwp_asan {
60 namespace options {
61 Backtrace_t getBacktraceFunction() { return Backtrace; }
62 PrintBacktrace_t getPrintBacktraceFunction() { return PrintBacktrace; }
63 } // namespace options
64 } // namespace gwp_asan
65