blob: f7abb9335812c4bdd4ca5ee352ffc5ba5a0b9c3f [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
Kostya Serebryany019b76f2011-11-30 01:07:02 +000022// Get the stack trace with the given pc and bp.
23// The pc will be in the position 0 of the resulting stack trace.
24// The bp may refer to the current frame or to the caller's frame.
Sergey Matveevaf179b82013-05-08 12:45:55 +000025#if SANITIZER_WINDOWS
Evgeniy Stepanov769d46f2014-02-11 13:38:57 +000026#define GET_STACK_TRACE_WITH_PC_BP_AND_CONTEXT(max_s, pc, bp, context, fast) \
27 StackTrace stack; \
28 stack.Unwind(max_s, pc, bp, context, 0, 0, fast)
Sergey Matveevaf179b82013-05-08 12:45:55 +000029#else
Evgeniy Stepanov769d46f2014-02-11 13:38:57 +000030#define GET_STACK_TRACE_WITH_PC_BP_AND_CONTEXT(max_s, pc, bp, context, fast) \
Timur Iskhodzhanova10c46f2013-11-29 12:53:30 +000031 StackTrace stack; \
32 { \
33 AsanThread *t; \
34 stack.size = 0; \
35 if (asan_inited) { \
36 if ((t = GetCurrentThread()) && !t->isUnwinding()) { \
37 uptr stack_top = t->stack_top(); \
38 uptr stack_bottom = t->stack_bottom(); \
39 ScopedUnwinding unwind_scope(t); \
Evgeniy Stepanov769d46f2014-02-11 13:38:57 +000040 stack.Unwind(max_s, pc, bp, context, stack_top, stack_bottom, fast); \
Timur Iskhodzhanova10c46f2013-11-29 12:53:30 +000041 } else if (t == 0 && !fast) { \
42 /* If GetCurrentThread() has failed, try to do slow unwind anyways. */ \
Evgeniy Stepanov769d46f2014-02-11 13:38:57 +000043 stack.Unwind(max_s, pc, bp, context, 0, 0, false); \
Timur Iskhodzhanova10c46f2013-11-29 12:53:30 +000044 } \
45 } \
Sergey Matveevaf179b82013-05-08 12:45:55 +000046 }
47#endif // SANITIZER_WINDOWS
Kostya Serebryany019b76f2011-11-30 01:07:02 +000048
Alexey Samsonov2d3a67b2012-01-17 06:35:31 +000049// NOTE: A Rule of thumb is to retrieve stack trace in the interceptors
50// as early as possible (in functions exposed to the user), as we generally
51// don't want stack trace to contain functions from ASan internals.
52
Evgeniy Stepanov769d46f2014-02-11 13:38:57 +000053#define GET_STACK_TRACE(max_size, fast) \
54 GET_STACK_TRACE_WITH_PC_BP_AND_CONTEXT(max_size, StackTrace::GetCurrentPc(), \
55 GET_CURRENT_FRAME(), 0, fast)
Kostya Serebryany019b76f2011-11-30 01:07:02 +000056
Evgeniy Stepanov769d46f2014-02-11 13:38:57 +000057#define GET_STACK_TRACE_FATAL(pc, bp) \
58 GET_STACK_TRACE_WITH_PC_BP_AND_CONTEXT(kStackTraceMax, pc, bp, 0, \
59 common_flags()->fast_unwind_on_fatal)
60
61#define GET_STACK_TRACE_SIGNAL(pc, bp, context) \
62 GET_STACK_TRACE_WITH_PC_BP_AND_CONTEXT(kStackTraceMax, pc, bp, context, \
63 common_flags()->fast_unwind_on_fatal)
Kostya Serebryany019b76f2011-11-30 01:07:02 +000064
Sergey Matveev0c8ed9c2013-05-06 11:27:58 +000065#define GET_STACK_TRACE_FATAL_HERE \
66 GET_STACK_TRACE(kStackTraceMax, common_flags()->fast_unwind_on_fatal)
Kostya Serebryanybaf583c2012-12-13 09:34:23 +000067
Sergey Matveev0c8ed9c2013-05-06 11:27:58 +000068#define GET_STACK_TRACE_THREAD \
Kostya Serebryanybaf583c2012-12-13 09:34:23 +000069 GET_STACK_TRACE(kStackTraceMax, true)
70
Sergey Matveev0c8ed9c2013-05-06 11:27:58 +000071#define GET_STACK_TRACE_MALLOC \
72 GET_STACK_TRACE(common_flags()->malloc_context_size, \
73 common_flags()->fast_unwind_on_malloc)
Kostya Serebryanybaf583c2012-12-13 09:34:23 +000074
75#define GET_STACK_TRACE_FREE GET_STACK_TRACE_MALLOC
Kostya Serebryany019b76f2011-11-30 01:07:02 +000076
Sergey Matveevd8fb4d82013-12-03 18:24:28 +000077#define PRINT_CURRENT_STACK() \
78 { \
79 GET_STACK_TRACE_FATAL_HERE; \
Alexey Samsonovf2c76592013-12-19 11:25:05 +000080 stack.Print(); \
Chandler Carruthbbff2782012-06-25 06:53:10 +000081 }
Kostya Serebryany019b76f2011-11-30 01:07:02 +000082
83#endif // ASAN_STACK_H