blob: 7cee75528d2dfaf4b9344be5cff4c286680fc2dd [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
Kostya Serebryany019b76f2011-11-30 01:07:02 +000024// Get the stack trace with the given pc and bp.
25// The pc will be in the position 0 of the resulting stack trace.
26// The bp may refer to the current frame or to the caller's frame.
Alexey Samsonov3d9adc02014-03-04 13:12:25 +000027ALWAYS_INLINE
Alexey Samsonov9c859272014-10-26 03:35:14 +000028void GetStackTraceWithPcBpAndContext(BufferedStackTrace *stack, uptr max_depth,
29 uptr pc, uptr bp, void *context,
30 bool fast) {
Sergey Matveevaf179b82013-05-08 12:45:55 +000031#if SANITIZER_WINDOWS
Alexey Samsonov3d9adc02014-03-04 13:12:25 +000032 stack->Unwind(max_depth, pc, bp, context, 0, 0, fast);
Sergey Matveevaf179b82013-05-08 12:45:55 +000033#else
Alexey Samsonov3d9adc02014-03-04 13:12:25 +000034 AsanThread *t;
35 stack->size = 0;
Kostya Serebryanyb9e31d72014-05-14 14:03:31 +000036 if (LIKELY(asan_inited)) {
Alexey Samsonov3d9adc02014-03-04 13:12:25 +000037 if ((t = GetCurrentThread()) && !t->isUnwinding()) {
38 uptr stack_top = t->stack_top();
39 uptr stack_bottom = t->stack_bottom();
40 ScopedUnwinding unwind_scope(t);
41 stack->Unwind(max_depth, pc, bp, context, stack_top, stack_bottom, fast);
42 } else if (t == 0 && !fast) {
43 /* If GetCurrentThread() has failed, try to do slow unwind anyways. */
44 stack->Unwind(max_depth, pc, bp, context, 0, 0, false);
45 }
Sergey Matveevaf179b82013-05-08 12:45:55 +000046 }
47#endif // SANITIZER_WINDOWS
Alexey Samsonov3d9adc02014-03-04 13:12:25 +000048}
49
50} // namespace __asan
Kostya Serebryany019b76f2011-11-30 01:07:02 +000051
Alexey Samsonov2d3a67b2012-01-17 06:35:31 +000052// NOTE: A Rule of thumb is to retrieve stack trace in the interceptors
53// as early as possible (in functions exposed to the user), as we generally
54// don't want stack trace to contain functions from ASan internals.
55
Evgeniy Stepanov769d46f2014-02-11 13:38:57 +000056#define GET_STACK_TRACE(max_size, fast) \
Alexey Samsonov9c859272014-10-26 03:35:14 +000057 BufferedStackTrace stack; \
Alexey Samsonov3d9adc02014-03-04 13:12:25 +000058 if (max_size <= 2) { \
59 stack.size = max_size; \
60 if (max_size > 0) { \
61 stack.top_frame_bp = GET_CURRENT_FRAME(); \
Alexey Samsonov9c859272014-10-26 03:35:14 +000062 stack.trace_buffer[0] = StackTrace::GetCurrentPc(); \
Alexey Samsonov359c1052014-03-04 14:06:11 +000063 if (max_size > 1) \
Alexey Samsonov9c859272014-10-26 03:35:14 +000064 stack.trace_buffer[1] = GET_CALLER_PC(); \
Alexey Samsonov3d9adc02014-03-04 13:12:25 +000065 } \
Alexey Samsonov3d9adc02014-03-04 13:12:25 +000066 } else { \
67 GetStackTraceWithPcBpAndContext(&stack, max_size, \
68 StackTrace::GetCurrentPc(), \
69 GET_CURRENT_FRAME(), 0, fast); \
70 }
Kostya Serebryany019b76f2011-11-30 01:07:02 +000071
Alexey Samsonov3d9adc02014-03-04 13:12:25 +000072#define GET_STACK_TRACE_FATAL(pc, bp) \
Alexey Samsonov9c859272014-10-26 03:35:14 +000073 BufferedStackTrace stack; \
Alexey Samsonov3d9adc02014-03-04 13:12:25 +000074 GetStackTraceWithPcBpAndContext(&stack, kStackTraceMax, pc, bp, 0, \
75 common_flags()->fast_unwind_on_fatal)
Evgeniy Stepanov769d46f2014-02-11 13:38:57 +000076
Alexey Samsonov3d9adc02014-03-04 13:12:25 +000077#define GET_STACK_TRACE_SIGNAL(pc, bp, context) \
Alexey Samsonov9c859272014-10-26 03:35:14 +000078 BufferedStackTrace stack; \
Alexey Samsonov3d9adc02014-03-04 13:12:25 +000079 GetStackTraceWithPcBpAndContext(&stack, kStackTraceMax, pc, bp, context, \
80 common_flags()->fast_unwind_on_fatal)
Kostya Serebryany019b76f2011-11-30 01:07:02 +000081
Sergey Matveev0c8ed9c2013-05-06 11:27:58 +000082#define GET_STACK_TRACE_FATAL_HERE \
83 GET_STACK_TRACE(kStackTraceMax, common_flags()->fast_unwind_on_fatal)
Kostya Serebryanybaf583c2012-12-13 09:34:23 +000084
Evgeniy Stepanovf518a4e2014-10-14 09:36:24 +000085#define GET_STACK_TRACE_CHECK_HERE \
86 GET_STACK_TRACE(kStackTraceMax, common_flags()->fast_unwind_on_check)
87
Sergey Matveev0c8ed9c2013-05-06 11:27:58 +000088#define GET_STACK_TRACE_THREAD \
Kostya Serebryanybaf583c2012-12-13 09:34:23 +000089 GET_STACK_TRACE(kStackTraceMax, true)
90
Sergey Matveev0c8ed9c2013-05-06 11:27:58 +000091#define GET_STACK_TRACE_MALLOC \
92 GET_STACK_TRACE(common_flags()->malloc_context_size, \
93 common_flags()->fast_unwind_on_malloc)
Kostya Serebryanybaf583c2012-12-13 09:34:23 +000094
95#define GET_STACK_TRACE_FREE GET_STACK_TRACE_MALLOC
Kostya Serebryany019b76f2011-11-30 01:07:02 +000096
Sergey Matveevd8fb4d82013-12-03 18:24:28 +000097#define PRINT_CURRENT_STACK() \
98 { \
99 GET_STACK_TRACE_FATAL_HERE; \
Alexey Samsonovf2c76592013-12-19 11:25:05 +0000100 stack.Print(); \
Chandler Carruthbbff2782012-06-25 06:53:10 +0000101 }
Kostya Serebryany019b76f2011-11-30 01:07:02 +0000102
Evgeniy Stepanovf518a4e2014-10-14 09:36:24 +0000103#define PRINT_CURRENT_STACK_CHECK() \
104 { \
105 GET_STACK_TRACE_CHECK_HERE; \
106 stack.Print(); \
107 }
108
Kostya Serebryany019b76f2011-11-30 01:07:02 +0000109#endif // ASAN_STACK_H