1 //===--------------------------- Unwind-sjlj.c ----------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is dual licensed under the MIT and the University of Illinois Open
6 // Source Licenses. See LICENSE.TXT for details.
9 // Implements setjump-longjump based C++ exceptions
11 //===----------------------------------------------------------------------===//
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;
35 // set by calling function before registering to be the landing pad
36 uint32_t resumeLocation;
38 // set by personality handler to be parameters passed to landing pad function
39 uint32_t resumeParameters[4];
41 // set by calling function before registering
42 __personality_routine personality; // arm offset=24
43 uintptr_t lsda; // arm offset=28
45 // variable length array, contains registers to restore
46 // 0 = r7, 1 = pc, 2 = sp
50 #if defined(_LIBUNWIND_HAS_NO_THREADS)
51 # define _LIBUNWIND_THREAD_LOCAL
53 # if __STDC_VERSION__ >= 201112L
54 # define _LIBUNWIND_THREAD_LOCAL _Thread_local
55 # elif defined(_WIN32)
56 # define _LIBUNWIND_THREAD_LOCAL __declspec(thread)
57 # elif defined(__GNUC__) || defined(__clang__)
58 # define _LIBUNWIND_THREAD_LOCAL __thread
60 # error Unable to create thread local storage
65 #if !defined(FOR_DYLD)
67 #if defined(__APPLE__)
68 #include <System/pthread_machdep.h>
70 static _LIBUNWIND_THREAD_LOCAL struct _Unwind_FunctionContext *stack = NULL;
73 static struct _Unwind_FunctionContext *__Unwind_SjLj_GetTopOfFunctionStack() {
74 #if defined(__APPLE__)
75 return _pthread_getspecific_direct(__PTK_LIBC_DYLD_Unwind_SjLj_Key);
82 __Unwind_SjLj_SetTopOfFunctionStack(struct _Unwind_FunctionContext *fc) {
83 #if defined(__APPLE__)
84 _pthread_setspecific_direct(__PTK_LIBC_DYLD_Unwind_SjLj_Key, fc);
93 /// Called at start of each function that catches exceptions
94 _LIBUNWIND_EXPORT void
95 _Unwind_SjLj_Register(struct _Unwind_FunctionContext *fc) {
96 fc->prev = __Unwind_SjLj_GetTopOfFunctionStack();
97 __Unwind_SjLj_SetTopOfFunctionStack(fc);
101 /// Called at end of each function that catches exceptions
102 _LIBUNWIND_EXPORT void
103 _Unwind_SjLj_Unregister(struct _Unwind_FunctionContext *fc) {
104 __Unwind_SjLj_SetTopOfFunctionStack(fc->prev);
108 static _Unwind_Reason_Code
109 unwind_phase1(struct _Unwind_Exception *exception_object) {
110 _Unwind_FunctionContext_t c = __Unwind_SjLj_GetTopOfFunctionStack();
111 _LIBUNWIND_TRACE_UNWINDING("unwind_phase1: initial function-context=%p", c);
113 // walk each frame looking for a place to stop
114 for (bool handlerNotFound = true; handlerNotFound; c = c->prev) {
116 // check for no more frames
118 _LIBUNWIND_TRACE_UNWINDING("unwind_phase1(ex_ojb=%p): reached "
119 "bottom => _URC_END_OF_STACK",
121 return _URC_END_OF_STACK;
124 _LIBUNWIND_TRACE_UNWINDING("unwind_phase1: function-context=%p", c);
125 // if there is a personality routine, ask it if it will want to stop at this
127 if (c->personality != NULL) {
128 _LIBUNWIND_TRACE_UNWINDING("unwind_phase1(ex_ojb=%p): calling "
129 "personality function %p",
130 exception_object, c->personality);
131 _Unwind_Reason_Code personalityResult = (*c->personality)(
132 1, _UA_SEARCH_PHASE, exception_object->exception_class,
133 exception_object, (struct _Unwind_Context *)c);
134 switch (personalityResult) {
135 case _URC_HANDLER_FOUND:
136 // found a catch clause or locals that need destructing in this frame
137 // stop search and remember function context
138 handlerNotFound = false;
139 exception_object->private_2 = (uintptr_t) c;
140 _LIBUNWIND_TRACE_UNWINDING("unwind_phase1(ex_ojb=%p): "
141 "_URC_HANDLER_FOUND", exception_object);
142 return _URC_NO_REASON;
144 case _URC_CONTINUE_UNWIND:
145 _LIBUNWIND_TRACE_UNWINDING("unwind_phase1(ex_ojb=%p): "
146 "_URC_CONTINUE_UNWIND", exception_object);
147 // continue unwinding
151 // something went wrong
152 _LIBUNWIND_TRACE_UNWINDING(
153 "unwind_phase1(ex_ojb=%p): _URC_FATAL_PHASE1_ERROR",
155 return _URC_FATAL_PHASE1_ERROR;
159 return _URC_NO_REASON;
163 static _Unwind_Reason_Code
164 unwind_phase2(struct _Unwind_Exception *exception_object) {
165 _LIBUNWIND_TRACE_UNWINDING("unwind_phase2(ex_ojb=%p)", exception_object);
167 // walk each frame until we reach where search phase said to stop
168 _Unwind_FunctionContext_t c = __Unwind_SjLj_GetTopOfFunctionStack();
170 _LIBUNWIND_TRACE_UNWINDING("unwind_phase2s(ex_ojb=%p): context=%p",
171 exception_object, c);
173 // check for no more frames
175 _LIBUNWIND_TRACE_UNWINDING("unwind_phase2(ex_ojb=%p): unw_step() reached "
176 "bottom => _URC_END_OF_STACK",
178 return _URC_END_OF_STACK;
181 // if there is a personality routine, tell it we are unwinding
182 if (c->personality != NULL) {
183 _Unwind_Action action = _UA_CLEANUP_PHASE;
184 if ((uintptr_t) c == exception_object->private_2)
185 action = (_Unwind_Action)(
187 _UA_HANDLER_FRAME); // tell personality this was the frame it marked
189 _Unwind_Reason_Code personalityResult =
190 (*c->personality)(1, action, exception_object->exception_class,
191 exception_object, (struct _Unwind_Context *)c);
192 switch (personalityResult) {
193 case _URC_CONTINUE_UNWIND:
194 // continue unwinding
195 _LIBUNWIND_TRACE_UNWINDING(
196 "unwind_phase2(ex_ojb=%p): _URC_CONTINUE_UNWIND",
198 if ((uintptr_t) c == exception_object->private_2) {
199 // phase 1 said we would stop at this frame, but we did not...
200 _LIBUNWIND_ABORT("during phase1 personality function said it would "
201 "stop here, but now if phase2 it did not stop here");
204 case _URC_INSTALL_CONTEXT:
205 _LIBUNWIND_TRACE_UNWINDING("unwind_phase2(ex_ojb=%p): "
206 "_URC_INSTALL_CONTEXT, will resume at "
208 exception_object, c->jbuf[1]);
209 // personality routine says to transfer control to landing pad
210 // we may get control back if landing pad calls _Unwind_Resume()
211 __Unwind_SjLj_SetTopOfFunctionStack(c);
212 __builtin_longjmp(c->jbuf, 1);
213 // unw_resume() only returns if there was an error
214 return _URC_FATAL_PHASE2_ERROR;
216 // something went wrong
217 _LIBUNWIND_DEBUG_LOG("personality function returned unknown result %d",
219 return _URC_FATAL_PHASE2_ERROR;
225 // clean up phase did not resume at the frame that the search phase said it
227 return _URC_FATAL_PHASE2_ERROR;
231 static _Unwind_Reason_Code
232 unwind_phase2_forced(struct _Unwind_Exception *exception_object,
233 _Unwind_Stop_Fn stop, void *stop_parameter) {
234 // walk each frame until we reach where search phase said to stop
235 _Unwind_FunctionContext_t c = __Unwind_SjLj_GetTopOfFunctionStack();
238 // get next frame (skip over first which is _Unwind_RaiseException)
240 _LIBUNWIND_TRACE_UNWINDING("unwind_phase2(ex_ojb=%p): unw_step() reached "
241 "bottom => _URC_END_OF_STACK",
243 return _URC_END_OF_STACK;
246 // call stop function at each frame
247 _Unwind_Action action =
248 (_Unwind_Action)(_UA_FORCE_UNWIND | _UA_CLEANUP_PHASE);
249 _Unwind_Reason_Code stopResult =
250 (*stop)(1, action, exception_object->exception_class, exception_object,
251 (struct _Unwind_Context *)c, stop_parameter);
252 _LIBUNWIND_TRACE_UNWINDING("unwind_phase2_forced(ex_ojb=%p): "
253 "stop function returned %d",
254 exception_object, stopResult);
255 if (stopResult != _URC_NO_REASON) {
256 _LIBUNWIND_TRACE_UNWINDING("unwind_phase2_forced(ex_ojb=%p): "
257 "stopped by stop function",
259 return _URC_FATAL_PHASE2_ERROR;
262 // if there is a personality routine, tell it we are unwinding
263 if (c->personality != NULL) {
264 __personality_routine p = (__personality_routine) c->personality;
265 _LIBUNWIND_TRACE_UNWINDING("unwind_phase2_forced(ex_ojb=%p): "
266 "calling personality function %p",
267 exception_object, p);
268 _Unwind_Reason_Code personalityResult =
269 (*p)(1, action, exception_object->exception_class, exception_object,
270 (struct _Unwind_Context *)c);
271 switch (personalityResult) {
272 case _URC_CONTINUE_UNWIND:
273 _LIBUNWIND_TRACE_UNWINDING("unwind_phase2_forced(ex_ojb=%p): "
274 "personality returned _URC_CONTINUE_UNWIND",
276 // destructors called, continue unwinding
278 case _URC_INSTALL_CONTEXT:
279 _LIBUNWIND_TRACE_UNWINDING("unwind_phase2_forced(ex_ojb=%p): "
280 "personality returned _URC_INSTALL_CONTEXT",
282 // we may get control back if landing pad calls _Unwind_Resume()
283 __Unwind_SjLj_SetTopOfFunctionStack(c);
284 __builtin_longjmp(c->jbuf, 1);
287 // something went wrong
288 _LIBUNWIND_TRACE_UNWINDING("unwind_phase2_forced(ex_ojb=%p): "
289 "personality returned %d, "
290 "_URC_FATAL_PHASE2_ERROR",
291 exception_object, personalityResult);
292 return _URC_FATAL_PHASE2_ERROR;
298 // call stop function one last time and tell it we've reached the end of the
300 _LIBUNWIND_TRACE_UNWINDING("unwind_phase2_forced(ex_ojb=%p): calling stop "
301 "function with _UA_END_OF_STACK",
303 _Unwind_Action lastAction =
304 (_Unwind_Action)(_UA_FORCE_UNWIND | _UA_CLEANUP_PHASE | _UA_END_OF_STACK);
305 (*stop)(1, lastAction, exception_object->exception_class, exception_object,
306 (struct _Unwind_Context *)c, stop_parameter);
308 // clean up phase did not resume at the frame that the search phase said it
310 return _URC_FATAL_PHASE2_ERROR;
314 /// Called by __cxa_throw. Only returns if there is a fatal error
315 _LIBUNWIND_EXPORT _Unwind_Reason_Code
316 _Unwind_SjLj_RaiseException(struct _Unwind_Exception *exception_object) {
317 _LIBUNWIND_TRACE_API("_Unwind_SjLj_RaiseException(ex_obj=%p)", exception_object);
319 // mark that this is a non-forced unwind, so _Unwind_Resume() can do the right
321 exception_object->private_1 = 0;
322 exception_object->private_2 = 0;
324 // phase 1: the search phase
325 _Unwind_Reason_Code phase1 = unwind_phase1(exception_object);
326 if (phase1 != _URC_NO_REASON)
329 // phase 2: the clean up phase
330 return unwind_phase2(exception_object);
335 /// When _Unwind_RaiseException() is in phase2, it hands control
336 /// to the personality function at each frame. The personality
337 /// may force a jump to a landing pad in that function, the landing
338 /// pad code may then call _Unwind_Resume() to continue with the
339 /// unwinding. Note: the call to _Unwind_Resume() is from compiler
340 /// geneated user code. All other _Unwind_* routines are called
341 /// by the C++ runtime __cxa_* routines.
343 /// Re-throwing an exception is implemented by having the code call
344 /// __cxa_rethrow() which in turn calls _Unwind_Resume_or_Rethrow()
345 _LIBUNWIND_EXPORT void
346 _Unwind_SjLj_Resume(struct _Unwind_Exception *exception_object) {
347 _LIBUNWIND_TRACE_API("_Unwind_SjLj_Resume(ex_obj=%p)", exception_object);
349 if (exception_object->private_1 != 0)
350 unwind_phase2_forced(exception_object,
351 (_Unwind_Stop_Fn) exception_object->private_1,
352 (void *)exception_object->private_2);
354 unwind_phase2(exception_object);
356 // clients assume _Unwind_Resume() does not return, so all we can do is abort.
357 _LIBUNWIND_ABORT("_Unwind_SjLj_Resume() can't return");
361 /// Called by __cxa_rethrow().
362 _LIBUNWIND_EXPORT _Unwind_Reason_Code
363 _Unwind_SjLj_Resume_or_Rethrow(struct _Unwind_Exception *exception_object) {
364 _LIBUNWIND_TRACE_API("__Unwind_SjLj_Resume_or_Rethrow(ex_obj=%p), "
366 exception_object, exception_object->private_1);
367 // If this is non-forced and a stopping place was found, then this is a
369 // Call _Unwind_RaiseException() as if this was a new exception.
370 if (exception_object->private_1 == 0) {
371 return _Unwind_SjLj_RaiseException(exception_object);
372 // should return if there is no catch clause, so that __cxa_rethrow can call
376 // Call through to _Unwind_Resume() which distiguishes between forced and
377 // regular exceptions.
378 _Unwind_SjLj_Resume(exception_object);
379 _LIBUNWIND_ABORT("__Unwind_SjLj_Resume_or_Rethrow() called "
380 "_Unwind_SjLj_Resume() which unexpectedly returned");
384 /// Called by personality handler during phase 2 to get LSDA for current frame.
385 _LIBUNWIND_EXPORT uintptr_t
386 _Unwind_GetLanguageSpecificData(struct _Unwind_Context *context) {
387 _Unwind_FunctionContext_t ufc = (_Unwind_FunctionContext_t) context;
388 _LIBUNWIND_TRACE_API("_Unwind_GetLanguageSpecificData(context=%p) "
389 "=> 0x%0lX", context, ufc->lsda);
394 /// Called by personality handler during phase 2 to get register values.
395 _LIBUNWIND_EXPORT uintptr_t _Unwind_GetGR(struct _Unwind_Context *context,
397 _LIBUNWIND_TRACE_API("_Unwind_GetGR(context=%p, reg=%d)",
399 _Unwind_FunctionContext_t ufc = (_Unwind_FunctionContext_t) context;
400 return ufc->resumeParameters[index];
404 /// Called by personality handler during phase 2 to alter register values.
405 _LIBUNWIND_EXPORT void _Unwind_SetGR(struct _Unwind_Context *context, int index,
406 uintptr_t new_value) {
407 _LIBUNWIND_TRACE_API("_Unwind_SetGR(context=%p, reg=%d, value=0x%0lX)"
408 , context, index, new_value);
409 _Unwind_FunctionContext_t ufc = (_Unwind_FunctionContext_t) context;
410 ufc->resumeParameters[index] = new_value;
414 /// Called by personality handler during phase 2 to get instruction pointer.
415 _LIBUNWIND_EXPORT uintptr_t _Unwind_GetIP(struct _Unwind_Context *context) {
416 _Unwind_FunctionContext_t ufc = (_Unwind_FunctionContext_t) context;
417 _LIBUNWIND_TRACE_API("_Unwind_GetIP(context=%p) => 0x%lX", context,
418 ufc->resumeLocation + 1);
419 return ufc->resumeLocation + 1;
423 /// Called by personality handler during phase 2 to get instruction pointer.
424 /// ipBefore is a boolean that says if IP is already adjusted to be the call
425 /// site address. Normally IP is the return address.
426 _LIBUNWIND_EXPORT uintptr_t _Unwind_GetIPInfo(struct _Unwind_Context *context,
428 _Unwind_FunctionContext_t ufc = (_Unwind_FunctionContext_t) context;
430 _LIBUNWIND_TRACE_API("_Unwind_GetIPInfo(context=%p, %p) => 0x%lX",
431 context, ipBefore, ufc->resumeLocation + 1);
432 return ufc->resumeLocation + 1;
436 /// Called by personality handler during phase 2 to alter instruction pointer.
437 _LIBUNWIND_EXPORT void _Unwind_SetIP(struct _Unwind_Context *context,
438 uintptr_t new_value) {
439 _LIBUNWIND_TRACE_API("_Unwind_SetIP(context=%p, value=0x%0lX)",
441 _Unwind_FunctionContext_t ufc = (_Unwind_FunctionContext_t) context;
442 ufc->resumeLocation = new_value - 1;
446 /// Called by personality handler during phase 2 to find the start of the
448 _LIBUNWIND_EXPORT uintptr_t
449 _Unwind_GetRegionStart(struct _Unwind_Context *context) {
450 // Not supported or needed for sjlj based unwinding
452 _LIBUNWIND_TRACE_API("_Unwind_GetRegionStart(context=%p)", context);
457 /// Called by personality handler during phase 2 if a foreign exception
459 _LIBUNWIND_EXPORT void
460 _Unwind_DeleteException(struct _Unwind_Exception *exception_object) {
461 _LIBUNWIND_TRACE_API("_Unwind_DeleteException(ex_obj=%p)",
463 if (exception_object->exception_cleanup != NULL)
464 (*exception_object->exception_cleanup)(_URC_FOREIGN_EXCEPTION_CAUGHT,
470 /// Called by personality handler during phase 2 to get base address for data
471 /// relative encodings.
472 _LIBUNWIND_EXPORT uintptr_t
473 _Unwind_GetDataRelBase(struct _Unwind_Context *context) {
474 // Not supported or needed for sjlj based unwinding
476 _LIBUNWIND_TRACE_API("_Unwind_GetDataRelBase(context=%p)", context);
477 _LIBUNWIND_ABORT("_Unwind_GetDataRelBase() not implemented");
481 /// Called by personality handler during phase 2 to get base address for text
482 /// relative encodings.
483 _LIBUNWIND_EXPORT uintptr_t
484 _Unwind_GetTextRelBase(struct _Unwind_Context *context) {
485 // Not supported or needed for sjlj based unwinding
487 _LIBUNWIND_TRACE_API("_Unwind_GetTextRelBase(context=%p)", context);
488 _LIBUNWIND_ABORT("_Unwind_GetTextRelBase() not implemented");
492 /// Called by personality handler to get "Call Frame Area" for current frame.
493 _LIBUNWIND_EXPORT uintptr_t _Unwind_GetCFA(struct _Unwind_Context *context) {
494 _LIBUNWIND_TRACE_API("_Unwind_GetCFA(context=%p)", context);
495 if (context != NULL) {
496 _Unwind_FunctionContext_t ufc = (_Unwind_FunctionContext_t) context;
497 // Setjmp/longjmp based exceptions don't have a true CFA.
498 // Instead, the SP in the jmpbuf is the closest approximation.
499 return (uintptr_t) ufc->jbuf[2];
504 #endif // defined(_LIBUNWIND_BUILD_SJLJ_APIS)