1 //===-- asan_stack.h --------------------------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file is a part of AddressSanitizer, an address sanity checker.
12 // ASan-private header for asan_stack.cc.
13 //===----------------------------------------------------------------------===//
17 #include "asan_flags.h"
18 #include "asan_thread.h"
19 #include "sanitizer_common/sanitizer_flags.h"
20 #include "sanitizer_common/sanitizer_stacktrace.h"
24 // Get the stack trace with the given pc and bp.
25 // The pc will be in the position 0 of the resulting stack trace.
26 // The bp may refer to the current frame or to the caller's frame.
28 void GetStackTraceWithPcBpAndContext(BufferedStackTrace *stack, uptr max_depth,
29 uptr pc, uptr bp, void *context,
32 stack->Unwind(max_depth, pc, bp, context, 0, 0, fast);
36 if (LIKELY(asan_inited)) {
37 if ((t = GetCurrentThread()) && !t->isUnwinding()) {
38 // On FreeBSD the slow unwinding that leverages _Unwind_Backtrace()
39 // yields the call stack of the signal's handler and not of the code
40 // that raised the signal (as it does on Linux).
41 if (SANITIZER_FREEBSD && t->isInDeadlySignal()) fast = true;
42 uptr stack_top = t->stack_top();
43 uptr stack_bottom = t->stack_bottom();
44 ScopedUnwinding unwind_scope(t);
45 stack->Unwind(max_depth, pc, bp, context, stack_top, stack_bottom, fast);
46 } else if (t == 0 && !fast) {
47 /* If GetCurrentThread() has failed, try to do slow unwind anyways. */
48 stack->Unwind(max_depth, pc, bp, context, 0, 0, false);
51 #endif // SANITIZER_WINDOWS
56 // NOTE: A Rule of thumb is to retrieve stack trace in the interceptors
57 // as early as possible (in functions exposed to the user), as we generally
58 // don't want stack trace to contain functions from ASan internals.
60 #define GET_STACK_TRACE(max_size, fast) \
61 BufferedStackTrace stack; \
62 if (max_size <= 2) { \
63 stack.size = max_size; \
65 stack.top_frame_bp = GET_CURRENT_FRAME(); \
66 stack.trace_buffer[0] = StackTrace::GetCurrentPc(); \
68 stack.trace_buffer[1] = GET_CALLER_PC(); \
71 GetStackTraceWithPcBpAndContext(&stack, max_size, \
72 StackTrace::GetCurrentPc(), \
73 GET_CURRENT_FRAME(), 0, fast); \
76 #define GET_STACK_TRACE_FATAL(pc, bp) \
77 BufferedStackTrace stack; \
78 GetStackTraceWithPcBpAndContext(&stack, kStackTraceMax, pc, bp, 0, \
79 common_flags()->fast_unwind_on_fatal)
81 #define GET_STACK_TRACE_SIGNAL(sig) \
82 BufferedStackTrace stack; \
83 GetStackTraceWithPcBpAndContext(&stack, kStackTraceMax, \
84 (sig).pc, (sig).bp, (sig).context, \
85 common_flags()->fast_unwind_on_fatal)
87 #define GET_STACK_TRACE_FATAL_HERE \
88 GET_STACK_TRACE(kStackTraceMax, common_flags()->fast_unwind_on_fatal)
90 #define GET_STACK_TRACE_CHECK_HERE \
91 GET_STACK_TRACE(kStackTraceMax, common_flags()->fast_unwind_on_check)
93 #define GET_STACK_TRACE_THREAD \
94 GET_STACK_TRACE(kStackTraceMax, true)
96 #define GET_STACK_TRACE_MALLOC \
97 GET_STACK_TRACE(common_flags()->malloc_context_size, \
98 common_flags()->fast_unwind_on_malloc)
100 #define GET_STACK_TRACE_FREE GET_STACK_TRACE_MALLOC
102 #define PRINT_CURRENT_STACK() \
104 GET_STACK_TRACE_FATAL_HERE; \
108 #define PRINT_CURRENT_STACK_CHECK() \
110 GET_STACK_TRACE_CHECK_HERE; \
114 #endif // ASAN_STACK_H