asan_stack.h revision ee3925515e4c7966f3ef489f687aa7e5692806a9
1//===-- 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 21static const uptr kStackTraceMax = 64; 22 23struct AsanStackTrace { 24 uptr size; 25 uptr max_size; 26 uptr trace[kStackTraceMax]; 27 static void PrintStack(uptr *addr, uptr size); 28 void PrintStack() { 29 PrintStack(this->trace, this->size); 30 } 31 void CopyTo(uptr *dst, uptr dst_size) { 32 for (uptr i = 0; i < size && i < dst_size; i++) 33 dst[i] = trace[i]; 34 for (uptr i = size; i < dst_size; i++) 35 dst[i] = 0; 36 } 37 38 void CopyFrom(uptr *src, uptr src_size) { 39 size = src_size; 40 if (size > kStackTraceMax) size = kStackTraceMax; 41 for (uptr i = 0; i < size; i++) { 42 trace[i] = src[i]; 43 } 44 } 45 46 void GetStackTrace(uptr max_s, uptr pc, uptr bp); 47 48 void FastUnwindStack(uptr pc, uptr bp); 49 50 static uptr GetCurrentPc(); 51 52 static uptr CompressStack(AsanStackTrace *stack, 53 u32 *compressed, uptr size); 54 static void UncompressStack(AsanStackTrace *stack, 55 u32 *compressed, uptr size); 56}; 57 58} // namespace __asan 59 60// 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 \ 63 uptr bp = GET_CURRENT_FRAME(); \ 64 uptr pc = GET_CALLER_PC(); \ 65 uptr local_stack; \ 66 uptr sp = (uptr)&local_stack; 67 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 \ 71 uptr bp = GET_CURRENT_FRAME(); \ 72 uptr pc = AsanStackTrace::GetCurrentPc(); \ 73 uptr local_stack; \ 74 uptr sp = (uptr)&local_stack; 75 76// 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. 80#define GET_STACK_TRACE_WITH_PC_AND_BP(max_s, pc, bp) \ 81 AsanStackTrace stack; \ 82 stack.GetStackTrace(max_s, pc, bp); \ 83 84// 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 88#define GET_STACK_TRACE_HERE(max_size) \ 89 GET_STACK_TRACE_WITH_PC_AND_BP(max_size, \ 90 AsanStackTrace::GetCurrentPc(), GET_CURRENT_FRAME()) \ 91 92#define GET_STACK_TRACE_HERE_FOR_MALLOC \ 93 GET_STACK_TRACE_HERE(FLAG_malloc_context_size) 94 95#define GET_STACK_TRACE_HERE_FOR_FREE(ptr) \ 96 GET_STACK_TRACE_HERE(FLAG_malloc_context_size) 97 98#define PRINT_CURRENT_STACK() \ 99 { \ 100 GET_STACK_TRACE_HERE(kStackTraceMax); \ 101 stack.PrintStack(); \ 102 } \ 103 104#endif // ASAN_STACK_H 105