Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 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 | #ifndef ASAN_STACK_H |
| 15 | #define ASAN_STACK_H |
| 16 | |
Kostya Serebryany | a30c8f9 | 2012-12-13 09:34:23 +0000 | [diff] [blame] | 17 | #include "asan_flags.h" |
Sergey Matveev | 736cf49 | 2013-05-08 12:45:55 +0000 | [diff] [blame] | 18 | #include "asan_thread.h" |
Sergey Matveev | ed20ebe | 2013-05-06 11:27:58 +0000 | [diff] [blame] | 19 | #include "sanitizer_common/sanitizer_flags.h" |
| 20 | #include "sanitizer_common/sanitizer_stacktrace.h" |
Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 21 | |
| 22 | namespace __asan { |
| 23 | |
Stephen Hines | 86277eb | 2015-03-23 12:06:32 -0700 | [diff] [blame] | 24 | static const u32 kDefaultMallocContextSize = 30; |
| 25 | |
| 26 | void SetMallocContextSize(u32 size); |
| 27 | u32 GetMallocContextSize(); |
| 28 | |
Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 29 | // Get the stack trace with the given pc and bp. |
| 30 | // The pc will be in the position 0 of the resulting stack trace. |
| 31 | // The bp may refer to the current frame or to the caller's frame. |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 32 | ALWAYS_INLINE |
Stephen Hines | 6d18623 | 2014-11-26 17:56:19 -0800 | [diff] [blame] | 33 | void GetStackTraceWithPcBpAndContext(BufferedStackTrace *stack, uptr max_depth, |
| 34 | uptr pc, uptr bp, void *context, |
| 35 | bool fast) { |
Sergey Matveev | 736cf49 | 2013-05-08 12:45:55 +0000 | [diff] [blame] | 36 | #if SANITIZER_WINDOWS |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 37 | stack->Unwind(max_depth, pc, bp, context, 0, 0, fast); |
Sergey Matveev | 736cf49 | 2013-05-08 12:45:55 +0000 | [diff] [blame] | 38 | #else |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 39 | AsanThread *t; |
| 40 | stack->size = 0; |
| 41 | if (LIKELY(asan_inited)) { |
| 42 | if ((t = GetCurrentThread()) && !t->isUnwinding()) { |
Stephen Hines | 6d18623 | 2014-11-26 17:56:19 -0800 | [diff] [blame] | 43 | // On FreeBSD the slow unwinding that leverages _Unwind_Backtrace() |
| 44 | // yields the call stack of the signal's handler and not of the code |
| 45 | // that raised the signal (as it does on Linux). |
| 46 | if (SANITIZER_FREEBSD && t->isInDeadlySignal()) fast = true; |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 47 | uptr stack_top = t->stack_top(); |
| 48 | uptr stack_bottom = t->stack_bottom(); |
| 49 | ScopedUnwinding unwind_scope(t); |
| 50 | stack->Unwind(max_depth, pc, bp, context, stack_top, stack_bottom, fast); |
| 51 | } else if (t == 0 && !fast) { |
| 52 | /* If GetCurrentThread() has failed, try to do slow unwind anyways. */ |
| 53 | stack->Unwind(max_depth, pc, bp, context, 0, 0, false); |
| 54 | } |
Sergey Matveev | 736cf49 | 2013-05-08 12:45:55 +0000 | [diff] [blame] | 55 | } |
| 56 | #endif // SANITIZER_WINDOWS |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 57 | } |
| 58 | |
| 59 | } // namespace __asan |
Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 60 | |
Alexey Samsonov | 55cdfc6 | 2012-01-17 06:35:31 +0000 | [diff] [blame] | 61 | // NOTE: A Rule of thumb is to retrieve stack trace in the interceptors |
| 62 | // as early as possible (in functions exposed to the user), as we generally |
| 63 | // don't want stack trace to contain functions from ASan internals. |
| 64 | |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 65 | #define GET_STACK_TRACE(max_size, fast) \ |
Stephen Hines | 6d18623 | 2014-11-26 17:56:19 -0800 | [diff] [blame] | 66 | BufferedStackTrace stack; \ |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 67 | if (max_size <= 2) { \ |
| 68 | stack.size = max_size; \ |
| 69 | if (max_size > 0) { \ |
| 70 | stack.top_frame_bp = GET_CURRENT_FRAME(); \ |
Stephen Hines | 6d18623 | 2014-11-26 17:56:19 -0800 | [diff] [blame] | 71 | stack.trace_buffer[0] = StackTrace::GetCurrentPc(); \ |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 72 | if (max_size > 1) \ |
Stephen Hines | 6d18623 | 2014-11-26 17:56:19 -0800 | [diff] [blame] | 73 | stack.trace_buffer[1] = GET_CALLER_PC(); \ |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 74 | } \ |
| 75 | } else { \ |
| 76 | GetStackTraceWithPcBpAndContext(&stack, max_size, \ |
| 77 | StackTrace::GetCurrentPc(), \ |
| 78 | GET_CURRENT_FRAME(), 0, fast); \ |
| 79 | } |
Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 80 | |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 81 | #define GET_STACK_TRACE_FATAL(pc, bp) \ |
Stephen Hines | 6d18623 | 2014-11-26 17:56:19 -0800 | [diff] [blame] | 82 | BufferedStackTrace stack; \ |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 83 | GetStackTraceWithPcBpAndContext(&stack, kStackTraceMax, pc, bp, 0, \ |
| 84 | common_flags()->fast_unwind_on_fatal) |
| 85 | |
Stephen Hines | 86277eb | 2015-03-23 12:06:32 -0700 | [diff] [blame] | 86 | #define GET_STACK_TRACE_SIGNAL(sig) \ |
Stephen Hines | 6d18623 | 2014-11-26 17:56:19 -0800 | [diff] [blame] | 87 | BufferedStackTrace stack; \ |
Stephen Hines | 86277eb | 2015-03-23 12:06:32 -0700 | [diff] [blame] | 88 | GetStackTraceWithPcBpAndContext(&stack, kStackTraceMax, \ |
| 89 | (sig).pc, (sig).bp, (sig).context, \ |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 90 | common_flags()->fast_unwind_on_fatal) |
Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 91 | |
Sergey Matveev | ed20ebe | 2013-05-06 11:27:58 +0000 | [diff] [blame] | 92 | #define GET_STACK_TRACE_FATAL_HERE \ |
| 93 | GET_STACK_TRACE(kStackTraceMax, common_flags()->fast_unwind_on_fatal) |
Kostya Serebryany | a30c8f9 | 2012-12-13 09:34:23 +0000 | [diff] [blame] | 94 | |
Stephen Hines | 6d18623 | 2014-11-26 17:56:19 -0800 | [diff] [blame] | 95 | #define GET_STACK_TRACE_CHECK_HERE \ |
| 96 | GET_STACK_TRACE(kStackTraceMax, common_flags()->fast_unwind_on_check) |
| 97 | |
Sergey Matveev | ed20ebe | 2013-05-06 11:27:58 +0000 | [diff] [blame] | 98 | #define GET_STACK_TRACE_THREAD \ |
Kostya Serebryany | a30c8f9 | 2012-12-13 09:34:23 +0000 | [diff] [blame] | 99 | GET_STACK_TRACE(kStackTraceMax, true) |
| 100 | |
Stephen Hines | 86277eb | 2015-03-23 12:06:32 -0700 | [diff] [blame] | 101 | #define GET_STACK_TRACE_MALLOC \ |
| 102 | GET_STACK_TRACE(GetMallocContextSize(), common_flags()->fast_unwind_on_malloc) |
Kostya Serebryany | a30c8f9 | 2012-12-13 09:34:23 +0000 | [diff] [blame] | 103 | |
| 104 | #define GET_STACK_TRACE_FREE GET_STACK_TRACE_MALLOC |
Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 105 | |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 106 | #define PRINT_CURRENT_STACK() \ |
| 107 | { \ |
| 108 | GET_STACK_TRACE_FATAL_HERE; \ |
| 109 | stack.Print(); \ |
Chandler Carruth | 6bae39d | 2012-06-25 06:53:10 +0000 | [diff] [blame] | 110 | } |
Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 111 | |
Stephen Hines | 6d18623 | 2014-11-26 17:56:19 -0800 | [diff] [blame] | 112 | #define PRINT_CURRENT_STACK_CHECK() \ |
| 113 | { \ |
| 114 | GET_STACK_TRACE_CHECK_HERE; \ |
| 115 | stack.Print(); \ |
| 116 | } |
| 117 | |
Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 118 | #endif // ASAN_STACK_H |