asan_stack.h revision 96a575f05b2a45774170a118ea69ddae3659b645
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#ifndef ASAN_STACK_H
15#define ASAN_STACK_H
16
17#include "asan_flags.h"
18#include "asan_thread.h"
19#include "sanitizer_common/sanitizer_flags.h"
20#include "sanitizer_common/sanitizer_stacktrace.h"
21
22namespace __asan {
23
24void PrintStack(StackTrace *stack);
25
26}  // namespace __asan
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#if SANITIZER_WINDOWS
32#define GET_STACK_TRACE_WITH_PC_AND_BP(max_s, pc, bp, fast) \
33  StackTrace stack;                                         \
34  GetStackTrace(&stack, max_s, pc, bp, 0, 0, fast)
35#else
36#define GET_STACK_TRACE_WITH_PC_AND_BP(max_s, pc, bp, fast)                \
37  StackTrace stack;                                                        \
38  {                                                                        \
39    AsanThread *t;                                                         \
40    stack.size = 0;                                                        \
41    if (asan_inited && (t = GetCurrentThread()) && !t->isUnwinding()) {    \
42      uptr stack_top = t->stack_top();                                     \
43      uptr stack_bottom = t->stack_bottom();                               \
44      ScopedUnwinding unwind_scope(t);                                     \
45      GetStackTrace(&stack, max_s, pc, bp, stack_top, stack_bottom, fast); \
46    }                                                                      \
47  }
48#endif  // SANITIZER_WINDOWS
49
50// NOTE: A Rule of thumb is to retrieve stack trace in the interceptors
51// as early as possible (in functions exposed to the user), as we generally
52// don't want stack trace to contain functions from ASan internals.
53
54#define GET_STACK_TRACE(max_size, fast)                       \
55  GET_STACK_TRACE_WITH_PC_AND_BP(max_size,                    \
56      StackTrace::GetCurrentPc(), GET_CURRENT_FRAME(), fast)
57
58#define GET_STACK_TRACE_FATAL(pc, bp)                                 \
59  GET_STACK_TRACE_WITH_PC_AND_BP(kStackTraceMax, pc, bp,              \
60                                 common_flags()->fast_unwind_on_fatal)
61
62#define GET_STACK_TRACE_FATAL_HERE                                \
63  GET_STACK_TRACE(kStackTraceMax, common_flags()->fast_unwind_on_fatal)
64
65#define GET_STACK_TRACE_THREAD                                    \
66  GET_STACK_TRACE(kStackTraceMax, true)
67
68#define GET_STACK_TRACE_MALLOC                                    \
69  GET_STACK_TRACE(common_flags()->malloc_context_size,            \
70                  common_flags()->fast_unwind_on_malloc)
71
72#define GET_STACK_TRACE_FREE GET_STACK_TRACE_MALLOC
73
74#define PRINT_CURRENT_STACK()                    \
75  {                                              \
76    GET_STACK_TRACE(kStackTraceMax,              \
77      common_flags()->fast_unwind_on_fatal);     \
78    PrintStack(&stack);                          \
79  }
80
81#endif  // ASAN_STACK_H
82