[Reland][Runtimes] Merge 'compile_commands.json' files from runtimes build (#116303)
[llvm-project.git] / compiler-rt / lib / sanitizer_common / sanitizer_unwind_fuchsia.cpp
blobf3eb8591dcbc4858968f414267b3fa2bd5616409
1 //===------------------ sanitizer_unwind_fuchsia.cpp
2 //---------------------------===//
3 //
4 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
5 // See https://llvm.org/LICENSE.txt for license information.
6 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 /// Sanitizer unwind Fuchsia specific functions.
12 //===----------------------------------------------------------------------===//
14 #include "sanitizer_platform.h"
15 #if SANITIZER_FUCHSIA
17 # include <limits.h>
18 # include <unwind.h>
20 # include "sanitizer_common.h"
21 # include "sanitizer_stacktrace.h"
23 namespace __sanitizer {
25 # if SANITIZER_CAN_SLOW_UNWIND
26 struct UnwindTraceArg {
27 BufferedStackTrace *stack;
28 u32 max_depth;
31 _Unwind_Reason_Code Unwind_Trace(struct _Unwind_Context *ctx, void *param) {
32 UnwindTraceArg *arg = static_cast<UnwindTraceArg *>(param);
33 CHECK_LT(arg->stack->size, arg->max_depth);
34 uptr pc = _Unwind_GetIP(ctx);
35 if (pc < GetPageSizeCached())
36 return _URC_NORMAL_STOP;
37 arg->stack->trace_buffer[arg->stack->size++] = pc;
38 return (arg->stack->size == arg->max_depth ? _URC_NORMAL_STOP
39 : _URC_NO_REASON);
42 void BufferedStackTrace::UnwindSlow(uptr pc, u32 max_depth) {
43 CHECK_GE(max_depth, 2);
44 size = 0;
45 UnwindTraceArg arg = {this, Min(max_depth + 1, kStackTraceMax)};
46 _Unwind_Backtrace(Unwind_Trace, &arg);
47 CHECK_GT(size, 0);
48 // We need to pop a few frames so that pc is on top.
49 uptr to_pop = LocatePcInTrace(pc);
50 // trace_buffer[0] belongs to the current function so we always pop it,
51 // unless there is only 1 frame in the stack trace (1 frame is always better
52 // than 0!).
53 PopStackFrames(Min(to_pop, static_cast<uptr>(1)));
54 trace_buffer[0] = pc;
57 void BufferedStackTrace::UnwindSlow(uptr pc, void *context, u32 max_depth) {
58 CHECK(context);
59 CHECK_GE(max_depth, 2);
60 UNREACHABLE("signal context doesn't exist");
62 # endif // SANITIZER_CAN_SLOW_UNWIND
64 } // namespace __sanitizer
66 #endif // SANITIZER_FUCHSIA