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