Home | History | Annotate | Download | only in asan
      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 && t->isInDeadlySignal()) fast = true;
     48       uptr stack_top = t->stack_top();
     49       uptr stack_bottom = t->stack_bottom();
     50       ScopedUnwinding unwind_scope(t);
     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       }
     55     } else if (!t && !fast) {
     56       /* If GetCurrentThread() has failed, try to do slow unwind anyways. */
     57       stack->Unwind(max_depth, pc, bp, context, 0, 0, false);
     58     }
     59   }
     60 #endif // SANITIZER_WINDOWS
     61 }
     62 
     63 } // namespace __asan
     64 
     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 
     69 #define GET_STACK_TRACE(max_size, fast)                                        \
     70   BufferedStackTrace stack;                                                    \
     71   if (max_size <= 2) {                                                         \
     72     stack.size = max_size;                                                     \
     73     if (max_size > 0) {                                                        \
     74       stack.top_frame_bp = GET_CURRENT_FRAME();                                \
     75       stack.trace_buffer[0] = StackTrace::GetCurrentPc();                      \
     76       if (max_size > 1)                                                        \
     77         stack.trace_buffer[1] = GET_CALLER_PC();                               \
     78     }                                                                          \
     79   } else {                                                                     \
     80     GetStackTraceWithPcBpAndContext(&stack, max_size,                          \
     81                                     StackTrace::GetCurrentPc(),                \
     82                                     GET_CURRENT_FRAME(), 0, fast);             \
     83   }
     84 
     85 #define GET_STACK_TRACE_FATAL(pc, bp)                                          \
     86   BufferedStackTrace stack;                                                    \
     87   GetStackTraceWithPcBpAndContext(&stack, kStackTraceMax, pc, bp, 0,           \
     88                                   common_flags()->fast_unwind_on_fatal)
     89 
     90 #define GET_STACK_TRACE_SIGNAL(sig)                                            \
     91   BufferedStackTrace stack;                                                    \
     92   GetStackTraceWithPcBpAndContext(&stack, kStackTraceMax,                      \
     93                                   (sig).pc, (sig).bp, (sig).context,           \
     94                                   common_flags()->fast_unwind_on_fatal)
     95 
     96 #define GET_STACK_TRACE_FATAL_HERE                                \
     97   GET_STACK_TRACE(kStackTraceMax, common_flags()->fast_unwind_on_fatal)
     98 
     99 #define GET_STACK_TRACE_CHECK_HERE                                \
    100   GET_STACK_TRACE(kStackTraceMax, common_flags()->fast_unwind_on_check)
    101 
    102 #define GET_STACK_TRACE_THREAD                                    \
    103   GET_STACK_TRACE(kStackTraceMax, true)
    104 
    105 #define GET_STACK_TRACE_MALLOC                                                 \
    106   GET_STACK_TRACE(GetMallocContextSize(), common_flags()->fast_unwind_on_malloc)
    107 
    108 #define GET_STACK_TRACE_FREE GET_STACK_TRACE_MALLOC
    109 
    110 #define PRINT_CURRENT_STACK()   \
    111   {                             \
    112     GET_STACK_TRACE_FATAL_HERE; \
    113     stack.Print();              \
    114   }
    115 
    116 #define PRINT_CURRENT_STACK_CHECK() \
    117   {                                 \
    118     GET_STACK_TRACE_CHECK_HERE;     \
    119     stack.Print();                  \
    120   }
    121 
    122 #endif // ASAN_STACK_H
    123