1//===-- sanitizer_stacktrace.h ----------------------------------*- 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// This file is shared between AddressSanitizer and ThreadSanitizer
10// run-time libraries.
11//===----------------------------------------------------------------------===//
12#ifndef SANITIZER_STACKTRACE_H
13#define SANITIZER_STACKTRACE_H
14
15#include "sanitizer_internal_defs.h"
16
17namespace __sanitizer {
18
19struct BufferedStackTrace;
20
21static const u32 kStackTraceMax = 256;
22
23#if SANITIZER_LINUX && defined(__mips__)
24# define SANITIZER_CAN_FAST_UNWIND 0
25#elif SANITIZER_WINDOWS
26# define SANITIZER_CAN_FAST_UNWIND 0
27#elif SANITIZER_OPENBSD
28# define SANITIZER_CAN_FAST_UNWIND 0
29#else
30# define SANITIZER_CAN_FAST_UNWIND 1
31#endif
32
33// Fast unwind is the only option on Mac for now; we will need to
34// revisit this macro when slow unwind works on Mac, see
35// https://github.com/google/sanitizers/issues/137
36#if SANITIZER_MAC || SANITIZER_OPENBSD || SANITIZER_RTEMS
37# define SANITIZER_CAN_SLOW_UNWIND 0
38#else
39# define SANITIZER_CAN_SLOW_UNWIND 1
40#endif
41
42struct StackTrace {
43  const uptr *trace;
44  u32 size;
45  u32 tag;
46
47  static const int TAG_UNKNOWN = 0;
48  static const int TAG_ALLOC = 1;
49  static const int TAG_DEALLOC = 2;
50  static const int TAG_CUSTOM = 100; // Tool specific tags start here.
51
52  StackTrace() : trace(nullptr), size(0), tag(0) {}
53  StackTrace(const uptr *trace, u32 size) : trace(trace), size(size), tag(0) {}
54  StackTrace(const uptr *trace, u32 size, u32 tag)
55      : trace(trace), size(size), tag(tag) {}
56
57  // Prints a symbolized stacktrace, followed by an empty line.
58  void Print() const;
59
60  static bool WillUseFastUnwind(bool request_fast_unwind) {
61    if (!SANITIZER_CAN_FAST_UNWIND)
62      return false;
63    if (!SANITIZER_CAN_SLOW_UNWIND)
64      return true;
65    return request_fast_unwind;
66  }
67
68  static uptr GetCurrentPc();
69  static inline uptr GetPreviousInstructionPc(uptr pc);
70  static uptr GetNextInstructionPc(uptr pc);
71  typedef bool (*SymbolizeCallback)(const void *pc, char *out_buffer,
72                                    int out_size);
73};
74
75// Performance-critical, must be in the header.
76ALWAYS_INLINE
77uptr StackTrace::GetPreviousInstructionPc(uptr pc) {
78#if defined(__arm__)
79  // T32 (Thumb) branch instructions might be 16 or 32 bit long,
80  // so we return (pc-2) in that case in order to be safe.
81  // For A32 mode we return (pc-4) because all instructions are 32 bit long.
82  return (pc - 3) & (~1);
83#elif defined(__powerpc__) || defined(__powerpc64__) || defined(__aarch64__)
84  // PCs are always 4 byte aligned.
85  return pc - 4;
86#elif defined(__sparc__) || defined(__mips__)
87  return pc - 8;
88#else
89  return pc - 1;
90#endif
91}
92
93// StackTrace that owns the buffer used to store the addresses.
94struct BufferedStackTrace : public StackTrace {
95  uptr trace_buffer[kStackTraceMax];
96  uptr top_frame_bp;  // Optional bp of a top frame.
97
98  BufferedStackTrace() : StackTrace(trace_buffer, 0), top_frame_bp(0) {}
99
100  void Init(const uptr *pcs, uptr cnt, uptr extra_top_pc = 0);
101
102  // Get the stack trace with the given pc and bp.
103  // The pc will be in the position 0 of the resulting stack trace.
104  // The bp may refer to the current frame or to the caller's frame.
105  void Unwind(uptr pc, uptr bp, void *context, bool request_fast,
106              u32 max_depth = kStackTraceMax) {
107    top_frame_bp = (max_depth > 0) ? bp : 0;
108    // Small max_depth optimization
109    if (max_depth <= 1) {
110      if (max_depth == 1)
111        trace_buffer[0] = pc;
112      size = max_depth;
113      return;
114    }
115    UnwindImpl(pc, bp, context, request_fast, max_depth);
116  }
117
118  void Unwind(u32 max_depth, uptr pc, uptr bp, void *context, uptr stack_top,
119              uptr stack_bottom, bool request_fast_unwind);
120
121  void Reset() {
122    *static_cast<StackTrace *>(this) = StackTrace(trace_buffer, 0);
123    top_frame_bp = 0;
124  }
125
126 private:
127  // Every runtime defines its own implementation of this method
128  void UnwindImpl(uptr pc, uptr bp, void *context, bool request_fast,
129                  u32 max_depth);
130
131  // UnwindFast/Slow have platform-specific implementations
132  void UnwindFast(uptr pc, uptr bp, uptr stack_top, uptr stack_bottom,
133                  u32 max_depth);
134  void UnwindSlow(uptr pc, u32 max_depth);
135  void UnwindSlow(uptr pc, void *context, u32 max_depth);
136
137  void PopStackFrames(uptr count);
138  uptr LocatePcInTrace(uptr pc);
139
140  BufferedStackTrace(const BufferedStackTrace &) = delete;
141  void operator=(const BufferedStackTrace &) = delete;
142
143  friend class FastUnwindTest;
144};
145
146// Check if given pointer points into allocated stack area.
147static inline bool IsValidFrame(uptr frame, uptr stack_top, uptr stack_bottom) {
148  return frame > stack_bottom && frame < stack_top - 2 * sizeof (uhwptr);
149}
150
151}  // namespace __sanitizer
152
153// Use this macro if you want to print stack trace with the caller
154// of the current function in the top frame.
155#define GET_CALLER_PC_BP \
156  uptr bp = GET_CURRENT_FRAME();              \
157  uptr pc = GET_CALLER_PC();
158
159#define GET_CALLER_PC_BP_SP \
160  GET_CALLER_PC_BP;                           \
161  uptr local_stack;                           \
162  uptr sp = (uptr)&local_stack
163
164// Use this macro if you want to print stack trace with the current
165// function in the top frame.
166#define GET_CURRENT_PC_BP \
167  uptr bp = GET_CURRENT_FRAME();              \
168  uptr pc = StackTrace::GetCurrentPc()
169
170#define GET_CURRENT_PC_BP_SP \
171  GET_CURRENT_PC_BP;                          \
172  uptr local_stack;                           \
173  uptr sp = (uptr)&local_stack
174
175
176#endif  // SANITIZER_STACKTRACE_H
177