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.
|
|
|
|
//===----------------------------------------------------------------------===//
|
2015-10-01 08:22:21 +08:00
|
|
|
|
2011-11-30 09:07:02 +08:00
|
|
|
#ifndef ASAN_STACK_H
|
|
|
|
#define ASAN_STACK_H
|
|
|
|
|
2012-12-13 17:34:23 +08:00
|
|
|
#include "asan_flags.h"
|
2013-05-08 20:45:55 +08:00
|
|
|
#include "asan_thread.h"
|
2013-05-06 19:27:58 +08:00
|
|
|
#include "sanitizer_common/sanitizer_flags.h"
|
|
|
|
#include "sanitizer_common/sanitizer_stacktrace.h"
|
2011-11-30 09:07:02 +08:00
|
|
|
|
2014-03-04 21:12:25 +08:00
|
|
|
namespace __asan {
|
|
|
|
|
2014-12-16 09:23:03 +08:00
|
|
|
static const u32 kDefaultMallocContextSize = 30;
|
|
|
|
|
|
|
|
void SetMallocContextSize(u32 size);
|
|
|
|
u32 GetMallocContextSize();
|
|
|
|
|
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.
|
2014-03-04 21:12:25 +08:00
|
|
|
ALWAYS_INLINE
|
2014-10-26 11:35:14 +08:00
|
|
|
void GetStackTraceWithPcBpAndContext(BufferedStackTrace *stack, uptr max_depth,
|
|
|
|
uptr pc, uptr bp, void *context,
|
|
|
|
bool fast) {
|
2013-05-08 20:45:55 +08:00
|
|
|
#if SANITIZER_WINDOWS
|
2014-03-04 21:12:25 +08:00
|
|
|
stack->Unwind(max_depth, pc, bp, context, 0, 0, fast);
|
2013-05-08 20:45:55 +08:00
|
|
|
#else
|
2014-03-04 21:12:25 +08:00
|
|
|
AsanThread *t;
|
|
|
|
stack->size = 0;
|
2014-05-14 22:03:31 +08:00
|
|
|
if (LIKELY(asan_inited)) {
|
2014-03-04 21:12:25 +08:00
|
|
|
if ((t = GetCurrentThread()) && !t->isUnwinding()) {
|
2014-11-10 23:22:04 +08:00
|
|
|
// On FreeBSD the slow unwinding that leverages _Unwind_Backtrace()
|
|
|
|
// yields the call stack of the signal's handler and not of the code
|
|
|
|
// that raised the signal (as it does on Linux).
|
|
|
|
if (SANITIZER_FREEBSD && t->isInDeadlySignal()) fast = true;
|
2014-03-04 21:12:25 +08:00
|
|
|
uptr stack_top = t->stack_top();
|
|
|
|
uptr stack_bottom = t->stack_bottom();
|
|
|
|
ScopedUnwinding unwind_scope(t);
|
2016-05-18 14:09:26 +08:00
|
|
|
if (!SANITIZER_MIPS || IsValidFrame(bp, stack_top, stack_bottom)) {
|
|
|
|
stack->Unwind(max_depth, pc, bp, context, stack_top, stack_bottom,
|
|
|
|
fast);
|
|
|
|
}
|
2015-10-01 08:22:21 +08:00
|
|
|
} else if (!t && !fast) {
|
2014-03-04 21:12:25 +08:00
|
|
|
/* If GetCurrentThread() has failed, try to do slow unwind anyways. */
|
|
|
|
stack->Unwind(max_depth, pc, bp, context, 0, 0, false);
|
|
|
|
}
|
2013-05-08 20:45:55 +08:00
|
|
|
}
|
2015-10-01 08:22:21 +08:00
|
|
|
#endif // SANITIZER_WINDOWS
|
2014-03-04 21:12:25 +08:00
|
|
|
}
|
|
|
|
|
2015-10-01 08:22:21 +08:00
|
|
|
} // namespace __asan
|
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.
|
|
|
|
|
2014-02-11 21:38:57 +08:00
|
|
|
#define GET_STACK_TRACE(max_size, fast) \
|
2014-10-26 11:35:14 +08:00
|
|
|
BufferedStackTrace stack; \
|
2014-03-04 21:12:25 +08:00
|
|
|
if (max_size <= 2) { \
|
|
|
|
stack.size = max_size; \
|
|
|
|
if (max_size > 0) { \
|
|
|
|
stack.top_frame_bp = GET_CURRENT_FRAME(); \
|
2014-10-26 11:35:14 +08:00
|
|
|
stack.trace_buffer[0] = StackTrace::GetCurrentPc(); \
|
2014-03-04 22:06:11 +08:00
|
|
|
if (max_size > 1) \
|
2014-10-26 11:35:14 +08:00
|
|
|
stack.trace_buffer[1] = GET_CALLER_PC(); \
|
2014-03-04 21:12:25 +08:00
|
|
|
} \
|
|
|
|
} else { \
|
|
|
|
GetStackTraceWithPcBpAndContext(&stack, max_size, \
|
|
|
|
StackTrace::GetCurrentPc(), \
|
|
|
|
GET_CURRENT_FRAME(), 0, fast); \
|
|
|
|
}
|
2011-11-30 09:07:02 +08:00
|
|
|
|
2014-03-04 21:12:25 +08:00
|
|
|
#define GET_STACK_TRACE_FATAL(pc, bp) \
|
2014-10-26 11:35:14 +08:00
|
|
|
BufferedStackTrace stack; \
|
2014-03-04 21:12:25 +08:00
|
|
|
GetStackTraceWithPcBpAndContext(&stack, kStackTraceMax, pc, bp, 0, \
|
|
|
|
common_flags()->fast_unwind_on_fatal)
|
2014-02-11 21:38:57 +08:00
|
|
|
|
2014-11-25 21:00:21 +08:00
|
|
|
#define GET_STACK_TRACE_SIGNAL(sig) \
|
2014-10-26 11:35:14 +08:00
|
|
|
BufferedStackTrace stack; \
|
2014-11-25 21:00:21 +08:00
|
|
|
GetStackTraceWithPcBpAndContext(&stack, kStackTraceMax, \
|
|
|
|
(sig).pc, (sig).bp, (sig).context, \
|
2014-03-04 21:12:25 +08:00
|
|
|
common_flags()->fast_unwind_on_fatal)
|
2011-11-30 09:07:02 +08:00
|
|
|
|
2013-05-06 19:27:58 +08:00
|
|
|
#define GET_STACK_TRACE_FATAL_HERE \
|
|
|
|
GET_STACK_TRACE(kStackTraceMax, common_flags()->fast_unwind_on_fatal)
|
2012-12-13 17:34:23 +08:00
|
|
|
|
2014-10-14 17:36:24 +08:00
|
|
|
#define GET_STACK_TRACE_CHECK_HERE \
|
|
|
|
GET_STACK_TRACE(kStackTraceMax, common_flags()->fast_unwind_on_check)
|
|
|
|
|
2013-05-06 19:27:58 +08:00
|
|
|
#define GET_STACK_TRACE_THREAD \
|
2012-12-13 17:34:23 +08:00
|
|
|
GET_STACK_TRACE(kStackTraceMax, true)
|
|
|
|
|
2014-12-16 09:23:03 +08:00
|
|
|
#define GET_STACK_TRACE_MALLOC \
|
|
|
|
GET_STACK_TRACE(GetMallocContextSize(), common_flags()->fast_unwind_on_malloc)
|
2012-12-13 17:34:23 +08:00
|
|
|
|
|
|
|
#define GET_STACK_TRACE_FREE GET_STACK_TRACE_MALLOC
|
2011-11-30 09:07:02 +08:00
|
|
|
|
2013-12-04 02:24:28 +08:00
|
|
|
#define PRINT_CURRENT_STACK() \
|
|
|
|
{ \
|
|
|
|
GET_STACK_TRACE_FATAL_HERE; \
|
2013-12-19 19:25:05 +08:00
|
|
|
stack.Print(); \
|
2012-06-25 14:53:10 +08:00
|
|
|
}
|
2011-11-30 09:07:02 +08:00
|
|
|
|
2014-10-14 17:36:24 +08:00
|
|
|
#define PRINT_CURRENT_STACK_CHECK() \
|
|
|
|
{ \
|
|
|
|
GET_STACK_TRACE_CHECK_HERE; \
|
|
|
|
stack.Print(); \
|
|
|
|
}
|
|
|
|
|
2015-10-01 08:22:21 +08:00
|
|
|
#endif // ASAN_STACK_H
|