1//===-- asan_stack.h --------------------------------------------*- C++ -*-===// 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// ASan-private header for asan_stack.cpp. 12//===----------------------------------------------------------------------===// 13 14#ifndef ASAN_STACK_H 15#define ASAN_STACK_H 16 17#include "asan_flags.h" 18#include "asan_thread.h" 19#include "sanitizer_common/sanitizer_flags.h" 20#include "sanitizer_common/sanitizer_stacktrace.h" 21 22namespace __asan { 23 24static const u32 kDefaultMallocContextSize = 30; 25 26void SetMallocContextSize(u32 size); 27u32 GetMallocContextSize(); 28 29} // namespace __asan 30 31// NOTE: A Rule of thumb is to retrieve stack trace in the interceptors 32// as early as possible (in functions exposed to the user), as we generally 33// don't want stack trace to contain functions from ASan internals. 34 35#define GET_STACK_TRACE(max_size, fast) \ 36 BufferedStackTrace stack; \ 37 if (max_size <= 2) { \ 38 stack.size = max_size; \ 39 if (max_size > 0) { \ 40 stack.top_frame_bp = GET_CURRENT_FRAME(); \ 41 stack.trace_buffer[0] = StackTrace::GetCurrentPc(); \ 42 if (max_size > 1) stack.trace_buffer[1] = GET_CALLER_PC(); \ 43 } \ 44 } else { \ 45 stack.Unwind(StackTrace::GetCurrentPc(), \ 46 GET_CURRENT_FRAME(), nullptr, fast, max_size); \ 47 } 48 49#define GET_STACK_TRACE_FATAL(pc, bp) \ 50 BufferedStackTrace stack; \ 51 stack.Unwind(pc, bp, nullptr, \ 52 common_flags()->fast_unwind_on_fatal) 53 54#define GET_STACK_TRACE_SIGNAL(sig) \ 55 BufferedStackTrace stack; \ 56 stack.Unwind((sig).pc, (sig).bp, (sig).context, \ 57 common_flags()->fast_unwind_on_fatal) 58 59#define GET_STACK_TRACE_FATAL_HERE \ 60 GET_STACK_TRACE(kStackTraceMax, common_flags()->fast_unwind_on_fatal) 61 62#define GET_STACK_TRACE_CHECK_HERE \ 63 GET_STACK_TRACE(kStackTraceMax, common_flags()->fast_unwind_on_check) 64 65#define GET_STACK_TRACE_THREAD \ 66 GET_STACK_TRACE(kStackTraceMax, true) 67 68#define GET_STACK_TRACE_MALLOC \ 69 GET_STACK_TRACE(GetMallocContextSize(), common_flags()->fast_unwind_on_malloc) 70 71#define GET_STACK_TRACE_FREE GET_STACK_TRACE_MALLOC 72 73#define PRINT_CURRENT_STACK() \ 74 { \ 75 GET_STACK_TRACE_FATAL_HERE; \ 76 stack.Print(); \ 77 } 78 79#define PRINT_CURRENT_STACK_CHECK() \ 80 { \ 81 GET_STACK_TRACE_CHECK_HERE; \ 82 stack.Print(); \ 83 } 84 85#endif // ASAN_STACK_H 86