Add limit for maximal alignment options (PR c/84310).
[official-gcc.git] / libsanitizer / sanitizer_common / sanitizer_unwind_linux_libcdep.cc
blob7dba9e7ccd2b4ca03bd6b9983fe687ce9a573655
1 //===-- sanitizer_unwind_linux_libcdep.cc ---------------------------------===//
2 //
3 // This file is distributed under the University of Illinois Open Source
4 // License. See LICENSE.TXT for details.
5 //
6 //===----------------------------------------------------------------------===//
7 //
8 // This file contains the unwind.h-based (aka "slow") stack unwinding routines
9 // available to the tools on Linux, Android, NetBSD and FreeBSD.
10 //===----------------------------------------------------------------------===//
12 #include "sanitizer_platform.h"
13 #if SANITIZER_FREEBSD || SANITIZER_LINUX || SANITIZER_NETBSD
14 #include "sanitizer_common.h"
15 #include "sanitizer_stacktrace.h"
17 #if SANITIZER_ANDROID
18 #include <dlfcn.h> // for dlopen()
19 #endif
21 #if SANITIZER_FREEBSD
22 #define _GNU_SOURCE // to declare _Unwind_Backtrace() from <unwind.h>
23 #endif
24 #include <unwind.h>
26 namespace __sanitizer {
28 //------------------------- SlowUnwindStack -----------------------------------
30 typedef struct {
31 uptr absolute_pc;
32 uptr stack_top;
33 uptr stack_size;
34 } backtrace_frame_t;
36 extern "C" {
37 typedef void *(*acquire_my_map_info_list_func)();
38 typedef void (*release_my_map_info_list_func)(void *map);
39 typedef sptr (*unwind_backtrace_signal_arch_func)(
40 void *siginfo, void *sigcontext, void *map_info_list,
41 backtrace_frame_t *backtrace, uptr ignore_depth, uptr max_depth);
42 acquire_my_map_info_list_func acquire_my_map_info_list;
43 release_my_map_info_list_func release_my_map_info_list;
44 unwind_backtrace_signal_arch_func unwind_backtrace_signal_arch;
45 } // extern "C"
47 #if SANITIZER_ANDROID
48 void SanitizerInitializeUnwinder() {
49 if (AndroidGetApiLevel() >= ANDROID_LOLLIPOP_MR1) return;
51 // Pre-lollipop Android can not unwind through signal handler frames with
52 // libgcc unwinder, but it has a libcorkscrew.so library with the necessary
53 // workarounds.
54 void *p = dlopen("libcorkscrew.so", RTLD_LAZY);
55 if (!p) {
56 VReport(1,
57 "Failed to open libcorkscrew.so. You may see broken stack traces "
58 "in SEGV reports.");
59 return;
61 acquire_my_map_info_list =
62 (acquire_my_map_info_list_func)(uptr)dlsym(p, "acquire_my_map_info_list");
63 release_my_map_info_list =
64 (release_my_map_info_list_func)(uptr)dlsym(p, "release_my_map_info_list");
65 unwind_backtrace_signal_arch = (unwind_backtrace_signal_arch_func)(uptr)dlsym(
66 p, "unwind_backtrace_signal_arch");
67 if (!acquire_my_map_info_list || !release_my_map_info_list ||
68 !unwind_backtrace_signal_arch) {
69 VReport(1,
70 "Failed to find one of the required symbols in libcorkscrew.so. "
71 "You may see broken stack traces in SEGV reports.");
72 acquire_my_map_info_list = 0;
73 unwind_backtrace_signal_arch = 0;
74 release_my_map_info_list = 0;
77 #endif
79 #if defined(__arm__) && !SANITIZER_NETBSD
80 // NetBSD uses dwarf EH
81 #define UNWIND_STOP _URC_END_OF_STACK
82 #define UNWIND_CONTINUE _URC_NO_REASON
83 #else
84 #define UNWIND_STOP _URC_NORMAL_STOP
85 #define UNWIND_CONTINUE _URC_NO_REASON
86 #endif
88 uptr Unwind_GetIP(struct _Unwind_Context *ctx) {
89 #if defined(__arm__) && !SANITIZER_MAC
90 uptr val;
91 _Unwind_VRS_Result res = _Unwind_VRS_Get(ctx, _UVRSC_CORE,
92 15 /* r15 = PC */, _UVRSD_UINT32, &val);
93 CHECK(res == _UVRSR_OK && "_Unwind_VRS_Get failed");
94 // Clear the Thumb bit.
95 return val & ~(uptr)1;
96 #else
97 return _Unwind_GetIP(ctx);
98 #endif
101 struct UnwindTraceArg {
102 BufferedStackTrace *stack;
103 u32 max_depth;
106 _Unwind_Reason_Code Unwind_Trace(struct _Unwind_Context *ctx, void *param) {
107 UnwindTraceArg *arg = (UnwindTraceArg*)param;
108 CHECK_LT(arg->stack->size, arg->max_depth);
109 uptr pc = Unwind_GetIP(ctx);
110 const uptr kPageSize = GetPageSizeCached();
111 // Let's assume that any pointer in the 0th page (i.e. <0x1000 on i386 and
112 // x86_64) is invalid and stop unwinding here. If we're adding support for
113 // a platform where this isn't true, we need to reconsider this check.
114 if (pc < kPageSize) return UNWIND_STOP;
115 arg->stack->trace_buffer[arg->stack->size++] = pc;
116 if (arg->stack->size == arg->max_depth) return UNWIND_STOP;
117 return UNWIND_CONTINUE;
120 void BufferedStackTrace::SlowUnwindStack(uptr pc, u32 max_depth) {
121 CHECK_GE(max_depth, 2);
122 size = 0;
123 UnwindTraceArg arg = {this, Min(max_depth + 1, kStackTraceMax)};
124 _Unwind_Backtrace(Unwind_Trace, &arg);
125 // We need to pop a few frames so that pc is on top.
126 uptr to_pop = LocatePcInTrace(pc);
127 // trace_buffer[0] belongs to the current function so we always pop it,
128 // unless there is only 1 frame in the stack trace (1 frame is always better
129 // than 0!).
130 // 1-frame stacks don't normally happen, but this depends on the actual
131 // unwinder implementation (libgcc, libunwind, etc) which is outside of our
132 // control.
133 if (to_pop == 0 && size > 1)
134 to_pop = 1;
135 PopStackFrames(to_pop);
136 trace_buffer[0] = pc;
139 void BufferedStackTrace::SlowUnwindStackWithContext(uptr pc, void *context,
140 u32 max_depth) {
141 CHECK_GE(max_depth, 2);
142 if (!unwind_backtrace_signal_arch) {
143 SlowUnwindStack(pc, max_depth);
144 return;
147 void *map = acquire_my_map_info_list();
148 CHECK(map);
149 InternalScopedBuffer<backtrace_frame_t> frames(kStackTraceMax);
150 // siginfo argument appears to be unused.
151 sptr res = unwind_backtrace_signal_arch(/* siginfo */ 0, context, map,
152 frames.data(),
153 /* ignore_depth */ 0, max_depth);
154 release_my_map_info_list(map);
155 if (res < 0) return;
156 CHECK_LE((uptr)res, kStackTraceMax);
158 size = 0;
159 // +2 compensate for libcorkscrew unwinder returning addresses of call
160 // instructions instead of raw return addresses.
161 for (sptr i = 0; i < res; ++i)
162 trace_buffer[size++] = frames[i].absolute_pc + 2;
165 } // namespace __sanitizer
167 #endif // SANITIZER_FREEBSD || SANITIZER_LINUX || SANITIZER_NETBSD