2011-11-30 09:07:02 +08:00
|
|
|
//===-- asan_stack.h --------------------------------------------*- C++ -*-===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file is a part of AddressSanitizer, an address sanity checker.
|
|
|
|
//
|
|
|
|
// ASan-private header for asan_stack.cc.
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
#ifndef ASAN_STACK_H
|
|
|
|
#define ASAN_STACK_H
|
|
|
|
|
|
|
|
#include "asan_internal.h"
|
|
|
|
|
|
|
|
namespace __asan {
|
|
|
|
|
2012-05-31 22:35:53 +08:00
|
|
|
static const uptr kStackTraceMax = 64;
|
2011-11-30 09:07:02 +08:00
|
|
|
|
2012-08-28 19:54:30 +08:00
|
|
|
struct StackTrace {
|
2012-05-31 22:35:53 +08:00
|
|
|
uptr size;
|
|
|
|
uptr max_size;
|
|
|
|
uptr trace[kStackTraceMax];
|
|
|
|
static void PrintStack(uptr *addr, uptr size);
|
2011-11-30 09:07:02 +08:00
|
|
|
void PrintStack() {
|
|
|
|
PrintStack(this->trace, this->size);
|
|
|
|
}
|
2012-05-31 22:35:53 +08:00
|
|
|
void CopyTo(uptr *dst, uptr dst_size) {
|
|
|
|
for (uptr i = 0; i < size && i < dst_size; i++)
|
2011-11-30 09:07:02 +08:00
|
|
|
dst[i] = trace[i];
|
2012-05-31 22:35:53 +08:00
|
|
|
for (uptr i = size; i < dst_size; i++)
|
2011-11-30 09:07:02 +08:00
|
|
|
dst[i] = 0;
|
|
|
|
}
|
|
|
|
|
2012-05-31 22:35:53 +08:00
|
|
|
void CopyFrom(uptr *src, uptr src_size) {
|
2011-11-30 09:07:02 +08:00
|
|
|
size = src_size;
|
|
|
|
if (size > kStackTraceMax) size = kStackTraceMax;
|
2012-05-31 22:35:53 +08:00
|
|
|
for (uptr i = 0; i < size; i++) {
|
2011-11-30 09:07:02 +08:00
|
|
|
trace[i] = src[i];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-08-28 21:25:55 +08:00
|
|
|
void FastUnwindStack(uptr pc, uptr bp, uptr stack_top, uptr stack_bottom);
|
2012-01-19 19:34:18 +08:00
|
|
|
|
2012-05-31 22:35:53 +08:00
|
|
|
static uptr GetCurrentPc();
|
2011-11-30 09:07:02 +08:00
|
|
|
|
2012-08-28 19:54:30 +08:00
|
|
|
static uptr CompressStack(StackTrace *stack,
|
2012-05-31 23:02:07 +08:00
|
|
|
u32 *compressed, uptr size);
|
2012-08-28 19:54:30 +08:00
|
|
|
static void UncompressStack(StackTrace *stack,
|
2012-05-31 23:02:07 +08:00
|
|
|
u32 *compressed, uptr size);
|
2011-11-30 09:07:02 +08:00
|
|
|
};
|
|
|
|
|
2012-08-28 21:25:55 +08:00
|
|
|
void GetStackTrace(StackTrace *trace, uptr max_s, uptr pc, uptr bp);
|
|
|
|
|
|
|
|
|
|
|
|
|
2011-11-30 09:07:02 +08:00
|
|
|
} // namespace __asan
|
|
|
|
|
2012-03-15 09:36:00 +08:00
|
|
|
// Use this macro if you want to print stack trace with the caller
|
|
|
|
// of the current function in the top frame.
|
|
|
|
#define GET_CALLER_PC_BP_SP \
|
2012-05-31 22:35:53 +08:00
|
|
|
uptr bp = GET_CURRENT_FRAME(); \
|
|
|
|
uptr pc = GET_CALLER_PC(); \
|
|
|
|
uptr local_stack; \
|
2012-06-25 14:53:10 +08:00
|
|
|
uptr sp = (uptr)&local_stack
|
2012-03-15 09:36:00 +08:00
|
|
|
|
|
|
|
// Use this macro if you want to print stack trace with the current
|
|
|
|
// function in the top frame.
|
|
|
|
#define GET_CURRENT_PC_BP_SP \
|
2012-05-31 22:35:53 +08:00
|
|
|
uptr bp = GET_CURRENT_FRAME(); \
|
2012-08-28 19:54:30 +08:00
|
|
|
uptr pc = StackTrace::GetCurrentPc(); \
|
2012-05-31 22:35:53 +08:00
|
|
|
uptr local_stack; \
|
2012-06-25 14:53:10 +08:00
|
|
|
uptr sp = (uptr)&local_stack
|
2012-03-15 09:36:00 +08:00
|
|
|
|
2011-11-30 09:07:02 +08:00
|
|
|
// Get the stack trace with the given pc and bp.
|
|
|
|
// The pc will be in the position 0 of the resulting stack trace.
|
|
|
|
// The bp may refer to the current frame or to the caller's frame.
|
|
|
|
// fast_unwind is currently unused.
|
2012-01-19 19:34:18 +08:00
|
|
|
#define GET_STACK_TRACE_WITH_PC_AND_BP(max_s, pc, bp) \
|
2012-08-28 19:54:30 +08:00
|
|
|
StackTrace stack; \
|
2012-08-28 21:25:55 +08:00
|
|
|
GetStackTrace(&stack, max_s, pc, bp)
|
2011-11-30 09:07:02 +08:00
|
|
|
|
2012-01-17 14:35:31 +08:00
|
|
|
// NOTE: A Rule of thumb is to retrieve stack trace in the interceptors
|
|
|
|
// as early as possible (in functions exposed to the user), as we generally
|
|
|
|
// don't want stack trace to contain functions from ASan internals.
|
|
|
|
|
2012-01-19 19:34:18 +08:00
|
|
|
#define GET_STACK_TRACE_HERE(max_size) \
|
|
|
|
GET_STACK_TRACE_WITH_PC_AND_BP(max_size, \
|
2012-08-28 19:54:30 +08:00
|
|
|
StackTrace::GetCurrentPc(), GET_CURRENT_FRAME())
|
2011-11-30 09:07:02 +08:00
|
|
|
|
2012-01-19 19:34:18 +08:00
|
|
|
#define GET_STACK_TRACE_HERE_FOR_MALLOC \
|
2012-07-09 22:36:04 +08:00
|
|
|
GET_STACK_TRACE_HERE(flags()->malloc_context_size)
|
2011-11-30 09:07:02 +08:00
|
|
|
|
2012-01-19 19:34:18 +08:00
|
|
|
#define GET_STACK_TRACE_HERE_FOR_FREE(ptr) \
|
2012-07-09 22:36:04 +08:00
|
|
|
GET_STACK_TRACE_HERE(flags()->malloc_context_size)
|
2011-11-30 09:07:02 +08:00
|
|
|
|
|
|
|
#define PRINT_CURRENT_STACK() \
|
|
|
|
{ \
|
2012-01-19 19:34:18 +08:00
|
|
|
GET_STACK_TRACE_HERE(kStackTraceMax); \
|
2011-11-30 09:07:02 +08:00
|
|
|
stack.PrintStack(); \
|
2012-06-25 14:53:10 +08:00
|
|
|
}
|
2011-11-30 09:07:02 +08:00
|
|
|
|
|
|
|
#endif // ASAN_STACK_H
|