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 #include "sanitizer_platform.h"
17 
18 namespace __sanitizer {
19 
20 struct BufferedStackTrace;
21 
22 static const u32 kStackTraceMax = 256;
23 
24 #if SANITIZER_LINUX && defined(__mips__)
25 # define SANITIZER_CAN_FAST_UNWIND 0
26 #elif SANITIZER_WINDOWS
27 # define SANITIZER_CAN_FAST_UNWIND 0
28 #else
29 # define SANITIZER_CAN_FAST_UNWIND 1
30 #endif
31 
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
37 #else
38 # define SANITIZER_CAN_SLOW_UNWIND 1
39 #endif
40 
41 struct StackTrace {
42   const uptr *trace;
43   u32 size;
44   u32 tag;
45 
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.
50 
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) {}
55 
56   // Prints a symbolized stacktrace, followed by an empty line.
57   void Print() const;
58 
59   static bool WillUseFastUnwind(bool request_fast_unwind) {
60     if (!SANITIZER_CAN_FAST_UNWIND)
61       return false;
62     if (!SANITIZER_CAN_SLOW_UNWIND)
63       return true;
64     return request_fast_unwind;
65   }
66 
67   static uptr GetCurrentPc();
68   static inline uptr GetPreviousInstructionPc(uptr pc);
69   static uptr GetNextInstructionPc(uptr pc);
70 };
71 
72 // Performance-critical, must be in the header.
73 ALWAYS_INLINE
74 uptr StackTrace::GetPreviousInstructionPc(uptr pc) {
75 #if defined(__arm__)
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.
82   return pc - 4;
83 #elif defined(__sparc__) || defined(__mips__)
84   return pc - 8;
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
92   return pc - 2;
93 #else
94   return pc - 1;
95 #endif
96 }
97 
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.
102 
103   BufferedStackTrace() : StackTrace(trace_buffer, 0), top_frame_bp(0) {}
104 
105   void Init(const uptr *pcs, uptr cnt, uptr extra_top_pc = 0);
106 
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) {
115       if (max_depth == 1)
116         trace_buffer[0] = pc;
117       size = max_depth;
118       return;
119     }
120     UnwindImpl(pc, bp, context, request_fast, max_depth);
121   }
122 
123   void Unwind(u32 max_depth, uptr pc, uptr bp, void *context, uptr stack_top,
124               uptr stack_bottom, bool request_fast_unwind);
125 
126   void Reset() {
127     *static_cast<StackTrace *>(this) = StackTrace(trace_buffer, 0);
128     top_frame_bp = 0;
129   }
130 
131  private:
132   // Every runtime defines its own implementation of this method
133   void UnwindImpl(uptr pc, uptr bp, void *context, bool request_fast,
134                   u32 max_depth);
135 
136   // UnwindFast/Slow have platform-specific implementations
137   void UnwindFast(uptr pc, uptr bp, uptr stack_top, uptr stack_bottom,
138                   u32 max_depth);
139   void UnwindSlow(uptr pc, u32 max_depth);
140   void UnwindSlow(uptr pc, void *context, u32 max_depth);
141 
142   void PopStackFrames(uptr count);
143   uptr LocatePcInTrace(uptr pc);
144 
145   BufferedStackTrace(const BufferedStackTrace &) = delete;
146   void operator=(const BufferedStackTrace &) = delete;
147 
148   friend class FastUnwindTest;
149 };
150 
151 #if defined(__s390x__)
152 static const uptr kFrameSize = 160;
153 #elif defined(__s390__)
154 static const uptr kFrameSize = 96;
155 #else
156 static const uptr kFrameSize = 2 * sizeof(uhwptr);
157 #endif
158 
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;
162 }
163 
164 }  // namespace __sanitizer
165 
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();
171 
172 #define GET_CALLER_PC_BP_SP \
173   GET_CALLER_PC_BP;                           \
174   uptr local_stack;                           \
175   uptr sp = (uptr)&local_stack
176 
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()
182 
183 #define GET_CURRENT_PC_BP_SP \
184   GET_CURRENT_PC_BP;                          \
185   uptr local_stack;                           \
186   uptr sp = (uptr)&local_stack
187 
188 
189 #endif  // SANITIZER_STACKTRACE_H
190