asan_stack.h revision 6d95869fa900da9ddd68e15e2aa065854cfa176b
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);
25void PrintStack(const uptr *trace, uptr size);
26
27}  // namespace __asan
28
29// Get the stack trace with the given pc and bp.
30// The pc will be in the position 0 of the resulting stack trace.
31// The bp may refer to the current frame or to the caller's frame.
32#if SANITIZER_WINDOWS
33#define GET_STACK_TRACE_WITH_PC_AND_BP(max_s, pc, bp, fast) \
34  StackTrace stack;                                         \
35  stack.Unwind(max_s, pc, bp, 0, 0, fast)
36#else
37#define GET_STACK_TRACE_WITH_PC_AND_BP(max_s, pc, bp, fast)                \
38  StackTrace stack;                                                        \
39  {                                                                        \
40    AsanThread *t;                                                         \
41    stack.size = 0;                                                        \
42    if (asan_inited && (t = GetCurrentThread()) && !t->isUnwinding()) {    \
43      uptr stack_top = t->stack_top();                                     \
44      uptr stack_bottom = t->stack_bottom();                               \
45      ScopedUnwinding unwind_scope(t);                                     \
46      stack.Unwind(max_s, pc, bp, stack_top, stack_bottom, fast);          \
47    }                                                                      \
48  }
49#endif  // SANITIZER_WINDOWS
50
51// NOTE: A Rule of thumb is to retrieve stack trace in the interceptors
52// as early as possible (in functions exposed to the user), as we generally
53// don't want stack trace to contain functions from ASan internals.
54
55#define GET_STACK_TRACE(max_size, fast)                       \
56  GET_STACK_TRACE_WITH_PC_AND_BP(max_size,                    \
57      StackTrace::GetCurrentPc(), GET_CURRENT_FRAME(), fast)
58
59#define GET_STACK_TRACE_FATAL(pc, bp)                                 \
60  GET_STACK_TRACE_WITH_PC_AND_BP(kStackTraceMax, pc, bp,              \
61                                 common_flags()->fast_unwind_on_fatal)
62
63#define GET_STACK_TRACE_FATAL_HERE                                \
64  GET_STACK_TRACE(kStackTraceMax, common_flags()->fast_unwind_on_fatal)
65
66#define GET_STACK_TRACE_THREAD                                    \
67  GET_STACK_TRACE(kStackTraceMax, true)
68
69#define GET_STACK_TRACE_MALLOC                                    \
70  GET_STACK_TRACE(common_flags()->malloc_context_size,            \
71                  common_flags()->fast_unwind_on_malloc)
72
73#define GET_STACK_TRACE_FREE GET_STACK_TRACE_MALLOC
74
75#define PRINT_CURRENT_STACK()                    \
76  {                                              \
77    GET_STACK_TRACE(kStackTraceMax,              \
78      common_flags()->fast_unwind_on_fatal);     \
79    PrintStack(&stack);                          \
80  }
81
82#endif  // ASAN_STACK_H
83