Kostya Serebryany | 019b76f | 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 | //===----------------------------------------------------------------------===// |
Vedant Kumar | 59ba7b8 | 2015-10-01 00:22:21 +0000 | [diff] [blame] | 14 | |
Kostya Serebryany | 019b76f | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 15 | #ifndef ASAN_STACK_H |
| 16 | #define ASAN_STACK_H |
| 17 | |
Kostya Serebryany | baf583c | 2012-12-13 09:34:23 +0000 | [diff] [blame] | 18 | #include "asan_flags.h" |
Sergey Matveev | af179b8 | 2013-05-08 12:45:55 +0000 | [diff] [blame] | 19 | #include "asan_thread.h" |
Sergey Matveev | 0c8ed9c | 2013-05-06 11:27:58 +0000 | [diff] [blame] | 20 | #include "sanitizer_common/sanitizer_flags.h" |
| 21 | #include "sanitizer_common/sanitizer_stacktrace.h" |
Kostya Serebryany | 019b76f | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 22 | |
Alexey Samsonov | 3d9adc0 | 2014-03-04 13:12:25 +0000 | [diff] [blame] | 23 | namespace __asan { |
| 24 | |
Alexey Samsonov | bba821b | 2014-12-16 01:23:03 +0000 | [diff] [blame] | 25 | static const u32 kDefaultMallocContextSize = 30; |
| 26 | |
| 27 | void SetMallocContextSize(u32 size); |
| 28 | u32 GetMallocContextSize(); |
| 29 | |
Kostya Serebryany | 019b76f | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 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. |
Alexey Samsonov | 3d9adc0 | 2014-03-04 13:12:25 +0000 | [diff] [blame] | 33 | ALWAYS_INLINE |
Alexey Samsonov | 9c85927 | 2014-10-26 03:35:14 +0000 | [diff] [blame] | 34 | void GetStackTraceWithPcBpAndContext(BufferedStackTrace *stack, uptr max_depth, |
| 35 | uptr pc, uptr bp, void *context, |
| 36 | bool fast) { |
Sergey Matveev | af179b8 | 2013-05-08 12:45:55 +0000 | [diff] [blame] | 37 | #if SANITIZER_WINDOWS |
Alexey Samsonov | 3d9adc0 | 2014-03-04 13:12:25 +0000 | [diff] [blame] | 38 | stack->Unwind(max_depth, pc, bp, context, 0, 0, fast); |
Sergey Matveev | af179b8 | 2013-05-08 12:45:55 +0000 | [diff] [blame] | 39 | #else |
Alexey Samsonov | 3d9adc0 | 2014-03-04 13:12:25 +0000 | [diff] [blame] | 40 | AsanThread *t; |
| 41 | stack->size = 0; |
Kostya Serebryany | b9e31d7 | 2014-05-14 14:03:31 +0000 | [diff] [blame] | 42 | if (LIKELY(asan_inited)) { |
Alexey Samsonov | 3d9adc0 | 2014-03-04 13:12:25 +0000 | [diff] [blame] | 43 | if ((t = GetCurrentThread()) && !t->isUnwinding()) { |
Viktor Kutuzov | dc6cbfe | 2014-11-10 15:22:04 +0000 | [diff] [blame] | 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 && t->isInDeadlySignal()) fast = true; |
Alexey Samsonov | 3d9adc0 | 2014-03-04 13:12:25 +0000 | [diff] [blame] | 48 | uptr stack_top = t->stack_top(); |
| 49 | uptr stack_bottom = t->stack_bottom(); |
| 50 | ScopedUnwinding unwind_scope(t); |
Sagar Thakur | 7bec3a9 | 2016-05-18 06:09:26 +0000 | [diff] [blame] | 51 | if (!SANITIZER_MIPS || IsValidFrame(bp, stack_top, stack_bottom)) { |
| 52 | stack->Unwind(max_depth, pc, bp, context, stack_top, stack_bottom, |
| 53 | fast); |
| 54 | } |
Vedant Kumar | 59ba7b8 | 2015-10-01 00:22:21 +0000 | [diff] [blame] | 55 | } else if (!t && !fast) { |
Alexey Samsonov | 3d9adc0 | 2014-03-04 13:12:25 +0000 | [diff] [blame] | 56 | /* If GetCurrentThread() has failed, try to do slow unwind anyways. */ |
| 57 | stack->Unwind(max_depth, pc, bp, context, 0, 0, false); |
| 58 | } |
Sergey Matveev | af179b8 | 2013-05-08 12:45:55 +0000 | [diff] [blame] | 59 | } |
Vedant Kumar | 59ba7b8 | 2015-10-01 00:22:21 +0000 | [diff] [blame] | 60 | #endif // SANITIZER_WINDOWS |
Alexey Samsonov | 3d9adc0 | 2014-03-04 13:12:25 +0000 | [diff] [blame] | 61 | } |
| 62 | |
Vedant Kumar | 59ba7b8 | 2015-10-01 00:22:21 +0000 | [diff] [blame] | 63 | } // namespace __asan |
Kostya Serebryany | 019b76f | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 64 | |
Alexey Samsonov | 2d3a67b | 2012-01-17 06:35:31 +0000 | [diff] [blame] | 65 | // NOTE: A Rule of thumb is to retrieve stack trace in the interceptors |
| 66 | // as early as possible (in functions exposed to the user), as we generally |
| 67 | // don't want stack trace to contain functions from ASan internals. |
| 68 | |
Evgeniy Stepanov | 769d46f | 2014-02-11 13:38:57 +0000 | [diff] [blame] | 69 | #define GET_STACK_TRACE(max_size, fast) \ |
Alexey Samsonov | 9c85927 | 2014-10-26 03:35:14 +0000 | [diff] [blame] | 70 | BufferedStackTrace stack; \ |
Alexey Samsonov | 3d9adc0 | 2014-03-04 13:12:25 +0000 | [diff] [blame] | 71 | if (max_size <= 2) { \ |
| 72 | stack.size = max_size; \ |
| 73 | if (max_size > 0) { \ |
| 74 | stack.top_frame_bp = GET_CURRENT_FRAME(); \ |
Alexey Samsonov | 9c85927 | 2014-10-26 03:35:14 +0000 | [diff] [blame] | 75 | stack.trace_buffer[0] = StackTrace::GetCurrentPc(); \ |
Alexey Samsonov | 359c105 | 2014-03-04 14:06:11 +0000 | [diff] [blame] | 76 | if (max_size > 1) \ |
Alexey Samsonov | 9c85927 | 2014-10-26 03:35:14 +0000 | [diff] [blame] | 77 | stack.trace_buffer[1] = GET_CALLER_PC(); \ |
Alexey Samsonov | 3d9adc0 | 2014-03-04 13:12:25 +0000 | [diff] [blame] | 78 | } \ |
Alexey Samsonov | 3d9adc0 | 2014-03-04 13:12:25 +0000 | [diff] [blame] | 79 | } else { \ |
| 80 | GetStackTraceWithPcBpAndContext(&stack, max_size, \ |
| 81 | StackTrace::GetCurrentPc(), \ |
| 82 | GET_CURRENT_FRAME(), 0, fast); \ |
| 83 | } |
Kostya Serebryany | 019b76f | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 84 | |
Alexey Samsonov | 3d9adc0 | 2014-03-04 13:12:25 +0000 | [diff] [blame] | 85 | #define GET_STACK_TRACE_FATAL(pc, bp) \ |
Alexey Samsonov | 9c85927 | 2014-10-26 03:35:14 +0000 | [diff] [blame] | 86 | BufferedStackTrace stack; \ |
Alexey Samsonov | 3d9adc0 | 2014-03-04 13:12:25 +0000 | [diff] [blame] | 87 | GetStackTraceWithPcBpAndContext(&stack, kStackTraceMax, pc, bp, 0, \ |
| 88 | common_flags()->fast_unwind_on_fatal) |
Evgeniy Stepanov | 769d46f | 2014-02-11 13:38:57 +0000 | [diff] [blame] | 89 | |
Viktor Kutuzov | 9b75b75 | 2014-11-25 13:00:21 +0000 | [diff] [blame] | 90 | #define GET_STACK_TRACE_SIGNAL(sig) \ |
Alexey Samsonov | 9c85927 | 2014-10-26 03:35:14 +0000 | [diff] [blame] | 91 | BufferedStackTrace stack; \ |
Viktor Kutuzov | 9b75b75 | 2014-11-25 13:00:21 +0000 | [diff] [blame] | 92 | GetStackTraceWithPcBpAndContext(&stack, kStackTraceMax, \ |
| 93 | (sig).pc, (sig).bp, (sig).context, \ |
Alexey Samsonov | 3d9adc0 | 2014-03-04 13:12:25 +0000 | [diff] [blame] | 94 | common_flags()->fast_unwind_on_fatal) |
Kostya Serebryany | 019b76f | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 95 | |
Sergey Matveev | 0c8ed9c | 2013-05-06 11:27:58 +0000 | [diff] [blame] | 96 | #define GET_STACK_TRACE_FATAL_HERE \ |
| 97 | GET_STACK_TRACE(kStackTraceMax, common_flags()->fast_unwind_on_fatal) |
Kostya Serebryany | baf583c | 2012-12-13 09:34:23 +0000 | [diff] [blame] | 98 | |
Evgeniy Stepanov | f518a4e | 2014-10-14 09:36:24 +0000 | [diff] [blame] | 99 | #define GET_STACK_TRACE_CHECK_HERE \ |
| 100 | GET_STACK_TRACE(kStackTraceMax, common_flags()->fast_unwind_on_check) |
| 101 | |
Sergey Matveev | 0c8ed9c | 2013-05-06 11:27:58 +0000 | [diff] [blame] | 102 | #define GET_STACK_TRACE_THREAD \ |
Kostya Serebryany | baf583c | 2012-12-13 09:34:23 +0000 | [diff] [blame] | 103 | GET_STACK_TRACE(kStackTraceMax, true) |
| 104 | |
Alexey Samsonov | bba821b | 2014-12-16 01:23:03 +0000 | [diff] [blame] | 105 | #define GET_STACK_TRACE_MALLOC \ |
| 106 | GET_STACK_TRACE(GetMallocContextSize(), common_flags()->fast_unwind_on_malloc) |
Kostya Serebryany | baf583c | 2012-12-13 09:34:23 +0000 | [diff] [blame] | 107 | |
| 108 | #define GET_STACK_TRACE_FREE GET_STACK_TRACE_MALLOC |
Kostya Serebryany | 019b76f | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 109 | |
Sergey Matveev | d8fb4d8 | 2013-12-03 18:24:28 +0000 | [diff] [blame] | 110 | #define PRINT_CURRENT_STACK() \ |
| 111 | { \ |
| 112 | GET_STACK_TRACE_FATAL_HERE; \ |
Alexey Samsonov | f2c7659 | 2013-12-19 11:25:05 +0000 | [diff] [blame] | 113 | stack.Print(); \ |
Chandler Carruth | bbff278 | 2012-06-25 06:53:10 +0000 | [diff] [blame] | 114 | } |
Kostya Serebryany | 019b76f | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 115 | |
Evgeniy Stepanov | f518a4e | 2014-10-14 09:36:24 +0000 | [diff] [blame] | 116 | #define PRINT_CURRENT_STACK_CHECK() \ |
| 117 | { \ |
| 118 | GET_STACK_TRACE_CHECK_HERE; \ |
| 119 | stack.Print(); \ |
| 120 | } |
| 121 | |
Vedant Kumar | 59ba7b8 | 2015-10-01 00:22:21 +0000 | [diff] [blame] | 122 | #endif // ASAN_STACK_H |