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 GetStackTrace(BufferedStackTrace *stack, uptr max_depth, uptr pc, uptr bp, 35 void *context, bool fast) { 36 #if SANITIZER_WINDOWS 37 stack->Unwind(max_depth, pc, bp, context, 0, 0, fast); 38 #else 39 AsanThread *t; 40 stack->size = 0; 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, 48 fast); 49 } 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); 53 } 54 } 55 #endif // SANITIZER_WINDOWS 56 } 57 58 } // namespace __asan 59 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. 63 64 #define GET_STACK_TRACE(max_size, fast) \ 65 BufferedStackTrace stack; \ 66 if (max_size <= 2) { \ 67 stack.size = max_size; \ 68 if (max_size > 0) { \ 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(); \ 72 } \ 73 } else { \ 74 GetStackTrace(&stack, max_size, StackTrace::GetCurrentPc(), \ 75 GET_CURRENT_FRAME(), 0, fast); \ 76 } 77 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) 82 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) 87 88 #define GET_STACK_TRACE_FATAL_HERE \ 89 GET_STACK_TRACE(kStackTraceMax, common_flags()->fast_unwind_on_fatal) 90 91 #define GET_STACK_TRACE_CHECK_HERE \ 92 GET_STACK_TRACE(kStackTraceMax, common_flags()->fast_unwind_on_check) 93 94 #define GET_STACK_TRACE_THREAD \ 95 GET_STACK_TRACE(kStackTraceMax, true) 96 97 #define GET_STACK_TRACE_MALLOC \ 98 GET_STACK_TRACE(GetMallocContextSize(), common_flags()->fast_unwind_on_malloc) 99 100 #define GET_STACK_TRACE_FREE GET_STACK_TRACE_MALLOC 101 102 #define PRINT_CURRENT_STACK() \ 103 { \ 104 GET_STACK_TRACE_FATAL_HERE; \ 105 stack.Print(); \ 106 } 107 108 #define PRINT_CURRENT_STACK_CHECK() \ 109 { \ 110 GET_STACK_TRACE_CHECK_HERE; \ 111 stack.Print(); \ 112 } 113 114 #endif // ASAN_STACK_H 115