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
);
70 typedef bool (*SymbolizeCallback
)(const void *pc
, char *out_buffer
,
74 // Performance-critical, must be in the header.
76 uptr
StackTrace::GetPreviousInstructionPc(uptr pc
) {
78 // T32 (Thumb) branch instructions might be 16 or 32 bit long,
79 // so we return (pc-2) in that case in order to be safe.
80 // For A32 mode we return (pc-4) because all instructions are 32 bit long.
81 return (pc
- 3) & (~1);
82 #elif defined(__powerpc__) || defined(__powerpc64__) || defined(__aarch64__)
83 // PCs are always 4 byte aligned.
85 #elif defined(__sparc__) || defined(__mips__)
87 #elif SANITIZER_RISCV64
88 // RV-64 has variable instruciton length...
89 // C extentions gives us 2-byte instructoins
90 // RV-64 has 4-byte instructions
91 // + RISCV architecture allows instructions up to 8 bytes
92 // It seems difficult to figure out the exact instruction length -
93 // pc - 2 seems like a safe option for the purposes of stack tracing
100 // StackTrace that owns the buffer used to store the addresses.
101 struct BufferedStackTrace
: public StackTrace
{
102 uptr trace_buffer
[kStackTraceMax
];
103 uptr top_frame_bp
; // Optional bp of a top frame.
105 BufferedStackTrace() : StackTrace(trace_buffer
, 0), top_frame_bp(0) {}
107 void Init(const uptr
*pcs
, uptr cnt
, uptr extra_top_pc
= 0);
109 // Get the stack trace with the given pc and bp.
110 // The pc will be in the position 0 of the resulting stack trace.
111 // The bp may refer to the current frame or to the caller's frame.
112 void Unwind(uptr pc
, uptr bp
, void *context
, bool request_fast
,
113 u32 max_depth
= kStackTraceMax
) {
114 top_frame_bp
= (max_depth
> 0) ? bp
: 0;
115 // Small max_depth optimization
116 if (max_depth
<= 1) {
118 trace_buffer
[0] = pc
;
122 UnwindImpl(pc
, bp
, context
, request_fast
, max_depth
);
125 void Unwind(u32 max_depth
, uptr pc
, uptr bp
, void *context
, uptr stack_top
,
126 uptr stack_bottom
, bool request_fast_unwind
);
129 *static_cast<StackTrace
*>(this) = StackTrace(trace_buffer
, 0);
134 // Every runtime defines its own implementation of this method
135 void UnwindImpl(uptr pc
, uptr bp
, void *context
, bool request_fast
,
138 // UnwindFast/Slow have platform-specific implementations
139 void UnwindFast(uptr pc
, uptr bp
, uptr stack_top
, uptr stack_bottom
,
141 void UnwindSlow(uptr pc
, u32 max_depth
);
142 void UnwindSlow(uptr pc
, void *context
, u32 max_depth
);
144 void PopStackFrames(uptr count
);
145 uptr
LocatePcInTrace(uptr pc
);
147 BufferedStackTrace(const BufferedStackTrace
&) = delete;
148 void operator=(const BufferedStackTrace
&) = delete;
150 friend class FastUnwindTest
;
153 #if defined(__s390x__)
154 static const uptr kFrameSize
= 160;
155 #elif defined(__s390__)
156 static const uptr kFrameSize
= 96;
158 static const uptr kFrameSize
= 2 * sizeof(uhwptr
);
161 // Check if given pointer points into allocated stack area.
162 static inline bool IsValidFrame(uptr frame
, uptr stack_top
, uptr stack_bottom
) {
163 return frame
> stack_bottom
&& frame
< stack_top
- kFrameSize
;
166 } // namespace __sanitizer
168 // Use this macro if you want to print stack trace with the caller
169 // of the current function in the top frame.
170 #define GET_CALLER_PC_BP \
171 uptr bp = GET_CURRENT_FRAME(); \
172 uptr pc = GET_CALLER_PC();
174 #define GET_CALLER_PC_BP_SP \
177 uptr sp = (uptr)&local_stack
179 // Use this macro if you want to print stack trace with the current
180 // function in the top frame.
181 #define GET_CURRENT_PC_BP \
182 uptr bp = GET_CURRENT_FRAME(); \
183 uptr pc = StackTrace::GetCurrentPc()
185 #define GET_CURRENT_PC_BP_SP \
188 uptr sp = (uptr)&local_stack
191 #endif // SANITIZER_STACKTRACE_H