1 //===----------------------------------------------------------------------===//
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
8 // Implements setjump-longjump based C++ exceptions
10 //===----------------------------------------------------------------------===//
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.
29 #if defined(_LIBUNWIND_BUILD_SJLJ_APIS)
31 struct _Unwind_FunctionContext
{
32 // next function in stack of handlers
33 struct _Unwind_FunctionContext
*prev
;
36 // VE requires to store 64 bit pointers in the buffer for SjLj exception.
37 // We expand the size of values defined here. This size must be matched
38 // to the size returned by TargetMachine::getSjLjDataSize().
40 // set by calling function before registering to be the landing pad
41 uint64_t resumeLocation
;
43 // set by personality handler to be parameters passed to landing pad function
44 uint64_t resumeParameters
[4];
46 // set by calling function before registering to be the landing pad
47 uint32_t resumeLocation
;
49 // set by personality handler to be parameters passed to landing pad function
50 uint32_t resumeParameters
[4];
53 // set by calling function before registering
54 _Unwind_Personality_Fn personality
; // arm offset=24
55 uintptr_t lsda
; // arm offset=28
57 // variable length array, contains registers to restore
58 // 0 = r7, 1 = pc, 2 = sp
62 #if defined(_LIBUNWIND_HAS_NO_THREADS)
63 # define _LIBUNWIND_THREAD_LOCAL
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
72 # error Unable to create thread local storage
77 #if !defined(FOR_DYLD)
79 #if defined(__APPLE__)
80 #include <System/pthread_machdep.h>
82 static _LIBUNWIND_THREAD_LOCAL
struct _Unwind_FunctionContext
*stack
= NULL
;
85 static struct _Unwind_FunctionContext
*__Unwind_SjLj_GetTopOfFunctionStack() {
86 #if defined(__APPLE__)
87 return _pthread_getspecific_direct(__PTK_LIBC_DYLD_Unwind_SjLj_Key
);
94 __Unwind_SjLj_SetTopOfFunctionStack(struct _Unwind_FunctionContext
*fc
) {
95 #if defined(__APPLE__)
96 _pthread_setspecific_direct(__PTK_LIBC_DYLD_Unwind_SjLj_Key
, fc
);
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
);
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
);
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",
126 // walk each frame looking for a place to stop
127 for (bool handlerNotFound
= true; handlerNotFound
; c
= c
->prev
) {
129 // check for no more frames
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
;
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
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
;
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
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
;
175 return _URC_NO_REASON
;
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
);
184 // walk each frame until we reach where search phase said to stop
185 _Unwind_FunctionContext_t c
= __Unwind_SjLj_GetTopOfFunctionStack();
187 _LIBUNWIND_TRACE_UNWINDING("unwind_phase2s(ex_ojb=%p): context=%p",
188 (void *)exception_object
, (void *)c
);
190 // check for no more frames
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
;
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
)(
205 _UA_HANDLER_FRAME
); // tell personality this was the frame it marked
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");
222 case _URC_INSTALL_CONTEXT
:
223 _LIBUNWIND_TRACE_UNWINDING("unwind_phase2(ex_ojb=%p): "
224 "_URC_INSTALL_CONTEXT, will resume at "
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
;
234 // something went wrong
235 _LIBUNWIND_DEBUG_LOG("personality function returned unknown result %d",
237 return _URC_FATAL_PHASE2_ERROR
;
243 // clean up phase did not resume at the frame that the search phase said it
245 return _URC_FATAL_PHASE2_ERROR
;
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();
256 // get next frame (skip over first which is _Unwind_RaiseException)
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
;
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
;
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
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);
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
;
317 // call stop function one last time and tell it we've reached the end of the
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
);
327 // clean up phase did not resume at the frame that the search phase said it
329 return _URC_FATAL_PHASE2_ERROR
;
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
);
339 // mark that this is a non-forced unwind, so _Unwind_Resume() can do the right
341 exception_object
->private_1
= 0;
342 exception_object
->private_2
= 0;
344 // phase 1: the search phase
345 _Unwind_Reason_Code phase1
= unwind_phase1(exception_object
);
346 if (phase1
!= _URC_NO_REASON
)
349 // phase 2: the clean up phase
350 return unwind_phase2(exception_object
);
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 /// generated user code. All other _Unwind_* routines are called
361 /// by the C++ runtime __cxa_* routines.
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
);
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
);
375 unwind_phase2(exception_object
);
377 // clients assume _Unwind_Resume() does not return, so all we can do is abort.
378 _LIBUNWIND_ABORT("_Unwind_SjLj_Resume() can't return");
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
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
397 // Call through to _Unwind_Resume() which distinguishes 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");
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) "
411 (void *)context
, ufc
->lsda
);
416 /// Called by personality handler during phase 2 to get register values.
417 _LIBUNWIND_EXPORT
uintptr_t _Unwind_GetGR(struct _Unwind_Context
*context
,
419 _LIBUNWIND_TRACE_API("_Unwind_GetGR(context=%p, reg=%d)", (void *)context
,
421 _Unwind_FunctionContext_t ufc
= (_Unwind_FunctionContext_t
) context
;
422 return ufc
->resumeParameters
[index
];
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
431 (void *)context
, index
, new_value
);
432 _Unwind_FunctionContext_t ufc
= (_Unwind_FunctionContext_t
) context
;
433 ufc
->resumeParameters
[index
] = new_value
;
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;
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
,
451 _Unwind_FunctionContext_t ufc
= (_Unwind_FunctionContext_t
) context
;
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;
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;
470 /// Called by personality handler during phase 2 to find the start of the
472 _LIBUNWIND_EXPORT
uintptr_t
473 _Unwind_GetRegionStart(struct _Unwind_Context
*context
) {
474 // Not supported or needed for sjlj based unwinding
476 _LIBUNWIND_TRACE_API("_Unwind_GetRegionStart(context=%p)", (void *)context
);
481 /// Called by personality handler during phase 2 if a foreign exception
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
,
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
500 _LIBUNWIND_TRACE_API("_Unwind_GetDataRelBase(context=%p)", (void *)context
);
501 _LIBUNWIND_ABORT("_Unwind_GetDataRelBase() not implemented");
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
511 _LIBUNWIND_TRACE_API("_Unwind_GetTextRelBase(context=%p)", (void *)context
);
512 _LIBUNWIND_ABORT("_Unwind_GetTextRelBase() not implemented");
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];
528 #endif // defined(_LIBUNWIND_BUILD_SJLJ_APIS)