13cab2bb3Spatrick //===-- sanitizer_stacktrace.h ----------------------------------*- C++ -*-===//
23cab2bb3Spatrick //
33cab2bb3Spatrick // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
43cab2bb3Spatrick // See https://llvm.org/LICENSE.txt for license information.
53cab2bb3Spatrick // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
63cab2bb3Spatrick //
73cab2bb3Spatrick //===----------------------------------------------------------------------===//
83cab2bb3Spatrick //
93cab2bb3Spatrick // This file is shared between AddressSanitizer and ThreadSanitizer
103cab2bb3Spatrick // run-time libraries.
113cab2bb3Spatrick //===----------------------------------------------------------------------===//
123cab2bb3Spatrick #ifndef SANITIZER_STACKTRACE_H
133cab2bb3Spatrick #define SANITIZER_STACKTRACE_H
143cab2bb3Spatrick
15d89ec533Spatrick #include "sanitizer_common.h"
163cab2bb3Spatrick #include "sanitizer_internal_defs.h"
17d89ec533Spatrick #include "sanitizer_platform.h"
183cab2bb3Spatrick
193cab2bb3Spatrick namespace __sanitizer {
203cab2bb3Spatrick
213cab2bb3Spatrick struct BufferedStackTrace;
223cab2bb3Spatrick
23*810390e3Srobert static const u32 kStackTraceMax = 255;
243cab2bb3Spatrick
253cab2bb3Spatrick #if SANITIZER_LINUX && defined(__mips__)
263cab2bb3Spatrick # define SANITIZER_CAN_FAST_UNWIND 0
273cab2bb3Spatrick #elif SANITIZER_WINDOWS
283cab2bb3Spatrick # define SANITIZER_CAN_FAST_UNWIND 0
293cab2bb3Spatrick #else
303cab2bb3Spatrick # define SANITIZER_CAN_FAST_UNWIND 1
313cab2bb3Spatrick #endif
323cab2bb3Spatrick
333cab2bb3Spatrick // Fast unwind is the only option on Mac for now; we will need to
343cab2bb3Spatrick // revisit this macro when slow unwind works on Mac, see
353cab2bb3Spatrick // https://github.com/google/sanitizers/issues/137
36*810390e3Srobert #if SANITIZER_APPLE
373cab2bb3Spatrick # define SANITIZER_CAN_SLOW_UNWIND 0
383cab2bb3Spatrick #else
393cab2bb3Spatrick # define SANITIZER_CAN_SLOW_UNWIND 1
403cab2bb3Spatrick #endif
413cab2bb3Spatrick
423cab2bb3Spatrick struct StackTrace {
433cab2bb3Spatrick const uptr *trace;
443cab2bb3Spatrick u32 size;
453cab2bb3Spatrick u32 tag;
463cab2bb3Spatrick
473cab2bb3Spatrick static const int TAG_UNKNOWN = 0;
483cab2bb3Spatrick static const int TAG_ALLOC = 1;
493cab2bb3Spatrick static const int TAG_DEALLOC = 2;
503cab2bb3Spatrick static const int TAG_CUSTOM = 100; // Tool specific tags start here.
513cab2bb3Spatrick
StackTraceStackTrace523cab2bb3Spatrick StackTrace() : trace(nullptr), size(0), tag(0) {}
StackTraceStackTrace533cab2bb3Spatrick StackTrace(const uptr *trace, u32 size) : trace(trace), size(size), tag(0) {}
StackTraceStackTrace543cab2bb3Spatrick StackTrace(const uptr *trace, u32 size, u32 tag)
553cab2bb3Spatrick : trace(trace), size(size), tag(tag) {}
563cab2bb3Spatrick
573cab2bb3Spatrick // Prints a symbolized stacktrace, followed by an empty line.
583cab2bb3Spatrick void Print() const;
593cab2bb3Spatrick
60d89ec533Spatrick // Prints a symbolized stacktrace to the output string, followed by an empty
61d89ec533Spatrick // line.
62d89ec533Spatrick void PrintTo(InternalScopedString *output) const;
63d89ec533Spatrick
64d89ec533Spatrick // Prints a symbolized stacktrace to the output buffer, followed by an empty
65d89ec533Spatrick // line. Returns the number of symbols that should have been written to buffer
66d89ec533Spatrick // (not including trailing '\0'). Thus, the string is truncated iff return
67d89ec533Spatrick // value is not less than "out_buf_size".
68d89ec533Spatrick uptr PrintTo(char *out_buf, uptr out_buf_size) const;
69d89ec533Spatrick
WillUseFastUnwindStackTrace703cab2bb3Spatrick static bool WillUseFastUnwind(bool request_fast_unwind) {
713cab2bb3Spatrick if (!SANITIZER_CAN_FAST_UNWIND)
723cab2bb3Spatrick return false;
733cab2bb3Spatrick if (!SANITIZER_CAN_SLOW_UNWIND)
743cab2bb3Spatrick return true;
753cab2bb3Spatrick return request_fast_unwind;
763cab2bb3Spatrick }
773cab2bb3Spatrick
783cab2bb3Spatrick static uptr GetCurrentPc();
793cab2bb3Spatrick static inline uptr GetPreviousInstructionPc(uptr pc);
803cab2bb3Spatrick static uptr GetNextInstructionPc(uptr pc);
813cab2bb3Spatrick };
823cab2bb3Spatrick
833cab2bb3Spatrick // Performance-critical, must be in the header.
843cab2bb3Spatrick ALWAYS_INLINE
GetPreviousInstructionPc(uptr pc)853cab2bb3Spatrick uptr StackTrace::GetPreviousInstructionPc(uptr pc) {
863cab2bb3Spatrick #if defined(__arm__)
873cab2bb3Spatrick // T32 (Thumb) branch instructions might be 16 or 32 bit long,
883cab2bb3Spatrick // so we return (pc-2) in that case in order to be safe.
893cab2bb3Spatrick // For A32 mode we return (pc-4) because all instructions are 32 bit long.
903cab2bb3Spatrick return (pc - 3) & (~1);
913cab2bb3Spatrick #elif defined(__sparc__) || defined(__mips__)
923cab2bb3Spatrick return pc - 8;
93d89ec533Spatrick #elif SANITIZER_RISCV64
94d89ec533Spatrick // RV-64 has variable instruciton length...
95d89ec533Spatrick // C extentions gives us 2-byte instructoins
96d89ec533Spatrick // RV-64 has 4-byte instructions
97d89ec533Spatrick // + RISCV architecture allows instructions up to 8 bytes
98d89ec533Spatrick // It seems difficult to figure out the exact instruction length -
99d89ec533Spatrick // pc - 2 seems like a safe option for the purposes of stack tracing
100d89ec533Spatrick return pc - 2;
101*810390e3Srobert #elif SANITIZER_S390 || SANITIZER_I386 || SANITIZER_X32 || SANITIZER_X64
1023cab2bb3Spatrick return pc - 1;
103*810390e3Srobert #else
104*810390e3Srobert return pc - 4;
1053cab2bb3Spatrick #endif
1063cab2bb3Spatrick }
1073cab2bb3Spatrick
1083cab2bb3Spatrick // StackTrace that owns the buffer used to store the addresses.
1093cab2bb3Spatrick struct BufferedStackTrace : public StackTrace {
1103cab2bb3Spatrick uptr trace_buffer[kStackTraceMax];
1113cab2bb3Spatrick uptr top_frame_bp; // Optional bp of a top frame.
1123cab2bb3Spatrick
BufferedStackTraceBufferedStackTrace1133cab2bb3Spatrick BufferedStackTrace() : StackTrace(trace_buffer, 0), top_frame_bp(0) {}
1143cab2bb3Spatrick
1153cab2bb3Spatrick void Init(const uptr *pcs, uptr cnt, uptr extra_top_pc = 0);
1163cab2bb3Spatrick
1173cab2bb3Spatrick // Get the stack trace with the given pc and bp.
1183cab2bb3Spatrick // The pc will be in the position 0 of the resulting stack trace.
1193cab2bb3Spatrick // The bp may refer to the current frame or to the caller's frame.
1203cab2bb3Spatrick void Unwind(uptr pc, uptr bp, void *context, bool request_fast,
1213cab2bb3Spatrick u32 max_depth = kStackTraceMax) {
1223cab2bb3Spatrick top_frame_bp = (max_depth > 0) ? bp : 0;
1233cab2bb3Spatrick // Small max_depth optimization
1243cab2bb3Spatrick if (max_depth <= 1) {
1253cab2bb3Spatrick if (max_depth == 1)
1263cab2bb3Spatrick trace_buffer[0] = pc;
1273cab2bb3Spatrick size = max_depth;
1283cab2bb3Spatrick return;
1293cab2bb3Spatrick }
1303cab2bb3Spatrick UnwindImpl(pc, bp, context, request_fast, max_depth);
1313cab2bb3Spatrick }
1323cab2bb3Spatrick
1333cab2bb3Spatrick void Unwind(u32 max_depth, uptr pc, uptr bp, void *context, uptr stack_top,
1343cab2bb3Spatrick uptr stack_bottom, bool request_fast_unwind);
1353cab2bb3Spatrick
ResetBufferedStackTrace1363cab2bb3Spatrick void Reset() {
1373cab2bb3Spatrick *static_cast<StackTrace *>(this) = StackTrace(trace_buffer, 0);
1383cab2bb3Spatrick top_frame_bp = 0;
1393cab2bb3Spatrick }
1403cab2bb3Spatrick
1413cab2bb3Spatrick private:
1423cab2bb3Spatrick // Every runtime defines its own implementation of this method
1433cab2bb3Spatrick void UnwindImpl(uptr pc, uptr bp, void *context, bool request_fast,
1443cab2bb3Spatrick u32 max_depth);
1453cab2bb3Spatrick
1463cab2bb3Spatrick // UnwindFast/Slow have platform-specific implementations
1473cab2bb3Spatrick void UnwindFast(uptr pc, uptr bp, uptr stack_top, uptr stack_bottom,
1483cab2bb3Spatrick u32 max_depth);
1493cab2bb3Spatrick void UnwindSlow(uptr pc, u32 max_depth);
1503cab2bb3Spatrick void UnwindSlow(uptr pc, void *context, u32 max_depth);
1513cab2bb3Spatrick
1523cab2bb3Spatrick void PopStackFrames(uptr count);
1533cab2bb3Spatrick uptr LocatePcInTrace(uptr pc);
1543cab2bb3Spatrick
1553cab2bb3Spatrick BufferedStackTrace(const BufferedStackTrace &) = delete;
1563cab2bb3Spatrick void operator=(const BufferedStackTrace &) = delete;
1573cab2bb3Spatrick
1583cab2bb3Spatrick friend class FastUnwindTest;
1593cab2bb3Spatrick };
1603cab2bb3Spatrick
161d89ec533Spatrick #if defined(__s390x__)
162d89ec533Spatrick static const uptr kFrameSize = 160;
163d89ec533Spatrick #elif defined(__s390__)
164d89ec533Spatrick static const uptr kFrameSize = 96;
165d89ec533Spatrick #else
166d89ec533Spatrick static const uptr kFrameSize = 2 * sizeof(uhwptr);
167d89ec533Spatrick #endif
168d89ec533Spatrick
1693cab2bb3Spatrick // Check if given pointer points into allocated stack area.
IsValidFrame(uptr frame,uptr stack_top,uptr stack_bottom)1703cab2bb3Spatrick static inline bool IsValidFrame(uptr frame, uptr stack_top, uptr stack_bottom) {
171d89ec533Spatrick return frame > stack_bottom && frame < stack_top - kFrameSize;
1723cab2bb3Spatrick }
1733cab2bb3Spatrick
1743cab2bb3Spatrick } // namespace __sanitizer
1753cab2bb3Spatrick
1763cab2bb3Spatrick // Use this macro if you want to print stack trace with the caller
1773cab2bb3Spatrick // of the current function in the top frame.
1783cab2bb3Spatrick #define GET_CALLER_PC_BP \
1793cab2bb3Spatrick uptr bp = GET_CURRENT_FRAME(); \
1803cab2bb3Spatrick uptr pc = GET_CALLER_PC();
1813cab2bb3Spatrick
1823cab2bb3Spatrick #define GET_CALLER_PC_BP_SP \
1833cab2bb3Spatrick GET_CALLER_PC_BP; \
1843cab2bb3Spatrick uptr local_stack; \
1853cab2bb3Spatrick uptr sp = (uptr)&local_stack
1863cab2bb3Spatrick
1873cab2bb3Spatrick // Use this macro if you want to print stack trace with the current
1883cab2bb3Spatrick // function in the top frame.
1893cab2bb3Spatrick #define GET_CURRENT_PC_BP \
1903cab2bb3Spatrick uptr bp = GET_CURRENT_FRAME(); \
1913cab2bb3Spatrick uptr pc = StackTrace::GetCurrentPc()
1923cab2bb3Spatrick
1933cab2bb3Spatrick #define GET_CURRENT_PC_BP_SP \
1943cab2bb3Spatrick GET_CURRENT_PC_BP; \
1953cab2bb3Spatrick uptr local_stack; \
1963cab2bb3Spatrick uptr sp = (uptr)&local_stack
1973cab2bb3Spatrick
198d89ec533Spatrick // GET_CURRENT_PC() is equivalent to StackTrace::GetCurrentPc().
199d89ec533Spatrick // Optimized x86 version is faster than GetCurrentPc because
200d89ec533Spatrick // it does not involve a function call, instead it reads RIP register.
201d89ec533Spatrick // Reads of RIP by an instruction return RIP pointing to the next
202d89ec533Spatrick // instruction, which is exactly what we want here, thus 0 offset.
203d89ec533Spatrick // It needs to be a macro because otherwise we will get the name
204d89ec533Spatrick // of this function on the top of most stacks. Attribute artificial
205d89ec533Spatrick // does not do what it claims to do, unfortunatley. And attribute
206d89ec533Spatrick // __nodebug__ is clang-only. If we would have an attribute that
207d89ec533Spatrick // would remove this function from debug info, we could simply make
208d89ec533Spatrick // StackTrace::GetCurrentPc() faster.
209d89ec533Spatrick #if defined(__x86_64__)
210d89ec533Spatrick # define GET_CURRENT_PC() \
211*810390e3Srobert (__extension__({ \
212d89ec533Spatrick uptr pc; \
213d89ec533Spatrick asm("lea 0(%%rip), %0" : "=r"(pc)); \
214d89ec533Spatrick pc; \
215*810390e3Srobert }))
216d89ec533Spatrick #else
217d89ec533Spatrick # define GET_CURRENT_PC() StackTrace::GetCurrentPc()
218d89ec533Spatrick #endif
2193cab2bb3Spatrick
2203cab2bb3Spatrick #endif // SANITIZER_STACKTRACE_H
221