llvm-project
89 строк · 2.3 Кб
1//===-- asan_stack.cpp ----------------------------------------------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// This file is a part of AddressSanitizer, an address sanity checker.
10//
11// Code for ASan stack trace.
12//===----------------------------------------------------------------------===//
13#include "asan_internal.h"14#include "asan_stack.h"15#include "sanitizer_common/sanitizer_atomic.h"16
17namespace __asan {18
19static atomic_uint32_t malloc_context_size;20
21void SetMallocContextSize(u32 size) {22atomic_store(&malloc_context_size, size, memory_order_release);23}
24
25u32 GetMallocContextSize() {26return atomic_load(&malloc_context_size, memory_order_acquire);27}
28
29namespace {30
31// ScopedUnwinding is a scope for stacktracing member of a context
32class ScopedUnwinding {33public:34explicit ScopedUnwinding(AsanThread *t) : thread(t) {35if (thread) {36can_unwind = !thread->isUnwinding();37thread->setUnwinding(true);38}39}40~ScopedUnwinding() {41if (thread)42thread->setUnwinding(false);43}44
45bool CanUnwind() const { return can_unwind; }46
47private:48AsanThread *thread = nullptr;49bool can_unwind = true;50};51
52} // namespace53
54} // namespace __asan55
56void __sanitizer::BufferedStackTrace::UnwindImpl(57uptr pc, uptr bp, void *context, bool request_fast, u32 max_depth) {58using namespace __asan;59size = 0;60if (UNLIKELY(!AsanInited()))61return;62request_fast = StackTrace::WillUseFastUnwind(request_fast);63AsanThread *t = GetCurrentThread();64ScopedUnwinding unwind_scope(t);65if (!unwind_scope.CanUnwind())66return;67if (request_fast) {68if (t) {69Unwind(max_depth, pc, bp, nullptr, t->stack_top(), t->stack_bottom(),70true);71}72return;73}74if (SANITIZER_MIPS && t &&75!IsValidFrame(bp, t->stack_top(), t->stack_bottom()))76return;77Unwind(max_depth, pc, bp, context, t ? t->stack_top() : 0,78t ? t->stack_bottom() : 0, false);79}
80
81// ------------------ Interface -------------- {{{1
82
83extern "C" {84SANITIZER_INTERFACE_ATTRIBUTE
85void __sanitizer_print_stack_trace() {86using namespace __asan;87PRINT_CURRENT_STACK();88}
89} // extern "C"90