blob: dedc5930219b4a66be57980c0867bab67bfe06c3 [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
22namespace __asan {
23
Kostya Serebryanya57b4e82012-08-28 13:49:49 +000024void PrintStack(StackTrace *stack);
Kostya Serebryany96288392013-10-18 14:50:44 +000025void PrintStack(const uptr *trace, uptr size);
Kostya Serebryanyee928772012-08-28 13:25:55 +000026
Kostya Serebryany019b76f2011-11-30 01:07:02 +000027} // namespace __asan
28
29// 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.
Sergey Matveevaf179b82013-05-08 12:45:55 +000032#if SANITIZER_WINDOWS
Evgeniy Stepanov58dbe062013-09-12 08:16:28 +000033#define GET_STACK_TRACE_WITH_PC_AND_BP(max_s, pc, bp, fast) \
34 StackTrace stack; \
Alexey Samsonovc129e652013-10-12 12:23:00 +000035 stack.Unwind(max_s, pc, bp, 0, 0, fast)
Sergey Matveevaf179b82013-05-08 12:45:55 +000036#else
Timur Iskhodzhanova10c46f2013-11-29 12:53:30 +000037#define GET_STACK_TRACE_WITH_PC_AND_BP(max_s, pc, bp, fast) \
38 StackTrace stack; \
39 { \
40 AsanThread *t; \
41 stack.size = 0; \
42 if (asan_inited) { \
43 if ((t = GetCurrentThread()) && !t->isUnwinding()) { \
44 uptr stack_top = t->stack_top(); \
45 uptr stack_bottom = t->stack_bottom(); \
46 ScopedUnwinding unwind_scope(t); \
47 stack.Unwind(max_s, pc, bp, stack_top, stack_bottom, fast); \
48 } else if (t == 0 && !fast) { \
49 /* If GetCurrentThread() has failed, try to do slow unwind anyways. */ \
50 stack.Unwind(max_s, pc, bp, 0, 0, false); \
51 } \
52 } \
Sergey Matveevaf179b82013-05-08 12:45:55 +000053 }
54#endif // SANITIZER_WINDOWS
Kostya Serebryany019b76f2011-11-30 01:07:02 +000055
Alexey Samsonov2d3a67b2012-01-17 06:35:31 +000056// NOTE: A Rule of thumb is to retrieve stack trace in the interceptors
57// as early as possible (in functions exposed to the user), as we generally
58// don't want stack trace to contain functions from ASan internals.
59
Kostya Serebryanybaf583c2012-12-13 09:34:23 +000060#define GET_STACK_TRACE(max_size, fast) \
Evgeniy Stepanov84c44a82012-01-19 11:34:18 +000061 GET_STACK_TRACE_WITH_PC_AND_BP(max_size, \
Kostya Serebryanybaf583c2012-12-13 09:34:23 +000062 StackTrace::GetCurrentPc(), GET_CURRENT_FRAME(), fast)
Kostya Serebryany019b76f2011-11-30 01:07:02 +000063
Kostya Serebryanybaf583c2012-12-13 09:34:23 +000064#define GET_STACK_TRACE_FATAL(pc, bp) \
65 GET_STACK_TRACE_WITH_PC_AND_BP(kStackTraceMax, pc, bp, \
Sergey Matveev0c8ed9c2013-05-06 11:27:58 +000066 common_flags()->fast_unwind_on_fatal)
Kostya Serebryany019b76f2011-11-30 01:07:02 +000067
Sergey Matveev0c8ed9c2013-05-06 11:27:58 +000068#define GET_STACK_TRACE_FATAL_HERE \
69 GET_STACK_TRACE(kStackTraceMax, common_flags()->fast_unwind_on_fatal)
Kostya Serebryanybaf583c2012-12-13 09:34:23 +000070
Sergey Matveev0c8ed9c2013-05-06 11:27:58 +000071#define GET_STACK_TRACE_THREAD \
Kostya Serebryanybaf583c2012-12-13 09:34:23 +000072 GET_STACK_TRACE(kStackTraceMax, true)
73
Sergey Matveev0c8ed9c2013-05-06 11:27:58 +000074#define GET_STACK_TRACE_MALLOC \
75 GET_STACK_TRACE(common_flags()->malloc_context_size, \
76 common_flags()->fast_unwind_on_malloc)
Kostya Serebryanybaf583c2012-12-13 09:34:23 +000077
78#define GET_STACK_TRACE_FREE GET_STACK_TRACE_MALLOC
Kostya Serebryany019b76f2011-11-30 01:07:02 +000079
80#define PRINT_CURRENT_STACK() \
81 { \
Kostya Serebryanybaf583c2012-12-13 09:34:23 +000082 GET_STACK_TRACE(kStackTraceMax, \
Sergey Matveev0c8ed9c2013-05-06 11:27:58 +000083 common_flags()->fast_unwind_on_fatal); \
Kostya Serebryanya57b4e82012-08-28 13:49:49 +000084 PrintStack(&stack); \
Chandler Carruthbbff2782012-06-25 06:53:10 +000085 }
Kostya Serebryany019b76f2011-11-30 01:07:02 +000086
87#endif // ASAN_STACK_H