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