1 //=-- lsan.h --------------------------------------------------------------===//
2 //
3 // This file is distributed under the University of Illinois Open Source
4 // License. See LICENSE.TXT for details.
5 //
6 //===----------------------------------------------------------------------===//
7 //
8 // This file is a part of LeakSanitizer.
9 // Private header for standalone LSan RTL.
10 //
11 //===----------------------------------------------------------------------===//
12 
13 #include "lsan_thread.h"
14 #include "sanitizer_common/sanitizer_flags.h"
15 #include "sanitizer_common/sanitizer_stacktrace.h"
16 
17 #define GET_STACK_TRACE(max_size, fast)                       \
18   __sanitizer::BufferedStackTrace stack;                      \
19   GetStackTraceWithPcBpAndContext(&stack, max_size,           \
20                                   StackTrace::GetCurrentPc(), \
21                                   GET_CURRENT_FRAME(), nullptr, fast);
22 
23 #define GET_STACK_TRACE_FATAL \
24   GET_STACK_TRACE(kStackTraceMax, common_flags()->fast_unwind_on_fatal)
25 
26 #define GET_STACK_TRACE_MALLOC                                      \
27   GET_STACK_TRACE(__sanitizer::common_flags()->malloc_context_size, \
28                   common_flags()->fast_unwind_on_malloc)
29 
30 #define GET_STACK_TRACE_THREAD GET_STACK_TRACE(kStackTraceMax, true)
31 
32 namespace __lsan {
33 
34 void InitializeInterceptors();
35 void ReplaceSystemMalloc();
36 
37 #define ENSURE_LSAN_INITED do {   \
38   CHECK(!lsan_init_is_running);   \
39   if (!lsan_inited)               \
40     __lsan_init();                \
41 } while (0)
42 
43 // Get the stack trace with the given pc and bp.
44 // The pc will be in the position 0 of the resulting stack trace.
45 // The bp may refer to the current frame or to the caller's frame.
46 ALWAYS_INLINE
GetStackTraceWithPcBpAndContext(__sanitizer::BufferedStackTrace * stack,__sanitizer::uptr max_depth,__sanitizer::uptr pc,__sanitizer::uptr bp,void * context,bool fast)47 void GetStackTraceWithPcBpAndContext(__sanitizer::BufferedStackTrace *stack,
48                                      __sanitizer::uptr max_depth,
49                                      __sanitizer::uptr pc, __sanitizer::uptr bp,
50                                      void *context, bool fast) {
51   uptr stack_top = 0, stack_bottom = 0;
52   ThreadContext *t;
53   if (fast && (t = CurrentThreadContext())) {
54     stack_top = t->stack_end();
55     stack_bottom = t->stack_begin();
56   }
57   if (!SANITIZER_MIPS || IsValidFrame(bp, stack_top, stack_bottom)) {
58     stack->Unwind(max_depth, pc, bp, context, stack_top, stack_bottom, fast);
59   }
60 }
61 
62 }  // namespace __lsan
63 
64 extern bool lsan_inited;
65 extern bool lsan_init_is_running;
66 
67 extern "C" SANITIZER_INTERFACE_ATTRIBUTE void __lsan_init();
68