1 //===-- asan_stack.h --------------------------------------------*- C++ -*-===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file is a part of AddressSanitizer, an address sanity checker.
11 //
12 // ASan-private header for asan_stack.cc.
13 //===----------------------------------------------------------------------===//
14
15 #ifndef ASAN_STACK_H
16 #define ASAN_STACK_H
17
18 #include "asan_flags.h"
19 #include "asan_thread.h"
20 #include "sanitizer_common/sanitizer_flags.h"
21 #include "sanitizer_common/sanitizer_stacktrace.h"
22
23 namespace __asan {
24
25 static const u32 kDefaultMallocContextSize = 30;
26
27 void SetMallocContextSize(u32 size);
28 u32 GetMallocContextSize();
29
30 // Get the stack trace with the given pc and bp.
31 // The pc will be in the position 0 of the resulting stack trace.
32 // The bp may refer to the current frame or to the caller's frame.
33 ALWAYS_INLINE
GetStackTraceWithPcBpAndContext(BufferedStackTrace * stack,uptr max_depth,uptr pc,uptr bp,void * context,bool fast)34 void GetStackTraceWithPcBpAndContext(BufferedStackTrace *stack, uptr max_depth,
35 uptr pc, uptr bp, void *context,
36 bool fast) {
37 #if SANITIZER_WINDOWS
38 stack->Unwind(max_depth, pc, bp, context, 0, 0, fast);
39 #else
40 AsanThread *t;
41 stack->size = 0;
42 if (LIKELY(asan_inited)) {
43 if ((t = GetCurrentThread()) && !t->isUnwinding()) {
44 // On FreeBSD the slow unwinding that leverages _Unwind_Backtrace()
45 // yields the call stack of the signal's handler and not of the code
46 // that raised the signal (as it does on Linux).
47 if (SANITIZER_FREEBSD && t->isInDeadlySignal()) fast = true;
48 uptr stack_top = t->stack_top();
49 uptr stack_bottom = t->stack_bottom();
50 ScopedUnwinding unwind_scope(t);
51 stack->Unwind(max_depth, pc, bp, context, stack_top, stack_bottom, fast);
52 } else if (!t && !fast) {
53 /* If GetCurrentThread() has failed, try to do slow unwind anyways. */
54 stack->Unwind(max_depth, pc, bp, context, 0, 0, false);
55 }
56 }
57 #endif // SANITIZER_WINDOWS
58 }
59
60 } // namespace __asan
61
62 // NOTE: A Rule of thumb is to retrieve stack trace in the interceptors
63 // as early as possible (in functions exposed to the user), as we generally
64 // don't want stack trace to contain functions from ASan internals.
65
66 #define GET_STACK_TRACE(max_size, fast) \
67 BufferedStackTrace stack; \
68 if (max_size <= 2) { \
69 stack.size = max_size; \
70 if (max_size > 0) { \
71 stack.top_frame_bp = GET_CURRENT_FRAME(); \
72 stack.trace_buffer[0] = StackTrace::GetCurrentPc(); \
73 if (max_size > 1) \
74 stack.trace_buffer[1] = GET_CALLER_PC(); \
75 } \
76 } else { \
77 GetStackTraceWithPcBpAndContext(&stack, max_size, \
78 StackTrace::GetCurrentPc(), \
79 GET_CURRENT_FRAME(), 0, fast); \
80 }
81
82 #define GET_STACK_TRACE_FATAL(pc, bp) \
83 BufferedStackTrace stack; \
84 GetStackTraceWithPcBpAndContext(&stack, kStackTraceMax, pc, bp, 0, \
85 common_flags()->fast_unwind_on_fatal)
86
87 #define GET_STACK_TRACE_SIGNAL(sig) \
88 BufferedStackTrace stack; \
89 GetStackTraceWithPcBpAndContext(&stack, kStackTraceMax, \
90 (sig).pc, (sig).bp, (sig).context, \
91 common_flags()->fast_unwind_on_fatal)
92
93 #define GET_STACK_TRACE_FATAL_HERE \
94 GET_STACK_TRACE(kStackTraceMax, common_flags()->fast_unwind_on_fatal)
95
96 #define GET_STACK_TRACE_CHECK_HERE \
97 GET_STACK_TRACE(kStackTraceMax, common_flags()->fast_unwind_on_check)
98
99 #define GET_STACK_TRACE_THREAD \
100 GET_STACK_TRACE(kStackTraceMax, true)
101
102 #define GET_STACK_TRACE_MALLOC \
103 GET_STACK_TRACE(GetMallocContextSize(), common_flags()->fast_unwind_on_malloc)
104
105 #define GET_STACK_TRACE_FREE GET_STACK_TRACE_MALLOC
106
107 #define PRINT_CURRENT_STACK() \
108 { \
109 GET_STACK_TRACE_FATAL_HERE; \
110 stack.Print(); \
111 }
112
113 #define PRINT_CURRENT_STACK_CHECK() \
114 { \
115 GET_STACK_TRACE_CHECK_HERE; \
116 stack.Print(); \
117 }
118
119 #endif // ASAN_STACK_H
120