1 //===-- sanitizer_stacktrace.h ----------------------------------*- C++ -*-===//
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
7 //===----------------------------------------------------------------------===//
9 // This file is shared between AddressSanitizer and ThreadSanitizer
10 // run-time libraries.
11 //===----------------------------------------------------------------------===//
12 #ifndef SANITIZER_STACKTRACE_H
13 #define SANITIZER_STACKTRACE_H
15 #include "sanitizer_internal_defs.h"
16 #include "sanitizer_platform.h"
18 namespace __sanitizer {
20 struct BufferedStackTrace;
22 static const u32 kStackTraceMax = 256;
24 #if SANITIZER_LINUX && defined(__mips__)
25 # define SANITIZER_CAN_FAST_UNWIND 0
26 #elif SANITIZER_WINDOWS
27 # define SANITIZER_CAN_FAST_UNWIND 0
29 # define SANITIZER_CAN_FAST_UNWIND 1
32 // Fast unwind is the only option on Mac for now; we will need to
33 // revisit this macro when slow unwind works on Mac, see
34 // https://github.com/google/sanitizers/issues/137
35 #if SANITIZER_MAC || SANITIZER_RTEMS
36 # define SANITIZER_CAN_SLOW_UNWIND 0
38 # define SANITIZER_CAN_SLOW_UNWIND 1
46 static const int TAG_UNKNOWN = 0;
47 static const int TAG_ALLOC = 1;
48 static const int TAG_DEALLOC = 2;
49 static const int TAG_CUSTOM = 100; // Tool specific tags start here.
51 StackTrace() : trace(nullptr), size(0), tag(0) {}
52 StackTrace(const uptr *trace, u32 size) : trace(trace), size(size), tag(0) {}
53 StackTrace(const uptr *trace, u32 size, u32 tag)
54 : trace(trace), size(size), tag(tag) {}
56 // Prints a symbolized stacktrace, followed by an empty line.
59 static bool WillUseFastUnwind(bool request_fast_unwind) {
60 if (!SANITIZER_CAN_FAST_UNWIND)
62 if (!SANITIZER_CAN_SLOW_UNWIND)
64 return request_fast_unwind;
67 static uptr GetCurrentPc();
68 static inline uptr GetPreviousInstructionPc(uptr pc);
69 static uptr GetNextInstructionPc(uptr pc);
72 // Performance-critical, must be in the header.
74 uptr StackTrace::GetPreviousInstructionPc(uptr pc) {
76 // T32 (Thumb) branch instructions might be 16 or 32 bit long,
77 // so we return (pc-2) in that case in order to be safe.
78 // For A32 mode we return (pc-4) because all instructions are 32 bit long.
79 return (pc - 3) & (~1);
80 #elif defined(__powerpc__) || defined(__powerpc64__) || defined(__aarch64__)
81 // PCs are always 4 byte aligned.
83 #elif defined(__sparc__) || defined(__mips__)
85 #elif SANITIZER_RISCV64
86 // RV-64 has variable instruciton length...
87 // C extentions gives us 2-byte instructoins
88 // RV-64 has 4-byte instructions
89 // + RISCV architecture allows instructions up to 8 bytes
90 // It seems difficult to figure out the exact instruction length -
91 // pc - 2 seems like a safe option for the purposes of stack tracing
98 // StackTrace that owns the buffer used to store the addresses.
99 struct BufferedStackTrace : public StackTrace {
100 uptr trace_buffer[kStackTraceMax];
101 uptr top_frame_bp; // Optional bp of a top frame.
103 BufferedStackTrace() : StackTrace(trace_buffer, 0), top_frame_bp(0) {}
105 void Init(const uptr *pcs, uptr cnt, uptr extra_top_pc = 0);
107 // Get the stack trace with the given pc and bp.
108 // The pc will be in the position 0 of the resulting stack trace.
109 // The bp may refer to the current frame or to the caller's frame.
110 void Unwind(uptr pc, uptr bp, void *context, bool request_fast,
111 u32 max_depth = kStackTraceMax) {
112 top_frame_bp = (max_depth > 0) ? bp : 0;
113 // Small max_depth optimization
114 if (max_depth <= 1) {
116 trace_buffer[0] = pc;
120 UnwindImpl(pc, bp, context, request_fast, max_depth);
123 void Unwind(u32 max_depth, uptr pc, uptr bp, void *context, uptr stack_top,
124 uptr stack_bottom, bool request_fast_unwind);
127 *static_cast<StackTrace *>(this) = StackTrace(trace_buffer, 0);
132 // Every runtime defines its own implementation of this method
133 void UnwindImpl(uptr pc, uptr bp, void *context, bool request_fast,
136 // UnwindFast/Slow have platform-specific implementations
137 void UnwindFast(uptr pc, uptr bp, uptr stack_top, uptr stack_bottom,
139 void UnwindSlow(uptr pc, u32 max_depth);
140 void UnwindSlow(uptr pc, void *context, u32 max_depth);
142 void PopStackFrames(uptr count);
143 uptr LocatePcInTrace(uptr pc);
145 BufferedStackTrace(const BufferedStackTrace &) = delete;
146 void operator=(const BufferedStackTrace &) = delete;
148 friend class FastUnwindTest;
151 #if defined(__s390x__)
152 static const uptr kFrameSize = 160;
153 #elif defined(__s390__)
154 static const uptr kFrameSize = 96;
156 static const uptr kFrameSize = 2 * sizeof(uhwptr);
159 // Check if given pointer points into allocated stack area.
160 static inline bool IsValidFrame(uptr frame, uptr stack_top, uptr stack_bottom) {
161 return frame > stack_bottom && frame < stack_top - kFrameSize;
164 } // namespace __sanitizer
166 // Use this macro if you want to print stack trace with the caller
167 // of the current function in the top frame.
168 #define GET_CALLER_PC_BP \
169 uptr bp = GET_CURRENT_FRAME(); \
170 uptr pc = GET_CALLER_PC();
172 #define GET_CALLER_PC_BP_SP \
175 uptr sp = (uptr)&local_stack
177 // Use this macro if you want to print stack trace with the current
178 // function in the top frame.
179 #define GET_CURRENT_PC_BP \
180 uptr bp = GET_CURRENT_FRAME(); \
181 uptr pc = StackTrace::GetCurrentPc()
183 #define GET_CURRENT_PC_BP_SP \
186 uptr sp = (uptr)&local_stack
189 #endif // SANITIZER_STACKTRACE_H