blob: a754515c19c4e64ee7121cc5905ed1d45708747e [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
17#include "asan_internal.h"
18
19namespace __asan {
20
Kostya Serebryany8d032042012-05-31 14:35:53 +000021static const uptr kStackTraceMax = 64;
Kostya Serebryany019b76f2011-11-30 01:07:02 +000022
23struct AsanStackTrace {
Kostya Serebryany8d032042012-05-31 14:35:53 +000024 uptr size;
25 uptr max_size;
26 uptr trace[kStackTraceMax];
27 static void PrintStack(uptr *addr, uptr size);
Kostya Serebryany019b76f2011-11-30 01:07:02 +000028 void PrintStack() {
29 PrintStack(this->trace, this->size);
30 }
Kostya Serebryany8d032042012-05-31 14:35:53 +000031 void CopyTo(uptr *dst, uptr dst_size) {
32 for (uptr i = 0; i < size && i < dst_size; i++)
Kostya Serebryany019b76f2011-11-30 01:07:02 +000033 dst[i] = trace[i];
Kostya Serebryany8d032042012-05-31 14:35:53 +000034 for (uptr i = size; i < dst_size; i++)
Kostya Serebryany019b76f2011-11-30 01:07:02 +000035 dst[i] = 0;
36 }
37
Kostya Serebryany8d032042012-05-31 14:35:53 +000038 void CopyFrom(uptr *src, uptr src_size) {
Kostya Serebryany019b76f2011-11-30 01:07:02 +000039 size = src_size;
40 if (size > kStackTraceMax) size = kStackTraceMax;
Kostya Serebryany8d032042012-05-31 14:35:53 +000041 for (uptr i = 0; i < size; i++) {
Kostya Serebryany019b76f2011-11-30 01:07:02 +000042 trace[i] = src[i];
43 }
44 }
45
Kostya Serebryany8d032042012-05-31 14:35:53 +000046 void GetStackTrace(uptr max_s, uptr pc, uptr bp);
Evgeniy Stepanov84c44a82012-01-19 11:34:18 +000047
Kostya Serebryany8d032042012-05-31 14:35:53 +000048 void FastUnwindStack(uptr pc, uptr bp);
Evgeniy Stepanov84c44a82012-01-19 11:34:18 +000049
Kostya Serebryany8d032042012-05-31 14:35:53 +000050 static uptr GetCurrentPc();
Kostya Serebryany019b76f2011-11-30 01:07:02 +000051
Kostya Serebryany8d032042012-05-31 14:35:53 +000052 static uptr CompressStack(AsanStackTrace *stack,
53 uint32_t *compressed, uptr size);
Kostya Serebryany019b76f2011-11-30 01:07:02 +000054 static void UncompressStack(AsanStackTrace *stack,
Kostya Serebryany8d032042012-05-31 14:35:53 +000055 uint32_t *compressed, uptr size);
Kostya Serebryany019b76f2011-11-30 01:07:02 +000056};
57
58} // namespace __asan
59
Kostya Serebryany54a3cac2012-03-15 01:36:00 +000060// Use this macro if you want to print stack trace with the caller
61// of the current function in the top frame.
62#define GET_CALLER_PC_BP_SP \
Kostya Serebryany8d032042012-05-31 14:35:53 +000063 uptr bp = GET_CURRENT_FRAME(); \
64 uptr pc = GET_CALLER_PC(); \
65 uptr local_stack; \
66 uptr sp = (uptr)&local_stack;
Kostya Serebryany54a3cac2012-03-15 01:36:00 +000067
68// Use this macro if you want to print stack trace with the current
69// function in the top frame.
70#define GET_CURRENT_PC_BP_SP \
Kostya Serebryany8d032042012-05-31 14:35:53 +000071 uptr bp = GET_CURRENT_FRAME(); \
72 uptr pc = AsanStackTrace::GetCurrentPc(); \
73 uptr local_stack; \
74 uptr sp = (uptr)&local_stack;
Kostya Serebryany54a3cac2012-03-15 01:36:00 +000075
Kostya Serebryany019b76f2011-11-30 01:07:02 +000076// Get the stack trace with the given pc and bp.
77// The pc will be in the position 0 of the resulting stack trace.
78// The bp may refer to the current frame or to the caller's frame.
79// fast_unwind is currently unused.
Evgeniy Stepanov84c44a82012-01-19 11:34:18 +000080#define GET_STACK_TRACE_WITH_PC_AND_BP(max_s, pc, bp) \
81 AsanStackTrace stack; \
82 stack.GetStackTrace(max_s, pc, bp); \
Kostya Serebryany019b76f2011-11-30 01:07:02 +000083
Alexey Samsonov2d3a67b2012-01-17 06:35:31 +000084// NOTE: A Rule of thumb is to retrieve stack trace in the interceptors
85// as early as possible (in functions exposed to the user), as we generally
86// don't want stack trace to contain functions from ASan internals.
87
Evgeniy Stepanov84c44a82012-01-19 11:34:18 +000088#define GET_STACK_TRACE_HERE(max_size) \
89 GET_STACK_TRACE_WITH_PC_AND_BP(max_size, \
90 AsanStackTrace::GetCurrentPc(), GET_CURRENT_FRAME()) \
Kostya Serebryany019b76f2011-11-30 01:07:02 +000091
Evgeniy Stepanov84c44a82012-01-19 11:34:18 +000092#define GET_STACK_TRACE_HERE_FOR_MALLOC \
93 GET_STACK_TRACE_HERE(FLAG_malloc_context_size)
Kostya Serebryany019b76f2011-11-30 01:07:02 +000094
Evgeniy Stepanov84c44a82012-01-19 11:34:18 +000095#define GET_STACK_TRACE_HERE_FOR_FREE(ptr) \
96 GET_STACK_TRACE_HERE(FLAG_malloc_context_size)
Kostya Serebryany019b76f2011-11-30 01:07:02 +000097
98#define PRINT_CURRENT_STACK() \
99 { \
Evgeniy Stepanov84c44a82012-01-19 11:34:18 +0000100 GET_STACK_TRACE_HERE(kStackTraceMax); \
Kostya Serebryany019b76f2011-11-30 01:07:02 +0000101 stack.PrintStack(); \
102 } \
103
104#endif // ASAN_STACK_H