Add support for ARMv8-R architecture
[official-gcc.git] / libsanitizer / asan / asan_stack.h
blob2b8738156ea00b65f0b6ef1ec3417b77961709dd
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 //===----------------------------------------------------------------------===//
13 #ifndef ASAN_STACK_H
14 #define ASAN_STACK_H
16 #include "asan_flags.h"
17 #include "asan_thread.h"
18 #include "sanitizer_common/sanitizer_flags.h"
19 #include "sanitizer_common/sanitizer_stacktrace.h"
21 namespace __asan {
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.
31 ALWAYS_INLINE
32 void GetStackTraceWithPcBpAndContext(BufferedStackTrace *stack, uptr max_depth,
33 uptr pc, uptr bp, void *context,
34 bool fast) {
35 #if SANITIZER_WINDOWS
36 stack->Unwind(max_depth, pc, bp, context, 0, 0, fast);
37 #else
38 AsanThread *t;
39 stack->size = 0;
40 if (LIKELY(asan_inited)) {
41 if ((t = GetCurrentThread()) && !t->isUnwinding()) {
42 // On FreeBSD the slow unwinding that leverages _Unwind_Backtrace()
43 // yields the call stack of the signal's handler and not of the code
44 // that raised the signal (as it does on Linux).
45 if (SANITIZER_FREEBSD && t->isInDeadlySignal()) fast = true;
46 uptr stack_top = t->stack_top();
47 uptr stack_bottom = t->stack_bottom();
48 ScopedUnwinding unwind_scope(t);
49 if (!SANITIZER_MIPS || IsValidFrame(bp, stack_top, stack_bottom)) {
50 stack->Unwind(max_depth, pc, bp, context, stack_top, stack_bottom,
51 fast);
53 } else if (!t && !fast) {
54 /* If GetCurrentThread() has failed, try to do slow unwind anyways. */
55 stack->Unwind(max_depth, pc, bp, context, 0, 0, false);
58 #endif // SANITIZER_WINDOWS
61 } // namespace __asan
63 // NOTE: A Rule of thumb is to retrieve stack trace in the interceptors
64 // as early as possible (in functions exposed to the user), as we generally
65 // don't want stack trace to contain functions from ASan internals.
67 #define GET_STACK_TRACE(max_size, fast) \
68 BufferedStackTrace stack; \
69 if (max_size <= 2) { \
70 stack.size = max_size; \
71 if (max_size > 0) { \
72 stack.top_frame_bp = GET_CURRENT_FRAME(); \
73 stack.trace_buffer[0] = StackTrace::GetCurrentPc(); \
74 if (max_size > 1) \
75 stack.trace_buffer[1] = GET_CALLER_PC(); \
76 } \
77 } else { \
78 GetStackTraceWithPcBpAndContext(&stack, max_size, \
79 StackTrace::GetCurrentPc(), \
80 GET_CURRENT_FRAME(), 0, fast); \
83 #define GET_STACK_TRACE_FATAL(pc, bp) \
84 BufferedStackTrace stack; \
85 GetStackTraceWithPcBpAndContext(&stack, kStackTraceMax, pc, bp, 0, \
86 common_flags()->fast_unwind_on_fatal)
88 #define GET_STACK_TRACE_SIGNAL(sig) \
89 BufferedStackTrace stack; \
90 GetStackTraceWithPcBpAndContext(&stack, kStackTraceMax, \
91 (sig).pc, (sig).bp, (sig).context, \
92 common_flags()->fast_unwind_on_fatal)
94 #define GET_STACK_TRACE_FATAL_HERE \
95 GET_STACK_TRACE(kStackTraceMax, common_flags()->fast_unwind_on_fatal)
97 #define GET_STACK_TRACE_CHECK_HERE \
98 GET_STACK_TRACE(kStackTraceMax, common_flags()->fast_unwind_on_check)
100 #define GET_STACK_TRACE_THREAD \
101 GET_STACK_TRACE(kStackTraceMax, true)
103 #define GET_STACK_TRACE_MALLOC \
104 GET_STACK_TRACE(GetMallocContextSize(), common_flags()->fast_unwind_on_malloc)
106 #define GET_STACK_TRACE_FREE GET_STACK_TRACE_MALLOC
108 #define PRINT_CURRENT_STACK() \
110 GET_STACK_TRACE_FATAL_HERE; \
111 stack.Print(); \
114 #define PRINT_CURRENT_STACK_CHECK() \
116 GET_STACK_TRACE_CHECK_HERE; \
117 stack.Print(); \
120 #endif // ASAN_STACK_H