blob: cc95e0f30a335d8bdf8a261862ee31eb198712e0 [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//===----------------------------------------------------------------------===//
Vedant Kumar59ba7b82015-10-01 00:22:21 +000014
Kostya Serebryany019b76f2011-11-30 01:07:02 +000015#ifndef ASAN_STACK_H
16#define ASAN_STACK_H
17
Kostya Serebryanybaf583c2012-12-13 09:34:23 +000018#include "asan_flags.h"
Sergey Matveevaf179b82013-05-08 12:45:55 +000019#include "asan_thread.h"
Sergey Matveev0c8ed9c2013-05-06 11:27:58 +000020#include "sanitizer_common/sanitizer_flags.h"
21#include "sanitizer_common/sanitizer_stacktrace.h"
Kostya Serebryany019b76f2011-11-30 01:07:02 +000022
Alexey Samsonov3d9adc02014-03-04 13:12:25 +000023namespace __asan {
24
Alexey Samsonovbba821b2014-12-16 01:23:03 +000025static const u32 kDefaultMallocContextSize = 30;
26
27void SetMallocContextSize(u32 size);
28u32 GetMallocContextSize();
29
Kostya Serebryany019b76f2011-11-30 01:07:02 +000030// 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 Samsonov3d9adc02014-03-04 13:12:25 +000033ALWAYS_INLINE
Alexey Samsonov9c859272014-10-26 03:35:14 +000034void GetStackTraceWithPcBpAndContext(BufferedStackTrace *stack, uptr max_depth,
35 uptr pc, uptr bp, void *context,
36 bool fast) {
Sergey Matveevaf179b82013-05-08 12:45:55 +000037#if SANITIZER_WINDOWS
Alexey Samsonov3d9adc02014-03-04 13:12:25 +000038 stack->Unwind(max_depth, pc, bp, context, 0, 0, fast);
Sergey Matveevaf179b82013-05-08 12:45:55 +000039#else
Alexey Samsonov3d9adc02014-03-04 13:12:25 +000040 AsanThread *t;
41 stack->size = 0;
Kostya Serebryanyb9e31d72014-05-14 14:03:31 +000042 if (LIKELY(asan_inited)) {
Alexey Samsonov3d9adc02014-03-04 13:12:25 +000043 if ((t = GetCurrentThread()) && !t->isUnwinding()) {
Viktor Kutuzovdc6cbfe2014-11-10 15:22:04 +000044 // 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 Samsonov3d9adc02014-03-04 13:12:25 +000048 uptr stack_top = t->stack_top();
49 uptr stack_bottom = t->stack_bottom();
50 ScopedUnwinding unwind_scope(t);
Sagar Thakur7bec3a92016-05-18 06:09:26 +000051 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 Kumar59ba7b82015-10-01 00:22:21 +000055 } else if (!t && !fast) {
Alexey Samsonov3d9adc02014-03-04 13:12:25 +000056 /* If GetCurrentThread() has failed, try to do slow unwind anyways. */
57 stack->Unwind(max_depth, pc, bp, context, 0, 0, false);
58 }
Sergey Matveevaf179b82013-05-08 12:45:55 +000059 }
Vedant Kumar59ba7b82015-10-01 00:22:21 +000060#endif // SANITIZER_WINDOWS
Alexey Samsonov3d9adc02014-03-04 13:12:25 +000061}
62
Vedant Kumar59ba7b82015-10-01 00:22:21 +000063} // namespace __asan
Kostya Serebryany019b76f2011-11-30 01:07:02 +000064
Alexey Samsonov2d3a67b2012-01-17 06:35:31 +000065// 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 Stepanov769d46f2014-02-11 13:38:57 +000069#define GET_STACK_TRACE(max_size, fast) \
Alexey Samsonov9c859272014-10-26 03:35:14 +000070 BufferedStackTrace stack; \
Alexey Samsonov3d9adc02014-03-04 13:12:25 +000071 if (max_size <= 2) { \
72 stack.size = max_size; \
73 if (max_size > 0) { \
74 stack.top_frame_bp = GET_CURRENT_FRAME(); \
Alexey Samsonov9c859272014-10-26 03:35:14 +000075 stack.trace_buffer[0] = StackTrace::GetCurrentPc(); \
Alexey Samsonov359c1052014-03-04 14:06:11 +000076 if (max_size > 1) \
Alexey Samsonov9c859272014-10-26 03:35:14 +000077 stack.trace_buffer[1] = GET_CALLER_PC(); \
Alexey Samsonov3d9adc02014-03-04 13:12:25 +000078 } \
Alexey Samsonov3d9adc02014-03-04 13:12:25 +000079 } else { \
80 GetStackTraceWithPcBpAndContext(&stack, max_size, \
81 StackTrace::GetCurrentPc(), \
82 GET_CURRENT_FRAME(), 0, fast); \
83 }
Kostya Serebryany019b76f2011-11-30 01:07:02 +000084
Alexey Samsonov3d9adc02014-03-04 13:12:25 +000085#define GET_STACK_TRACE_FATAL(pc, bp) \
Alexey Samsonov9c859272014-10-26 03:35:14 +000086 BufferedStackTrace stack; \
Alexey Samsonov3d9adc02014-03-04 13:12:25 +000087 GetStackTraceWithPcBpAndContext(&stack, kStackTraceMax, pc, bp, 0, \
88 common_flags()->fast_unwind_on_fatal)
Evgeniy Stepanov769d46f2014-02-11 13:38:57 +000089
Viktor Kutuzov9b75b752014-11-25 13:00:21 +000090#define GET_STACK_TRACE_SIGNAL(sig) \
Alexey Samsonov9c859272014-10-26 03:35:14 +000091 BufferedStackTrace stack; \
Viktor Kutuzov9b75b752014-11-25 13:00:21 +000092 GetStackTraceWithPcBpAndContext(&stack, kStackTraceMax, \
93 (sig).pc, (sig).bp, (sig).context, \
Alexey Samsonov3d9adc02014-03-04 13:12:25 +000094 common_flags()->fast_unwind_on_fatal)
Kostya Serebryany019b76f2011-11-30 01:07:02 +000095
Sergey Matveev0c8ed9c2013-05-06 11:27:58 +000096#define GET_STACK_TRACE_FATAL_HERE \
97 GET_STACK_TRACE(kStackTraceMax, common_flags()->fast_unwind_on_fatal)
Kostya Serebryanybaf583c2012-12-13 09:34:23 +000098
Evgeniy Stepanovf518a4e2014-10-14 09:36:24 +000099#define GET_STACK_TRACE_CHECK_HERE \
100 GET_STACK_TRACE(kStackTraceMax, common_flags()->fast_unwind_on_check)
101
Sergey Matveev0c8ed9c2013-05-06 11:27:58 +0000102#define GET_STACK_TRACE_THREAD \
Kostya Serebryanybaf583c2012-12-13 09:34:23 +0000103 GET_STACK_TRACE(kStackTraceMax, true)
104
Alexey Samsonovbba821b2014-12-16 01:23:03 +0000105#define GET_STACK_TRACE_MALLOC \
106 GET_STACK_TRACE(GetMallocContextSize(), common_flags()->fast_unwind_on_malloc)
Kostya Serebryanybaf583c2012-12-13 09:34:23 +0000107
108#define GET_STACK_TRACE_FREE GET_STACK_TRACE_MALLOC
Kostya Serebryany019b76f2011-11-30 01:07:02 +0000109
Sergey Matveevd8fb4d82013-12-03 18:24:28 +0000110#define PRINT_CURRENT_STACK() \
111 { \
112 GET_STACK_TRACE_FATAL_HERE; \
Alexey Samsonovf2c76592013-12-19 11:25:05 +0000113 stack.Print(); \
Chandler Carruthbbff2782012-06-25 06:53:10 +0000114 }
Kostya Serebryany019b76f2011-11-30 01:07:02 +0000115
Evgeniy Stepanovf518a4e2014-10-14 09:36:24 +0000116#define PRINT_CURRENT_STACK_CHECK() \
117 { \
118 GET_STACK_TRACE_CHECK_HERE; \
119 stack.Print(); \
120 }
121
Vedant Kumar59ba7b82015-10-01 00:22:21 +0000122#endif // ASAN_STACK_H