2012-06-04 21:50:10 +08:00
|
|
|
//===-- asan_stack.cc -----------------------------------------------------===//
|
2011-11-30 09:07:02 +08:00
|
|
|
//
|
2019-01-19 16:50:56 +08:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2011-11-30 09:07:02 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file is a part of AddressSanitizer, an address sanity checker.
|
|
|
|
//
|
|
|
|
// Code for ASan stack trace.
|
|
|
|
//===----------------------------------------------------------------------===//
|
2013-01-31 21:46:14 +08:00
|
|
|
#include "asan_internal.h"
|
2011-11-30 09:07:02 +08:00
|
|
|
#include "asan_stack.h"
|
2014-12-16 09:23:03 +08:00
|
|
|
#include "sanitizer_common/sanitizer_atomic.h"
|
|
|
|
|
|
|
|
namespace __asan {
|
|
|
|
|
|
|
|
static atomic_uint32_t malloc_context_size;
|
|
|
|
|
|
|
|
void SetMallocContextSize(u32 size) {
|
|
|
|
atomic_store(&malloc_context_size, size, memory_order_release);
|
|
|
|
}
|
|
|
|
|
|
|
|
u32 GetMallocContextSize() {
|
|
|
|
return atomic_load(&malloc_context_size, memory_order_acquire);
|
|
|
|
}
|
|
|
|
|
2019-03-05 12:36:56 +08:00
|
|
|
namespace {
|
|
|
|
|
|
|
|
// ScopedUnwinding is a scope for stacktracing member of a context
|
|
|
|
class ScopedUnwinding {
|
|
|
|
public:
|
2019-03-05 13:40:05 +08:00
|
|
|
explicit ScopedUnwinding(AsanThread *t) : thread(t) {
|
|
|
|
if (thread) {
|
|
|
|
can_unwind = !thread->isUnwinding();
|
|
|
|
thread->setUnwinding(true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
~ScopedUnwinding() {
|
|
|
|
if (thread)
|
|
|
|
thread->setUnwinding(false);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CanUnwind() const { return can_unwind; }
|
2019-03-05 12:36:56 +08:00
|
|
|
|
|
|
|
private:
|
2019-03-05 13:40:05 +08:00
|
|
|
AsanThread *thread = nullptr;
|
|
|
|
bool can_unwind = true;
|
2019-03-05 12:36:56 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
2014-12-16 09:23:03 +08:00
|
|
|
} // namespace __asan
|
2012-08-22 21:31:37 +08:00
|
|
|
|
2019-03-02 06:10:49 +08:00
|
|
|
void __sanitizer::BufferedStackTrace::UnwindImpl(
|
|
|
|
uptr pc, uptr bp, void *context, bool request_fast, u32 max_depth) {
|
2019-02-28 06:16:02 +08:00
|
|
|
using namespace __asan;
|
2019-03-05 10:50:49 +08:00
|
|
|
size = 0;
|
|
|
|
if (UNLIKELY(!asan_inited))
|
|
|
|
return;
|
2019-03-05 13:52:34 +08:00
|
|
|
request_fast = StackTrace::WillUseFastUnwind(request_fast);
|
2019-03-05 10:50:49 +08:00
|
|
|
AsanThread *t = GetCurrentThread();
|
2019-03-05 13:40:05 +08:00
|
|
|
ScopedUnwinding unwind_scope(t);
|
|
|
|
if (!unwind_scope.CanUnwind())
|
|
|
|
return;
|
2019-03-05 13:52:34 +08:00
|
|
|
if (request_fast) {
|
|
|
|
if (t) {
|
|
|
|
Unwind(max_depth, pc, bp, nullptr, t->stack_top(), t->stack_bottom(),
|
|
|
|
true);
|
2019-03-05 12:36:49 +08:00
|
|
|
}
|
|
|
|
return;
|
2019-02-28 06:16:02 +08:00
|
|
|
}
|
2019-03-05 13:52:34 +08:00
|
|
|
if (SANITIZER_MIPS && t &&
|
|
|
|
!IsValidFrame(bp, t->stack_top(), t->stack_bottom()))
|
2019-03-05 12:36:49 +08:00
|
|
|
return;
|
2019-03-05 13:52:34 +08:00
|
|
|
Unwind(max_depth, pc, bp, context, 0, 0, false);
|
2019-02-28 06:16:02 +08:00
|
|
|
}
|
|
|
|
|
2012-08-22 21:31:37 +08:00
|
|
|
// ------------------ Interface -------------- {{{1
|
|
|
|
|
2013-12-04 16:17:45 +08:00
|
|
|
extern "C" {
|
|
|
|
SANITIZER_INTERFACE_ATTRIBUTE
|
|
|
|
void __sanitizer_print_stack_trace() {
|
2013-12-04 02:24:28 +08:00
|
|
|
using namespace __asan;
|
|
|
|
PRINT_CURRENT_STACK();
|
|
|
|
}
|
2013-12-04 16:17:45 +08:00
|
|
|
} // extern "C"
|