blob: 0d483b9a182f3303b37404d71353dd73d86667a0 [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
Kostya Serebryany6b0d7752012-08-28 11:54:30 +000023struct StackTrace {
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 Serebryanyee928772012-08-28 13:25:55 +000046 void FastUnwindStack(uptr pc, uptr bp, uptr stack_top, uptr stack_bottom);
Evgeniy Stepanov84c44a82012-01-19 11:34:18 +000047
Kostya Serebryany8d032042012-05-31 14:35:53 +000048 static uptr GetCurrentPc();
Kostya Serebryany019b76f2011-11-30 01:07:02 +000049
Kostya Serebryany6b0d7752012-08-28 11:54:30 +000050 static uptr CompressStack(StackTrace *stack,
Kostya Serebryany1d35d152012-05-31 15:02:07 +000051 u32 *compressed, uptr size);
Kostya Serebryany6b0d7752012-08-28 11:54:30 +000052 static void UncompressStack(StackTrace *stack,
Kostya Serebryany1d35d152012-05-31 15:02:07 +000053 u32 *compressed, uptr size);
Kostya Serebryany019b76f2011-11-30 01:07:02 +000054};
55
Kostya Serebryanyee928772012-08-28 13:25:55 +000056void GetStackTrace(StackTrace *trace, uptr max_s, uptr pc, uptr bp);
57
58
59
Kostya Serebryany019b76f2011-11-30 01:07:02 +000060} // namespace __asan
61
Kostya Serebryany54a3cac2012-03-15 01:36:00 +000062// Use this macro if you want to print stack trace with the caller
63// of the current function in the top frame.
64#define GET_CALLER_PC_BP_SP \
Kostya Serebryany8d032042012-05-31 14:35:53 +000065 uptr bp = GET_CURRENT_FRAME(); \
66 uptr pc = GET_CALLER_PC(); \
67 uptr local_stack; \
Chandler Carruthbbff2782012-06-25 06:53:10 +000068 uptr sp = (uptr)&local_stack
Kostya Serebryany54a3cac2012-03-15 01:36:00 +000069
70// Use this macro if you want to print stack trace with the current
71// function in the top frame.
72#define GET_CURRENT_PC_BP_SP \
Kostya Serebryany8d032042012-05-31 14:35:53 +000073 uptr bp = GET_CURRENT_FRAME(); \
Kostya Serebryany6b0d7752012-08-28 11:54:30 +000074 uptr pc = StackTrace::GetCurrentPc(); \
Kostya Serebryany8d032042012-05-31 14:35:53 +000075 uptr local_stack; \
Chandler Carruthbbff2782012-06-25 06:53:10 +000076 uptr sp = (uptr)&local_stack
Kostya Serebryany54a3cac2012-03-15 01:36:00 +000077
Kostya Serebryany019b76f2011-11-30 01:07:02 +000078// Get the stack trace with the given pc and bp.
79// The pc will be in the position 0 of the resulting stack trace.
80// The bp may refer to the current frame or to the caller's frame.
81// fast_unwind is currently unused.
Evgeniy Stepanov84c44a82012-01-19 11:34:18 +000082#define GET_STACK_TRACE_WITH_PC_AND_BP(max_s, pc, bp) \
Kostya Serebryany6b0d7752012-08-28 11:54:30 +000083 StackTrace stack; \
Kostya Serebryanyee928772012-08-28 13:25:55 +000084 GetStackTrace(&stack, max_s, pc, bp)
Kostya Serebryany019b76f2011-11-30 01:07:02 +000085
Alexey Samsonov2d3a67b2012-01-17 06:35:31 +000086// NOTE: A Rule of thumb is to retrieve stack trace in the interceptors
87// as early as possible (in functions exposed to the user), as we generally
88// don't want stack trace to contain functions from ASan internals.
89
Evgeniy Stepanov84c44a82012-01-19 11:34:18 +000090#define GET_STACK_TRACE_HERE(max_size) \
91 GET_STACK_TRACE_WITH_PC_AND_BP(max_size, \
Kostya Serebryany6b0d7752012-08-28 11:54:30 +000092 StackTrace::GetCurrentPc(), GET_CURRENT_FRAME())
Kostya Serebryany019b76f2011-11-30 01:07:02 +000093
Evgeniy Stepanov84c44a82012-01-19 11:34:18 +000094#define GET_STACK_TRACE_HERE_FOR_MALLOC \
Alexey Samsonov34efb8e2012-07-09 14:36:04 +000095 GET_STACK_TRACE_HERE(flags()->malloc_context_size)
Kostya Serebryany019b76f2011-11-30 01:07:02 +000096
Evgeniy Stepanov84c44a82012-01-19 11:34:18 +000097#define GET_STACK_TRACE_HERE_FOR_FREE(ptr) \
Alexey Samsonov34efb8e2012-07-09 14:36:04 +000098 GET_STACK_TRACE_HERE(flags()->malloc_context_size)
Kostya Serebryany019b76f2011-11-30 01:07:02 +000099
100#define PRINT_CURRENT_STACK() \
101 { \
Evgeniy Stepanov84c44a82012-01-19 11:34:18 +0000102 GET_STACK_TRACE_HERE(kStackTraceMax); \
Kostya Serebryany019b76f2011-11-30 01:07:02 +0000103 stack.PrintStack(); \
Chandler Carruthbbff2782012-06-25 06:53:10 +0000104 }
Kostya Serebryany019b76f2011-11-30 01:07:02 +0000105
106#endif // ASAN_STACK_H