xref: /freebsd/contrib/llvm-project/libunwind/src/Unwind-sjlj.c (revision 4d846d260e2b9a3d4d0a701462568268cbfe7a5b)
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 setjump-longjump based C++ exceptions
9 //
10 //===----------------------------------------------------------------------===//
11 
12 #include <unwind.h>
13 
14 #include <inttypes.h>
15 #include <stdint.h>
16 #include <stdbool.h>
17 #include <stdlib.h>
18 
19 #include "config.h"
20 
21 /// With SJLJ based exceptions, any function that has a catch clause or needs to
22 /// do any clean up when an exception propagates through it, needs to call
23 /// \c _Unwind_SjLj_Register at the start of the function and
24 /// \c _Unwind_SjLj_Unregister at the end.  The register function is called with
25 /// the address of a block of memory in the function's stack frame.  The runtime
26 /// keeps a linked list (stack) of these blocks - one per thread.  The calling
27 /// function also sets the personality and lsda fields of the block.
28 
29 #if defined(_LIBUNWIND_BUILD_SJLJ_APIS)
30 
31 struct _Unwind_FunctionContext {
32   // next function in stack of handlers
33   struct _Unwind_FunctionContext *prev;
34 
35 #if defined(__ve__)
36   // VE requires to store 64 bit pointers in the buffer for SjLj execption.
37   // We expand the size of values defined here.  This size must be matched
38   // to the size returned by TargetMachine::getSjLjDataSize().
39 
40   // set by calling function before registering to be the landing pad
41   uint64_t                        resumeLocation;
42 
43   // set by personality handler to be parameters passed to landing pad function
44   uint64_t                        resumeParameters[4];
45 #else
46   // set by calling function before registering to be the landing pad
47   uint32_t                        resumeLocation;
48 
49   // set by personality handler to be parameters passed to landing pad function
50   uint32_t                        resumeParameters[4];
51 #endif
52 
53   // set by calling function before registering
54   _Unwind_Personality_Fn personality;          // arm offset=24
55   uintptr_t                       lsda;        // arm offset=28
56 
57   // variable length array, contains registers to restore
58   // 0 = r7, 1 = pc, 2 = sp
59   void                           *jbuf[];
60 };
61 
62 #if defined(_LIBUNWIND_HAS_NO_THREADS)
63 # define _LIBUNWIND_THREAD_LOCAL
64 #else
65 # if __STDC_VERSION__ >= 201112L
66 #  define _LIBUNWIND_THREAD_LOCAL _Thread_local
67 # elif defined(_MSC_VER)
68 #  define _LIBUNWIND_THREAD_LOCAL __declspec(thread)
69 # elif defined(__GNUC__) || defined(__clang__)
70 #  define _LIBUNWIND_THREAD_LOCAL __thread
71 # else
72 #  error Unable to create thread local storage
73 # endif
74 #endif
75 
76 
77 #if !defined(FOR_DYLD)
78 
79 #if defined(__APPLE__)
80 #include <System/pthread_machdep.h>
81 #else
82 static _LIBUNWIND_THREAD_LOCAL struct _Unwind_FunctionContext *stack = NULL;
83 #endif
84 
85 static struct _Unwind_FunctionContext *__Unwind_SjLj_GetTopOfFunctionStack() {
86 #if defined(__APPLE__)
87   return _pthread_getspecific_direct(__PTK_LIBC_DYLD_Unwind_SjLj_Key);
88 #else
89   return stack;
90 #endif
91 }
92 
93 static void
94 __Unwind_SjLj_SetTopOfFunctionStack(struct _Unwind_FunctionContext *fc) {
95 #if defined(__APPLE__)
96   _pthread_setspecific_direct(__PTK_LIBC_DYLD_Unwind_SjLj_Key, fc);
97 #else
98   stack = fc;
99 #endif
100 }
101 
102 #endif
103 
104 
105 /// Called at start of each function that catches exceptions
106 _LIBUNWIND_EXPORT void
107 _Unwind_SjLj_Register(struct _Unwind_FunctionContext *fc) {
108   fc->prev = __Unwind_SjLj_GetTopOfFunctionStack();
109   __Unwind_SjLj_SetTopOfFunctionStack(fc);
110 }
111 
112 
113 /// Called at end of each function that catches exceptions
114 _LIBUNWIND_EXPORT void
115 _Unwind_SjLj_Unregister(struct _Unwind_FunctionContext *fc) {
116   __Unwind_SjLj_SetTopOfFunctionStack(fc->prev);
117 }
118 
119 
120 static _Unwind_Reason_Code
121 unwind_phase1(struct _Unwind_Exception *exception_object) {
122   _Unwind_FunctionContext_t c = __Unwind_SjLj_GetTopOfFunctionStack();
123   _LIBUNWIND_TRACE_UNWINDING("unwind_phase1: initial function-context=%p",
124                              (void *)c);
125 
126   // walk each frame looking for a place to stop
127   for (bool handlerNotFound = true; handlerNotFound; c = c->prev) {
128 
129     // check for no more frames
130     if (c == NULL) {
131       _LIBUNWIND_TRACE_UNWINDING("unwind_phase1(ex_ojb=%p): reached "
132                                  "bottom => _URC_END_OF_STACK",
133                                  (void *)exception_object);
134       return _URC_END_OF_STACK;
135     }
136 
137     _LIBUNWIND_TRACE_UNWINDING("unwind_phase1: function-context=%p", (void *)c);
138     // if there is a personality routine, ask it if it will want to stop at this
139     // frame
140     if (c->personality != NULL) {
141       _LIBUNWIND_TRACE_UNWINDING("unwind_phase1(ex_ojb=%p): calling "
142                                  "personality function %p",
143                                  (void *)exception_object,
144                                  (void *)c->personality);
145       _Unwind_Reason_Code personalityResult = (*c->personality)(
146           1, _UA_SEARCH_PHASE, exception_object->exception_class,
147           exception_object, (struct _Unwind_Context *)c);
148       switch (personalityResult) {
149       case _URC_HANDLER_FOUND:
150         // found a catch clause or locals that need destructing in this frame
151         // stop search and remember function context
152         handlerNotFound = false;
153         exception_object->private_2 = (uintptr_t) c;
154         _LIBUNWIND_TRACE_UNWINDING("unwind_phase1(ex_ojb=%p): "
155                                    "_URC_HANDLER_FOUND",
156                                    (void *)exception_object);
157         return _URC_NO_REASON;
158 
159       case _URC_CONTINUE_UNWIND:
160         _LIBUNWIND_TRACE_UNWINDING("unwind_phase1(ex_ojb=%p): "
161                                    "_URC_CONTINUE_UNWIND",
162                                    (void *)exception_object);
163         // continue unwinding
164         break;
165 
166       default:
167         // something went wrong
168         _LIBUNWIND_TRACE_UNWINDING(
169             "unwind_phase1(ex_ojb=%p): _URC_FATAL_PHASE1_ERROR",
170             (void *)exception_object);
171         return _URC_FATAL_PHASE1_ERROR;
172       }
173     }
174   }
175   return _URC_NO_REASON;
176 }
177 
178 
179 static _Unwind_Reason_Code
180 unwind_phase2(struct _Unwind_Exception *exception_object) {
181   _LIBUNWIND_TRACE_UNWINDING("unwind_phase2(ex_ojb=%p)",
182                              (void *)exception_object);
183 
184   // walk each frame until we reach where search phase said to stop
185   _Unwind_FunctionContext_t c = __Unwind_SjLj_GetTopOfFunctionStack();
186   while (true) {
187     _LIBUNWIND_TRACE_UNWINDING("unwind_phase2s(ex_ojb=%p): context=%p",
188                                (void *)exception_object, (void *)c);
189 
190     // check for no more frames
191     if (c == NULL) {
192       _LIBUNWIND_TRACE_UNWINDING(
193           "unwind_phase2(ex_ojb=%p): __unw_step() reached "
194           "bottom => _URC_END_OF_STACK",
195           (void *)exception_object);
196       return _URC_END_OF_STACK;
197     }
198 
199     // if there is a personality routine, tell it we are unwinding
200     if (c->personality != NULL) {
201       _Unwind_Action action = _UA_CLEANUP_PHASE;
202       if ((uintptr_t) c == exception_object->private_2)
203         action = (_Unwind_Action)(
204             _UA_CLEANUP_PHASE |
205             _UA_HANDLER_FRAME); // tell personality this was the frame it marked
206                                 // in phase 1
207       _Unwind_Reason_Code personalityResult =
208           (*c->personality)(1, action, exception_object->exception_class,
209                             exception_object, (struct _Unwind_Context *)c);
210       switch (personalityResult) {
211       case _URC_CONTINUE_UNWIND:
212         // continue unwinding
213         _LIBUNWIND_TRACE_UNWINDING(
214             "unwind_phase2(ex_ojb=%p): _URC_CONTINUE_UNWIND",
215             (void *)exception_object);
216         if ((uintptr_t) c == exception_object->private_2) {
217           // phase 1 said we would stop at this frame, but we did not...
218           _LIBUNWIND_ABORT("during phase1 personality function said it would "
219                            "stop here, but now if phase2 it did not stop here");
220         }
221         break;
222       case _URC_INSTALL_CONTEXT:
223         _LIBUNWIND_TRACE_UNWINDING("unwind_phase2(ex_ojb=%p): "
224                                    "_URC_INSTALL_CONTEXT, will resume at "
225                                    "landing pad %p",
226                                    (void *)exception_object, c->jbuf[1]);
227         // personality routine says to transfer control to landing pad
228         // we may get control back if landing pad calls _Unwind_Resume()
229         __Unwind_SjLj_SetTopOfFunctionStack(c);
230         __builtin_longjmp(c->jbuf, 1);
231         // __unw_resume() only returns if there was an error
232         return _URC_FATAL_PHASE2_ERROR;
233       default:
234         // something went wrong
235         _LIBUNWIND_DEBUG_LOG("personality function returned unknown result %d",
236                       personalityResult);
237         return _URC_FATAL_PHASE2_ERROR;
238       }
239     }
240     c = c->prev;
241   }
242 
243   // clean up phase did not resume at the frame that the search phase said it
244   // would
245   return _URC_FATAL_PHASE2_ERROR;
246 }
247 
248 
249 static _Unwind_Reason_Code
250 unwind_phase2_forced(struct _Unwind_Exception *exception_object,
251                      _Unwind_Stop_Fn stop, void *stop_parameter) {
252   // walk each frame until we reach where search phase said to stop
253   _Unwind_FunctionContext_t c = __Unwind_SjLj_GetTopOfFunctionStack();
254   while (true) {
255 
256     // get next frame (skip over first which is _Unwind_RaiseException)
257     if (c == NULL) {
258       _LIBUNWIND_TRACE_UNWINDING(
259           "unwind_phase2(ex_ojb=%p): __unw_step() reached "
260           "bottom => _URC_END_OF_STACK",
261           (void *)exception_object);
262       return _URC_END_OF_STACK;
263     }
264 
265     // call stop function at each frame
266     _Unwind_Action action =
267         (_Unwind_Action)(_UA_FORCE_UNWIND | _UA_CLEANUP_PHASE);
268     _Unwind_Reason_Code stopResult =
269         (*stop)(1, action, exception_object->exception_class, exception_object,
270                 (struct _Unwind_Context *)c, stop_parameter);
271     _LIBUNWIND_TRACE_UNWINDING("unwind_phase2_forced(ex_ojb=%p): "
272                                "stop function returned %d",
273                                (void *)exception_object, stopResult);
274     if (stopResult != _URC_NO_REASON) {
275       _LIBUNWIND_TRACE_UNWINDING("unwind_phase2_forced(ex_ojb=%p): "
276                                  "stopped by stop function",
277                                  (void *)exception_object);
278       return _URC_FATAL_PHASE2_ERROR;
279     }
280 
281     // if there is a personality routine, tell it we are unwinding
282     if (c->personality != NULL) {
283       _Unwind_Personality_Fn p = (_Unwind_Personality_Fn)c->personality;
284       _LIBUNWIND_TRACE_UNWINDING("unwind_phase2_forced(ex_ojb=%p): "
285                                  "calling personality function %p",
286                                  (void *)exception_object, (void *)p);
287       _Unwind_Reason_Code personalityResult =
288           (*p)(1, action, exception_object->exception_class, exception_object,
289                (struct _Unwind_Context *)c);
290       switch (personalityResult) {
291       case _URC_CONTINUE_UNWIND:
292         _LIBUNWIND_TRACE_UNWINDING("unwind_phase2_forced(ex_ojb=%p):  "
293                                    "personality returned _URC_CONTINUE_UNWIND",
294                                    (void *)exception_object);
295         // destructors called, continue unwinding
296         break;
297       case _URC_INSTALL_CONTEXT:
298         _LIBUNWIND_TRACE_UNWINDING("unwind_phase2_forced(ex_ojb=%p): "
299                                    "personality returned _URC_INSTALL_CONTEXT",
300                                    (void *)exception_object);
301         // we may get control back if landing pad calls _Unwind_Resume()
302         __Unwind_SjLj_SetTopOfFunctionStack(c);
303         __builtin_longjmp(c->jbuf, 1);
304         break;
305       default:
306         // something went wrong
307         _LIBUNWIND_TRACE_UNWINDING("unwind_phase2_forced(ex_ojb=%p): "
308                                    "personality returned %d, "
309                                    "_URC_FATAL_PHASE2_ERROR",
310                                    (void *)exception_object, personalityResult);
311         return _URC_FATAL_PHASE2_ERROR;
312       }
313     }
314     c = c->prev;
315   }
316 
317   // call stop function one last time and tell it we've reached the end of the
318   // stack
319   _LIBUNWIND_TRACE_UNWINDING("unwind_phase2_forced(ex_ojb=%p): calling stop "
320                              "function with _UA_END_OF_STACK",
321                              (void *)exception_object);
322   _Unwind_Action lastAction =
323       (_Unwind_Action)(_UA_FORCE_UNWIND | _UA_CLEANUP_PHASE | _UA_END_OF_STACK);
324   (*stop)(1, lastAction, exception_object->exception_class, exception_object,
325           (struct _Unwind_Context *)c, stop_parameter);
326 
327   // clean up phase did not resume at the frame that the search phase said it
328   // would
329   return _URC_FATAL_PHASE2_ERROR;
330 }
331 
332 
333 /// Called by __cxa_throw.  Only returns if there is a fatal error
334 _LIBUNWIND_EXPORT _Unwind_Reason_Code
335 _Unwind_SjLj_RaiseException(struct _Unwind_Exception *exception_object) {
336   _LIBUNWIND_TRACE_API("_Unwind_SjLj_RaiseException(ex_obj=%p)",
337                        (void *)exception_object);
338 
339   // mark that this is a non-forced unwind, so _Unwind_Resume() can do the right
340   // thing
341   exception_object->private_1 = 0;
342   exception_object->private_2 = 0;
343 
344   // phase 1: the search phase
345   _Unwind_Reason_Code phase1 = unwind_phase1(exception_object);
346   if (phase1 != _URC_NO_REASON)
347     return phase1;
348 
349   // phase 2: the clean up phase
350   return unwind_phase2(exception_object);
351 }
352 
353 
354 
355 /// When _Unwind_RaiseException() is in phase2, it hands control
356 /// to the personality function at each frame.  The personality
357 /// may force a jump to a landing pad in that function, the landing
358 /// pad code may then call _Unwind_Resume() to continue with the
359 /// unwinding.  Note: the call to _Unwind_Resume() is from compiler
360 /// geneated user code.  All other _Unwind_* routines are called
361 /// by the C++ runtime __cxa_* routines.
362 ///
363 /// Re-throwing an exception is implemented by having the code call
364 /// __cxa_rethrow() which in turn calls _Unwind_Resume_or_Rethrow()
365 _LIBUNWIND_EXPORT void
366 _Unwind_SjLj_Resume(struct _Unwind_Exception *exception_object) {
367   _LIBUNWIND_TRACE_API("_Unwind_SjLj_Resume(ex_obj=%p)",
368                        (void *)exception_object);
369 
370   if (exception_object->private_1 != 0)
371     unwind_phase2_forced(exception_object,
372                          (_Unwind_Stop_Fn) exception_object->private_1,
373                          (void *)exception_object->private_2);
374   else
375     unwind_phase2(exception_object);
376 
377   // clients assume _Unwind_Resume() does not return, so all we can do is abort.
378   _LIBUNWIND_ABORT("_Unwind_SjLj_Resume() can't return");
379 }
380 
381 
382 ///  Called by __cxa_rethrow().
383 _LIBUNWIND_EXPORT _Unwind_Reason_Code
384 _Unwind_SjLj_Resume_or_Rethrow(struct _Unwind_Exception *exception_object) {
385   _LIBUNWIND_TRACE_API("__Unwind_SjLj_Resume_or_Rethrow(ex_obj=%p), "
386                        "private_1=%" PRIuPTR,
387                        (void *)exception_object, exception_object->private_1);
388   // If this is non-forced and a stopping place was found, then this is a
389   // re-throw.
390   // Call _Unwind_RaiseException() as if this was a new exception.
391   if (exception_object->private_1 == 0) {
392     return _Unwind_SjLj_RaiseException(exception_object);
393     // should return if there is no catch clause, so that __cxa_rethrow can call
394     // std::terminate()
395   }
396 
397   // Call through to _Unwind_Resume() which distiguishes between forced and
398   // regular exceptions.
399   _Unwind_SjLj_Resume(exception_object);
400   _LIBUNWIND_ABORT("__Unwind_SjLj_Resume_or_Rethrow() called "
401                     "_Unwind_SjLj_Resume() which unexpectedly returned");
402 }
403 
404 
405 /// Called by personality handler during phase 2 to get LSDA for current frame.
406 _LIBUNWIND_EXPORT uintptr_t
407 _Unwind_GetLanguageSpecificData(struct _Unwind_Context *context) {
408   _Unwind_FunctionContext_t ufc = (_Unwind_FunctionContext_t) context;
409   _LIBUNWIND_TRACE_API("_Unwind_GetLanguageSpecificData(context=%p) "
410                        "=> 0x%" PRIuPTR,
411                        (void *)context, ufc->lsda);
412   return ufc->lsda;
413 }
414 
415 
416 /// Called by personality handler during phase 2 to get register values.
417 _LIBUNWIND_EXPORT uintptr_t _Unwind_GetGR(struct _Unwind_Context *context,
418                                           int index) {
419   _LIBUNWIND_TRACE_API("_Unwind_GetGR(context=%p, reg=%d)", (void *)context,
420                        index);
421   _Unwind_FunctionContext_t ufc = (_Unwind_FunctionContext_t) context;
422   return ufc->resumeParameters[index];
423 }
424 
425 
426 /// Called by personality handler during phase 2 to alter register values.
427 _LIBUNWIND_EXPORT void _Unwind_SetGR(struct _Unwind_Context *context, int index,
428                                      uintptr_t new_value) {
429   _LIBUNWIND_TRACE_API("_Unwind_SetGR(context=%p, reg=%d, value=0x%" PRIuPTR
430                        ")",
431                        (void *)context, index, new_value);
432   _Unwind_FunctionContext_t ufc = (_Unwind_FunctionContext_t) context;
433   ufc->resumeParameters[index] = new_value;
434 }
435 
436 
437 /// Called by personality handler during phase 2 to get instruction pointer.
438 _LIBUNWIND_EXPORT uintptr_t _Unwind_GetIP(struct _Unwind_Context *context) {
439   _Unwind_FunctionContext_t ufc = (_Unwind_FunctionContext_t) context;
440   _LIBUNWIND_TRACE_API("_Unwind_GetIP(context=%p) => 0x%" PRIu32,
441                        (void *)context, ufc->resumeLocation + 1);
442   return ufc->resumeLocation + 1;
443 }
444 
445 
446 /// Called by personality handler during phase 2 to get instruction pointer.
447 /// ipBefore is a boolean that says if IP is already adjusted to be the call
448 /// site address.  Normally IP is the return address.
449 _LIBUNWIND_EXPORT uintptr_t _Unwind_GetIPInfo(struct _Unwind_Context *context,
450                                               int *ipBefore) {
451   _Unwind_FunctionContext_t ufc = (_Unwind_FunctionContext_t) context;
452   *ipBefore = 0;
453   _LIBUNWIND_TRACE_API("_Unwind_GetIPInfo(context=%p, %p) => 0x%" PRIu32,
454                        (void *)context, (void *)ipBefore,
455                        ufc->resumeLocation + 1);
456   return ufc->resumeLocation + 1;
457 }
458 
459 
460 /// Called by personality handler during phase 2 to alter instruction pointer.
461 _LIBUNWIND_EXPORT void _Unwind_SetIP(struct _Unwind_Context *context,
462                                      uintptr_t new_value) {
463   _LIBUNWIND_TRACE_API("_Unwind_SetIP(context=%p, value=0x%" PRIuPTR ")",
464                        (void *)context, new_value);
465   _Unwind_FunctionContext_t ufc = (_Unwind_FunctionContext_t) context;
466   ufc->resumeLocation = new_value - 1;
467 }
468 
469 
470 /// Called by personality handler during phase 2 to find the start of the
471 /// function.
472 _LIBUNWIND_EXPORT uintptr_t
473 _Unwind_GetRegionStart(struct _Unwind_Context *context) {
474   // Not supported or needed for sjlj based unwinding
475   (void)context;
476   _LIBUNWIND_TRACE_API("_Unwind_GetRegionStart(context=%p)", (void *)context);
477   return 0;
478 }
479 
480 
481 /// Called by personality handler during phase 2 if a foreign exception
482 /// is caught.
483 _LIBUNWIND_EXPORT void
484 _Unwind_DeleteException(struct _Unwind_Exception *exception_object) {
485   _LIBUNWIND_TRACE_API("_Unwind_DeleteException(ex_obj=%p)",
486                        (void *)exception_object);
487   if (exception_object->exception_cleanup != NULL)
488     (*exception_object->exception_cleanup)(_URC_FOREIGN_EXCEPTION_CAUGHT,
489                                            exception_object);
490 }
491 
492 
493 
494 /// Called by personality handler during phase 2 to get base address for data
495 /// relative encodings.
496 _LIBUNWIND_EXPORT uintptr_t
497 _Unwind_GetDataRelBase(struct _Unwind_Context *context) {
498   // Not supported or needed for sjlj based unwinding
499   (void)context;
500   _LIBUNWIND_TRACE_API("_Unwind_GetDataRelBase(context=%p)", (void *)context);
501   _LIBUNWIND_ABORT("_Unwind_GetDataRelBase() not implemented");
502 }
503 
504 
505 /// Called by personality handler during phase 2 to get base address for text
506 /// relative encodings.
507 _LIBUNWIND_EXPORT uintptr_t
508 _Unwind_GetTextRelBase(struct _Unwind_Context *context) {
509   // Not supported or needed for sjlj based unwinding
510   (void)context;
511   _LIBUNWIND_TRACE_API("_Unwind_GetTextRelBase(context=%p)", (void *)context);
512   _LIBUNWIND_ABORT("_Unwind_GetTextRelBase() not implemented");
513 }
514 
515 
516 /// Called by personality handler to get "Call Frame Area" for current frame.
517 _LIBUNWIND_EXPORT uintptr_t _Unwind_GetCFA(struct _Unwind_Context *context) {
518   _LIBUNWIND_TRACE_API("_Unwind_GetCFA(context=%p)", (void *)context);
519   if (context != NULL) {
520     _Unwind_FunctionContext_t ufc = (_Unwind_FunctionContext_t) context;
521     // Setjmp/longjmp based exceptions don't have a true CFA.
522     // Instead, the SP in the jmpbuf is the closest approximation.
523     return (uintptr_t) ufc->jbuf[2];
524   }
525   return 0;
526 }
527 
528 #endif // defined(_LIBUNWIND_BUILD_SJLJ_APIS)
529