1 //===----------------------------------------------------------------------===//
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 //  Implements gcc extensions to the C++ ABI Exception Handling Level 1.
9 //
10 //===----------------------------------------------------------------------===//
11 
12 #include <inttypes.h>
13 #include <stdbool.h>
14 #include <stdint.h>
15 #include <stdio.h>
16 #include <stdlib.h>
17 #include <string.h>
18 
19 #include "config.h"
20 #include "libunwind_ext.h"
21 #include "libunwind.h"
22 #include "Unwind-EHABI.h"
23 #include "unwind.h"
24 
25 #if defined(_LIBUNWIND_BUILD_ZERO_COST_APIS)
26 
27 #if defined(_LIBUNWIND_SUPPORT_SEH_UNWIND)
28 #define PRIVATE_1 private_[0]
29 #elif defined(_LIBUNWIND_ARM_EHABI)
30 #define PRIVATE_1 unwinder_cache.reserved1
31 #else
32 #define PRIVATE_1 private_1
33 #endif
34 
35 ///  Called by __cxa_rethrow().
36 _LIBUNWIND_EXPORT _Unwind_Reason_Code
37 _Unwind_Resume_or_Rethrow(_Unwind_Exception *exception_object) {
38   _LIBUNWIND_TRACE_API(
39       "_Unwind_Resume_or_Rethrow(ex_obj=%p), private_1=%" PRIdPTR,
40       (void *)exception_object, (intptr_t)exception_object->PRIVATE_1);
41 
42   // If this is non-forced and a stopping place was found, then this is a
43   // re-throw.
44   // Call _Unwind_RaiseException() as if this was a new exception
45   if (exception_object->PRIVATE_1 == 0) {
46     return _Unwind_RaiseException(exception_object);
47     // Will return if there is no catch clause, so that __cxa_rethrow can call
48     // std::terminate().
49   }
50 
51   // Call through to _Unwind_Resume() which distiguishes between forced and
52   // regular exceptions.
53   _Unwind_Resume(exception_object);
54   _LIBUNWIND_ABORT("_Unwind_Resume_or_Rethrow() called _Unwind_RaiseException()"
55                    " which unexpectedly returned");
56 }
57 
58 /// Called by personality handler during phase 2 to get base address for data
59 /// relative encodings.
60 _LIBUNWIND_EXPORT uintptr_t
61 _Unwind_GetDataRelBase(struct _Unwind_Context *context) {
62   (void)context;
63   _LIBUNWIND_TRACE_API("_Unwind_GetDataRelBase(context=%p)", (void *)context);
64   _LIBUNWIND_ABORT("_Unwind_GetDataRelBase() not implemented");
65 }
66 
67 
68 /// Called by personality handler during phase 2 to get base address for text
69 /// relative encodings.
70 _LIBUNWIND_EXPORT uintptr_t
71 _Unwind_GetTextRelBase(struct _Unwind_Context *context) {
72   (void)context;
73   _LIBUNWIND_TRACE_API("_Unwind_GetTextRelBase(context=%p)", (void *)context);
74   _LIBUNWIND_ABORT("_Unwind_GetTextRelBase() not implemented");
75 }
76 
77 
78 /// Scans unwind information to find the function that contains the
79 /// specified code address "pc".
80 _LIBUNWIND_EXPORT void *_Unwind_FindEnclosingFunction(void *pc) {
81   _LIBUNWIND_TRACE_API("_Unwind_FindEnclosingFunction(pc=%p)", pc);
82   // This is slow, but works.
83   // We create an unwind cursor then alter the IP to be pc
84   unw_cursor_t cursor;
85   unw_context_t uc;
86   unw_proc_info_t info;
87   __unw_getcontext(&uc);
88   __unw_init_local(&cursor, &uc);
89   __unw_set_reg(&cursor, UNW_REG_IP, (unw_word_t)(intptr_t)pc);
90   if (__unw_get_proc_info(&cursor, &info) == UNW_ESUCCESS)
91     return (void *)(intptr_t) info.start_ip;
92   else
93     return NULL;
94 }
95 
96 /// Walk every frame and call trace function at each one.  If trace function
97 /// returns anything other than _URC_NO_REASON, then walk is terminated.
98 _LIBUNWIND_EXPORT _Unwind_Reason_Code
99 _Unwind_Backtrace(_Unwind_Trace_Fn callback, void *ref) {
100   unw_cursor_t cursor;
101   unw_context_t uc;
102   __unw_getcontext(&uc);
103   __unw_init_local(&cursor, &uc);
104 
105   _LIBUNWIND_TRACE_API("_Unwind_Backtrace(callback=%p)",
106                        (void *)(uintptr_t)callback);
107 
108 #if defined(_LIBUNWIND_ARM_EHABI)
109   // Create a mock exception object for force unwinding.
110   _Unwind_Exception ex;
111   memset(&ex, '\0', sizeof(ex));
112   strcpy((char *)&ex.exception_class, "CLNGUNW");
113 #endif
114 
115   // walk each frame
116   while (true) {
117     _Unwind_Reason_Code result;
118 
119 #if !defined(_LIBUNWIND_ARM_EHABI)
120     // ask libunwind to get next frame (skip over first frame which is
121     // _Unwind_Backtrace())
122     if (__unw_step(&cursor) <= 0) {
123       _LIBUNWIND_TRACE_UNWINDING(" _backtrace: ended because cursor reached "
124                                  "bottom of stack, returning %d",
125                                  _URC_END_OF_STACK);
126       return _URC_END_OF_STACK;
127     }
128 #else
129     // Get the information for this frame.
130     unw_proc_info_t frameInfo;
131     if (__unw_get_proc_info(&cursor, &frameInfo) != UNW_ESUCCESS) {
132       return _URC_END_OF_STACK;
133     }
134 
135     // Update the pr_cache in the mock exception object.
136     const uint32_t* unwindInfo = (uint32_t *) frameInfo.unwind_info;
137     ex.pr_cache.fnstart = frameInfo.start_ip;
138     ex.pr_cache.ehtp = (_Unwind_EHT_Header *) unwindInfo;
139     ex.pr_cache.additional= frameInfo.flags;
140 
141     struct _Unwind_Context *context = (struct _Unwind_Context *)&cursor;
142     // Get and call the personality function to unwind the frame.
143     _Unwind_Personality_Fn handler = (_Unwind_Personality_Fn)frameInfo.handler;
144     if (handler == NULL) {
145       return _URC_END_OF_STACK;
146     }
147     if (handler(_US_VIRTUAL_UNWIND_FRAME | _US_FORCE_UNWIND, &ex, context) !=
148             _URC_CONTINUE_UNWIND) {
149       return _URC_END_OF_STACK;
150     }
151 #endif // defined(_LIBUNWIND_ARM_EHABI)
152 
153     // debugging
154     if (_LIBUNWIND_TRACING_UNWINDING) {
155       char functionName[512];
156       unw_proc_info_t frame;
157       unw_word_t offset;
158       __unw_get_proc_name(&cursor, functionName, 512, &offset);
159       __unw_get_proc_info(&cursor, &frame);
160       _LIBUNWIND_TRACE_UNWINDING(
161           " _backtrace: start_ip=0x%" PRIxPTR ", func=%s, lsda=0x%" PRIxPTR ", context=%p",
162           frame.start_ip, functionName, frame.lsda,
163           (void *)&cursor);
164     }
165 
166     // call trace function with this frame
167     result = (*callback)((struct _Unwind_Context *)(&cursor), ref);
168     if (result != _URC_NO_REASON) {
169       _LIBUNWIND_TRACE_UNWINDING(
170           " _backtrace: ended because callback returned %d", result);
171       return result;
172     }
173   }
174 }
175 #ifdef __arm__
176 /* Preserve legacy libgcc (pre r318024) ARM ABI mistake */
177 __sym_compat(_Unwind_Backtrace, _Unwind_Backtrace, GCC_3.3);
178 #endif
179 
180 
181 /// Find DWARF unwind info for an address 'pc' in some function.
182 _LIBUNWIND_EXPORT const void *_Unwind_Find_FDE(const void *pc,
183                                                struct dwarf_eh_bases *bases) {
184   // This is slow, but works.
185   // We create an unwind cursor then alter the IP to be pc
186   unw_cursor_t cursor;
187   unw_context_t uc;
188   unw_proc_info_t info;
189   __unw_getcontext(&uc);
190   __unw_init_local(&cursor, &uc);
191   __unw_set_reg(&cursor, UNW_REG_IP, (unw_word_t)(intptr_t)pc);
192   __unw_get_proc_info(&cursor, &info);
193   bases->tbase = (uintptr_t)info.extra;
194   bases->dbase = 0; // dbase not used on Mac OS X
195   bases->func = (uintptr_t)info.start_ip;
196   _LIBUNWIND_TRACE_API("_Unwind_Find_FDE(pc=%p) => %p", pc,
197                   (void *)(intptr_t) info.unwind_info);
198   return (void *)(intptr_t) info.unwind_info;
199 }
200 
201 /// Returns the CFA (call frame area, or stack pointer at start of function)
202 /// for the current context.
203 _LIBUNWIND_EXPORT uintptr_t _Unwind_GetCFA(struct _Unwind_Context *context) {
204   unw_cursor_t *cursor = (unw_cursor_t *)context;
205   unw_word_t result;
206   __unw_get_reg(cursor, UNW_REG_SP, &result);
207   _LIBUNWIND_TRACE_API("_Unwind_GetCFA(context=%p) => 0x%" PRIxPTR,
208                        (void *)context, result);
209   return (uintptr_t)result;
210 }
211 
212 
213 /// Called by personality handler during phase 2 to get instruction pointer.
214 /// ipBefore is a boolean that says if IP is already adjusted to be the call
215 /// site address.  Normally IP is the return address.
216 _LIBUNWIND_EXPORT uintptr_t _Unwind_GetIPInfo(struct _Unwind_Context *context,
217                                               int *ipBefore) {
218   _LIBUNWIND_TRACE_API("_Unwind_GetIPInfo(context=%p)", (void *)context);
219   int isSignalFrame = __unw_is_signal_frame((unw_cursor_t *)context);
220   // Negative means some kind of error (probably UNW_ENOINFO), but we have no
221   // good way to report that, and this maintains backward compatibility with the
222   // implementation that hard-coded zero in every case, even signal frames.
223   if (isSignalFrame <= 0)
224     *ipBefore = 0;
225   else
226     *ipBefore = 1;
227   return _Unwind_GetIP(context);
228 }
229 
230 #if defined(_LIBUNWIND_SUPPORT_DWARF_UNWIND)
231 
232 #if defined(__FreeBSD__)
233 
234 // Based on LLVM's lib/ExecutionEngine/RuntimeDyld/RTDyldMemoryManager.cpp
235 // and XXX should be fixed to be alignment-safe.
236 static void processFDE(const char *addr, bool isDeregister) {
237   uint64_t length;
238   while ((length = *((const uint32_t *)addr)) != 0) {
239     const char *p = addr + 4;
240     if (length == 0xffffffff) {
241       length = *((const uint64_t *)p);
242       p += 8;
243     }
244     uint32_t offset = *((const uint32_t *)p);
245     if (offset != 0) {
246       if (isDeregister)
247         __unw_remove_dynamic_fde((unw_word_t)(uintptr_t)addr);
248       else
249         __unw_add_dynamic_fde((unw_word_t)(uintptr_t)addr);
250     }
251     addr = p + length;
252   }
253 }
254 
255 /// Called by programs with dynamic code generators that want to register
256 /// dynamically generated FDEs, with a libgcc-compatible API.
257 
258 _LIBUNWIND_EXPORT void __register_frame(const void *addr) {
259   _LIBUNWIND_TRACE_API("__register_frame(%p)", addr);
260   processFDE(addr, false);
261 }
262 
263 /// Called by programs with dynamic code generators that want to unregister
264 /// dynamically generated FDEs, with a libgcc-compatible API.
265 _LIBUNWIND_EXPORT void __deregister_frame(const void *addr) {
266   _LIBUNWIND_TRACE_API("__deregister_frame(%p)", addr);
267   processFDE(addr, true);
268 }
269 
270 #else // defined(__FreeBSD__)
271 
272 /// Called by programs with dynamic code generators that want
273 /// to register a dynamically generated FDE.
274 /// This function has existed on Mac OS X since 10.4, but
275 /// was broken until 10.6.
276 _LIBUNWIND_EXPORT void __register_frame(const void *fde) {
277   _LIBUNWIND_TRACE_API("__register_frame(%p)", fde);
278   __unw_add_dynamic_fde((unw_word_t)(uintptr_t)fde);
279 }
280 
281 /// Called by programs with dynamic code generators that want
282 /// to unregister a dynamically generated FDE.
283 /// This function has existed on Mac OS X since 10.4, but
284 /// was broken until 10.6.
285 _LIBUNWIND_EXPORT void __deregister_frame(const void *fde) {
286   _LIBUNWIND_TRACE_API("__deregister_frame(%p)", fde);
287   __unw_remove_dynamic_fde((unw_word_t)(uintptr_t)fde);
288 }
289 
290 #endif // defined(__FreeBSD__)
291 
292 // The following register/deregister functions are gcc extensions.
293 // They have existed on Mac OS X, but have never worked because Mac OS X
294 // before 10.6 used keymgr to track known FDEs, but these functions
295 // never got updated to use keymgr.
296 // For now, we implement these as do-nothing functions to keep any existing
297 // applications working.  We also add the not in 10.6 symbol so that nwe
298 // application won't be able to use them.
299 
300 #if defined(_LIBUNWIND_SUPPORT_FRAME_APIS)
301 _LIBUNWIND_EXPORT void __register_frame_info_bases(const void *fde, void *ob,
302                                                    void *tb, void *db) {
303   (void)fde;
304   (void)ob;
305   (void)tb;
306   (void)db;
307  _LIBUNWIND_TRACE_API("__register_frame_info_bases(%p,%p, %p, %p)",
308                             fde, ob, tb, db);
309   // do nothing, this function never worked in Mac OS X
310 }
311 
312 _LIBUNWIND_EXPORT void __register_frame_info(const void *fde, void *ob) {
313   (void)fde;
314   (void)ob;
315   _LIBUNWIND_TRACE_API("__register_frame_info(%p, %p)", fde, ob);
316   // do nothing, this function never worked in Mac OS X
317 }
318 
319 _LIBUNWIND_EXPORT void __register_frame_info_table_bases(const void *fde,
320                                                          void *ob, void *tb,
321                                                          void *db) {
322   (void)fde;
323   (void)ob;
324   (void)tb;
325   (void)db;
326   _LIBUNWIND_TRACE_API("__register_frame_info_table_bases"
327                              "(%p,%p, %p, %p)", fde, ob, tb, db);
328   // do nothing, this function never worked in Mac OS X
329 }
330 
331 _LIBUNWIND_EXPORT void __register_frame_info_table(const void *fde, void *ob) {
332   (void)fde;
333   (void)ob;
334   _LIBUNWIND_TRACE_API("__register_frame_info_table(%p, %p)", fde, ob);
335   // do nothing, this function never worked in Mac OS X
336 }
337 
338 _LIBUNWIND_EXPORT void __register_frame_table(const void *fde) {
339   (void)fde;
340   _LIBUNWIND_TRACE_API("__register_frame_table(%p)", fde);
341   // do nothing, this function never worked in Mac OS X
342 }
343 
344 _LIBUNWIND_EXPORT void *__deregister_frame_info(const void *fde) {
345   (void)fde;
346   _LIBUNWIND_TRACE_API("__deregister_frame_info(%p)", fde);
347   // do nothing, this function never worked in Mac OS X
348   return NULL;
349 }
350 
351 _LIBUNWIND_EXPORT void *__deregister_frame_info_bases(const void *fde) {
352   (void)fde;
353   _LIBUNWIND_TRACE_API("__deregister_frame_info_bases(%p)", fde);
354   // do nothing, this function never worked in Mac OS X
355   return NULL;
356 }
357 #endif // defined(_LIBUNWIND_SUPPORT_FRAME_APIS)
358 
359 #endif // defined(_LIBUNWIND_SUPPORT_DWARF_UNWIND)
360 
361 #endif // defined(_LIBUNWIND_BUILD_ZERO_COST_APIS)
362