1 //===-- asan_stack.h --------------------------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file is a part of AddressSanitizer, an address sanity checker.
12 // ASan-private header for asan_stack.cc.
13 //===----------------------------------------------------------------------===//
18 #include "asan_flags.h"
19 #include "asan_thread.h"
20 #include "sanitizer_common/sanitizer_flags.h"
21 #include "sanitizer_common/sanitizer_stacktrace.h"
25 static const u32 kDefaultMallocContextSize
= 30;
27 void SetMallocContextSize(u32 size
);
28 u32
GetMallocContextSize();
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.
34 void GetStackTrace(BufferedStackTrace
*stack
, uptr max_depth
, uptr pc
, uptr bp
,
35 void *context
, bool fast
) {
37 stack
->Unwind(max_depth
, pc
, bp
, context
, 0, 0, fast
);
41 if (LIKELY(asan_inited
)) {
42 if ((t
= GetCurrentThread()) && !t
->isUnwinding()) {
43 uptr stack_top
= t
->stack_top();
44 uptr stack_bottom
= t
->stack_bottom();
45 ScopedUnwinding
unwind_scope(t
);
46 if (!SANITIZER_MIPS
|| IsValidFrame(bp
, stack_top
, stack_bottom
)) {
47 stack
->Unwind(max_depth
, pc
, bp
, context
, stack_top
, stack_bottom
,
50 } else if (!t
&& !fast
) {
51 /* If GetCurrentThread() has failed, try to do slow unwind anyways. */
52 stack
->Unwind(max_depth
, pc
, bp
, context
, 0, 0, false);
55 #endif // SANITIZER_WINDOWS
60 // NOTE: A Rule of thumb is to retrieve stack trace in the interceptors
61 // as early as possible (in functions exposed to the user), as we generally
62 // don't want stack trace to contain functions from ASan internals.
64 #define GET_STACK_TRACE(max_size, fast) \
65 BufferedStackTrace stack; \
66 if (max_size <= 2) { \
67 stack.size = max_size; \
69 stack.top_frame_bp = GET_CURRENT_FRAME(); \
70 stack.trace_buffer[0] = StackTrace::GetCurrentPc(); \
71 if (max_size > 1) stack.trace_buffer[1] = GET_CALLER_PC(); \
74 GetStackTrace(&stack, max_size, StackTrace::GetCurrentPc(), \
75 GET_CURRENT_FRAME(), 0, fast); \
78 #define GET_STACK_TRACE_FATAL(pc, bp) \
79 BufferedStackTrace stack; \
80 GetStackTrace(&stack, kStackTraceMax, pc, bp, 0, \
81 common_flags()->fast_unwind_on_fatal)
83 #define GET_STACK_TRACE_SIGNAL(sig) \
84 BufferedStackTrace stack; \
85 GetStackTrace(&stack, kStackTraceMax, (sig).pc, (sig).bp, (sig).context, \
86 common_flags()->fast_unwind_on_fatal)
88 #define GET_STACK_TRACE_FATAL_HERE \
89 GET_STACK_TRACE(kStackTraceMax, common_flags()->fast_unwind_on_fatal)
91 #define GET_STACK_TRACE_CHECK_HERE \
92 GET_STACK_TRACE(kStackTraceMax, common_flags()->fast_unwind_on_check)
94 #define GET_STACK_TRACE_THREAD \
95 GET_STACK_TRACE(kStackTraceMax, true)
97 #define GET_STACK_TRACE_MALLOC \
98 GET_STACK_TRACE(GetMallocContextSize(), common_flags()->fast_unwind_on_malloc)
100 #define GET_STACK_TRACE_FREE GET_STACK_TRACE_MALLOC
102 #define PRINT_CURRENT_STACK() \
104 GET_STACK_TRACE_FATAL_HERE; \
108 #define PRINT_CURRENT_STACK_CHECK() \
110 GET_STACK_TRACE_CHECK_HERE; \
114 #endif // ASAN_STACK_H