xref: /freebsd/contrib/llvm-project/compiler-rt/lib/sanitizer_common/sanitizer_stacktrace.h (revision 06c3fb2749bda94cb5201f81ffdb8fa6c3161b2e)
10b57cec5SDimitry Andric //===-- sanitizer_stacktrace.h ----------------------------------*- C++ -*-===//
20b57cec5SDimitry Andric //
30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
60b57cec5SDimitry Andric //
70b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
80b57cec5SDimitry Andric //
90b57cec5SDimitry Andric // This file is shared between AddressSanitizer and ThreadSanitizer
100b57cec5SDimitry Andric // run-time libraries.
110b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
120b57cec5SDimitry Andric #ifndef SANITIZER_STACKTRACE_H
130b57cec5SDimitry Andric #define SANITIZER_STACKTRACE_H
140b57cec5SDimitry Andric 
15fe6060f1SDimitry Andric #include "sanitizer_common.h"
160b57cec5SDimitry Andric #include "sanitizer_internal_defs.h"
17e8d8bef9SDimitry Andric #include "sanitizer_platform.h"
180b57cec5SDimitry Andric 
190b57cec5SDimitry Andric namespace __sanitizer {
200b57cec5SDimitry Andric 
210b57cec5SDimitry Andric struct BufferedStackTrace;
220b57cec5SDimitry Andric 
234824e7fdSDimitry Andric static const u32 kStackTraceMax = 255;
240b57cec5SDimitry Andric 
250b57cec5SDimitry Andric #if SANITIZER_LINUX && defined(__mips__)
260b57cec5SDimitry Andric # define SANITIZER_CAN_FAST_UNWIND 0
270b57cec5SDimitry Andric #elif SANITIZER_WINDOWS
280b57cec5SDimitry Andric # define SANITIZER_CAN_FAST_UNWIND 0
290b57cec5SDimitry Andric #else
300b57cec5SDimitry Andric # define SANITIZER_CAN_FAST_UNWIND 1
310b57cec5SDimitry Andric #endif
320b57cec5SDimitry Andric 
330b57cec5SDimitry Andric // Fast unwind is the only option on Mac for now; we will need to
340b57cec5SDimitry Andric // revisit this macro when slow unwind works on Mac, see
350b57cec5SDimitry Andric // https://github.com/google/sanitizers/issues/137
3681ad6265SDimitry Andric #if SANITIZER_APPLE
370b57cec5SDimitry Andric #  define SANITIZER_CAN_SLOW_UNWIND 0
380b57cec5SDimitry Andric #else
390b57cec5SDimitry Andric # define SANITIZER_CAN_SLOW_UNWIND 1
400b57cec5SDimitry Andric #endif
410b57cec5SDimitry Andric 
420b57cec5SDimitry Andric struct StackTrace {
430b57cec5SDimitry Andric   const uptr *trace;
440b57cec5SDimitry Andric   u32 size;
450b57cec5SDimitry Andric   u32 tag;
460b57cec5SDimitry Andric 
470b57cec5SDimitry Andric   static const int TAG_UNKNOWN = 0;
480b57cec5SDimitry Andric   static const int TAG_ALLOC = 1;
490b57cec5SDimitry Andric   static const int TAG_DEALLOC = 2;
500b57cec5SDimitry Andric   static const int TAG_CUSTOM = 100; // Tool specific tags start here.
510b57cec5SDimitry Andric 
StackTraceStackTrace520b57cec5SDimitry Andric   StackTrace() : trace(nullptr), size(0), tag(0) {}
StackTraceStackTrace530b57cec5SDimitry Andric   StackTrace(const uptr *trace, u32 size) : trace(trace), size(size), tag(0) {}
StackTraceStackTrace540b57cec5SDimitry Andric   StackTrace(const uptr *trace, u32 size, u32 tag)
550b57cec5SDimitry Andric       : trace(trace), size(size), tag(tag) {}
560b57cec5SDimitry Andric 
570b57cec5SDimitry Andric   // Prints a symbolized stacktrace, followed by an empty line.
580b57cec5SDimitry Andric   void Print() const;
590b57cec5SDimitry Andric 
60fe6060f1SDimitry Andric   // Prints a symbolized stacktrace to the output string, followed by an empty
61fe6060f1SDimitry Andric   // line.
62fe6060f1SDimitry Andric   void PrintTo(InternalScopedString *output) const;
63fe6060f1SDimitry Andric 
64fe6060f1SDimitry Andric   // Prints a symbolized stacktrace to the output buffer, followed by an empty
65fe6060f1SDimitry Andric   // line. Returns the number of symbols that should have been written to buffer
66fe6060f1SDimitry Andric   // (not including trailing '\0'). Thus, the string is truncated iff return
67fe6060f1SDimitry Andric   // value is not less than "out_buf_size".
68fe6060f1SDimitry Andric   uptr PrintTo(char *out_buf, uptr out_buf_size) const;
69fe6060f1SDimitry Andric 
WillUseFastUnwindStackTrace700b57cec5SDimitry Andric   static bool WillUseFastUnwind(bool request_fast_unwind) {
710b57cec5SDimitry Andric     if (!SANITIZER_CAN_FAST_UNWIND)
720b57cec5SDimitry Andric       return false;
730b57cec5SDimitry Andric     if (!SANITIZER_CAN_SLOW_UNWIND)
740b57cec5SDimitry Andric       return true;
750b57cec5SDimitry Andric     return request_fast_unwind;
760b57cec5SDimitry Andric   }
770b57cec5SDimitry Andric 
780b57cec5SDimitry Andric   static uptr GetCurrentPc();
790b57cec5SDimitry Andric   static inline uptr GetPreviousInstructionPc(uptr pc);
800b57cec5SDimitry Andric   static uptr GetNextInstructionPc(uptr pc);
810b57cec5SDimitry Andric };
820b57cec5SDimitry Andric 
830b57cec5SDimitry Andric // Performance-critical, must be in the header.
840b57cec5SDimitry Andric ALWAYS_INLINE
GetPreviousInstructionPc(uptr pc)850b57cec5SDimitry Andric uptr StackTrace::GetPreviousInstructionPc(uptr pc) {
860b57cec5SDimitry Andric #if defined(__arm__)
870b57cec5SDimitry Andric   // T32 (Thumb) branch instructions might be 16 or 32 bit long,
880b57cec5SDimitry Andric   // so we return (pc-2) in that case in order to be safe.
890b57cec5SDimitry Andric   // For A32 mode we return (pc-4) because all instructions are 32 bit long.
900b57cec5SDimitry Andric   return (pc - 3) & (~1);
910b57cec5SDimitry Andric #elif defined(__sparc__) || defined(__mips__)
920b57cec5SDimitry Andric   return pc - 8;
93e8d8bef9SDimitry Andric #elif SANITIZER_RISCV64
94*06c3fb27SDimitry Andric   // RV-64 has variable instruction length...
95e8d8bef9SDimitry Andric   // C extentions gives us 2-byte instructoins
96e8d8bef9SDimitry Andric   // RV-64 has 4-byte instructions
97*06c3fb27SDimitry Andric   // + RISC-V architecture allows instructions up to 8 bytes
98e8d8bef9SDimitry Andric   // It seems difficult to figure out the exact instruction length -
99e8d8bef9SDimitry Andric   // pc - 2 seems like a safe option for the purposes of stack tracing
100e8d8bef9SDimitry Andric   return pc - 2;
10181ad6265SDimitry Andric #elif SANITIZER_S390 || SANITIZER_I386 || SANITIZER_X32 || SANITIZER_X64
1020b57cec5SDimitry Andric   return pc - 1;
10381ad6265SDimitry Andric #else
10481ad6265SDimitry Andric   return pc - 4;
1050b57cec5SDimitry Andric #endif
1060b57cec5SDimitry Andric }
1070b57cec5SDimitry Andric 
1080b57cec5SDimitry Andric // StackTrace that owns the buffer used to store the addresses.
1090b57cec5SDimitry Andric struct BufferedStackTrace : public StackTrace {
1100b57cec5SDimitry Andric   uptr trace_buffer[kStackTraceMax];
1110b57cec5SDimitry Andric   uptr top_frame_bp;  // Optional bp of a top frame.
1120b57cec5SDimitry Andric 
BufferedStackTraceBufferedStackTrace1130b57cec5SDimitry Andric   BufferedStackTrace() : StackTrace(trace_buffer, 0), top_frame_bp(0) {}
1140b57cec5SDimitry Andric 
1150b57cec5SDimitry Andric   void Init(const uptr *pcs, uptr cnt, uptr extra_top_pc = 0);
1160b57cec5SDimitry Andric 
1170b57cec5SDimitry Andric   // Get the stack trace with the given pc and bp.
1180b57cec5SDimitry Andric   // The pc will be in the position 0 of the resulting stack trace.
1190b57cec5SDimitry Andric   // The bp may refer to the current frame or to the caller's frame.
1200b57cec5SDimitry Andric   void Unwind(uptr pc, uptr bp, void *context, bool request_fast,
1210b57cec5SDimitry Andric               u32 max_depth = kStackTraceMax) {
1220b57cec5SDimitry Andric     top_frame_bp = (max_depth > 0) ? bp : 0;
1230b57cec5SDimitry Andric     // Small max_depth optimization
1240b57cec5SDimitry Andric     if (max_depth <= 1) {
1250b57cec5SDimitry Andric       if (max_depth == 1)
1260b57cec5SDimitry Andric         trace_buffer[0] = pc;
1270b57cec5SDimitry Andric       size = max_depth;
1280b57cec5SDimitry Andric       return;
1290b57cec5SDimitry Andric     }
1300b57cec5SDimitry Andric     UnwindImpl(pc, bp, context, request_fast, max_depth);
1310b57cec5SDimitry Andric   }
1320b57cec5SDimitry Andric 
1330b57cec5SDimitry Andric   void Unwind(u32 max_depth, uptr pc, uptr bp, void *context, uptr stack_top,
1340b57cec5SDimitry Andric               uptr stack_bottom, bool request_fast_unwind);
1350b57cec5SDimitry Andric 
ResetBufferedStackTrace1360b57cec5SDimitry Andric   void Reset() {
1370b57cec5SDimitry Andric     *static_cast<StackTrace *>(this) = StackTrace(trace_buffer, 0);
1380b57cec5SDimitry Andric     top_frame_bp = 0;
1390b57cec5SDimitry Andric   }
1400b57cec5SDimitry Andric 
1410b57cec5SDimitry Andric  private:
1420b57cec5SDimitry Andric   // Every runtime defines its own implementation of this method
1430b57cec5SDimitry Andric   void UnwindImpl(uptr pc, uptr bp, void *context, bool request_fast,
1440b57cec5SDimitry Andric                   u32 max_depth);
1450b57cec5SDimitry Andric 
1460b57cec5SDimitry Andric   // UnwindFast/Slow have platform-specific implementations
1470b57cec5SDimitry Andric   void UnwindFast(uptr pc, uptr bp, uptr stack_top, uptr stack_bottom,
1480b57cec5SDimitry Andric                   u32 max_depth);
1490b57cec5SDimitry Andric   void UnwindSlow(uptr pc, u32 max_depth);
1500b57cec5SDimitry Andric   void UnwindSlow(uptr pc, void *context, u32 max_depth);
1510b57cec5SDimitry Andric 
1520b57cec5SDimitry Andric   void PopStackFrames(uptr count);
1530b57cec5SDimitry Andric   uptr LocatePcInTrace(uptr pc);
1540b57cec5SDimitry Andric 
1550b57cec5SDimitry Andric   BufferedStackTrace(const BufferedStackTrace &) = delete;
1560b57cec5SDimitry Andric   void operator=(const BufferedStackTrace &) = delete;
1570b57cec5SDimitry Andric 
1580b57cec5SDimitry Andric   friend class FastUnwindTest;
1590b57cec5SDimitry Andric };
1600b57cec5SDimitry Andric 
161e8d8bef9SDimitry Andric #if defined(__s390x__)
162e8d8bef9SDimitry Andric static const uptr kFrameSize = 160;
163e8d8bef9SDimitry Andric #elif defined(__s390__)
164e8d8bef9SDimitry Andric static const uptr kFrameSize = 96;
165e8d8bef9SDimitry Andric #else
166e8d8bef9SDimitry Andric static const uptr kFrameSize = 2 * sizeof(uhwptr);
167e8d8bef9SDimitry Andric #endif
168e8d8bef9SDimitry Andric 
1690b57cec5SDimitry Andric // Check if given pointer points into allocated stack area.
IsValidFrame(uptr frame,uptr stack_top,uptr stack_bottom)1700b57cec5SDimitry Andric static inline bool IsValidFrame(uptr frame, uptr stack_top, uptr stack_bottom) {
171e8d8bef9SDimitry Andric   return frame > stack_bottom && frame < stack_top - kFrameSize;
1720b57cec5SDimitry Andric }
1730b57cec5SDimitry Andric 
1740b57cec5SDimitry Andric }  // namespace __sanitizer
1750b57cec5SDimitry Andric 
1760b57cec5SDimitry Andric // Use this macro if you want to print stack trace with the caller
1770b57cec5SDimitry Andric // of the current function in the top frame.
1780b57cec5SDimitry Andric #define GET_CALLER_PC_BP \
1790b57cec5SDimitry Andric   uptr bp = GET_CURRENT_FRAME();              \
1800b57cec5SDimitry Andric   uptr pc = GET_CALLER_PC();
1810b57cec5SDimitry Andric 
1820b57cec5SDimitry Andric #define GET_CALLER_PC_BP_SP \
1830b57cec5SDimitry Andric   GET_CALLER_PC_BP;                           \
1840b57cec5SDimitry Andric   uptr local_stack;                           \
1850b57cec5SDimitry Andric   uptr sp = (uptr)&local_stack
1860b57cec5SDimitry Andric 
1870b57cec5SDimitry Andric // Use this macro if you want to print stack trace with the current
1880b57cec5SDimitry Andric // function in the top frame.
1890b57cec5SDimitry Andric #define GET_CURRENT_PC_BP \
1900b57cec5SDimitry Andric   uptr bp = GET_CURRENT_FRAME();              \
1910b57cec5SDimitry Andric   uptr pc = StackTrace::GetCurrentPc()
1920b57cec5SDimitry Andric 
1930b57cec5SDimitry Andric #define GET_CURRENT_PC_BP_SP \
1940b57cec5SDimitry Andric   GET_CURRENT_PC_BP;                          \
1950b57cec5SDimitry Andric   uptr local_stack;                           \
1960b57cec5SDimitry Andric   uptr sp = (uptr)&local_stack
1970b57cec5SDimitry Andric 
198fe6060f1SDimitry Andric // GET_CURRENT_PC() is equivalent to StackTrace::GetCurrentPc().
199fe6060f1SDimitry Andric // Optimized x86 version is faster than GetCurrentPc because
200fe6060f1SDimitry Andric // it does not involve a function call, instead it reads RIP register.
201fe6060f1SDimitry Andric // Reads of RIP by an instruction return RIP pointing to the next
202fe6060f1SDimitry Andric // instruction, which is exactly what we want here, thus 0 offset.
203fe6060f1SDimitry Andric // It needs to be a macro because otherwise we will get the name
204fe6060f1SDimitry Andric // of this function on the top of most stacks. Attribute artificial
205fe6060f1SDimitry Andric // does not do what it claims to do, unfortunatley. And attribute
206fe6060f1SDimitry Andric // __nodebug__ is clang-only. If we would have an attribute that
207fe6060f1SDimitry Andric // would remove this function from debug info, we could simply make
208fe6060f1SDimitry Andric // StackTrace::GetCurrentPc() faster.
209fe6060f1SDimitry Andric #if defined(__x86_64__)
210fe6060f1SDimitry Andric #  define GET_CURRENT_PC()                \
211349cc55cSDimitry Andric     (__extension__({                      \
212fe6060f1SDimitry Andric       uptr pc;                            \
213fe6060f1SDimitry Andric       asm("lea 0(%%rip), %0" : "=r"(pc)); \
214fe6060f1SDimitry Andric       pc;                                 \
215349cc55cSDimitry Andric     }))
216fe6060f1SDimitry Andric #else
217fe6060f1SDimitry Andric #  define GET_CURRENT_PC() StackTrace::GetCurrentPc()
218fe6060f1SDimitry Andric #endif
2190b57cec5SDimitry Andric 
2200b57cec5SDimitry Andric #endif  // SANITIZER_STACKTRACE_H
221