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 15 #ifndef ASAN_STACK_H 16 #define ASAN_STACK_H 17 18 #include "asan_flags.h" 19 #include "asan_thread.h" 20 #include "sanitizer_common/sanitizer_flags.h" 21 #include "sanitizer_common/sanitizer_stacktrace.h" 22 23 namespace __asan { 24 25 static const u32 kDefaultMallocContextSize = 30; 26 27 void SetMallocContextSize(u32 size); 28 u32 GetMallocContextSize(); 29 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. 33 ALWAYS_INLINE 34 void GetStackTraceWithPcBpAndContext(BufferedStackTrace *stack, uptr max_depth, 35 uptr pc, uptr bp, void *context, 36 bool fast) { 37 #if SANITIZER_WINDOWS 38 stack->Unwind(max_depth, pc, bp, context, 0, 0, fast); 39 #else 40 AsanThread *t; 41 stack->size = 0; 42 if (LIKELY(asan_inited)) { 43 if ((t = GetCurrentThread()) && !t->isUnwinding()) { 44 // On FreeBSD the slow unwinding that leverages _Unwind_Backtrace() 45 // yields the call stack of the signal's handler and not of the code 46 // that raised the signal (as it does on Linux). 47 if ((SANITIZER_FREEBSD || SANITIZER_NETBSD) && t->isInDeadlySignal()) 48 fast = true; 49 uptr stack_top = t->stack_top(); 50 uptr stack_bottom = t->stack_bottom(); 51 ScopedUnwinding unwind_scope(t); 52 if (!SANITIZER_MIPS || IsValidFrame(bp, stack_top, stack_bottom)) { 53 stack->Unwind(max_depth, pc, bp, context, stack_top, stack_bottom, 54 fast); 55 } 56 } else if (!t && !fast) { 57 /* If GetCurrentThread() has failed, try to do slow unwind anyways. */ 58 stack->Unwind(max_depth, pc, bp, context, 0, 0, false); 59 } 60 } 61 #endif // SANITIZER_WINDOWS 62 } 63 64 } // namespace __asan 65 66 // NOTE: A Rule of thumb is to retrieve stack trace in the interceptors 67 // as early as possible (in functions exposed to the user), as we generally 68 // don't want stack trace to contain functions from ASan internals. 69 70 #define GET_STACK_TRACE(max_size, fast) \ 71 BufferedStackTrace stack; \ 72 if (max_size <= 2) { \ 73 stack.size = max_size; \ 74 if (max_size > 0) { \ 75 stack.top_frame_bp = GET_CURRENT_FRAME(); \ 76 stack.trace_buffer[0] = StackTrace::GetCurrentPc(); \ 77 if (max_size > 1) \ 78 stack.trace_buffer[1] = GET_CALLER_PC(); \ 79 } \ 80 } else { \ 81 GetStackTraceWithPcBpAndContext(&stack, max_size, \ 82 StackTrace::GetCurrentPc(), \ 83 GET_CURRENT_FRAME(), 0, fast); \ 84 } 85 86 #define GET_STACK_TRACE_FATAL(pc, bp) \ 87 BufferedStackTrace stack; \ 88 GetStackTraceWithPcBpAndContext(&stack, kStackTraceMax, pc, bp, 0, \ 89 common_flags()->fast_unwind_on_fatal) 90 91 #define GET_STACK_TRACE_SIGNAL(sig) \ 92 BufferedStackTrace stack; \ 93 GetStackTraceWithPcBpAndContext(&stack, kStackTraceMax, \ 94 (sig).pc, (sig).bp, (sig).context, \ 95 common_flags()->fast_unwind_on_fatal) 96 97 #define GET_STACK_TRACE_FATAL_HERE \ 98 GET_STACK_TRACE(kStackTraceMax, common_flags()->fast_unwind_on_fatal) 99 100 #define GET_STACK_TRACE_CHECK_HERE \ 101 GET_STACK_TRACE(kStackTraceMax, common_flags()->fast_unwind_on_check) 102 103 #define GET_STACK_TRACE_THREAD \ 104 GET_STACK_TRACE(kStackTraceMax, true) 105 106 #define GET_STACK_TRACE_MALLOC \ 107 GET_STACK_TRACE(GetMallocContextSize(), common_flags()->fast_unwind_on_malloc) 108 109 #define GET_STACK_TRACE_FREE GET_STACK_TRACE_MALLOC 110 111 #define PRINT_CURRENT_STACK() \ 112 { \ 113 GET_STACK_TRACE_FATAL_HERE; \ 114 stack.Print(); \ 115 } 116 117 #define PRINT_CURRENT_STACK_CHECK() \ 118 { \ 119 GET_STACK_TRACE_CHECK_HERE; \ 120 stack.Print(); \ 121 } 122 123 #endif // ASAN_STACK_H 124