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_common.h"
16#include "sanitizer_internal_defs.h"
17#include "sanitizer_platform.h"
18
19namespace __sanitizer {
20
21struct BufferedStackTrace;
22
23static const u32 kStackTraceMax = 255;
24
25#if SANITIZER_LINUX && \
26 (defined(__mips__) || defined(__alpha__) || defined(__hppa__))
27# define SANITIZER_CAN_FAST_UNWIND 0
28#elif SANITIZER_WINDOWS
29# define SANITIZER_CAN_FAST_UNWIND 0
30#else
31# define SANITIZER_CAN_FAST_UNWIND 1
32#endif
33
34// Fast unwind is the only option on Mac for now; we will need to
35// revisit this macro when slow unwind works on Mac, see
36// https://github.com/google/sanitizers/issues/137
37#if SANITIZER_APPLE
38# define SANITIZER_CAN_SLOW_UNWIND 0
39#else
40# define SANITIZER_CAN_SLOW_UNWIND 1
41#endif
42
43struct StackTrace {
44 const uptr *trace;
45 u32 size;
46 u32 tag;
47
48 static const int TAG_UNKNOWN = 0;
49 static const int TAG_ALLOC = 1;
50 static const int TAG_DEALLOC = 2;
51 static const int TAG_CUSTOM = 100; // Tool specific tags start here.
52
53 StackTrace() : trace(nullptr), size(0), tag(0) {}
54 StackTrace(const uptr *trace, u32 size) : trace(trace), size(size), tag(0) {}
55 StackTrace(const uptr *trace, u32 size, u32 tag)
56 : trace(trace), size(size), tag(tag) {}
57
58 // Prints a symbolized stacktrace, followed by an empty line.
59 void Print() const;
60
61 // Prints a symbolized stacktrace to the output string, followed by an empty
62 // line.
63 void PrintTo(InternalScopedString *output) const;
64
65 // Prints a symbolized stacktrace to the output buffer, followed by an empty
66 // line. Returns the number of symbols that should have been written to buffer
67 // (not including trailing '\0'). Thus, the string is truncated iff return
68 // value is not less than "out_buf_size".
69 uptr PrintTo(char *out_buf, uptr out_buf_size) const;
70
71 static bool WillUseFastUnwind(bool request_fast_unwind) {
72 if (!SANITIZER_CAN_FAST_UNWIND)
73 return false;
74 if (!SANITIZER_CAN_SLOW_UNWIND)
75 return true;
76 return request_fast_unwind;
77 }
78
79 static uptr GetCurrentPc();
80 static inline uptr GetPreviousInstructionPc(uptr pc);
81 static uptr GetNextInstructionPc(uptr pc);
82};
83
84// Performance-critical, must be in the header.
85ALWAYS_INLINE
86uptr StackTrace::GetPreviousInstructionPc(uptr pc) {
87#if defined(__arm__)
88 // T32 (Thumb) branch instructions might be 16 or 32 bit long,
89 // so we return (pc-2) in that case in order to be safe.
90 // For A32 mode we return (pc-4) because all instructions are 32 bit long.
91 return (pc - 3) & (~1);
92#elif defined(__sparc__) || defined(__mips__)
93 return pc - 8;
94#elif SANITIZER_RISCV64
95 // RV-64 has variable instruction length...
96 // C extensions give us 2-byte instructions
97 // RV-64 has 4-byte instructions
98 // + RISC-V architecture allows instructions up to 8 bytes
99 // It seems difficult to figure out the exact instruction length -
100 // pc - 2 seems like a safe option for the purposes of stack tracing
101 return pc - 2;
102#elif SANITIZER_S390 || SANITIZER_I386 || SANITIZER_X32 || SANITIZER_X64
103 return pc - 1;
104#else
105 return pc - 4;
106#endif
107}
108
109// StackTrace that owns the buffer used to store the addresses.
110struct BufferedStackTrace : public StackTrace {
111 uptr trace_buffer[kStackTraceMax];
112 uptr top_frame_bp; // Optional bp of a top frame.
113
114 BufferedStackTrace() : StackTrace(trace_buffer, 0), top_frame_bp(0) {}
115
116 void Init(const uptr *pcs, uptr cnt, uptr extra_top_pc = 0);
117
118 // Get the stack trace with the given pc and bp.
119 // The pc will be in the position 0 of the resulting stack trace.
120 // The bp may refer to the current frame or to the caller's frame.
121 void Unwind(uptr pc, uptr bp, void *context, bool request_fast,
122 u32 max_depth = kStackTraceMax) {
123 top_frame_bp = (max_depth > 0) ? bp : 0;
124 // Small max_depth optimization
125 if (max_depth <= 1) {
126 if (max_depth == 1)
127 trace_buffer[0] = pc;
128 size = max_depth;
129 return;
130 }
131 UnwindImpl(pc, bp, context, request_fast, max_depth);
132 }
133
134 void Unwind(u32 max_depth, uptr pc, uptr bp, void *context, uptr stack_top,
135 uptr stack_bottom, bool request_fast_unwind);
136
137 void Reset() {
138 *static_cast<StackTrace *>(this) = StackTrace(trace_buffer, 0);
139 top_frame_bp = 0;
140 }
141
142 private:
143 // Every runtime defines its own implementation of this method
144 void UnwindImpl(uptr pc, uptr bp, void *context, bool request_fast,
145 u32 max_depth);
146
147 // UnwindFast/Slow have platform-specific implementations
148 void UnwindFast(uptr pc, uptr bp, uptr stack_top, uptr stack_bottom,
149 u32 max_depth);
150 void UnwindSlow(uptr pc, u32 max_depth);
151 void UnwindSlow(uptr pc, void *context, u32 max_depth);
152
153 void PopStackFrames(uptr count);
154 uptr LocatePcInTrace(uptr pc);
155
156 BufferedStackTrace(const BufferedStackTrace &) = delete;
157 void operator=(const BufferedStackTrace &) = delete;
158
159 friend class FastUnwindTest;
160};
161
162#if defined(__s390x__)
163static const uptr kFrameSize = 160;
164#elif defined(__s390__)
165static const uptr kFrameSize = 96;
166#else
167static const uptr kFrameSize = 2 * sizeof(uhwptr);
168#endif
169
170// Check if given pointer points into allocated stack area.
171static inline bool IsValidFrame(uptr frame, uptr stack_top, uptr stack_bottom) {
172 return frame > stack_bottom && frame < stack_top - kFrameSize;
173}
174
175} // namespace __sanitizer
176
177// Use this macro if you want to print stack trace with the caller
178// of the current function in the top frame.
179#define GET_CALLER_PC_BP \
180 uptr bp = GET_CURRENT_FRAME(); \
181 uptr pc = GET_CALLER_PC();
182
183#define GET_CALLER_PC_BP_SP \
184 GET_CALLER_PC_BP; \
185 uptr local_stack; \
186 uptr sp = (uptr)&local_stack
187
188// Use this macro if you want to print stack trace with the current
189// function in the top frame.
190#define GET_CURRENT_PC_BP \
191 uptr bp = GET_CURRENT_FRAME(); \
192 uptr pc = StackTrace::GetCurrentPc()
193
194#define GET_CURRENT_PC_BP_SP \
195 GET_CURRENT_PC_BP; \
196 uptr local_stack; \
197 uptr sp = (uptr)&local_stack
198
199// GET_CURRENT_PC() is equivalent to StackTrace::GetCurrentPc().
200// Optimized x86 version is faster than GetCurrentPc because
201// it does not involve a function call, instead it reads RIP register.
202// Reads of RIP by an instruction return RIP pointing to the next
203// instruction, which is exactly what we want here, thus 0 offset.
204// It needs to be a macro because otherwise we will get the name
205// of this function on the top of most stacks. Attribute artificial
206// does not do what it claims to do, unfortunatley. And attribute
207// __nodebug__ is clang-only. If we would have an attribute that
208// would remove this function from debug info, we could simply make
209// StackTrace::GetCurrentPc() faster.
210#if defined(__x86_64__)
211# define GET_CURRENT_PC() \
212 (__extension__({ \
213 uptr pc; \
214 asm("lea 0(%%rip), %0" : "=r"(pc)); \
215 pc; \
216 }))
217#else
218# define GET_CURRENT_PC() StackTrace::GetCurrentPc()
219#endif
220
221#endif // SANITIZER_STACKTRACE_H
222