1 //===-- asan_stack.h --------------------------------------------*- C++ -*-===//
3 // This file is distributed under the University of Illinois Open Source
4 // License. See LICENSE.TXT for details.
6 //===----------------------------------------------------------------------===//
8 // This file is a part of AddressSanitizer, an address sanity checker.
10 // ASan-private header for asan_stack.cc.
11 //===----------------------------------------------------------------------===//
16 #include "asan_flags.h"
17 #include "asan_thread.h"
18 #include "sanitizer_common/sanitizer_flags.h"
19 #include "sanitizer_common/sanitizer_stacktrace.h"
23 static const u32 kDefaultMallocContextSize
= 30;
25 void SetMallocContextSize(u32 size
);
26 u32
GetMallocContextSize();
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.
32 void GetStackTraceWithPcBpAndContext(BufferedStackTrace
*stack
, uptr max_depth
,
33 uptr pc
, uptr bp
, void *context
,
36 stack
->Unwind(max_depth
, pc
, bp
, context
, 0, 0, fast
);
40 if (LIKELY(asan_inited
)) {
41 if ((t
= GetCurrentThread()) && !t
->isUnwinding()) {
42 uptr stack_top
= t
->stack_top();
43 uptr stack_bottom
= t
->stack_bottom();
44 ScopedUnwinding
unwind_scope(t
);
45 if (!SANITIZER_MIPS
|| IsValidFrame(bp
, stack_top
, stack_bottom
)) {
46 stack
->Unwind(max_depth
, pc
, bp
, context
, stack_top
, stack_bottom
,
49 } else if (!t
&& !fast
) {
50 /* If GetCurrentThread() has failed, try to do slow unwind anyways. */
51 stack
->Unwind(max_depth
, pc
, bp
, context
, 0, 0, false);
54 #endif // SANITIZER_WINDOWS
59 // NOTE: A Rule of thumb is to retrieve stack trace in the interceptors
60 // as early as possible (in functions exposed to the user), as we generally
61 // don't want stack trace to contain functions from ASan internals.
63 #define GET_STACK_TRACE(max_size, fast) \
64 BufferedStackTrace stack; \
65 if (max_size <= 2) { \
66 stack.size = max_size; \
68 stack.top_frame_bp = GET_CURRENT_FRAME(); \
69 stack.trace_buffer[0] = StackTrace::GetCurrentPc(); \
71 stack.trace_buffer[1] = GET_CALLER_PC(); \
74 GetStackTraceWithPcBpAndContext(&stack, max_size, \
75 StackTrace::GetCurrentPc(), \
76 GET_CURRENT_FRAME(), 0, fast); \
79 #define GET_STACK_TRACE_FATAL(pc, bp) \
80 BufferedStackTrace stack; \
81 GetStackTraceWithPcBpAndContext(&stack, kStackTraceMax, pc, bp, 0, \
82 common_flags()->fast_unwind_on_fatal)
84 #define GET_STACK_TRACE_SIGNAL(sig) \
85 BufferedStackTrace stack; \
86 GetStackTraceWithPcBpAndContext(&stack, kStackTraceMax, \
87 (sig).pc, (sig).bp, (sig).context, \
88 common_flags()->fast_unwind_on_fatal)
90 #define GET_STACK_TRACE_FATAL_HERE \
91 GET_STACK_TRACE(kStackTraceMax, common_flags()->fast_unwind_on_fatal)
93 #define GET_STACK_TRACE_CHECK_HERE \
94 GET_STACK_TRACE(kStackTraceMax, common_flags()->fast_unwind_on_check)
96 #define GET_STACK_TRACE_THREAD \
97 GET_STACK_TRACE(kStackTraceMax, true)
99 #define GET_STACK_TRACE_MALLOC \
100 GET_STACK_TRACE(GetMallocContextSize(), common_flags()->fast_unwind_on_malloc)
102 #define GET_STACK_TRACE_FREE GET_STACK_TRACE_MALLOC
104 #define PRINT_CURRENT_STACK() \
106 GET_STACK_TRACE_FATAL_HERE; \
110 #define PRINT_CURRENT_STACK_CHECK() \
112 GET_STACK_TRACE_CHECK_HERE; \
116 #endif // ASAN_STACK_H