1//===-- sanitizer_unwind_aix.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 contains the unwind.h-based (aka "slow") stack unwinding routines
10// available to the tools on AIX.
11//===----------------------------------------------------------------------===//
12
13#include "sanitizer_platform.h"
14
15#if SANITIZER_AIX
16# include <unwind.h>
17
18# include "sanitizer_common.h"
19# include "sanitizer_stacktrace.h"
20
21namespace __sanitizer {
22
23struct UnwindTraceArg {
24 BufferedStackTrace* stack;
25 u32 max_depth;
26};
27
28static _Unwind_Reason_Code Unwind_Trace(struct _Unwind_Context* ctx,
29 void* param) {
30 UnwindTraceArg* arg = (UnwindTraceArg*)param;
31 CHECK_LT(arg->stack->size, arg->max_depth);
32 uptr pc = _Unwind_GetIP(ctx);
33 // On AIX 32-bit and 64-bit, addresses up through 0x0fffffff are for kernel.
34 if (pc <= 0x0fffffff)
35 return _URC_NORMAL_STOP;
36 arg->stack->trace_buffer[arg->stack->size++] = pc;
37 if (arg->stack->size == arg->max_depth)
38 return _URC_NORMAL_STOP;
39 return _URC_NO_REASON;
40}
41
42void 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 // We need to pop a few frames so that pc is on top.
48 uptr to_pop = LocatePcInTrace(pc);
49 // trace_buffer[0] belongs to the current function so we always pop it,
50 // unless there is only 1 frame in the stack trace (1 frame is always better
51 // than 0!).
52 // 1-frame stacks don't normally happen, but this depends on the actual
53 // unwinder implementation (libgcc, libunwind, etc) which is outside of our
54 // control.
55 if (to_pop == 0 && size > 1)
56 to_pop = 1;
57
58 PopStackFrames(to_pop);
59 trace_buffer[0] = pc;
60}
61
62void BufferedStackTrace::UnwindSlow(uptr pc, void* context, u32 max_depth) {
63 CHECK(context);
64 UnwindSlow(pc, max_depth);
65}
66} // namespace __sanitizer
67
68#endif // SANITIZER_AIX
69