blob: 122967a152f8d9da4b379f7e3d631796b38841ca [file] [log] [blame]
Kostya Serebryany019b76f2011-11-30 01:07:02 +00001//===-- 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 Serebryanybaf583c2012-12-13 09:34:23 +000017#include "asan_flags.h"
Sergey Matveevaf179b82013-05-08 12:45:55 +000018#include "asan_thread.h"
Sergey Matveev0c8ed9c2013-05-06 11:27:58 +000019#include "sanitizer_common/sanitizer_flags.h"
20#include "sanitizer_common/sanitizer_stacktrace.h"
Kostya Serebryany019b76f2011-11-30 01:07:02 +000021
Alexey Samsonov3d9adc02014-03-04 13:12:25 +000022namespace __asan {
23
Alexey Samsonovbba821b2014-12-16 01:23:03 +000024static const u32 kDefaultMallocContextSize = 30;
25
26void SetMallocContextSize(u32 size);
27u32 GetMallocContextSize();
28
Kostya Serebryany019b76f2011-11-30 01:07:02 +000029// 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.
Alexey Samsonov3d9adc02014-03-04 13:12:25 +000032ALWAYS_INLINE
Alexey Samsonov9c859272014-10-26 03:35:14 +000033void GetStackTraceWithPcBpAndContext(BufferedStackTrace *stack, uptr max_depth,
34 uptr pc, uptr bp, void *context,
35 bool fast) {
Sergey Matveevaf179b82013-05-08 12:45:55 +000036#if SANITIZER_WINDOWS
Alexey Samsonov3d9adc02014-03-04 13:12:25 +000037 stack->Unwind(max_depth, pc, bp, context, 0, 0, fast);
Sergey Matveevaf179b82013-05-08 12:45:55 +000038#else
Alexey Samsonov3d9adc02014-03-04 13:12:25 +000039 AsanThread *t;
40 stack->size = 0;
Kostya Serebryanyb9e31d72014-05-14 14:03:31 +000041 if (LIKELY(asan_inited)) {
Alexey Samsonov3d9adc02014-03-04 13:12:25 +000042 if ((t = GetCurrentThread()) && !t->isUnwinding()) {
Viktor Kutuzovdc6cbfe2014-11-10 15:22:04 +000043 // 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;
Alexey Samsonov3d9adc02014-03-04 13:12:25 +000047 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 Matveevaf179b82013-05-08 12:45:55 +000055 }
56#endif // SANITIZER_WINDOWS
Alexey Samsonov3d9adc02014-03-04 13:12:25 +000057}
58
59} // namespace __asan
Kostya Serebryany019b76f2011-11-30 01:07:02 +000060
Alexey Samsonov2d3a67b2012-01-17 06:35:31 +000061// 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
Evgeniy Stepanov769d46f2014-02-11 13:38:57 +000065#define GET_STACK_TRACE(max_size, fast) \
Alexey Samsonov9c859272014-10-26 03:35:14 +000066 BufferedStackTrace stack; \
Alexey Samsonov3d9adc02014-03-04 13:12:25 +000067 if (max_size <= 2) { \
68 stack.size = max_size; \
69 if (max_size > 0) { \
70 stack.top_frame_bp = GET_CURRENT_FRAME(); \
Alexey Samsonov9c859272014-10-26 03:35:14 +000071 stack.trace_buffer[0] = StackTrace::GetCurrentPc(); \
Alexey Samsonov359c1052014-03-04 14:06:11 +000072 if (max_size > 1) \
Alexey Samsonov9c859272014-10-26 03:35:14 +000073 stack.trace_buffer[1] = GET_CALLER_PC(); \
Alexey Samsonov3d9adc02014-03-04 13:12:25 +000074 } \
Alexey Samsonov3d9adc02014-03-04 13:12:25 +000075 } else { \
76 GetStackTraceWithPcBpAndContext(&stack, max_size, \
77 StackTrace::GetCurrentPc(), \
78 GET_CURRENT_FRAME(), 0, fast); \
79 }
Kostya Serebryany019b76f2011-11-30 01:07:02 +000080
Alexey Samsonov3d9adc02014-03-04 13:12:25 +000081#define GET_STACK_TRACE_FATAL(pc, bp) \
Alexey Samsonov9c859272014-10-26 03:35:14 +000082 BufferedStackTrace stack; \
Alexey Samsonov3d9adc02014-03-04 13:12:25 +000083 GetStackTraceWithPcBpAndContext(&stack, kStackTraceMax, pc, bp, 0, \
84 common_flags()->fast_unwind_on_fatal)
Evgeniy Stepanov769d46f2014-02-11 13:38:57 +000085
Viktor Kutuzov9b75b752014-11-25 13:00:21 +000086#define GET_STACK_TRACE_SIGNAL(sig) \
Alexey Samsonov9c859272014-10-26 03:35:14 +000087 BufferedStackTrace stack; \
Viktor Kutuzov9b75b752014-11-25 13:00:21 +000088 GetStackTraceWithPcBpAndContext(&stack, kStackTraceMax, \
89 (sig).pc, (sig).bp, (sig).context, \
Alexey Samsonov3d9adc02014-03-04 13:12:25 +000090 common_flags()->fast_unwind_on_fatal)
Kostya Serebryany019b76f2011-11-30 01:07:02 +000091
Sergey Matveev0c8ed9c2013-05-06 11:27:58 +000092#define GET_STACK_TRACE_FATAL_HERE \
93 GET_STACK_TRACE(kStackTraceMax, common_flags()->fast_unwind_on_fatal)
Kostya Serebryanybaf583c2012-12-13 09:34:23 +000094
Evgeniy Stepanovf518a4e2014-10-14 09:36:24 +000095#define GET_STACK_TRACE_CHECK_HERE \
96 GET_STACK_TRACE(kStackTraceMax, common_flags()->fast_unwind_on_check)
97
Sergey Matveev0c8ed9c2013-05-06 11:27:58 +000098#define GET_STACK_TRACE_THREAD \
Kostya Serebryanybaf583c2012-12-13 09:34:23 +000099 GET_STACK_TRACE(kStackTraceMax, true)
100
Alexey Samsonovbba821b2014-12-16 01:23:03 +0000101#define GET_STACK_TRACE_MALLOC \
102 GET_STACK_TRACE(GetMallocContextSize(), common_flags()->fast_unwind_on_malloc)
Kostya Serebryanybaf583c2012-12-13 09:34:23 +0000103
104#define GET_STACK_TRACE_FREE GET_STACK_TRACE_MALLOC
Kostya Serebryany019b76f2011-11-30 01:07:02 +0000105
Sergey Matveevd8fb4d82013-12-03 18:24:28 +0000106#define PRINT_CURRENT_STACK() \
107 { \
108 GET_STACK_TRACE_FATAL_HERE; \
Alexey Samsonovf2c76592013-12-19 11:25:05 +0000109 stack.Print(); \
Chandler Carruthbbff2782012-06-25 06:53:10 +0000110 }
Kostya Serebryany019b76f2011-11-30 01:07:02 +0000111
Evgeniy Stepanovf518a4e2014-10-14 09:36:24 +0000112#define PRINT_CURRENT_STACK_CHECK() \
113 { \
114 GET_STACK_TRACE_CHECK_HERE; \
115 stack.Print(); \
116 }
117
Kostya Serebryany019b76f2011-11-30 01:07:02 +0000118#endif // ASAN_STACK_H