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 | //===----------------------------------------------------------------------===// |
| 14 | #ifndef ASAN_STACK_H |
| 15 | #define ASAN_STACK_H |
| 16 | |
| 17 | #include "asan_internal.h" |
| 18 | |
| 19 | namespace __asan { |
| 20 | |
| 21 | static const size_t kStackTraceMax = 64; |
| 22 | |
| 23 | struct AsanStackTrace { |
| 24 | size_t size; |
| 25 | size_t max_size; |
| 26 | uintptr_t trace[kStackTraceMax]; |
| 27 | static void PrintStack(uintptr_t *addr, size_t size); |
| 28 | void PrintStack() { |
| 29 | PrintStack(this->trace, this->size); |
| 30 | } |
| 31 | void CopyTo(uintptr_t *dst, size_t dst_size) { |
| 32 | for (size_t i = 0; i < size && i < dst_size; i++) |
| 33 | dst[i] = trace[i]; |
| 34 | for (size_t i = size; i < dst_size; i++) |
| 35 | dst[i] = 0; |
| 36 | } |
| 37 | |
| 38 | void CopyFrom(uintptr_t *src, size_t src_size) { |
| 39 | size = src_size; |
| 40 | if (size > kStackTraceMax) size = kStackTraceMax; |
| 41 | for (size_t i = 0; i < size; i++) { |
| 42 | trace[i] = src[i]; |
| 43 | } |
| 44 | } |
| 45 | |
| 46 | void FastUnwindStack(uintptr_t pc, uintptr_t bp); |
| 47 | // static _Unwind_Reason_Code Unwind_Trace( |
| 48 | // struct _Unwind_Context *ctx, void *param); |
| 49 | static uintptr_t GetCurrentPc(); |
| 50 | |
| 51 | static size_t CompressStack(AsanStackTrace *stack, |
| 52 | uint32_t *compressed, size_t size); |
| 53 | static void UncompressStack(AsanStackTrace *stack, |
| 54 | uint32_t *compressed, size_t size); |
| 55 | size_t full_frame_count; |
| 56 | }; |
| 57 | |
| 58 | } // namespace __asan |
| 59 | |
| 60 | // Get the stack trace with the given pc and bp. |
| 61 | // The pc will be in the position 0 of the resulting stack trace. |
| 62 | // The bp may refer to the current frame or to the caller's frame. |
| 63 | // fast_unwind is currently unused. |
| 64 | #define GET_STACK_TRACE_WITH_PC_AND_BP(max_s, fast_unwind, pc, bp) \ |
| 65 | AsanStackTrace stack; \ |
| 66 | { \ |
| 67 | uintptr_t saved_pc = pc; \ |
| 68 | uintptr_t saved_bp = bp; \ |
| 69 | stack.size = 0; \ |
| 70 | stack.full_frame_count = 0; \ |
| 71 | stack.trace[0] = saved_pc; \ |
| 72 | if ((max_s) > 1) { \ |
| 73 | stack.max_size = max_s; \ |
| 74 | stack.FastUnwindStack(saved_pc, saved_bp); \ |
| 75 | } \ |
| 76 | } \ |
| 77 | |
Alexey Samsonov | 2d3a67b | 2012-01-17 06:35:31 +0000 | [diff] [blame^] | 78 | // NOTE: A Rule of thumb is to retrieve stack trace in the interceptors |
| 79 | // as early as possible (in functions exposed to the user), as we generally |
| 80 | // don't want stack trace to contain functions from ASan internals. |
| 81 | |
Kostya Serebryany | 019b76f | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 82 | #define GET_STACK_TRACE_HERE(max_size, fast_unwind) \ |
| 83 | GET_STACK_TRACE_WITH_PC_AND_BP(max_size, fast_unwind, \ |
| 84 | AsanStackTrace::GetCurrentPc(), GET_CURRENT_FRAME()) \ |
| 85 | |
| 86 | #define GET_STACK_TRACE_HERE_FOR_MALLOC \ |
| 87 | GET_STACK_TRACE_HERE(FLAG_malloc_context_size, FLAG_fast_unwind) |
| 88 | |
| 89 | #define GET_STACK_TRACE_HERE_FOR_FREE(ptr) \ |
| 90 | GET_STACK_TRACE_HERE(FLAG_malloc_context_size, FLAG_fast_unwind) |
| 91 | |
| 92 | #define PRINT_CURRENT_STACK() \ |
| 93 | { \ |
| 94 | GET_STACK_TRACE_HERE(kStackTraceMax, false); \ |
| 95 | stack.PrintStack(); \ |
| 96 | } \ |
| 97 | |
| 98 | #endif // ASAN_STACK_H |